Skip to content

Commit

Permalink
Fix damage wraparound if very high damage (#11872)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuzzy2 committed Jan 6, 2022
1 parent 85da2e2 commit b81948a
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doc/lua_api.txt
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_entity.cpp
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_entity.h
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_player.cpp
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_player.h
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/tool.cpp
Expand Up @@ -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);
Expand All @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/tool.h
Expand Up @@ -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_)
{}
Expand Down

0 comments on commit b81948a

Please sign in to comment.