Fix variants of class items being allowed to drop

This commit is contained in:
rubenwardy 2020-03-14 17:50:24 +00:00
parent 93693a8dfd
commit d973815a18
7 changed files with 112 additions and 49 deletions

View file

@ -11,6 +11,22 @@ function ctf_classes.register(cname, def)
if def.properties.can_capture == nil then if def.properties.can_capture == nil then
def.properties.can_capture = true def.properties.can_capture = true
end end
def.properties.initial_stuff = def.properties.initial_stuff or {}
if not def.properties.item_blacklist then
def.properties.item_blacklist = {}
for i=1, #def.properties.initial_stuff do
table.insert(def.properties.item_blacklist, def.properties.initial_stuff[i])
end
end
if def.properties.additional_item_blacklist then
for i=1, #def.properties.additional_item_blacklist do
table.insert(def.properties.item_blacklist, def.properties.additional_item_blacklist[i])
end
end
def.properties.speed = def.properties.speed or 1 def.properties.speed = def.properties.speed or 1
def.properties.max_hp = def.properties.max_hp or 20 def.properties.max_hp = def.properties.max_hp or 20
end end

View file

@ -42,7 +42,7 @@ function ctf_classes.show_gui(name, player)
end end
fs[#fs + 1] = "]" fs[#fs + 1] = "]"
for i, item in pairs(class.properties.items or {}) do for i, item in pairs(class.properties.initial_stuff) do
fs[#fs + 1] = "item_image[" fs[#fs + 1] = "item_image["
fs[#fs + 1] = tostring(i * 0.5 - 0.4) fs[#fs + 1] = tostring(i * 0.5 - 0.4)
fs[#fs + 1] = ",2.25;0.5,0.5;" fs[#fs + 1] = ",2.25;0.5,0.5;"

View file

@ -21,7 +21,7 @@ ctf_classes.register("knight", {
max_hp = 30, max_hp = 30,
speed = 0.90, speed = 0.90,
items = { initial_stuff = {
"default:sword_steel", "default:sword_steel",
}, },
@ -39,11 +39,16 @@ ctf_classes.register("shooter", {
cons = {}, cons = {},
color = "#c60", color = "#c60",
properties = { properties = {
items = { initial_stuff = {
"shooter:rifle", "shooter:rifle",
"shooter:grapple_gun_loaded", "shooter:grapple_gun_loaded",
}, },
additional_item_blacklist = {
"shooter:grapple_gun",
"shooter:grapple_hook",
},
allowed_guns = { allowed_guns = {
"shooter:pistol", "shooter:pistol",
"shooter:rifle", "shooter:rifle",
@ -64,7 +69,7 @@ ctf_classes.register("medic", {
cons = {}, cons = {},
color = "#0af", color = "#0af",
properties = { properties = {
items = { initial_stuff = {
"ctf_bandages:bandage 20", "ctf_bandages:bandage 20",
}, },
@ -82,11 +87,15 @@ ctf_classes.register("rocketeer", {
cons = {}, cons = {},
color = "#fa0", color = "#fa0",
properties = { properties = {
items = { initial_stuff = {
"shooter:rocket_gun_loaded", "shooter:rocket_gun_loaded",
"shooter:rocket 4", "shooter:rocket 4",
}, },
additional_item_blacklist = {
"shooter:rocket_gun",
},
allowed_guns = { allowed_guns = {
"shooter:pistol", "shooter:pistol",
"shooter:smg", "shooter:smg",

View file

@ -1,28 +1,3 @@
give_initial_stuff.register_stuff_provider(function(player)
local class = ctf_classes.get(player)
return class.properties.items or {}
end, 1)
ctf_classes.register_on_changed(function(player, old, new)
local inv = player:get_inventory()
if old then
local items = old.properties.items or {}
for i = 1, #items do
inv:remove_item("main", ItemStack(items[i]))
end
end
do
assert(new)
local items = new.properties.items or {}
for i = 1, #items do
inv:add_item("main", ItemStack(items[i]))
end
end
end)
local function stack_list_to_map(stacks) local function stack_list_to_map(stacks)
local map = {} local map = {}
for i = 1, #stacks do for i = 1, #stacks do
@ -31,12 +6,42 @@ local function stack_list_to_map(stacks)
return map return map
end end
-- Returns true if the item shouldn't be allowed to be dropped etc
local function is_class_blacklisted(player, itemname)
local class = ctf_classes.get(player)
local items = stack_list_to_map(class.properties.item_blacklist or {})
return items[itemname]
end
give_initial_stuff.register_stuff_provider(function(player)
local class = ctf_classes.get(player)
return class.properties.initial_stuff or {}
end, 1)
ctf_classes.register_on_changed(function(player, old, new)
local inv = player:get_inventory()
if old then
local items = old.properties.item_blacklist or {}
for i = 1, #items do
inv:remove_item("main", ItemStack(items[i]))
end
end
do
assert(new)
local items = new.properties.initial_stuff or {}
for i = 1, #items do
inv:add_item("main", ItemStack(items[i]))
end
end
end)
local old_item_drop = minetest.item_drop local old_item_drop = minetest.item_drop
minetest.item_drop = function(itemstack, player, pos) minetest.item_drop = function(itemstack, player, pos)
local class = ctf_classes.get(player) if is_class_blacklisted(player, itemstack:get_name()) then
local items = stack_list_to_map(class.properties.items or {})
if items[itemstack:get_name()] then
minetest.chat_send_player(player:get_player_name(), minetest.chat_send_player(player:get_player_name(),
"You're not allowed to drop class items!") "You're not allowed to drop class items!")
return itemstack return itemstack
@ -47,10 +52,7 @@ end
local old_is_allowed = ctf_map.is_item_allowed_in_team_chest local old_is_allowed = ctf_map.is_item_allowed_in_team_chest
ctf_map.is_item_allowed_in_team_chest = function(listname, stack, player) ctf_map.is_item_allowed_in_team_chest = function(listname, stack, player)
local class = ctf_classes.get(player) if is_class_blacklisted(player, stack:get_name()) then
local items = stack_list_to_map(class.properties.items or {})
if items[stack:get_name()] then
minetest.chat_send_player(player:get_player_name(), minetest.chat_send_player(player:get_player_name(),
"You're not allowed to put class items in the chest!") "You're not allowed to put class items in the chest!")
return false return false
@ -58,3 +60,7 @@ ctf_map.is_item_allowed_in_team_chest = function(listname, stack, player)
return old_is_allowed(listname, stack, player) return old_is_allowed(listname, stack, player)
end end
end end
dropondie.register_drop_filter(function(player, itemname)
return not is_class_blacklisted(player, itemname)
end)

View file

@ -1,3 +1,3 @@
name = ctf_classes name = ctf_classes
depends = ctf, ctf_flag, ctf_colors, ctf_map_core, physics, shooter, hpregen, give_initial_stuff depends = ctf, ctf_flag, ctf_colors, ctf_map_core, physics, shooter, hpregen, give_initial_stuff, dropondie
description = Adds classes, including knight, shooter, and medic description = Adds classes, including knight, shooter, and medic

View file

@ -194,3 +194,22 @@ minetest.register_chatcommand("transfer_rankings", {
return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'." return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'."
end end
}) })
minetest.register_chatcommand("makepro", {
description = "Make self a pro",
privs = {ctf_admin = true},
func = function(name, param)
local stats, _ = ctf_stats.player(name)
if stats.kills < 1.5 * (stats.deaths + 1) then
stats.kills = 1.51 * (stats.deaths + 1)
end
if stats.score < 10000 then
stats.score = 10000
end
return true, "Done"
end
})

View file

@ -1,13 +1,26 @@
local blacklist_drop = {} dropondie = {}
local function drop(pos, itemstack) local registered_drop_filters = {}
-- return true to drop, false to destroy
function dropondie.register_drop_filter(func, priority)
table.insert(registered_drop_filters,
priority or (#registered_drop_filters + 1),
func)
end
local blacklist_drop = {}
dropondie.register_drop_filter(function(player, itemname)
return table.indexof(blacklist_drop, itemname) == -1
end)
local function drop(player, pos, itemstack)
local it = itemstack:take_item(itemstack:get_count()) local it = itemstack:take_item(itemstack:get_count())
local sname = it:get_name() local sname = it:get_name()
for _, item in pairs(blacklist_drop) do for i=1, #registered_drop_filters do
if sname == item then if not registered_drop_filters[i](player, sname) then
minetest.log("info", "[dropondie] Not dropping " .. sname) return itemstack
return
end end
end end
@ -24,9 +37,9 @@ local function drop(pos, itemstack)
return itemstack return itemstack
end end
local function drop_list(pos, inv, list) local function drop_list(player, pos, inv, list)
for i = 1, inv:get_size(list) do for i = 1, inv:get_size(list) do
drop(pos, inv:get_stack(list, i)) drop(player, pos, inv:get_stack(list, i))
inv:set_stack(list, i, nil) inv:set_stack(list, i, nil)
end end
end end
@ -39,8 +52,8 @@ local function drop_all(player)
for _, item in pairs(give_initial_stuff.get_stuff(player)) do for _, item in pairs(give_initial_stuff.get_stuff(player)) do
inv:remove_item("main", ItemStack(item)) inv:remove_item("main", ItemStack(item))
end end
drop_list(pos, inv, "main") drop_list(player, pos, inv, "main")
drop_list(pos, inv, "craft") drop_list(player, pos, inv, "craft")
end end
minetest.register_on_dieplayer(drop_all) minetest.register_on_dieplayer(drop_all)