]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Fix damage wraparound if very high damage (#11872)
authorWuzzy <wuzzy2@mail.ru>
Thu, 6 Jan 2022 20:16:35 +0000 (20:16 +0000)
committerGitHub <noreply@github.com>
Thu, 6 Jan 2022 20:16:35 +0000 (21:16 +0100)
doc/lua_api.txt
src/script/cpp_api/s_entity.cpp
src/script/cpp_api/s_entity.h
src/script/cpp_api/s_player.cpp
src/script/cpp_api/s_player.h
src/tool.cpp
src/tool.h

index d4dc19fdd09a4ecde6175cff20e8f81ab3ff9b6b..3edfd5bb1134f98d3a16fb67660f78af4e937fc1 100644 (file)
@@ -3524,7 +3524,7 @@ Helper functions
 * `minetest.get_hit_params(groups, tool_capabilities [, time_from_last_punch [, wear]])`:
     Simulates an item that punches an object.
     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).
     Parameters:
     * `groups`: Damage groups of the object
index 746f7013e99d6505770b308378a165d47f1b331f..06337b9e8c16528135226c416443d55aab8e25d9 100644 (file)
@@ -240,7 +240,7 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
 //                       tool_capabilities, direction, damage)
 bool ScriptApiEntity::luaentity_Punch(u16 id,
                ServerActiveObject *puncher, float time_from_last_punch,
-               const ToolCapabilities *toolcap, v3f dir, s16 damage)
+               const ToolCapabilities *toolcap, v3f dir, s32 damage)
 {
        SCRIPTAPI_PRECHECKHEADER
 
index b52f6e447a99e522293c737e942ae19bd4fe45cc..7658ae92236c07235a3f74e7d4bbc59673c91339 100644 (file)
@@ -42,7 +42,7 @@ class ScriptApiEntity
                const collisionMoveResult *moveresult);
        bool luaentity_Punch(u16 id,
                        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);
        void luaentity_Rightclick(u16 id, ServerActiveObject *clicker);
        void luaentity_on_attach_child(u16 id, ServerActiveObject *child);
index d3e6138dc4492ba17b4a09aef9a5a9bc618eb949..22b24f363ab26892e69aa977e722e24409312585 100644 (file)
@@ -60,7 +60,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
                float time_from_last_punch,
                const ToolCapabilities *toolcap,
                v3f dir,
-               s16 damage)
+               s32 damage)
 {
        SCRIPTAPI_PRECHECKHEADER
        // Get core.registered_on_punchplayers
index c0f141862106fbb9d78f8069b34cb18c51258203..e866aee464c9b0769ee60451ab78318da70a6150 100644 (file)
@@ -46,7 +46,7 @@ class ScriptApiPlayer : virtual public ScriptApiBase
        void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
        bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
                        float time_from_last_punch, const ToolCapabilities *toolcap,
-                       v3f dir, s16 damage);
+                       v3f dir, s32 damage);
        void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker);
        s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
                        const PlayerHPChangeReason &reason);
index b0749286d11042fa3cb474de57f821f56eb5cc47..075c6b3c5ff13f8bb5e5ada473d85d7c8e80f7ca 100644 (file)
@@ -306,7 +306,7 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
                const ToolCapabilities *tp, float time_from_last_punch,
                u16 initial_wear)
 {
-       s16 damage = 0;
+       s32 damage = 0;
        float result_wear = 0.0f;
        float punch_interval_multiplier =
                        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 *= punch_interval_multiplier;
        }
+       // Keep damage in sane bounds for simplicity
+       damage = rangelim(damage, -U16_MAX, U16_MAX);
 
        u32 wear_i = (u32) result_wear;
        return {damage, wear_i};
index 0e3388485113ec5c28fb5409bea61c463ca32784..8409f59afcd234da41fb2b2b58bdd0b7738abde3 100644 (file)
@@ -106,11 +106,11 @@ DigParams getDigParams(const ItemGroupList &groups,
 
 struct HitParams
 {
-       s16 hp;
+       s32 hp;
        // Caused wear
        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_),
                wear(wear_)
        {}