capturetheflag/mods/ctf_team_base/init.lua

235 lines
7.4 KiB
Lua
Raw Normal View History

2016-01-02 21:10:26 +00:00
local old = ctf.get_spawn
function ctf.get_spawn(tname)
local team = ctf.team(tname)
if team and #team.flags >= 1 then
local flag = team.flags[1]
local i = 0
local r = 6
while i < 6 do
i = i + 1
local pos = {x=0, z=0}
pos.x = flag.x + math.random() * 40 - 20
pos.z = math.random() * 30 + 49
if pos.x < -99 then
pos.x = -99
end
if pos.x > 99 then
pos.x = 99
end
if team.flags[1].z < 0 then
pos.z = -pos.z
end
local res = minetest.find_nodes_in_area_under_air(
{ x = pos.x - r, y = 2, z = pos.z - r},
{ x = pos.x + r, y = 17, z = pos.z + r},
{"default:dirt_with_grass"})
if #res > 0 then
res[1].y = res[1].y + 1
team.spawn = res[1]
return res[1]
end
end
return team.spawn or old(tname)
else
return team.spawn
end
end
local function max(a, b)
return (a > b) and a or b
end
2017-10-12 00:19:23 +00:00
local function get_is_player_pro(player)
local players = {}
for pname, pstat in pairs(ctf_stats.players) do
pstat.name = pname
pstat.color = nil
table.insert(players, pstat)
end
local pstat = ctf_stats.player(player:get_player_name())
local kd = pstat.kills / max(pstat.deaths, 1)
2018-01-09 03:00:29 +00:00
return pstat.score > 1000 and kd > 2
2017-10-12 00:19:23 +00:00
end
2015-12-01 00:13:59 +00:00
2015-12-01 00:38:30 +00:00
local colors = {"red", "blue"}
2017-10-12 17:55:27 +00:00
local chest_name_to_team = {}
for _, chest_color in pairs(colors) do
chest_name_to_team["ctf_team_base:chest_" .. chest_color] = chest_color
minetest.register_node("ctf_team_base:chest_" .. chest_color, {
2015-12-01 00:38:30 +00:00
description = "Chest",
tiles = {
2017-10-12 17:55:27 +00:00
"default_chest_top_" .. chest_color .. ".png",
"default_chest_top_" .. chest_color .. ".png",
"default_chest_side_" .. chest_color .. ".png",
"default_chest_side_" .. chest_color .. ".png",
"default_chest_side_" .. chest_color .. ".png",
"default_chest_front_" .. chest_color .. ".png"},
2015-12-01 00:38:30 +00:00
paramtype2 = "facedir",
2017-10-12 17:55:27 +00:00
groups = {immortal = 1, team_chest=1},
2015-12-01 00:38:30 +00:00
legacy_facedir_simple = true,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Chest")
local inv = meta:get_inventory()
2017-10-12 00:19:23 +00:00
inv:set_size("main", 5*4)
inv:set_size("pro", 3*4)
2015-12-01 00:38:30 +00:00
end,
2017-10-12 00:19:23 +00:00
on_rightclick = function(pos, node, player)
2017-10-12 17:55:27 +00:00
if chest_color ~= ctf.player(player:get_player_name()).team then
minetest.chat_send_player(player:get_player_name(), "You're not on team " .. chest_color)
return
end
2017-10-12 17:55:27 +00:00
local territory_owner = ctf.get_territory_owner(pos)
if chest_color ~= territory_owner then
ctf.warning("ctf_team_base", "Wrong chest, changing to " .. territory_owner .. " from " .. chest_color)
minetest.set_node(pos, "ctf_team_base:chest_" .. territory_owner)
end
2017-10-12 00:19:23 +00:00
local chestinv = "nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z
local is_pro = get_is_player_pro(player)
local formspec =
"size[8,9]" ..
"label[0,-0.2;" .. minetest.formspec_escape("Any team member can take from here") .. "]" ..
2017-10-12 00:19:23 +00:00
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"list[" .. chestinv .. ";main;0,0.3;5,4;]" ..
"background[5,-0.2;3.15,4.7;ctf_team_base_pro_only.png;false]" ..
"list[" .. chestinv .. ";pro;5,0.3;3,4;]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]"
if is_pro then
formspec = formspec .. "listring[current_name;pro]" ..
2018-01-09 03:00:29 +00:00
"label[5,-0.2;" .. minetest.formspec_escape("Pro players only (1k+ score, good KD)") .. "]"
2017-10-12 00:19:23 +00:00
else
formspec = formspec .. "listring[current_name;pro]" ..
2018-01-09 03:00:29 +00:00
"label[5,-0.2;" .. minetest.formspec_escape("You need 1k+ score and good KD") .. "]"
2017-10-12 00:19:23 +00:00
end
formspec = formspec ..
"listring[current_name;main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,4.85)
minetest.show_formspec(player:get_player_name(), "ctf_team_base:chest", formspec)
2015-12-01 00:38:30 +00:00
end,
2017-10-12 00:19:23 +00:00
allow_metadata_inventory_move = function(pos, from_list, from_index,
2015-12-01 00:38:30 +00:00
to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
2017-10-12 17:55:27 +00:00
if chest_color ~= ctf.player(player:get_player_name()).team then
minetest.chat_send_player(player:get_player_name(), "You're not on team " .. chest_color)
return 0
end
2017-10-12 00:19:23 +00:00
if (from_list ~= "pro" and to_list ~= "pro") or get_is_player_pro(player) then
return count
else
return 0
end
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
2017-10-12 17:55:27 +00:00
if chest_color ~= ctf.player(player:get_player_name()).team then
minetest.chat_send_player(player:get_player_name(), "You're not on team " .. chest_color)
return 0
end
2017-10-12 00:19:23 +00:00
if listname ~= "pro" or get_is_player_pro(player) then
return stack:get_count()
else
return 0
end
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
2017-10-12 17:55:27 +00:00
if chest_color ~= ctf.player(player:get_player_name()).team then
minetest.chat_send_player(player:get_player_name(), "You're not on team " .. chest_color)
return 0
end
2017-10-12 00:19:23 +00:00
if listname ~= "pro" or get_is_player_pro(player) then
return stack:get_count()
else
return 0
end
end,
can_dig = function(pos, player)
return false
2015-12-01 00:38:30 +00:00
end,
2017-10-12 17:55:27 +00:00
on_metadata_inventory_put = function(pos, listname, index, stack, player)
2015-12-01 00:38:30 +00:00
minetest.log("action", player:get_player_name() ..
2015-12-30 12:53:10 +00:00
" moves " .. (stack:get_name() or "stuff") .. " " .. (stack:get_count() or 0) .. " to chest at " .. minetest.pos_to_string(pos))
2015-12-01 00:38:30 +00:00
end,
2017-10-12 17:55:27 +00:00
on_metadata_inventory_take = function(pos, listname, index, stack, player)
2015-12-01 00:38:30 +00:00
minetest.log("action", player:get_player_name() ..
2015-12-30 12:53:10 +00:00
" takes " .. (stack:get_name() or "stuff") .. " " .. (stack:get_count() or 0) .. " from chest at " .. minetest.pos_to_string(pos))
2015-12-01 00:38:30 +00:00
end
})
end
2015-12-01 00:13:59 +00:00
2017-10-12 17:55:27 +00:00
minetest.register_abm({
nodenames = {"group:team_chest"},
interval = 2, -- Run every 10 seconds
chance = 1, -- Select every 1 in 50 nodes
action = function(pos, node, active_object_count, active_object_count_wider)
local current_owner = assert(chest_name_to_team[node.name])
local territory_owner = ctf.get_territory_owner(pos)
if territory_owner and current_owner ~= territory_owner then
ctf.warning("ctf_team_base", "Wrong chest, changing to " .. territory_owner .. " from " .. current_owner)
2017-10-12 19:51:07 +00:00
minetest.set_node(pos, { name = "ctf_team_base:chest_" .. territory_owner })
2017-10-12 17:55:27 +00:00
end
end
})
2018-01-21 23:09:07 +00:00
ctf_team_base = {}
function ctf_team_base.place(color, pos)
-- Spawn ind base
for x = pos.x - 2, pos.x + 2 do
for z = pos.z - 2, pos.z + 2 do
minetest.set_node({ x = x, y = pos.y - 1, z = z},
{name = "default:cobble"})
end
end
minetest.set_node({ x = pos.x, y = pos.y - 1, z = pos.z},
{name = "ctf_barrier:ind_stone"})
-- Check for trees
for y = pos.y, pos.y + 3 do
for x = pos.x - 3, pos.x + 3 do
for z = pos.z - 3, pos.z + 3 do
local pos = {x=x, y=y, z=z}
if minetest.get_node(pos).name == "default:tree" then
minetest.set_node(pos, {name="air"})
2015-12-01 00:13:59 +00:00
end
end
end
end
2018-01-21 23:09:07 +00:00
-- Spawn chest
local chest = {name = "ctf_team_base:chest_" .. color}
local dz = 2
if pos.z < 0 then
dz = -2
chest.param2 = minetest.dir_to_facedir({x=0,y=0,z=-1})
end
local pos = {
x = pos.x,
y = pos.y,
z = pos.z + dz
}
minetest.set_node(pos, chest)
local inv = minetest.get_meta(pos):get_inventory()
inv:add_item("main", ItemStack("default:cobble 99"))
inv:add_item("main", ItemStack("default:cobble 99"))
inv:add_item("main", ItemStack("default:cobble 99"))
inv:add_item("main", ItemStack("default:wood 99"))
inv:add_item("main", ItemStack("default:glass 5"))
inv:add_item("main", ItemStack("default:torch 10"))
end