2020-12-07 19:26:12 +00:00
|
|
|
ctf_marker = {}
|
|
|
|
|
2019-03-06 13:09:22 +00:00
|
|
|
-- Locally cache list of team members when adding
|
|
|
|
-- marker, because the members in the team needn't
|
|
|
|
-- be the same within an extended duration of time
|
|
|
|
local teams = {}
|
2019-03-11 18:55:26 +00:00
|
|
|
local visibility_time = 30
|
2019-03-06 13:09:22 +00:00
|
|
|
|
|
|
|
-- Convenience function that returns passed
|
|
|
|
-- string enclosed by color escape codes
|
|
|
|
local function msg(str)
|
|
|
|
if not str then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return minetest.colorize("#ABCDEF", str)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove waypoint element for valid players in team tname
|
2020-12-07 19:26:12 +00:00
|
|
|
function ctf_marker.remove_marker(tname)
|
|
|
|
if not teams[tname] then return end
|
|
|
|
|
2019-03-06 13:09:22 +00:00
|
|
|
for name, hud in pairs(teams[tname].players) do
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if player then
|
|
|
|
player:hud_remove(hud)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
teams[tname] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add waypoint element to all players in the same team as name
|
2020-12-07 19:26:12 +00:00
|
|
|
function ctf_marker.add_marker(name, tname, pos, str)
|
2019-03-06 13:09:22 +00:00
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local team = ctf.team(tname)
|
|
|
|
|
|
|
|
teams[tname] = {
|
|
|
|
players = {},
|
|
|
|
time = 0
|
|
|
|
}
|
2019-03-21 01:36:59 +00:00
|
|
|
|
2019-03-06 13:09:22 +00:00
|
|
|
for pname, _ in pairs(team.players) do
|
|
|
|
local tplayer = minetest.get_player_by_name(pname)
|
|
|
|
if tplayer then
|
|
|
|
teams[tname].players[pname] = tplayer:hud_add({
|
|
|
|
hud_elem_type = "waypoint",
|
2019-10-18 05:52:18 +00:00
|
|
|
name = str,
|
2020-05-05 17:07:37 +00:00
|
|
|
number = tonumber(ctf.flag_colors[team.data.color]),
|
2019-10-18 05:52:18 +00:00
|
|
|
world_pos = pos
|
2019-03-06 13:09:22 +00:00
|
|
|
})
|
|
|
|
end
|
2019-10-18 05:52:18 +00:00
|
|
|
minetest.log("action", name .. " placed a marker at " ..
|
|
|
|
minetest.pos_to_string(pos) .. ": '" .. str .. "'")
|
2019-03-06 13:09:22 +00:00
|
|
|
minetest.chat_send_player(pname,
|
2021-01-23 17:10:06 +00:00
|
|
|
msg("* " .. name .. " placed a marker!"))
|
2019-03-06 13:09:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
for tname, team in pairs(teams) do
|
|
|
|
-- Increment time of team marker
|
|
|
|
local time = team.time + dtime
|
|
|
|
teams[tname].time = time
|
|
|
|
|
|
|
|
-- If time > visibility_time, destroy team marker
|
|
|
|
if time >= visibility_time then
|
2020-12-07 19:26:12 +00:00
|
|
|
ctf_marker.remove_marker(tname)
|
2019-03-06 13:09:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_chatcommand("m", {
|
|
|
|
param = "[Optional description]",
|
|
|
|
description = "Allows players to share the location of where " ..
|
2019-10-18 05:52:18 +00:00
|
|
|
"they're looking at with their team-mates.",
|
2019-03-06 13:09:22 +00:00
|
|
|
privs = { interact = true },
|
|
|
|
func = function(name, param)
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Calculate marker pos
|
|
|
|
local dir = player:get_look_dir()
|
|
|
|
local p1 = vector.add(player:get_pos(),
|
2019-10-18 05:52:18 +00:00
|
|
|
{ x = 0, y = player:get_properties().eye_height, z = 0 })
|
2019-03-06 13:09:22 +00:00
|
|
|
p1 = vector.add(p1, dir)
|
|
|
|
local p2 = vector.add(p1, vector.multiply(dir, 500))
|
|
|
|
local pointed = minetest.raycast(p1, p2, true, true):next()
|
|
|
|
|
|
|
|
if not pointed then
|
|
|
|
minetest.chat_send_player(name, msg("Pointed thing out of range!"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local tname = ctf.player(name).team
|
|
|
|
|
|
|
|
-- Handle waypoint string
|
|
|
|
local str = (param and param:trim() ~= "") and param or name .. "'s marker"
|
|
|
|
if pointed.type == "object" then
|
|
|
|
local concat
|
|
|
|
local obj = pointed.ref
|
2020-04-24 19:16:35 +00:00
|
|
|
local entity = obj:get_luaentity()
|
|
|
|
|
|
|
|
-- If object is a player, append player name to display text
|
|
|
|
-- Else if obj is item entity, append item description and count to str.
|
2019-03-06 13:09:22 +00:00
|
|
|
if obj:is_player() then
|
|
|
|
concat = obj:get_player_name()
|
2020-04-24 19:16:35 +00:00
|
|
|
elseif entity then
|
2019-03-06 13:09:22 +00:00
|
|
|
if entity.name == "__builtin:item" then
|
|
|
|
local stack = ItemStack(entity.itemstring)
|
|
|
|
local itemdef = minetest.registered_items[stack:get_name()]
|
2020-04-24 19:16:35 +00:00
|
|
|
|
|
|
|
-- Fallback to itemstring if description doesn't exist
|
2019-03-06 13:09:22 +00:00
|
|
|
concat = itemdef.description or entity.itemstring
|
|
|
|
concat = concat .. " " .. stack:get_count()
|
|
|
|
else
|
|
|
|
-- Pointed thing is a trivial entity, abort
|
|
|
|
minetest.chat_send_player(name,
|
|
|
|
msg("Invalid marker position. Please try again."))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
str = concat and str .. " <" .. concat .. ">"
|
|
|
|
end
|
|
|
|
str = "[" .. str .. "]"
|
|
|
|
|
|
|
|
-- Remove existing marker if it exists
|
2020-12-07 19:26:12 +00:00
|
|
|
ctf_marker.remove_marker(tname)
|
2019-03-06 13:09:22 +00:00
|
|
|
|
2020-12-07 19:26:12 +00:00
|
|
|
ctf_marker.add_marker(name, tname, minetest.get_pointed_thing_position(pointed), str)
|
2019-03-06 13:09:22 +00:00
|
|
|
end
|
|
|
|
})
|