From 377199eed221baa1680c78edfc0dbaee73ebd280 Mon Sep 17 00:00:00 2001 From: AMS21 Date: Thu, 26 Oct 2023 21:05:47 +0200 Subject: [PATCH 1/3] xrGame: Refactor `script_game_object` money functions to use `u32` Since `CInventoryOwner` also uses `u32` for its `money` member --- src/xrGame/script_game_object.h | 4 ++-- src/xrGame/script_game_object_inventory_owner.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/xrGame/script_game_object.h b/src/xrGame/script_game_object.h index eb9da6b8eb5..d0d3c812f93 100644 --- a/src/xrGame/script_game_object.h +++ b/src/xrGame/script_game_object.h @@ -369,8 +369,8 @@ class CScriptGameObject void DropItemAndTeleport(CScriptGameObject* pItem, Fvector position); void ForEachInventoryItems(const luabind::functor& functor); void TransferItem(CScriptGameObject* pItem, CScriptGameObject* pForWho); - void TransferMoney(int money, CScriptGameObject* pForWho); - void GiveMoney(int money); + void TransferMoney(u32 money, CScriptGameObject* pForWho); + void GiveMoney(u32 money); u32 Money(); void MakeItemActive(CScriptGameObject* pItem); diff --git a/src/xrGame/script_game_object_inventory_owner.cpp b/src/xrGame/script_game_object_inventory_owner.cpp index b36b6d54899..52f14a171b0 100644 --- a/src/xrGame/script_game_object_inventory_owner.cpp +++ b/src/xrGame/script_game_object_inventory_owner.cpp @@ -474,7 +474,7 @@ u32 CScriptGameObject::Money() return pOurOwner->get_money(); } -void CScriptGameObject::TransferMoney(int money, CScriptGameObject* pForWho) +void CScriptGameObject::TransferMoney(u32 money, CScriptGameObject* pForWho) { if (!pForWho) { @@ -496,7 +496,7 @@ void CScriptGameObject::TransferMoney(int money, CScriptGameObject* pForWho) pOtherOwner->set_money(pOtherOwner->get_money() + money, true); } -void CScriptGameObject::GiveMoney(int money) +void CScriptGameObject::GiveMoney(u32 money) { CInventoryOwner* pOurOwner = smart_cast(&object()); VERIFY(pOurOwner); From a203c65f01ab193adabc85bcb447a7f124396970 Mon Sep 17 00:00:00 2001 From: AMS21 Date: Thu, 26 Oct 2023 21:07:19 +0200 Subject: [PATCH 2/3] xrGame: Fix logic error in `TransferMoney` Since both values are unsigned subtracting them from another will never produce a negative value. This bug was also present before the previous commit since the `money` variable was implicitly converted to an `u32` resulting in the same bug. --- src/xrGame/script_game_object_inventory_owner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xrGame/script_game_object_inventory_owner.cpp b/src/xrGame/script_game_object_inventory_owner.cpp index 52f14a171b0..db2b8852ce6 100644 --- a/src/xrGame/script_game_object_inventory_owner.cpp +++ b/src/xrGame/script_game_object_inventory_owner.cpp @@ -486,7 +486,7 @@ void CScriptGameObject::TransferMoney(u32 money, CScriptGameObject* pForWho) CInventoryOwner* pOtherOwner = smart_cast(&pForWho->object()); VERIFY(pOtherOwner); - if (pOurOwner->get_money() - money < 0) + if (pOurOwner->get_money() < money) { GEnv.ScriptEngine->script_log(LuaMessageType::Error, "Character does not have enought money"); return; From 9238c63368c461de0d76ce0e872e783e109c8dbb Mon Sep 17 00:00:00 2001 From: AMS21 Date: Thu, 26 Oct 2023 21:41:08 +0200 Subject: [PATCH 3/3] Misc: Fix typo `enought` -> `enough` --- src/xrGame/script_game_object_inventory_owner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xrGame/script_game_object_inventory_owner.cpp b/src/xrGame/script_game_object_inventory_owner.cpp index db2b8852ce6..da874796bb2 100644 --- a/src/xrGame/script_game_object_inventory_owner.cpp +++ b/src/xrGame/script_game_object_inventory_owner.cpp @@ -488,7 +488,7 @@ void CScriptGameObject::TransferMoney(u32 money, CScriptGameObject* pForWho) if (pOurOwner->get_money() < money) { - GEnv.ScriptEngine->script_log(LuaMessageType::Error, "Character does not have enought money"); + GEnv.ScriptEngine->script_log(LuaMessageType::Error, "Character does not have enough money"); return; }