Untangling ship queues, stage pre1: decoupling load/save funcs from the queues

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2776 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2007-06-21 05:21:00 +00:00
parent 454841d93f
commit 84a97dd63f
2 changed files with 107 additions and 64 deletions
+38 -19
View File
@@ -134,13 +134,11 @@ read_a16 (void *fp, UWORD *ar, COUNT count)
}
static void
LoadShipQueue (DECODE_REF fh, QUEUE *pQueue, BOOLEAN MakeQ)
LoadShipQueue (DECODE_REF fh, QUEUE *pQueue)
{
COUNT num_links;
cread_16 (fh, &num_links);
if (num_links && MakeQ)
InitQueue (pQueue, num_links, sizeof (SHIP_FRAGMENT));
while (num_links--)
{
@@ -151,20 +149,13 @@ LoadShipQueue (DECODE_REF fh, QUEUE *pQueue, BOOLEAN MakeQ)
cread_16 (fh, &Index);
if (pQueue == &GLOBAL (avail_race_q))
hStarShip = GetStarShipFromIndex (pQueue, Index);
else
hStarShip = CloneShipFragment (Index, pQueue, 0);
FragPtr = (SHIP_FRAGMENT*) LockStarShip (pQueue, hStarShip);
if (pQueue != &GLOBAL (avail_race_q))
{ // queues other than avail_race_q save SHIP_FRAGMENT elements
// Read SHIP_FRAGMENT elements
cread_16 (fh, &FragPtr->s.Player);
cread_8 (fh, &FragPtr->s.Captain);
cread_8 (fh, NULL); /* padding */
}
// Read SHIP_INFO elements
cread_16 (fh, &FragPtr->ShipInfo.ship_flags);
cread_8 (fh, &FragPtr->ShipInfo.var1);
@@ -178,12 +169,41 @@ LoadShipQueue (DECODE_REF fh, QUEUE *pQueue, BOOLEAN MakeQ)
cread_16 (fh, &FragPtr->ShipInfo.loc.x);
cread_16 (fh, &FragPtr->ShipInfo.loc.y);
if (pQueue == &GLOBAL (avail_race_q))
UnlockStarShip (pQueue, hStarShip);
}
}
static void
LoadRaceQueue (DECODE_REF fh, QUEUE *pQueue)
{
// avail_race_q contains information not about specific ships,
// but about a race.
EXTENDED_SHIP_FRAGMENT *ExtFragPtr =
(EXTENDED_SHIP_FRAGMENT*) FragPtr;
COUNT num_links;
cread_16 (fh, &num_links);
while (num_links--)
{
HSTARSHIP hStarShip;
EXTENDED_SHIP_FRAGMENT *ExtFragPtr;
COUNT Index;
BYTE tmpb;
cread_16 (fh, &Index);
hStarShip = GetStarShipFromIndex (pQueue, Index);
ExtFragPtr = (EXTENDED_SHIP_FRAGMENT *) LockStarShip (pQueue, hStarShip);
// Read EXTENDED_SHIP_INFO elements
cread_16 (fh, &ExtFragPtr->ShipInfo.ship_flags);
cread_8 (fh, &ExtFragPtr->ShipInfo.days_left);
cread_8 (fh, &ExtFragPtr->ShipInfo.growth_fract);
cread_8 (fh, &tmpb);
ExtFragPtr->ShipInfo.crew_level = tmpb;
cread_8 (fh, &tmpb);
ExtFragPtr->ShipInfo.max_crew = tmpb;
cread_8 (fh, &ExtFragPtr->ShipInfo.energy_level);
cread_8 (fh, &ExtFragPtr->ShipInfo.max_energy);
cread_16 (fh, &ExtFragPtr->ShipInfo.loc.x);
cread_16 (fh, &ExtFragPtr->ShipInfo.loc.y);
cread_16 (fh, &ExtFragPtr->ShipInfo.actual_strength);
cread_16 (fh, &ExtFragPtr->ShipInfo.known_strength);
@@ -194,7 +214,6 @@ LoadShipQueue (DECODE_REF fh, QUEUE *pQueue, BOOLEAN MakeQ)
cread_16 (fh, &ExtFragPtr->ShipInfo.dest_loc.x);
cread_16 (fh, &ExtFragPtr->ShipInfo.dest_loc.y);
cread_16 (fh, NULL); /* alignment padding */
}
UnlockStarShip (pQueue, hStarShip);
}
@@ -498,10 +517,10 @@ LoadGame (COUNT which_game, SUMMARY_DESC *SummPtr)
"save, Savegame may be corrupt!\n");
GLOBAL (GameClock.TimeCounter) = 0;
LoadShipQueue (fh, &GLOBAL (avail_race_q), FALSE);
LoadRaceQueue (fh, &GLOBAL (avail_race_q));
if (!(NextActivity & START_INTERPLANETARY))
LoadShipQueue (fh, &GLOBAL (npc_built_ship_q), FALSE);
LoadShipQueue (fh, &GLOBAL (built_ship_q), FALSE);
LoadShipQueue (fh, &GLOBAL (npc_built_ship_q));
LoadShipQueue (fh, &GLOBAL (built_ship_q));
// Load the game events (compressed)
cread_16 (fh, &num_links);
+39 -15
View File
@@ -129,23 +129,15 @@ SaveShipQueue (DECODE_REF fh, QUEUE *pQueue)
FragPtr = (SHIP_FRAGMENT*) LockStarShip (pQueue, hStarShip);
hNextShip = _GetSuccLink (FragPtr);
if (pQueue == &GLOBAL (avail_race_q))
Index = GetIndexFromStarShip (pQueue, hStarShip);
// The index is the position in the queue.
else
Index = GET_RACE_ID (FragPtr);
// Write the number identifying this ship type.
// See races.h; look for the enum containing NUM_AVAILABLE_RACES.
cwrite_16 (fh, Index);
if (pQueue != &GLOBAL (avail_race_q))
{ // queues other than avail_race_q save SHIP_FRAGMENT elements
// Write SHIP_FRAGMENT elements
cwrite_16 (fh, FragPtr->s.Player);
cwrite_8 (fh, FragPtr->s.Captain);
cwrite_8 (fh, 0); /* padding */
}
// Write SHIP_INFO elements
cwrite_16 (fh, FragPtr->ShipInfo.ship_flags);
cwrite_8 (fh, FragPtr->ShipInfo.var1);
@@ -157,12 +149,45 @@ SaveShipQueue (DECODE_REF fh, QUEUE *pQueue)
cwrite_16 (fh, FragPtr->ShipInfo.loc.x);
cwrite_16 (fh, FragPtr->ShipInfo.loc.y);
if (pQueue == &GLOBAL (avail_race_q))
UnlockStarShip (pQueue, hStarShip);
hStarShip = hNextShip;
}
}
static void
SaveRaceQueue (DECODE_REF fh, QUEUE *pQueue)
{
// avail_race_q contains information not about specific ships,
// but about a race.
EXTENDED_SHIP_FRAGMENT *ExtFragPtr =
(EXTENDED_SHIP_FRAGMENT*) FragPtr;
COUNT num_links;
HSTARSHIP hStarShip;
// Write the number of entries in the queue.
num_links = CountLinks (pQueue);
cwrite_16 (fh, num_links);
hStarShip = GetHeadLink (pQueue);
while (num_links--)
{
HSTARSHIP hNextShip;
EXTENDED_SHIP_FRAGMENT *ExtFragPtr;
COUNT Index;
ExtFragPtr = (EXTENDED_SHIP_FRAGMENT *) LockStarShip (pQueue, hStarShip);
hNextShip = _GetSuccLink (ExtFragPtr);
Index = GetIndexFromStarShip (pQueue, hStarShip);
// The index is the position in the queue.
cwrite_16 (fh, Index);
// Write EXTENDED_SHIP_INFO elements
cwrite_16 (fh, ExtFragPtr->ShipInfo.ship_flags);
cwrite_8 (fh, ExtFragPtr->ShipInfo.days_left);
cwrite_8 (fh, ExtFragPtr->ShipInfo.growth_fract);
cwrite_8 (fh, ExtFragPtr->ShipInfo.crew_level);
cwrite_8 (fh, ExtFragPtr->ShipInfo.max_crew);
cwrite_8 (fh, ExtFragPtr->ShipInfo.energy_level);
cwrite_8 (fh, ExtFragPtr->ShipInfo.max_energy);
cwrite_16 (fh, ExtFragPtr->ShipInfo.loc.x);
cwrite_16 (fh, ExtFragPtr->ShipInfo.loc.y);
cwrite_16 (fh, ExtFragPtr->ShipInfo.actual_strength);
cwrite_16 (fh, ExtFragPtr->ShipInfo.known_strength);
@@ -173,7 +198,6 @@ SaveShipQueue (DECODE_REF fh, QUEUE *pQueue)
cwrite_16 (fh, ExtFragPtr->ShipInfo.dest_loc.x);
cwrite_16 (fh, ExtFragPtr->ShipInfo.dest_loc.y);
cwrite_16 (fh, 0); /* alignment padding */
}
UnlockStarShip (pQueue, hStarShip);
hStarShip = hNextShip;
@@ -605,7 +629,7 @@ RetrySave:
// XXX: Restore: ShipStamp.frame is abused to store DWORD info
GLOBAL (ShipStamp.frame) = frame;
SaveShipQueue (fh, &GLOBAL (avail_race_q));
SaveRaceQueue (fh, &GLOBAL (avail_race_q));
if (!(GLOBAL (CurrentActivity) & START_INTERPLANETARY))
SaveShipQueue (fh, &GLOBAL (npc_built_ship_q));
SaveShipQueue (fh, &GLOBAL (built_ship_q));