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

View File

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