Skip to content
Permalink
Browse files
Fix damage wraparound if very high damage (#11872)
  • Loading branch information
Wuzzy2 committed Jan 6, 2022
1 parent 85da2e2 commit b81948a14c138517f6a227dac5b71f0b2facb33c
Showing with 10 additions and 8 deletions.
  1. +1 −1 doc/lua_api.txt
  2. +1 −1 src/script/cpp_api/s_entity.cpp
  3. +1 −1 src/script/cpp_api/s_entity.h
  4. +1 −1 src/script/cpp_api/s_player.cpp
  5. +1 −1 src/script/cpp_api/s_player.h
  6. +3 −1 src/tool.cpp
  7. +2 −2 src/tool.h
@@ -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
@@ -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

@@ -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);
@@ -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
@@ -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);
@@ -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};
@@ -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_)
{}

0 comments on commit b81948a

Please sign in to comment.