From 8d112e5191a67c90957c252392a1719e8e7acd85 Mon Sep 17 00:00:00 2001 From: Flugente Date: Thu, 8 Jul 2021 19:23:26 +0000 Subject: [PATCH] Added lua function for finding/creating/destroying items in a soldier's inventory git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@9132 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- Strategic/LuaInitNPCs.cpp | 68 ++++++++++++++++++++++++++++++++++++ Tactical/Soldier Control.cpp | 49 +++++++++++++++++++++++++- Tactical/Soldier Control.h | 4 ++- 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/Strategic/LuaInitNPCs.cpp b/Strategic/LuaInitNPCs.cpp index 0de2b07c..e2129e8a 100644 --- a/Strategic/LuaInitNPCs.cpp +++ b/Strategic/LuaInitNPCs.cpp @@ -837,6 +837,10 @@ static int l_TalkingMenuDialogue (lua_State *L); static int l_StartDialogueMessageBox (lua_State *L); static int l_CreateItemInv (lua_State *L); +static int l_CreateItemInvOrFloor ( lua_State *L ); +static int l_DestroyOneItemInInventory( lua_State *L ); +static int l_HasItemInInventory( lua_State *L ); + static int l_MercSalary(lua_State *L); static int l_PlayerTeamFull (lua_State *L); @@ -1165,6 +1169,9 @@ void IniFunction(lua_State *L, BOOLEAN bQuests ) lua_register(L, "SoldierGiveItem", l_SoldierGiveItem); lua_register(L, "ProfilGiveItem", l_SoldierGiveItem); lua_register(L, "AddItemToInventory", l_CreateItemInv); + lua_register(L, "CreateItemInvOrFloor", l_CreateItemInvOrFloor ); + lua_register(L, "DestroyOneItemInInventory", l_DestroyOneItemInInventory ); + lua_register(L, "HasItemInInventory", l_HasItemInInventory ); lua_register(L, "TacticalCharacterDialogueWithSpecialEvent", l_TacticalCharacterDialogueWithSpecialEvent); lua_register(L, "SetSalary", l_MercSalary); lua_register(L, "SetProfileStrategicInsertionData", l_ProfilesStrategicInsertionData); @@ -8452,6 +8459,67 @@ static int l_CreateItemInv(lua_State *L) return 0; } +static int l_CreateItemInvOrFloor( lua_State *L ) +{ + if ( lua_gettop( L ) >= 2 ) + { + UINT16 ubID = lua_tointeger( L, 1 ); + UINT16 usItem = lua_tointeger( L, 2 ); + + if ( ubID < TOTAL_SOLDIERS ) + { + SOLDIERTYPE* pSoldier = MercPtrs[ubID]; + if ( pSoldier ) + { + CreateItem( usItem, 100, &gTempObject ); + + if ( !AutoPlaceObject( pSoldier, &gTempObject, TRUE ) ) + { + AddItemToPool( pSoldier->sGridNo, &gTempObject, 1, pSoldier->pathing.bLevel, 0, -1 ); + } + } + } + } + + return 0; +} + +static int l_DestroyOneItemInInventory( lua_State *L ) +{ + if ( lua_gettop( L ) >= 2 ) + { + UINT16 ubID = lua_tointeger( L, 1 ); + UINT16 usItem = lua_tointeger( L, 2 ); + + if ( ubID < TOTAL_SOLDIERS ) + { + MercPtrs[ubID]->DestroyOneItemInInventory( usItem ); + } + } + + return 0; +} + +static int l_HasItemInInventory( lua_State *L ) +{ + bool Bool = FALSE; + + if ( lua_gettop( L ) >= 2 ) + { + UINT16 ubID = lua_tointeger( L, 1 ); + UINT16 usItem = lua_tointeger( L, 2 ); + + if ( ubID < TOTAL_SOLDIERS ) + { + Bool = MercPtrs[ubID]->HasItemInInventory( usItem ); + } + } + + lua_pushboolean( L, Bool ); + + return 1; +} + static int l_CreateKeyProfInvAndAddItemToPool(lua_State *L) { UINT8 n = lua_gettop(L); diff --git a/Tactical/Soldier Control.cpp b/Tactical/Soldier Control.cpp index 43e81b7c..13a124fa 100644 --- a/Tactical/Soldier Control.cpp +++ b/Tactical/Soldier Control.cpp @@ -21188,7 +21188,7 @@ OBJECTTYPE* SOLDIERTYPE::GetObjectWithItemFlag( UINT64 aFlag ) return NULL; } -void SOLDIERTYPE::DestroyOneObjectWithItemFlag( UINT64 aFlag ) +bool SOLDIERTYPE::DestroyOneObjectWithItemFlag( UINT64 aFlag ) { for ( INT8 bLoop = 0, invsize = (INT8)inv.size(); bLoop < invsize; ++bLoop ) { @@ -21204,9 +21204,56 @@ void SOLDIERTYPE::DestroyOneObjectWithItemFlag( UINT64 aFlag ) { DeleteObj( pObj ); } + + return true; } } } + + return false; +} + +bool SOLDIERTYPE::DestroyOneItemInInventory( UINT16 ausItem ) +{ + for ( INT8 bLoop = 0, invsize = (INT8)inv.size(); bLoop < invsize; ++bLoop ) + { + if ( inv[bLoop].exists() ) + { + OBJECTTYPE* pObj = &( inv[bLoop] ); + + if ( pObj && pObj->usItem == ausItem ) + { + pObj->RemoveObjectsFromStack( 1 ); + + if ( pObj->ubNumberOfObjects <= 0 ) + { + DeleteObj( pObj ); + } + + return true; + } + } + } + + return false; +} + +bool SOLDIERTYPE::HasItemInInventory( UINT16 ausItem ) +{ + for ( INT8 bLoop = 0, invsize = (INT8)inv.size(); bLoop < invsize; ++bLoop ) + { + if ( inv[bLoop].exists() ) + { + OBJECTTYPE* pObj = &( inv[bLoop] ); + + if ( pObj && pObj->usItem == ausItem ) + { + return true; + } + } + } + + return false; } #define BLOODDONATION_AMOUNT 10 diff --git a/Tactical/Soldier Control.h b/Tactical/Soldier Control.h index 264ad4e3..2437b3c7 100644 --- a/Tactical/Soldier Control.h +++ b/Tactical/Soldier Control.h @@ -2053,7 +2053,9 @@ public: void DrugAutoUse(); OBJECTTYPE* GetObjectWithItemFlag( UINT64 aFlag ); - void DestroyOneObjectWithItemFlag( UINT64 aFlag ); + bool DestroyOneObjectWithItemFlag( UINT64 aFlag ); + bool DestroyOneItemInInventory( UINT16 ausItem ); + bool HasItemInInventory( UINT16 ausItem ); // Flugente: can we fill a blood bag from this guy ? BOOLEAN IsValidBloodDonor();