From c334d4a0d43fbe0562c1c72ea03f51a8afd0d795 Mon Sep 17 00:00:00 2001 From: Asdow Date: Thu, 31 Mar 2022 15:10:19 +0000 Subject: [PATCH] Optimize RandomMagazine() called when deploying to tactical sector (by sun_alf) Timings Before: RandomMagazine: 90044 us (total 90044 us) RandomMagazine: 90007 us (total 180051 us) RandomMagazine: 100009 us (total 280060 us) After: RandomMagazine: 0 us (total 0 us) RandomMagazine: 0 us (total 0 us) RandomMagazine: 0 us (total 0 us) git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@9354 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- Init.cpp | 9 +++++++++ Tactical/Items.cpp | 27 ++++++--------------------- Tactical/Weapons.h | 3 ++- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Init.cpp b/Init.cpp index c72f5c28..d11e3354 100644 --- a/Init.cpp +++ b/Init.cpp @@ -1461,6 +1461,15 @@ UINT32 InitializeJA2(void) //} SGP_TRYCATCH_RETHROW(LoadExternalGameplayData(TABLEDATA_DIRECTORY, false),L"Loading external data failed"); + // sun_alf: set itemId to each Magazine to avoid searching over Item[] on each MagazineClassIndexToItemType() call. + for (int i = 0; i < gMAXITEMS_READ; i++) + { + if (Item[i].usItemClass == IC_AMMO) + { + Magazine[Item[i].ubClassIndex].uiIndex = Item[i].uiIndex; + } + } + // Load external text LoadAllExternalText(); diff --git a/Tactical/Items.cpp b/Tactical/Items.cpp index 83941cac..7eb25bc3 100644 --- a/Tactical/Items.cpp +++ b/Tactical/Items.cpp @@ -7982,25 +7982,8 @@ UINT16 UseKitPoints( OBJECTTYPE * pObj, UINT16 usPoints, SOLDIERTYPE *pSoldier ) UINT16 MagazineClassIndexToItemType(UINT16 usMagIndex) { - // Note: if any ammo items in the item table are separated from the main group, - // this function will have to be rewritten to scan the item table for an item - // with item class ammo, which has class index usMagIndex - DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String( "MagazineClassIndexToItemType" ) ); - - // WANNE: Now ammo can be inserted anywhere in Items.xml (before only on index > 70 [FIRST_AMMO] (fix by Realist) - //for (usLoop = FIRST_AMMO; usLoop < MAXITEMS; usLoop++) - UINT16 usLoop; - for (usLoop = 0; usLoop < gMAXITEMS_READ; ++usLoop ) - { - if (Item[usLoop].ubClassIndex == usMagIndex && Item[usLoop].usItemClass == IC_AMMO ) - { - DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String( "MagazineClassIndexToItemType: return %d", usLoop ) ); - return( usLoop ); - } - } - - DebugMsg( TOPIC_JA2, DBG_LEVEL_3, String( "MagazineClassIndexToItemType: return none, got to %d", usLoop ) ); - return(NONE); + // sun_alf: uiIndex is according itemId now + return Magazine[usMagIndex].uiIndex; } @@ -8133,14 +8116,16 @@ UINT16 RandomMagazine( UINT16 usItem, UINT8 ubPercentStandard, UINT8 maxCoolness usLoop = 0; while ( Magazine[ usLoop ].ubCalibre != NOAMMO ) { + // make sure we have space for additional possible mag + if (usPossibleMagCnt >= MAX_AMMO_TYPES_PER_GUN) + break; + loopItem = MagazineClassIndexToItemType(usLoop); if (Magazine[usLoop].ubCalibre == pWeapon->ubCalibre && Magazine[usLoop].ubMagSize == pWeapon->ubMagSize && ItemIsLegal(loopItem) && maxCoolness >= Item[loopItem].ubCoolness ) { - // store it! (make sure array is big enough) - Assert(usPossibleMagCnt < MAX_AMMO_TYPES_PER_GUN); // Madd: check to see if allowed by army if ( gArmyItemChoices[bSoldierClass][ENEMYAMMOTYPES].ubChoices > 0 ) { diff --git a/Tactical/Weapons.h b/Tactical/Weapons.h index 0bad729e..45c9f266 100644 --- a/Tactical/Weapons.h +++ b/Tactical/Weapons.h @@ -398,7 +398,8 @@ typedef struct UINT8 ubAmmoType; UINT8 ubMagType; - UINT32 uiIndex; + // sun_alf: once Magazines.xml is parsed, classIndex of a mag is index in Magazine[], and uiIndex field is not in use anymore, + UINT32 uiIndex; // so let's use it as itemId, i.e. back reference to Item[]. } MAGTYPE; typedef struct