Skip to content

Commit b81948a

Browse files
authored
Fix damage wraparound if very high damage (#11872)
1 parent 85da2e2 commit b81948a

File tree

7 files changed

+10
-8
lines changed

7 files changed

+10
-8
lines changed

doc/lua_api.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3524,7 +3524,7 @@ Helper functions
35243524
* `minetest.get_hit_params(groups, tool_capabilities [, time_from_last_punch [, wear]])`:
35253525
Simulates an item that punches an object.
35263526
Returns a table with the following fields:
3527-
* `hp`: How much damage the punch would cause.
3527+
* `hp`: How much damage the punch would cause (between -65535 and 65535).
35283528
* `wear`: How much wear would be added to the tool (ignored for non-tools).
35293529
Parameters:
35303530
* `groups`: Damage groups of the object

src/script/cpp_api/s_entity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
240240
// tool_capabilities, direction, damage)
241241
bool ScriptApiEntity::luaentity_Punch(u16 id,
242242
ServerActiveObject *puncher, float time_from_last_punch,
243-
const ToolCapabilities *toolcap, v3f dir, s16 damage)
243+
const ToolCapabilities *toolcap, v3f dir, s32 damage)
244244
{
245245
SCRIPTAPI_PRECHECKHEADER
246246

src/script/cpp_api/s_entity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ScriptApiEntity
4242
const collisionMoveResult *moveresult);
4343
bool luaentity_Punch(u16 id,
4444
ServerActiveObject *puncher, float time_from_last_punch,
45-
const ToolCapabilities *toolcap, v3f dir, s16 damage);
45+
const ToolCapabilities *toolcap, v3f dir, s32 damage);
4646
bool luaentity_on_death(u16 id, ServerActiveObject *killer);
4747
void luaentity_Rightclick(u16 id, ServerActiveObject *clicker);
4848
void luaentity_on_attach_child(u16 id, ServerActiveObject *child);

src/script/cpp_api/s_player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
6060
float time_from_last_punch,
6161
const ToolCapabilities *toolcap,
6262
v3f dir,
63-
s16 damage)
63+
s32 damage)
6464
{
6565
SCRIPTAPI_PRECHECKHEADER
6666
// Get core.registered_on_punchplayers

src/script/cpp_api/s_player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ScriptApiPlayer : virtual public ScriptApiBase
4646
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
4747
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
4848
float time_from_last_punch, const ToolCapabilities *toolcap,
49-
v3f dir, s16 damage);
49+
v3f dir, s32 damage);
5050
void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker);
5151
s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
5252
const PlayerHPChangeReason &reason);

src/tool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
306306
const ToolCapabilities *tp, float time_from_last_punch,
307307
u16 initial_wear)
308308
{
309-
s16 damage = 0;
309+
s32 damage = 0;
310310
float result_wear = 0.0f;
311311
float punch_interval_multiplier =
312312
rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
@@ -320,6 +320,8 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
320320
result_wear = calculateResultWear(tp->punch_attack_uses, initial_wear);
321321
result_wear *= punch_interval_multiplier;
322322
}
323+
// Keep damage in sane bounds for simplicity
324+
damage = rangelim(damage, -U16_MAX, U16_MAX);
323325

324326
u32 wear_i = (u32) result_wear;
325327
return {damage, wear_i};

src/tool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ DigParams getDigParams(const ItemGroupList &groups,
106106

107107
struct HitParams
108108
{
109-
s16 hp;
109+
s32 hp;
110110
// Caused wear
111111
u32 wear; // u32 because wear could be 65536 (single-use weapon)
112112

113-
HitParams(s16 hp_ = 0, u32 wear_ = 0):
113+
HitParams(s32 hp_ = 0, u32 wear_ = 0):
114114
hp(hp_),
115115
wear(wear_)
116116
{}

0 commit comments

Comments
 (0)