Reworked bow loading code

This commit is contained in:
the-real-herowl 2024-12-29 04:49:43 +01:00 committed by the-real-herowl
parent 14ec7fe98f
commit fa41870bb5

@ -26,6 +26,9 @@ local bow_load = {}
-- Another player table, this one stores the wield index of the bow being charged
local bow_index = {}
-- And yet another player table, this one stores the load level of the bow
local bow_load_level = {}
-- define FOV modifier(s)
mcl_fovapi.register_modifier({
name = "bowcomplete",
@ -197,6 +200,7 @@ local function reset_bow_state(player, also_reset_bows)
bow_load[player:get_player_name()] = nil
bow_index[player:get_player_name()] = nil
bow_load_level[player:get_player_name()] = nil
if minetest.get_modpath("playerphysics") then
playerphysics.remove_physics_factor(player, "speed", "mcl_bows:use_bow")
end
@ -205,18 +209,18 @@ local function reset_bow_state(player, also_reset_bows)
end
end
-- Bow in charging state
-- Old Bows in charging state, purely for conversion if some accidentally stayed behind
for level=0, 2 do
minetest.register_tool("mcl_bows:bow_"..level, {
description = S("Bow"),
_doc_items_create_entry = false,
inventory_image = "mcl_bows_bow_"..level..".png",
inventory_image = "mcl_bows_bow.png^vl_unknown.png",
wield_scale = mcl_vars.tool_wield_scale,
stack_max = 1,
range = 0, -- Pointing range to 0 to prevent punching with bow :D
groups = {not_in_creative_inventory=1, not_in_craft_guide=1, bow=1, cannot_block=1, enchantability=1},
-- Trick to disable digging as well
on_use = function() return end,
on_use = function(_, user) reset_bow_state(user, true) return end,
on_drop = function(itemstack, dropper, pos)
reset_bow_state(dropper)
itemstack:get_meta():set_string("active", "")
@ -230,7 +234,8 @@ for level=0, 2 do
return itemstack
end,
-- Prevent accidental interaction with itemframes and other nodes
on_place = function(itemstack)
on_place = function(itemstack, placer)
reset_bow_state(placer, true)
return itemstack
end,
touch_interaction = "short_dig_long_place",
@ -241,12 +246,11 @@ end
controls.register_on_release(function(player, key, time)
if key~="RMB" and key~="zoom" then return end
--local inv = minetest.get_inventory({type="player", name=player:get_player_name()})
local wielditem = player:get_wielded_item()
if (wielditem:get_name()=="mcl_bows:bow_0" or wielditem:get_name()=="mcl_bows:bow_1" or wielditem:get_name()=="mcl_bows:bow_2" or
wielditem:get_name()=="mcl_bows:bow_0_enchanted" or wielditem:get_name()=="mcl_bows:bow_1_enchanted" or wielditem:get_name()=="mcl_bows:bow_2_enchanted") then
local enchanted = mcl_enchanting.is_enchanted(wielditem:get_name())
local name = wielditem:get_name()
if name == "mcl_bows:bow" or name == "mcl_bows:bow_enchanted" then
local meta = wielditem:get_meta()
local enchanted = mcl_enchanting.is_enchanted(name)
local speed, damage
local p_load = bow_load[player:get_player_name()]
local charge
@ -287,9 +291,9 @@ controls.register_on_release(function(player, key, time)
local has_shot = player_shoot_arrow(wielditem, player, speed, damage, is_critical)
if enchanted then
wielditem:set_name("mcl_bows:bow_enchanted")
meta:set_string("inventory_image", "mcl_bows_bow.png"..mcl_enchanting.overlay)
else
wielditem:set_name("mcl_bows:bow")
meta:set_string("inventory_image", "mcl_bows_bow.png")
end
if has_shot and not minetest.is_creative_enabled(player:get_player_name()) then
@ -312,46 +316,50 @@ controls.register_on_hold(function(player, key, time)
if (key ~= "RMB" and key ~= "zoom") or not (creative or get_arrow(player)) then
return
end
--local inv = minetest.get_inventory({type="player", name=name})
local wielditem = player:get_wielded_item()
local meta = wielditem:get_meta()
if bow_load[name] == nil
and (wielditem:get_name()=="mcl_bows:bow" or wielditem:get_name()=="mcl_bows:bow_enchanted")
and (wielditem:get_meta():get("active") or key == "zoom") and (creative or get_arrow(player)) then
and (meta:get("active") or key == "zoom") and (creative or get_arrow(player)) then
local enchanted = mcl_enchanting.is_enchanted(wielditem:get_name())
if enchanted then
wielditem:set_name("mcl_bows:bow_0_enchanted")
else
wielditem:set_name("mcl_bows:bow_0")
end
local im_string = "mcl_bows_bow_0.png"
if enchanted then im_string = im_string .. mcl_enchanting.overlay end
meta:set_string("inventory_image", im_string)
player:set_wielded_item(wielditem)
if minetest.get_modpath("playerphysics") then
-- Slow player down when using bow
playerphysics.add_physics_factor(player, "speed", "mcl_bows:use_bow", PLAYER_USE_BOW_SPEED)
end
bow_load[name] = minetest.get_us_time()
bow_load_level[name] = 0
bow_index[name] = player:get_wield_index()
core.chat_send_all("init")
-- begin Bow Zoom.
mcl_fovapi.apply_modifier(player, "bowcomplete")
else
if player:get_wield_index() == bow_index[name] then
local level = 0
if type(bow_load[name]) == "number" then
if wielditem:get_name() == "mcl_bows:bow_0" and minetest.get_us_time() - bow_load[name] >= BOW_CHARGE_TIME_HALF then
wielditem:set_name("mcl_bows:bow_1")
elseif wielditem:get_name() == "mcl_bows:bow_0_enchanted" and minetest.get_us_time() - bow_load[name] >= BOW_CHARGE_TIME_HALF then
wielditem:set_name("mcl_bows:bow_1_enchanted")
elseif wielditem:get_name() == "mcl_bows:bow_1" and minetest.get_us_time() - bow_load[name] >= BOW_CHARGE_TIME_FULL then
wielditem:set_name("mcl_bows:bow_2")
elseif wielditem:get_name() == "mcl_bows:bow_1_enchanted" and minetest.get_us_time() - bow_load[name] >= BOW_CHARGE_TIME_FULL then
wielditem:set_name("mcl_bows:bow_2_enchanted")
end
if minetest.get_us_time() - bow_load[name] >= BOW_CHARGE_TIME_FULL then
if bow_load_level[name] == 2 then return end
level = 2
bow_load_level[name] = 2
elseif minetest.get_us_time() - bow_load[name] >= BOW_CHARGE_TIME_HALF then
if bow_load_level[name] == 1 then return end
level = 1
bow_load_level[name] = 1
else return end
else
if wielditem:get_name() == "mcl_bows:bow_0" or wielditem:get_name() == "mcl_bows:bow_1" or wielditem:get_name() == "mcl_bows:bow_2" then
wielditem:set_name("mcl_bows:bow")
elseif wielditem:get_name() == "mcl_bows:bow_0_enchanted" or wielditem:get_name() == "mcl_bows:bow_1_enchanted" or wielditem:get_name() == "mcl_bows:bow_2_enchanted" then
wielditem:set_name("mcl_bows:bow_enchanted")
end
level = -1
bow_load_level[name] = nil
end
local im_string = level == -1 and "mcl_bows_bow.png" or "mcl_bows_bow_"..level..".png"
if wielditem:get_name() == "mcl_bows:bow_enchanted" then
im_string = im_string .. mcl_enchanting.overlay
end
meta:set_string("inventory_image", im_string)
core.chat_send_all(im_string)
player:set_wielded_item(wielditem)
else
reset_bow_state(player, true)
@ -364,8 +372,9 @@ minetest.register_globalstep(function(dtime)
local name = player:get_player_name()
local wielditem = player:get_wielded_item()
local wieldindex = player:get_wield_index()
--local controls = player:get_player_control()
if type(bow_load[name]) == "number" and ((wielditem:get_name()~="mcl_bows:bow_0" and wielditem:get_name()~="mcl_bows:bow_1" and wielditem:get_name()~="mcl_bows:bow_2" and wielditem:get_name()~="mcl_bows:bow_0_enchanted" and wielditem:get_name()~="mcl_bows:bow_1_enchanted" and wielditem:get_name()~="mcl_bows:bow_2_enchanted") or wieldindex ~= bow_index[name]) then
if type(bow_load[name]) == "number"
and ((wielditem:get_name()~="mcl_bows:bow" and wielditem:get_name()~="mcl_bows:bow_enchanted")
or wieldindex ~= bow_index[name]) then
reset_bow_state(player, true)
end
end
@ -403,10 +412,3 @@ minetest.register_craft({
recipe = "group:bow",
burntime = 15,
})
-- Add entry aliases for the Help
if minetest.get_modpath("doc") then
doc.add_entry_alias("tools", "mcl_bows:bow", "tools", "mcl_bows:bow_0")
doc.add_entry_alias("tools", "mcl_bows:bow", "tools", "mcl_bows:bow_1")
doc.add_entry_alias("tools", "mcl_bows:bow", "tools", "mcl_bows:bow_2")
end