1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-10 18:51:05 +00:00

Allow nil puncher in object:punch (#14319)

This commit is contained in:
sfence 2024-04-28 17:55:04 +02:00 committed by GitHub
parent fc0ac64277
commit 72cb4e9bea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 89 additions and 36 deletions

View file

@ -262,8 +262,6 @@ bool ScriptApiEntity::luaentity_Punch(u16 id,
{
SCRIPTAPI_PRECHECKHEADER
assert(puncher);
int error_handler = PUSH_ERROR_HANDLER(L);
// Get core.luaentities[id]
@ -278,7 +276,10 @@ bool ScriptApiEntity::luaentity_Punch(u16 id,
}
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
objectrefGetOrCreate(L, puncher); // Clicker reference
if (puncher)
objectrefGetOrCreate(L, puncher); // Puncher reference
else
lua_pushnil(L);
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);

View file

@ -68,7 +68,10 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
lua_getfield(L, -1, "registered_on_punchplayers");
// Call callbacks
objectrefGetOrCreate(L, player);
objectrefGetOrCreate(L, hitter);
if (hitter)
objectrefGetOrCreate(L, hitter);
else
lua_pushnil(L);
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);

View file

@ -170,16 +170,21 @@ int ObjectRef::l_punch(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkObject<ObjectRef>(L, 1);
ObjectRef *puncher_ref = checkObject<ObjectRef>(L, 2);
ObjectRef *puncher_ref = lua_isnoneornil(L, 2) ? nullptr : checkObject<ObjectRef>(L, 2);
ServerActiveObject *sao = getobject(ref);
ServerActiveObject *puncher = getobject(puncher_ref);
if (sao == nullptr || puncher == nullptr)
ServerActiveObject *puncher = puncher_ref ? getobject(puncher_ref) : nullptr;
if (sao == nullptr)
return 0;
float time_from_last_punch = readParam<float>(L, 3, 1000000.0f);
ToolCapabilities toolcap = read_tool_capabilities(L, 4);
v3f dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
dir.normalize();
v3f dir;
if (puncher) {
dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
dir.normalize();
} else {
dir = readParam<v3f>(L, 5, v3f(0));
}
u32 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
lua_pushnumber(L, wear);

View file

@ -332,16 +332,16 @@ u32 LuaEntitySAO::punch(v3f dir,
return 0;
}
FATAL_ERROR_IF(!puncher, "Punch action called without SAO");
s32 old_hp = getHP();
ItemStack selected_item, hand_item;
ItemStack tool_item = puncher->getWieldedItem(&selected_item, &hand_item);
ItemStack tool_item;
if (puncher)
tool_item = puncher->getWieldedItem(&selected_item, &hand_item);
PunchDamageResult result = getPunchDamage(
m_armor_groups,
toolcap,
&tool_item,
puncher ? &tool_item : nullptr,
time_from_last_punch,
initial_wear);
@ -355,12 +355,16 @@ u32 LuaEntitySAO::punch(v3f dir,
}
}
actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
", hp=" << puncher->getHP() << ") punched " <<
getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
"), damage=" << (old_hp - (s32)getHP()) <<
(damage_handled ? " (handled by Lua)" : "") << std::endl;
if (puncher) {
actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
", hp=" << puncher->getHP() << ")";
} else {
actionstream << "(none)";
}
actionstream << " punched " <<
getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
"), damage=" << (old_hp - (s32)getHP()) <<
(damage_handled ? " (handled by Lua)" : "") << std::endl;
// TODO: give Lua control over wear
return result.wear;
}

View file

@ -462,11 +462,9 @@ u32 PlayerSAO::punch(v3f dir,
if (!toolcap)
return 0;
FATAL_ERROR_IF(!puncher, "Punch action called without SAO");
// No effect if PvP disabled or if immortal
if (isImmortal() || !g_settings->getBool("enable_pvp")) {
if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
if (puncher && puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
// create message and add to list
sendPunchCommand();
return 0;
@ -493,11 +491,16 @@ u32 PlayerSAO::punch(v3f dir,
}
}
actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
", hp=" << puncher->getHP() << ") punched " <<
getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
"), damage=" << (old_hp - (s32)getHP()) <<
(damage_handled ? " (handled by Lua)" : "") << std::endl;
if (puncher) {
actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
", hp=" << puncher->getHP() << ")";
} else {
actionstream << "(none)";
}
actionstream << " puched " <<
getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
"), damage=" << (old_hp - (s32)getHP()) <<
(damage_handled ? " (handled by Lua)" : "") << std::endl;
return hitparams.wear;
}

View file

@ -482,7 +482,7 @@ PunchDamageResult getPunchDamage(
{
HitParams hitparams = getHitParams(armor_groups, toolcap,
time_from_last_punch,
punchitem->wear);
punchitem ? punchitem->wear : 0);
result.did_punch = true;
result.wear = hitparams.wear;
result.damage = hitparams.hp;