Added 'offhand' parameter to the callbacks, some code cleanups

This commit is contained in:
Andrey2470T 2024-03-09 20:24:52 +03:00
parent 8167c04b83
commit f3fd2f6eaa
9 changed files with 36 additions and 33 deletions

@ -321,7 +321,7 @@ view_bobbing_amount (View bobbing factor) float 1.0 0.0 7.9
fall_bobbing_amount (Fall bobbing factor) float 0.03 0.0 100.0
# Draw main hand on left side of screen, offhand on right side.
mirror_hands (Mirror hands) bool false
swap_hands (Swap hands) bool false
[**Camera]
@ -2402,7 +2402,6 @@ keymap_screenshot (Screenshot) key KEY_F12
keymap_drop (Drop item key) key KEY_KEY_Q
# Key for swapping items between main hand and offhand.
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
keymap_swap_offhand (Swap hand items) key KEY_KEY_F
# Key to use view zoom when possible.

@ -9182,17 +9182,19 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and
-- When item is used with the 'punch/mine' key pointing at nothing (air)
},
on_place = function(itemstack, placer, pointed_thing),
-- When the 'place' key was pressed with the item one of the hands
on_place = function(itemstack, placer, pointed_thing, offhand),
-- When the 'place' key was pressed with the item in one of the hands
-- and a node was pointed at.
-- 'itemstack' may be the offhand item in cases where the main hand has
-- no on_place handler and no node_placement_prediction.
-- 'offhand' is a boolean indicating whether the callback was called
-- from the item of the main hand (false) or the offhand (true).
-- Shall place item and return the leftover itemstack
-- or nil to not modify the inventory.
-- The placer may be any ObjectRef or nil.
-- default: minetest.item_place
on_secondary_use = function(itemstack, user, pointed_thing),
on_secondary_use = function(itemstack, user, pointed_thing, offhand),
-- Same as on_place but called when not pointing at a node,
-- whereas `user` is the same as `placer` above.
-- default: nil

