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
This commit is contained in:
Asdow
2022-03-31 15:10:19 +00:00
parent 094c2edbde
commit c334d4a0d4
3 changed files with 17 additions and 22 deletions
+9
View File
@@ -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();
+6 -21
View File
@@ -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 )
{
+2 -1
View File
@@ -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