Fix damage wraparound if very high damage (#11872)

This commit is contained in:
Wuzzy 2022-01-06 20:16:35 +00:00 committed by GitHub
parent 85da2e284b
commit b81948a14c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 10 additions and 8 deletions

@ -3524,7 +3524,7 @@ Helper functions
* `minetest.get_hit_params(groups, tool_capabilities [, time_from_last_punch [, wear]])`: * `minetest.get_hit_params(groups, tool_capabilities [, time_from_last_punch [, wear]])`:
Simulates an item that punches an object. Simulates an item that punches an object.
Returns a table with the following fields: Returns a table with the following fields:
* `hp`: How much damage the punch would cause. * `hp`: How much damage the punch would cause (between -65535 and 65535).
* `wear`: How much wear would be added to the tool (ignored for non-tools). * `wear`: How much wear would be added to the tool (ignored for non-tools).
Parameters: Parameters:
* `groups`: Damage groups of the object * `groups`: Damage groups of the object

@ -240,7 +240,7 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
// tool_capabilities, direction, damage) // tool_capabilities, direction, damage)
bool ScriptApiEntity::luaentity_Punch(u16 id, bool ScriptApiEntity::luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch, ServerActiveObject *puncher, float time_from_last_punch,
const ToolCapabilities *toolcap, v3f dir, s16 damage) const ToolCapabilities *toolcap, v3f dir, s32 damage)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER

@ -42,7 +42,7 @@ public:
const collisionMoveResult *moveresult); const collisionMoveResult *moveresult);
bool luaentity_Punch(u16 id, bool luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch, ServerActiveObject *puncher, float time_from_last_punch,
const ToolCapabilities *toolcap, v3f dir, s16 damage); const ToolCapabilities *toolcap, v3f dir, s32 damage);
bool luaentity_on_death(u16 id, ServerActiveObject *killer); bool luaentity_on_death(u16 id, ServerActiveObject *killer);
void luaentity_Rightclick(u16 id, ServerActiveObject *clicker); void luaentity_Rightclick(u16 id, ServerActiveObject *clicker);
void luaentity_on_attach_child(u16 id, ServerActiveObject *child); void luaentity_on_attach_child(u16 id, ServerActiveObject *child);

@ -60,7 +60,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
float time_from_last_punch, float time_from_last_punch,
const ToolCapabilities *toolcap, const ToolCapabilities *toolcap,
v3f dir, v3f dir,
s16 damage) s32 damage)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
// Get core.registered_on_punchplayers // Get core.registered_on_punchplayers

@ -46,7 +46,7 @@ public:
void on_cheat(ServerActiveObject *player, const std::string &cheat_type); void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter, bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
float time_from_last_punch, const ToolCapabilities *toolcap, float time_from_last_punch, const ToolCapabilities *toolcap,
v3f dir, s16 damage); v3f dir, s32 damage);
void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker); void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker);
s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change, s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
const PlayerHPChangeReason &reason); const PlayerHPChangeReason &reason);

@ -306,7 +306,7 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
const ToolCapabilities *tp, float time_from_last_punch, const ToolCapabilities *tp, float time_from_last_punch,
u16 initial_wear) u16 initial_wear)
{ {
s16 damage = 0; s32 damage = 0;
float result_wear = 0.0f; float result_wear = 0.0f;
float punch_interval_multiplier = float punch_interval_multiplier =
rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f); rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
@ -320,6 +320,8 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
result_wear = calculateResultWear(tp->punch_attack_uses, initial_wear); result_wear = calculateResultWear(tp->punch_attack_uses, initial_wear);
result_wear *= punch_interval_multiplier; result_wear *= punch_interval_multiplier;
} }
// Keep damage in sane bounds for simplicity
damage = rangelim(damage, -U16_MAX, U16_MAX);
u32 wear_i = (u32) result_wear; u32 wear_i = (u32) result_wear;
return {damage, wear_i}; return {damage, wear_i};

@ -106,11 +106,11 @@ DigParams getDigParams(const ItemGroupList &groups,
struct HitParams struct HitParams
{ {
s16 hp; s32 hp;
// Caused wear // Caused wear
u32 wear; // u32 because wear could be 65536 (single-use weapon) u32 wear; // u32 because wear could be 65536 (single-use weapon)
HitParams(s16 hp_ = 0, u32 wear_ = 0): HitParams(s32 hp_ = 0, u32 wear_ = 0):
hp(hp_), hp(hp_),
wear(wear_) wear(wear_)
{} {}