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
def.properties.can_capture = true
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.max_hp = def.properties.max_hp or 20
end

View file

@ -42,7 +42,7 @@ function ctf_classes.show_gui(name, player)
end
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] = tostring(i * 0.5 - 0.4)
fs[#fs + 1] = ",2.25;0.5,0.5;"

View file

@ -21,7 +21,7 @@ ctf_classes.register("knight", {
max_hp = 30,
speed = 0.90,
items = {
initial_stuff = {
"default:sword_steel",
},
@ -39,11 +39,16 @@ ctf_classes.register("shooter", {
cons = {},
color = "#c60",
properties = {
items = {
initial_stuff = {
"shooter:rifle",
"shooter:grapple_gun_loaded",
},
additional_item_blacklist = {
"shooter:grapple_gun",
"shooter:grapple_hook",
},
allowed_guns = {
"shooter:pistol",
"shooter:rifle",
@ -64,7 +69,7 @@ ctf_classes.register("medic", {
cons = {},
color = "#0af",
properties = {
items = {
initial_stuff = {
"ctf_bandages:bandage 20",
},
@ -82,11 +87,15 @@ ctf_classes.register("rocketeer", {
cons = {},
color = "#fa0",
properties = {
items = {
initial_stuff = {
"shooter:rocket_gun_loaded",
"shooter:rocket 4",
},
additional_item_blacklist = {
"shooter:rocket_gun",
},
allowed_guns = {
"shooter:pistol",
"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 map = {}
for i = 1, #stacks do
@ -31,12 +6,42 @@ local function stack_list_to_map(stacks)
return map
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
minetest.item_drop = function(itemstack, player, pos)
local class = ctf_classes.get(player)
local items = stack_list_to_map(class.properties.items or {})
if items[itemstack:get_name()] then
if is_class_blacklisted(player, itemstack:get_name()) then
minetest.chat_send_player(player:get_player_name(),
"You're not allowed to drop class items!")
return itemstack
@ -47,10 +52,7 @@ end
local old_is_allowed = ctf_map.is_item_allowed_in_team_chest
ctf_map.is_item_allowed_in_team_chest = function(listname, stack, player)
local class = ctf_classes.get(player)
local items = stack_list_to_map(class.properties.items or {})
if items[stack:get_name()] then
if is_class_blacklisted(player, stack:get_name()) then
minetest.chat_send_player(player:get_player_name(),
"You're not allowed to put class items in the chest!")
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)
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
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

View file

@ -194,3 +194,22 @@ minetest.register_chatcommand("transfer_rankings", {
return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'."
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 sname = it:get_name()
for _, item in pairs(blacklist_drop) do
if sname == item then
minetest.log("info", "[dropondie] Not dropping " .. sname)
return
for i=1, #registered_drop_filters do
if not registered_drop_filters[i](player, sname) then
return itemstack
end
end
@ -24,9 +37,9 @@ local function drop(pos, itemstack)
return itemstack
end
local function drop_list(pos, inv, list)
local function drop_list(player, pos, inv, list)
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)
end
end
@ -39,8 +52,8 @@ local function drop_all(player)
for _, item in pairs(give_initial_stuff.get_stuff(player)) do
inv:remove_item("main", ItemStack(item))
end
drop_list(pos, inv, "main")
drop_list(pos, inv, "craft")
drop_list(player, pos, inv, "main")
drop_list(player, pos, inv, "craft")
end
minetest.register_on_dieplayer(drop_all)