World items patch (by Asdow).

Adds timing logs to savegame function, similar to how we already had in loading a save file. 
Uses a global struct for unloaded sectors' inventory instead of using map item temp files every time unloaded inventory is accessed.
From timing loading and saving functions with a save that has dozens of sectors with hundreds of items each, saving is a little bit slower than before, because map item temp files have to be written to disk just before the save, when that wasn't the case before (from ~0.3 s -> ~0.5s). It is offset by a decrease in loading a game (from ~1.1s -> 0.745s), once an older save is saved at least once. Any unloaded sector inventory access during gameplay is considerably faster and for example the autoresolve lag when militia is using sector equipment is completely gone for me.
Preserves save game compatibility

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@9277 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Sevenfm
2022-01-26 01:02:30 +00:00
parent be14552e32
commit 0fdaba84f6
13 changed files with 590 additions and 599 deletions
+124
View File
@@ -35,6 +35,130 @@
//Global dynamic array of all of the items in a loaded map.
std::vector<WORLDITEM> gWorldItems;//dnl ch75 261013
UINT32 guiNumWorldItems = 0;
WorldItems gAllWorldItems; // World items for all unloaded sectors
INT32 FindWorldItemSector(INT16 x, INT16 y, INT16 z)
{
for (size_t i = 0; i < gAllWorldItems.sectors.size(); i++)
{
const auto sector = gAllWorldItems.sectors[i];
if (sector.x == x && sector.y == y && sector.z == z)
{
return i;
}
}
return -1;
}
bool SectorIsInWorldItems(INT16 x, INT16 y, INT16 z)
{
for (size_t i = 0; i < gAllWorldItems.sectors.size(); i++)
{
const auto sector = gAllWorldItems.sectors[i];
if (sector.x == x && sector.y == y && sector.z == z)
{
return true;
}
}
return false;
}
void AddSectorItemsToWorldItems(INT16 x, INT16 y, INT16 z, UINT32 nItems, std::vector<WORLDITEM> &Items)
{
gAllWorldItems.sectors.push_back(SectorCoords{ x, y, z });
gAllWorldItems.NumItems.push_back(nItems);
gAllWorldItems.Items.push_back(Items);
}
void RemoveSectorFromWorldItems(INT16 x, INT16 y, INT16 z)
{
for (size_t i = 0; i < gAllWorldItems.sectors.size(); i++)
{
const auto sector = gAllWorldItems.sectors[i];
if (sector.x == x && sector.y == y && sector.z == z)
{
gAllWorldItems.sectors.erase(gAllWorldItems.sectors.begin() + i);
gAllWorldItems.NumItems.erase(gAllWorldItems.NumItems.begin() + i);
gAllWorldItems.Items.erase(gAllWorldItems.Items.begin() + i);
}
}
}
void UpdateWorldItems(INT16 x, INT16 y, INT16 z, UINT32 nItems, std::vector<WORLDITEM> &Items)
{
if (nItems == 0)
{
RemoveSectorFromWorldItems(x, y, z);
ReSetSectorFlag(x, y, z, SF_ITEM_TEMP_FILE_EXISTS);
}
else if (SectorIsInWorldItems(x, y, z))
{
const auto i = FindWorldItemSector(x, y, z);
gAllWorldItems.NumItems[i] = nItems;
gAllWorldItems.Items[i] = Items;
}
else
{
AddSectorItemsToWorldItems(x, y, z, nItems, Items);
SetSectorFlag(x, y, z, SF_ITEM_TEMP_FILE_EXISTS);
}
UINT32 visibleItemCount = 0;
for (size_t i = 0; i < nItems; ++i)
{
// if visible to player, then state fact
if (IsMapScreenWorldItemVisibleInMapInventory(&Items[i]))
{
visibleItemCount += Items[i].object.ubNumberOfObjects;
}
}
SetNumberOfVisibleWorldItemsInSectorStructureForSector(x, y, z, visibleItemCount);
}
void ClearAllWorldItems(void)
{
gAllWorldItems.sectors.clear();
gAllWorldItems.NumItems.clear();
gAllWorldItems.Items.clear();
}
INT32 GetAmountOfWorldItems(INT16 x, INT16 y, INT16 z)
{
const auto i = FindWorldItemSector(x,y, z);
if (i != -1)
{
return gAllWorldItems.NumItems[i];
}
else
{
return 0;
}
}
void PruneWorldItems(void)
{
for (size_t i = 0; i < gAllWorldItems.Items.size(); i++)
{
std::vector<WORLDITEM>& items = gAllWorldItems.Items[i];
for (INT32 j = items.size()-1; j > 0; j--)
{
if (items[j].fExists == false)
{
items.erase(items.begin()+j);
}
}
if (gAllWorldItems.Items[i].size() > 0)
{
gAllWorldItems.NumItems[i] = gAllWorldItems.Items[i].size();
}
else
{
gAllWorldItems.sectors.erase(gAllWorldItems.sectors.begin() + i);
gAllWorldItems.NumItems.erase(gAllWorldItems.NumItems.begin() + i);
gAllWorldItems.Items.erase(gAllWorldItems.Items.begin() + i);
}
}
}
WORLDBOMB * gWorldBombs = NULL;
UINT32 guiNumWorldBombs = 0;