@ -46,10 +46,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define WIELDMESH_AMPLITUDE_Y 10.0f
// Returns the fractional part of x
inline f32 my_modf(f32 x)
inline f32 frac_part(f32 x)
{
f32 dummy;
return modff(x, &dummy);
f32 integral_part;
return modff(x, &integral_part);
}
WieldNode::WieldNode(HandIndex index, Client *client, scene::ISceneManager *mgr) :
@ -65,7 +65,7 @@ WieldNode::WieldNode(HandIndex index, Client *client, scene::ISceneManager *mgr)
int WieldNode::getDirection()
{
return g_settings->getBool("mirror_hands") ? -m_direction : m_direction;
return g_settings->getBool("swap_hands") ? -m_direction : m_direction;
}
void WieldNode::step(f32 dtime)
@ -100,7 +100,7 @@ void WieldNode::step(f32 dtime)
}
}
static inline v2f dir(const v2f &pos_dist)
static inline v2f get_arm_dir(const v2f &pos_dist)
{
f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
@ -166,7 +166,7 @@ void WieldNode::addArmInertia(f32 player_yaw, v3f camera_direction)
WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
}
m_arm_dir = dir(m_offset);
m_arm_dir = get_arm_dir(m_offset);
} else {
/*
Now the arm gets back to its default position when the camera stops,
@ -243,10 +243,10 @@ void WieldNode::update(video::SColor player_light_color, f32 view_bobbing_anim,
rot *= core::RADTODEG;
pos.X *= direction;
} else {
f32 bobfrac = my_modf(view_bobbing_anim);
f32 bobfrac = frac_part(view_bobbing_anim);
pos.X *= direction;
pos.X -= sin(bobfrac*M_PI*2.0+M_PI*m_index) * 3.0 * direction;
pos.Y += sin(my_modf(bobfrac*2.0)*M_PI+M_PI*m_index) * 3.0;
pos.Y += sin(frac_part(bobfrac*2.0)*M_PI+M_PI*m_index) * 3.0;
}
m_meshnode->setPosition(pos);
@ -413,7 +413,7 @@ void Camera::step(f32 dtime)
}
else {
float was = m_view_bobbing_anim;
m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
m_view_bobbing_anim = frac_part(m_view_bobbing_anim + offset);
bool step = (was == 0 ||
(was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
(was > 0.5f && m_view_bobbing_anim <= 0.5f));
@ -522,7 +522,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
m_camera_mode < CAMERA_MODE_THIRD) {
f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
f32 bobfrac = frac_part(m_view_bobbing_anim * 2);
f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
f32 bobknob = 1.2;

@ -2221,7 +2221,6 @@ void Game::dropSelectedItem(bool single_item)
void Game::swapOffhand()
{
IMoveAction *a = new IMoveAction();
a->count = 0;
a->from_inv.setCurrentPlayer();
@ -2235,12 +2234,8 @@ void Game::swapOffhand()
client->getEnv().getLocalPlayer()->getWieldedItem(&selected, nullptr);
if (selected.name == "") {
auto tmp_list = a->from_list;
auto tmp_i = a->from_i;
a->from_list = a->to_list;
a->from_i = a->to_i;
a->to_list = tmp_list;
a->to_i = tmp_i;
std::swap(a->from_list, a->to_list);
std::swap(a->from_i, a->to_i);
}
client->inventoryAction(a);

@ -223,7 +223,7 @@ void set_default_settings()
settings->setDefault("enable_clouds", "true");
settings->setDefault("view_bobbing_amount", "1.0");
settings->setDefault("fall_bobbing_amount", "0.03");
settings->setDefault("mirror_hands", "false");
settings->setDefault("swap_hands", "false");
settings->setDefault("enable_3d_clouds", "true");
settings->setDefault("cloud_radius", "12");
settings->setDefault("menu_clouds", "true");

@ -1265,7 +1265,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// Do stuff
playersao->getPlayer()->current_used_hand = used_hand;
if (m_script->item_OnSecondaryUse(used_hand == MAINHAND ? main_item : offhand_item, playersao, pointed)) {
if (m_script->item_OnSecondaryUse(used_hand == MAINHAND ? main_item : offhand_item,
playersao, pointed, (bool)used_hand)) {
if (used_hand == OFFHAND
? (offhand_item.has_value() && playersao->setOffhandWieldedItem(*offhand_item))
: (main_item.has_value() && playersao->setWieldedItem(*main_item)))
@ -1278,7 +1279,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
}
else {
playersao->getPlayer()->current_used_hand = used_hand;
if (m_script->item_OnPlace(used_hand == MAINHAND ? main_item : offhand_item, playersao, pointed)) {
if (m_script->item_OnPlace(used_hand == MAINHAND ? main_item : offhand_item,
playersao, pointed, (bool)used_hand)) {
// Placement was handled in lua
// Apply returned ItemStack
@ -1345,7 +1347,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
pointed.type = POINTEDTHING_NOTHING; // can only ever be NOTHING
playersao->getPlayer()->current_used_hand = used_hand;
if (m_script->item_OnSecondaryUse(used_hand == MAINHAND ? main_item : offhand_item, playersao, pointed)) {
if (m_script->item_OnSecondaryUse(used_hand == MAINHAND ? main_item : offhand_item,
playersao, pointed, (bool)used_hand)) {
// Apply returned ItemStack
if (used_hand == OFFHAND
? (offhand_item.has_value() && playersao->setOffhandWieldedItem(*offhand_item))

@ -61,7 +61,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
}
bool ScriptApiItem::item_OnPlace(std::optional<ItemStack> &ret_item,
ServerActiveObject *placer, const PointedThing &pointed)
ServerActiveObject *placer, const PointedThing &pointed, bool offhand)
{
SCRIPTAPI_PRECHECKHEADER
@ -81,7 +81,9 @@ bool ScriptApiItem::item_OnPlace(std::optional<ItemStack> &ret_item,
objectrefGetOrCreate(L, placer);
pushPointedThing(pointed);
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
lua_pushboolean(L, (int)offhand);
PCALL_RES(lua_pcall(L, 4, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
ret_item = read_item(L, -1, getServer()->idef());
@ -126,7 +128,7 @@ bool ScriptApiItem::item_OnUse(std::optional<ItemStack> &ret_item,
}
bool ScriptApiItem::item_OnSecondaryUse(std::optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
ServerActiveObject *user, const PointedThing &pointed, bool offhand)
{
SCRIPTAPI_PRECHECKHEADER
@ -139,7 +141,9 @@ bool ScriptApiItem::item_OnSecondaryUse(std::optional<ItemStack> &ret_item,
LuaItemStack::create(L, item);
objectrefGetOrCreate(L, user);
pushPointedThing(pointed);
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
lua_pushboolean(L, (int)offhand);
PCALL_RES(lua_pcall(L, 4, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
ret_item = read_item(L, -1, getServer()->idef());

@ -46,11 +46,11 @@ class ScriptApiItem
bool item_OnDrop(ItemStack &item,
ServerActiveObject *dropper, v3f pos);
bool item_OnPlace(std::optional<ItemStack> &item,
ServerActiveObject *placer, const PointedThing &pointed);
ServerActiveObject *placer, const PointedThing &pointed, bool offhand);
bool item_OnUse(std::optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
bool item_OnSecondaryUse(std::optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
ServerActiveObject *user, const PointedThing &pointed, bool offhand);
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
bool item_CraftPredict(ItemStack &item, ServerActiveObject *user,

@ -455,7 +455,7 @@ int ModApiEnv::l_place_node(lua_State *L)
// Place it with a nullptr placer (appears in Lua as nil)
// or the given ObjectRef
bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed);
bool success = scriptIfaceItem->item_OnPlace(item, placer, pointed, false);
lua_pushboolean(L, success);
return 1;
}