Add class items

This commit is contained in:
rubenwardy 2020-03-13 21:58:42 +00:00
parent f610722b83
commit 02d527eaf5
18 changed files with 384 additions and 211 deletions

View file

@ -6,9 +6,24 @@ local blacklist = {
"default:aspen_leaves"
}
function ctf_map.is_item_allowed_in_team_chest(listname, stack, player)
if listname == "helper" then
return false
end
for _, itemstring in ipairs(blacklist) do
if stack:get_name() == itemstring then
return false
end
end
return true
end
local colors = {"red", "blue"}
for _, chest_color in pairs(colors) do
minetest.register_node("ctf_map_core:chest_" .. chest_color, {
local def = {
description = "Chest",
tiles = {
"default_chest_top_" .. chest_color .. ".png",
@ -22,185 +37,188 @@ for _, chest_color in pairs(colors) do
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()
inv:set_size("main", 4 * 7)
inv:set_size("pro", 4 * 7)
inv:set_size("helper", 1 * 1)
end,
on_rightclick = function(pos, node, player)
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
}
function def.on_construct(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Chest")
local inv = meta:get_inventory()
inv:set_size("main", 4 * 7)
inv:set_size("pro", 4 * 7)
inv:set_size("helper", 1 * 1)
end
function def.can_dig(pos, player)
return false
end
function def.on_rightclick(pos, node, player)
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return
end
local territory_owner = ctf.get_territory_owner(pos)
if chest_color ~= territory_owner then
if not territory_owner then
ctf.warning("ctf_map", "Unowned team chest")
minetest.set_node(pos, { name = "air" })
return
end
ctf.warning("ctf_map", "Wrong chest, changing to " ..
territory_owner .. " from " .. chest_color)
minetest.set_node(pos, "ctf_map_core:chest_" .. territory_owner)
end
local territory_owner = ctf.get_territory_owner(pos)
if chest_color ~= territory_owner then
if not territory_owner then
ctf.warning("ctf_map", "Unowned team chest")
minetest.set_node(pos, { name = "air" })
return
end
ctf.warning("ctf_map", "Wrong chest, changing to " ..
territory_owner .. " from " .. chest_color)
minetest.set_node(pos, "ctf_map_core:chest_" .. territory_owner)
end
local formspec = table.concat({
"size[8,12]",
default.gui_bg,
default.gui_bg_img,
default.gui_slots,
default.get_hotbar_bg(0,7.85),
"list[current_player;main;0,7.85;8,1;]",
"list[current_player;main;0,9.08;8,3;8]",
}, "")
local formspec = table.concat({
"size[8,12]",
default.gui_bg,
default.gui_bg_img,
default.gui_slots,
default.get_hotbar_bg(0,7.85),
"list[current_player;main;0,7.85;8,1;]",
"list[current_player;main;0,9.08;8,3;8]",
}, "")
if ctf_stats.player(name).score < 10 then
local msg = "You need at least 10 score to access the team chest.\n" ..
"Try killing an enemy player, or at least try to capture the flag.\n" ..
"Find resources in chests scattered around the map."
formspec = formspec .. "label[0.75,3;" .. minetest.formspec_escape(msg) .. "]"
minetest.show_formspec(name, "ctf_map_core:no_access", formspec)
return
end
if ctf_stats.player(name).score < 10 then
local msg = "You need at least 10 score to access the team chest.\n" ..
"Try killing an enemy player, or at least try to capture the flag.\n" ..
"Find resources in chests scattered around the map."
formspec = formspec .. "label[0.75,3;" .. minetest.formspec_escape(msg) .. "]"
minetest.show_formspec(name, "ctf_map_core:no_access", formspec)
return
end
local is_pro = ctf_stats.is_pro(name)
local chestinv = "nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z
local is_pro = ctf_stats.is_pro(name)
local chestinv = "nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z
formspec = formspec .. "list[" .. chestinv .. ";main;0,0.3;4,7;]" ..
"background[4,-0.2;4.15,7.7;ctf_map_pro_section.png;false]"
formspec = formspec .. "list[" .. chestinv .. ";main;0,0.3;4,7;]" ..
"background[4,-0.2;4.15,7.7;ctf_map_pro_section.png;false]"
if is_pro then
formspec = formspec .. "list[" .. chestinv .. ";pro;4,0.3;4,7;]" ..
"listring[" .. chestinv ..";pro]" ..
"listring[" .. chestinv .. ";helper]" ..
"label[5,-0.2;" ..
minetest.formspec_escape("Pro players only") .. "]"
else
formspec = formspec .. "label[4.75,3;" ..
minetest.formspec_escape("You need at least 10000" ..
"\nscore and 1.5+ KD to\naccess the pro section") .. "]"
end
if is_pro then
formspec = formspec .. "list[" .. chestinv .. ";pro;4,0.3;4,7;]" ..
"listring[" .. chestinv ..";pro]" ..
"listring[" .. chestinv .. ";helper]" ..
"label[5,-0.2;" ..
minetest.formspec_escape("Pro players only") .. "]"
else
formspec = formspec .. "label[4.75,3;" ..
minetest.formspec_escape("You need at least 10000" ..
"\nscore and 1.5+ KD to\naccess the pro section") .. "]"
end
formspec = formspec ..
"listring[" .. chestinv ..";main]" ..
"listring[current_player;main]"
formspec = formspec ..
"listring[" .. chestinv ..";main]" ..
"listring[current_player;main]"
minetest.show_formspec(name, "ctf_map_core:chest", formspec)
end
minetest.show_formspec(name, "ctf_map_core:chest", formspec)
end,
function def.allow_metadata_inventory_move(pos, from_list, from_index,
to_list, to_index, count, player)
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return 0
end
allow_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return 0
end
if ctf_stats.player(name).score < 10 then
return 0
end
if ctf_stats.player(name).score < 10 then
return 0
end
if (from_list ~= "pro" and to_list ~= "pro") or ctf_stats.is_pro(name) then
if to_list == "helper" then
-- handle move & overflow
local chestinv = minetest.get_inventory({type = "node", pos = pos})
local playerinv = player:get_inventory()
local stack = chestinv:get_stack(from_list, from_index)
local leftover = playerinv:add_item("main", stack)
local n_stack = stack
n_stack:set_count(stack:get_count() - leftover:get_count())
chestinv:remove_item("helper", stack)
chestinv:remove_item("pro", n_stack)
return 0
elseif from_list == "helper" then
return 0
else
return count
end
else
return 0
end
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if listname == "helper" then
return 0
end
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return 0
end
for _, itemstring in ipairs(blacklist) do
if stack:get_name() == itemstring then
return 0
end
end
local pstat = ctf_stats.player(name)
if not pstat or not pstat.score or pstat.score < 10 then
return 0
end
if listname ~= "pro" or ctf_stats.is_pro(name) then
if (from_list ~= "pro" and to_list ~= "pro") or ctf_stats.is_pro(name) then
if to_list == "helper" then
-- handle move & overflow
local chestinv = minetest.get_inventory({type = "node", pos = pos})
if chestinv:room_for_item("pro", stack) then
return stack:get_count()
else
-- handle overflow
local playerinv = player:get_inventory()
local leftovers = chestinv:add_item("pro", stack)
local leftover = chestinv:add_item("main", leftovers)
local n_stack = stack
n_stack:set_count(stack:get_count() - leftover:get_count())
playerinv:remove_item("main", n_stack)
return 0
end
local playerinv = player:get_inventory()
local stack = chestinv:get_stack(from_list, from_index)
local leftover = playerinv:add_item("main", stack)
local n_stack = stack
n_stack:set_count(stack:get_count() - leftover:get_count())
chestinv:remove_item("helper", stack)
chestinv:remove_item("pro", n_stack)
return 0
elseif from_list == "helper" then
return 0
else
return 0
end
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if listname == "helper" then
return 0
return count
end
else
return 0
end
end
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return 0
end
function def.allow_metadata_inventory_put(pos, listname, index, stack, player)
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return 0
end
if ctf_stats.player(name).score < 10 then
return 0
end
local pstat = ctf_stats.player(name)
if not pstat or not pstat.score or pstat.score < 10 then
return 0
end
if listname ~= "pro" or ctf_stats.is_pro(name) then
if not ctf_map.is_item_allowed_in_team_chest(listname, stack, player) then
return 0
end
if listname ~= "pro" or ctf_stats.is_pro(name) then
local chestinv = minetest.get_inventory({type = "node", pos = pos})
if chestinv:room_for_item("pro", stack) then
return stack:get_count()
else
-- handle overflow
local playerinv = player:get_inventory()
local leftovers = chestinv:add_item("pro", stack)
local leftover = chestinv:add_item("main", leftovers)
local n_stack = stack
n_stack:set_count(stack:get_count() - leftover:get_count())
playerinv:remove_item("main", n_stack)
return 0
end
end,
can_dig = function(pos, player)
return false
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" moves " .. (stack:get_name() or "stuff") .. " " ..
(stack:get_count() or 0) .. " to chest at " ..
minetest.pos_to_string(pos))
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" takes " .. (stack:get_name() or "stuff") .. " " ..
(stack:get_count() or 0) .. " from chest at " ..
minetest.pos_to_string(pos))
else
return 0
end
})
end
function def.allow_metadata_inventory_take(pos, listname, index, stack, player)
if listname == "helper" then
return 0
end
local name = player:get_player_name()
if chest_color ~= ctf.player(name).team then
minetest.chat_send_player(name, "You're not on team " .. chest_color)
return 0
end
if ctf_stats.player(name).score < 10 then
return 0
end
if listname ~= "pro" or ctf_stats.is_pro(name) then
return stack:get_count()
else
return 0
end
end
function def.on_metadata_inventory_put(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" moves " .. (stack:get_name() or "stuff") .. " " ..
(stack:get_count() or 0) .. " to chest at " ..
minetest.pos_to_string(pos))
end
function def.on_metadata_inventory_take(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" takes " .. (stack:get_name() or "stuff") .. " " ..
(stack:get_count() or 0) .. " from chest at " ..
minetest.pos_to_string(pos))
end
minetest.register_node("ctf_map_core:chest_" .. chest_color, def)
end

