minetest_x_bows/init.lua

356 lines
13 KiB
Lua
Raw Normal View History

2022-11-28 14:46:55 -06:00
--[[
X Bows. Adds bow and arrows with API.
2024-01-04 17:11:46 -06:00
Copyright (C) 2024 SaKeL
2022-11-28 14:46:55 -06:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to juraj.vajda@gmail.com
--]]
2022-10-12 20:13:06 -05:00
2022-11-03 12:06:43 -05:00
math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 9))--[[@as number]] )
2022-10-16 15:02:04 -05:00
2022-10-20 08:13:35 -05:00
local path = minetest.get_modpath('x_bows')
2021-03-13 20:39:42 -06:00
local mod_start_time = minetest.get_us_time()
local bow_charged_timer = 0
2022-10-20 08:13:35 -05:00
dofile(path .. '/api.lua')
dofile(path .. '/particle_effects.lua')
dofile(path .. '/nodes.lua')
dofile(path .. '/arrow.lua')
dofile(path .. '/items.lua')
2022-10-26 10:55:42 -05:00
if XBows.i3 then
2022-10-26 08:45:59 -05:00
XBowsQuiver:i3_register_page()
2022-10-26 10:55:42 -05:00
elseif XBows.unified_inventory then
2022-10-26 08:45:59 -05:00
XBowsQuiver:ui_register_page()
else
XBowsQuiver:sfinv_register_page()
end
minetest.register_on_joinplayer(function(player)
2022-11-03 12:06:43 -05:00
local inv_quiver = player:get_inventory() --[[@as InvRef]]
local inv_arrow = player:get_inventory() --[[@as InvRef]]
2022-11-01 22:10:47 -05:00
if XBows.settings.x_bows_show_3d_quiver and XBows.player_api then
---Order matters here
if XBows.skinsdb then
player_api.set_model(player, 'skinsdb_3d_armor_character_5.b3d')
elseif XBows._3d_armor then
2022-10-31 19:49:08 -05:00
player_api.set_model(player, 'x_bows_3d_armor_character.b3d')
else
player_api.set_model(player, 'x_bows_character.b3d')
end
2022-10-27 14:38:51 -05:00
end
inv_quiver:set_size('x_bows:quiver_inv', 1 * 1)
inv_arrow:set_size('x_bows:arrow_inv', 1 * 1)
2022-10-26 08:45:59 -05:00
2022-10-27 14:38:51 -05:00
local quiver_stack = player:get_inventory():get_stack('x_bows:quiver_inv', 1)
2022-10-26 08:45:59 -05:00
2022-10-27 14:38:51 -05:00
if quiver_stack and not quiver_stack:is_empty() then
local st_meta = quiver_stack:get_meta()
2022-10-26 08:45:59 -05:00
local quiver_id = st_meta:get_string('quiver_id')
2022-10-27 14:38:51 -05:00
---create detached inventory
2022-10-28 08:55:18 -05:00
local detached_inv = XBowsQuiver:get_or_create_detached_inv(
2022-10-26 08:45:59 -05:00
quiver_id,
player:get_player_name(),
st_meta:get_string('quiver_items')
)
2022-10-27 14:38:51 -05:00
---set model textures
2022-10-28 08:55:18 -05:00
if detached_inv:is_empty('main') then
XBowsQuiver.quiver_empty_state[player:get_player_name()] = false
2022-11-03 12:06:43 -05:00
XBowsQuiver:show_3d_quiver(player, { is_empty = true })
2022-10-28 08:55:18 -05:00
else
XBowsQuiver.quiver_empty_state[player:get_player_name()] = true
XBowsQuiver:show_3d_quiver(player)
end
2022-10-27 14:38:51 -05:00
else
---set model textures
2022-10-28 08:55:18 -05:00
XBowsQuiver:hide_3d_quiver(player)
2022-10-27 14:38:51 -05:00
end
2022-10-27 14:38:51 -05:00
XBows:reset_charged_bow(player, true)
XBowsQuiver:close_quiver(player)
end)
2022-11-01 22:10:47 -05:00
if XBows.settings.x_bows_show_3d_quiver and XBows.player_api then
local model_name = 'x_bows_character.b3d'
if XBows.skinsdb then
---skinsdb
model_name = 'skinsdb_3d_armor_character_5.b3d'
elseif XBows._3d_armor then
---3d armor
model_name = 'x_bows_3d_armor_character.b3d'
end
2022-10-31 19:49:08 -05:00
player_api.register_model(model_name, {
animation_speed = 30,
2022-11-03 12:06:43 -05:00
textures = { 'character.png' },
2022-10-31 19:49:08 -05:00
animations = {
-- Standard animations.
2022-11-03 12:06:43 -05:00
stand = { x = 0, y = 79 },
lay = { x = 162, y = 166, eye_height = 0.3, override_local = true,
collisionbox = { -0.6, 0.0, -0.6, 0.6, 0.3, 0.6 } },
walk = { x = 168, y = 187 },
mine = { x = 189, y = 198 },
walk_mine = { x = 200, y = 219 },
sit = { x = 81, y = 160, eye_height = 0.8, override_local = true,
collisionbox = { -0.3, 0.0, -0.3, 0.3, 1.0, 0.3 } }
2022-10-31 19:49:08 -05:00
},
2022-11-03 12:06:43 -05:00
collisionbox = { -0.3, 0.0, -0.3, 0.3, 1.7, 0.3 },
2022-10-31 19:49:08 -05:00
stepheight = 0.6,
eye_height = 1.47
})
end
2022-10-26 10:55:42 -05:00
---formspec callbacks
minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
---arrow inventory
if action == 'move' and inventory_info.to_list == 'x_bows:arrow_inv' then
local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
if minetest.get_item_group(stack:get_name(), 'arrow') ~= 0 then
return inventory_info.count
else
return 0
end
elseif action == 'move' and inventory_info.from_list == 'x_bows:arrow_inv' and inventory_info.to_list ~= 'x_bows:quiver_inv' then
2022-10-26 10:55:42 -05:00
local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
if minetest.get_item_group(stack:get_name(), 'arrow') ~= 0 then
return inventory_info.count
else
return 0
end
elseif action == 'put' and inventory_info.listname == 'x_bows:arrow_inv' then
if minetest.get_item_group(inventory_info.stack:get_name(), 'arrow') ~= 0 then
return inventory_info.stack:get_count()
else
return 0
end
elseif action == 'take' and inventory_info.listname == 'x_bows:arrow_inv' then
if minetest.get_item_group(inventory_info.stack:get_name(), 'arrow') ~= 0 then
return inventory_info.stack:get_count()
else
return 0
end
end
---quiver inventory
if action == 'move' and inventory_info.to_list == 'x_bows:quiver_inv' then
local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
if minetest.get_item_group(stack:get_name(), 'quiver') ~= 0 then
return inventory_info.count
else
return 0
end
elseif action == 'move' and inventory_info.from_list == 'x_bows:quiver_inv' then
local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
if minetest.get_item_group(stack:get_name(), 'quiver') ~= 0 then
return inventory_info.count
else
return 0
end
elseif action == 'put' and inventory_info.listname == 'x_bows:quiver_inv' then
if minetest.get_item_group(inventory_info.stack:get_name(), 'quiver') ~= 0 then
return inventory_info.stack:get_count()
else
return 0
end
elseif action == 'take' and inventory_info.listname == 'x_bows:quiver_inv' then
if minetest.get_item_group(inventory_info.stack:get_name(), 'quiver') ~= 0 then
return inventory_info.stack:get_count()
else
return 0
end
end
return inventory_info.count or inventory_info.stack:get_count()
end)
minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
---arrow
if action == 'move' and inventory_info.to_list == 'x_bows:arrow_inv' then
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
elseif action == 'move' and inventory_info.from_list == 'x_bows:arrow_inv' then
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
elseif action == 'put' and inventory_info.listname == 'x_bows:arrow_inv' then
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
elseif action == 'take' and inventory_info.listname == 'x_bows:arrow_inv' then
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
end
---quiver
if action == 'move' and inventory_info.to_list == 'x_bows:quiver_inv' then
local stack = inventory:get_stack(inventory_info.to_list, inventory_info.to_index)
---init detached inventory if not already
local st_meta = stack:get_meta()
local quiver_id = st_meta:get_string('quiver_id')
if quiver_id == '' then
2022-11-03 12:06:43 -05:00
quiver_id = stack:get_name() .. '_' .. XBows.uuid()
2022-10-26 10:55:42 -05:00
st_meta:set_string('quiver_id', quiver_id)
inventory:set_stack(inventory_info.to_list, inventory_info.to_index, stack)
end
2022-10-28 08:55:18 -05:00
local detached_inv = XBowsQuiver:get_or_create_detached_inv(
2022-10-26 10:55:42 -05:00
quiver_id,
player:get_player_name(),
st_meta:get_string('quiver_items')
)
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
2022-10-27 14:38:51 -05:00
---set player visual
2022-10-28 08:55:18 -05:00
if detached_inv:is_empty('main') then
XBowsQuiver.quiver_empty_state[player:get_player_name()] = false
2022-11-03 12:06:43 -05:00
XBowsQuiver:show_3d_quiver(player, { is_empty = true })
2022-10-28 08:55:18 -05:00
else
XBowsQuiver.quiver_empty_state[player:get_player_name()] = true
XBowsQuiver:show_3d_quiver(player)
end
2022-10-26 10:55:42 -05:00
elseif action == 'move' and inventory_info.from_list == 'x_bows:quiver_inv' then
2022-10-27 14:38:51 -05:00
local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
2022-10-26 10:55:42 -05:00
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
2022-10-27 14:38:51 -05:00
---set player visual
if stack:is_empty() then
2022-10-28 08:55:18 -05:00
XBowsQuiver:hide_3d_quiver(player)
2022-10-27 14:38:51 -05:00
end
2022-10-26 10:55:42 -05:00
elseif action == 'put' and inventory_info.listname == 'x_bows:quiver_inv' then
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
elseif action == 'take' and inventory_info.listname == 'x_bows:quiver_inv' then
if XBows.i3 then
i3.set_fs(player)
elseif XBows.unified_inventory then
unified_inventory.set_inventory_formspec(player, 'x_bows:quiver_page')
else
sfinv.set_player_inventory_formspec(player)
end
---set player visual
if inventory:is_empty(inventory_info.listname) then
XBowsQuiver:hide_3d_quiver(player)
end
2022-10-26 10:55:42 -05:00
end
end)
2022-10-28 08:55:18 -05:00
minetest.register_on_player_receive_fields(function(player, formname, fields)
if player and fields.quit then
XBowsQuiver:close_quiver(player, formname)
end
end)
2022-10-21 12:37:35 -05:00
---backwards compatibility
minetest.register_alias('x_bows:arrow_diamond_tipped_poison', 'x_bows:arrow_diamond')
2021-03-13 20:39:42 -06:00
-- sneak, fov adjustments when bow is charged
minetest.register_globalstep(function(dtime)
2022-10-16 15:02:04 -05:00
bow_charged_timer = bow_charged_timer + dtime
if bow_charged_timer > 0.5 then
for _, player in ipairs(minetest.get_connected_players()) do
2022-10-20 08:13:35 -05:00
local player_name = player:get_player_name()
local wielded_stack = player:get_wielded_item()
local wielded_stack_name = wielded_stack:get_name()
2022-10-16 15:02:04 -05:00
2022-10-20 08:13:35 -05:00
if not wielded_stack_name then
2022-10-16 15:02:04 -05:00
return
end
2022-10-20 08:13:35 -05:00
if not XBows.player_bow_sneak[player_name] then
XBows.player_bow_sneak[player_name] = {}
2022-10-16 15:02:04 -05:00
end
2022-11-03 12:06:43 -05:00
if minetest.get_item_group(wielded_stack_name, 'bow_charged') ~= 0
and not XBows.player_bow_sneak[player_name].sneak
then
2022-10-24 15:31:23 -05:00
--charged weapon
2022-10-20 08:13:35 -05:00
if XBows.playerphysics then
playerphysics.add_physics_factor(player, 'speed', 'x_bows:bow_charged_speed', 0.25)
elseif XBows.player_monoids then
player_monoids.speed:add_change(player, 0.25, 'x_bows:bow_charged_speed')
2022-10-16 15:02:04 -05:00
end
2022-10-20 08:13:35 -05:00
XBows.player_bow_sneak[player_name].sneak = true
2022-10-16 15:02:04 -05:00
player:set_fov(0.9, true, 0.4)
2022-11-03 12:06:43 -05:00
elseif minetest.get_item_group(wielded_stack_name, 'bow_charged') == 0
and XBows.player_bow_sneak[player_name].sneak
then
2022-10-20 08:13:35 -05:00
if XBows.playerphysics then
playerphysics.remove_physics_factor(player, 'speed', 'x_bows:bow_charged_speed')
elseif XBows.player_monoids then
player_monoids.speed:del_change(player, 'x_bows:bow_charged_speed')
2022-10-16 15:02:04 -05:00
end
2022-10-20 08:13:35 -05:00
XBows.player_bow_sneak[player_name].sneak = false
2022-10-16 15:02:04 -05:00
player:set_fov(0, true, 0.4)
end
2022-10-20 08:13:35 -05:00
XBows:reset_charged_bow(player)
2022-10-16 15:02:04 -05:00
end
bow_charged_timer = 0
end
2021-03-13 20:39:42 -06:00
end)
2021-03-01 12:44:27 -06:00
2021-03-13 20:39:42 -06:00
local mod_end_time = (minetest.get_us_time() - mod_start_time) / 1000000
2021-03-01 12:44:27 -06:00
2022-11-03 12:06:43 -05:00
print('[Mod] x_bows loaded.. [' .. mod_end_time .. 's]')