2018-01-27 18:45:52 +00:00
|
|
|
give_initial_stuff = {}
|
2018-01-21 22:10:37 +00:00
|
|
|
|
2020-10-13 23:22:19 +00:00
|
|
|
local tools = {"default:sword_", "default:pick_", "default:axe_", "default:shovel_"}
|
|
|
|
local tool_materials = {"stone", "steel", "bronze", "mese", "diamond"}
|
|
|
|
|
2020-05-07 08:57:37 +00:00
|
|
|
-- Add item to inv. Split item if count > stack_max using recursion
|
|
|
|
function give_initial_stuff.give_item(inv, item)
|
2020-10-13 23:22:19 +00:00
|
|
|
-- Remove any lower tier tools of the same type as the item
|
|
|
|
for _, tool in pairs(tools) do
|
|
|
|
if item:get_name():find(tool) then -- Find what tool the new item is
|
|
|
|
-- Get the 'tier' of the new item
|
|
|
|
local newtier = table.indexof(tool_materials, item:get_name():sub(tool:len() + 1))
|
|
|
|
|
|
|
|
for tier, mat in ipairs(tool_materials) do
|
|
|
|
if tier < newtier then
|
|
|
|
inv:remove_item("main", tool..mat) -- Will do nothing if tool doesn't exist
|
|
|
|
elseif inv:contains_item("main", tool..mat) then
|
|
|
|
return -- A higher tier tool is already in inventory
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Don't duplicate stacks
|
2021-02-07 20:53:20 +00:00
|
|
|
if inv:contains_item("main", item:get_name()) and not inv:contains_item("main", "shooter_rocket:rocket") then
|
2020-11-16 17:03:12 +00:00
|
|
|
local safeguard = 0
|
|
|
|
local itemcount = item:get_count()-5
|
|
|
|
if itemcount < 0 then itemcount = 0 end
|
|
|
|
|
|
|
|
-- Replace stack if it's smaller than what we want to add
|
|
|
|
while not inv:contains_item("main", ("%s %d"):format(item:get_name(), itemcount)) do
|
2020-10-13 23:22:19 +00:00
|
|
|
safeguard = safeguard + 1
|
2020-11-16 17:03:12 +00:00
|
|
|
|
|
|
|
inv:add_item("main", item:get_name() .. " 5")
|
2020-10-13 23:22:19 +00:00
|
|
|
|
|
|
|
if safeguard >= 500 then
|
|
|
|
minetest.log("error", "[give_initial_stuff] Something went wrong when filling stack "..dump(item:get_name()))
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-05-07 08:57:37 +00:00
|
|
|
inv:add_item("main", item:take_item(item:get_stack_max()))
|
|
|
|
|
|
|
|
-- If item isn't empty, add the leftovers again
|
|
|
|
if not item:is_empty() then
|
|
|
|
give_initial_stuff.give_item(inv, item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-27 18:45:52 +00:00
|
|
|
setmetatable(give_initial_stuff, {
|
2020-10-13 23:22:19 +00:00
|
|
|
__call = function(self, player, mode)
|
2019-03-19 16:54:43 +00:00
|
|
|
minetest.log("action", "Giving initial stuff to player "
|
|
|
|
.. player:get_player_name())
|
2018-01-27 18:45:52 +00:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
|
2020-10-13 23:22:19 +00:00
|
|
|
if mode ~= "replace_tools" then
|
|
|
|
inv:set_list("main", {})
|
|
|
|
inv:set_list("craft", {})
|
|
|
|
|
|
|
|
inv:set_size("craft", 1)
|
|
|
|
inv:set_size("craftresult", 0)
|
|
|
|
inv:set_size("hand", 0)
|
|
|
|
end
|
2018-05-04 18:01:53 +00:00
|
|
|
|
2020-03-13 21:58:42 +00:00
|
|
|
local items = give_initial_stuff.get_stuff(player)
|
2018-01-27 18:45:52 +00:00
|
|
|
|
|
|
|
for _, item in pairs(items) do
|
2020-10-13 23:22:19 +00:00
|
|
|
if mode == "replace_tools" then
|
|
|
|
for _, tool in pairs(tools) do
|
|
|
|
if item:find(tool) then
|
|
|
|
give_initial_stuff.give_item(inv, ItemStack(item))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
give_initial_stuff.give_item(inv, ItemStack(item))
|
|
|
|
end
|
2018-01-27 18:45:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2020-03-13 21:58:42 +00:00
|
|
|
local registered_stuff_providers = {}
|
|
|
|
function give_initial_stuff.register_stuff_provider(func, priority)
|
|
|
|
table.insert(registered_stuff_providers,
|
|
|
|
priority or (#registered_stuff_providers + 1),
|
|
|
|
func)
|
|
|
|
end
|
|
|
|
|
|
|
|
function give_initial_stuff.get_stuff(player)
|
2020-03-14 17:22:55 +00:00
|
|
|
local seen_stuff = {}
|
|
|
|
|
2020-03-13 21:58:42 +00:00
|
|
|
local stuff = {}
|
|
|
|
for i=1, #registered_stuff_providers do
|
|
|
|
local new_stuff = registered_stuff_providers[i](player)
|
|
|
|
assert(new_stuff)
|
|
|
|
|
|
|
|
for j=1, #new_stuff do
|
2020-03-14 17:22:55 +00:00
|
|
|
local name = ItemStack(new_stuff[j]):get_name()
|
|
|
|
if not seen_stuff[name] then
|
|
|
|
seen_stuff[name] = true
|
|
|
|
stuff[#stuff + 1] = new_stuff[j]
|
|
|
|
end
|
2020-03-13 21:58:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return stuff
|
2018-01-21 22:10:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2019-03-21 01:36:59 +00:00
|
|
|
player:set_hp(player:get_properties().hp_max)
|
2018-01-21 22:10:37 +00:00
|
|
|
give_initial_stuff(player)
|
|
|
|
end)
|
|
|
|
minetest.register_on_respawnplayer(give_initial_stuff)
|