View file

@ -1,35 +0,0 @@
give_initial_stuff = {}
setmetatable(give_initial_stuff, {
__call = function(self, player)
minetest.log("action", "Giving initial stuff to player "
.. player:get_player_name())
local inv = player:get_inventory()
inv:set_list("main", {})
inv:set_list("craft", {})
inv:set_size("craft", 1)
inv:set_size("craftresult", 0)
inv:set_size("hand", 0)
local items = give_initial_stuff.get_stuff()
for _, item in pairs(items) do
inv:add_item("main", item)
end
end
})
function give_initial_stuff.get_stuff()
return ctf_map.map and ctf_map.map.initial_stuff or {
"default:pick_stone",
"default:sword_stone",
"default:torch 3",
}
end
minetest.register_on_joinplayer(function(player)
player:set_hp(player:get_properties().hp_max)
give_initial_stuff(player)
end)
minetest.register_on_respawnplayer(give_initial_stuff)

View file

@ -34,10 +34,17 @@ dofile(modpath .. "/barrier.lua")
if minetest.get_modpath("ctf") then
dofile(modpath .. "/base.lua")
dofile(modpath .. "/chest.lua")
dofile(modpath .. "/give_initial_stuff.lua")
dofile(modpath .. "/meta_helpers.lua")
dofile(modpath .. "/schem_map.lua")
dofile(modpath .. "/maps_catalog.lua")
ctf_match.register_on_build_time_end(ctf_map.remove_middle_barrier)
give_initial_stuff.register_stuff_provider(function(player)
return ctf_map.map and ctf_map.map.initial_stuff or {
"default:pick_stone",
"default:sword_stone",
"default:torch 3",
}
end)
end

View file

@ -98,7 +98,7 @@ function ctf_map.register_treasures(map)
end
else
-- If treasure is a part of map's initial stuff, don't register it
local blacklist = ctf_map.map.initial_stuff or give_initial_stuff.get_stuff()
local blacklist = ctf_map.map.initial_stuff or {}
for _, def in pairs(ctf_treasure.get_default_treasures()) do
local is_valid = true
for _, b_item in pairs(blacklist) do

View file

@ -1,3 +1,3 @@
name = ctf_map_core
depends = default
optional_depends = ctf, ctf_match, ctf_stats, ctf_treasure, stairs, wool, irc, physics
optional_depends = ctf, ctf_match, ctf_stats, ctf_treasure, stairs, wool, irc, physics, give_initial_stuff