From 84a97dd63fef0e296da2372b3120fc9fa6d9057d Mon Sep 17 00:00:00 2001 From: avolkov Date: Thu, 21 Jun 2007 05:21:00 +0000 Subject: [PATCH] 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 --- sc2/src/sc2code/load.c | 87 +++++++++++++++++++++++++----------------- sc2/src/sc2code/save.c | 84 +++++++++++++++++++++++++--------------- 2 files changed, 107 insertions(+), 64 deletions(-) diff --git a/sc2/src/sc2code/load.c b/sc2/src/sc2code/load.c index 5ac7477cc..1f32e446d 100644 --- a/sc2/src/sc2code/load.c +++ b/sc2/src/sc2code/load.c @@ -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); - + 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_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,23 +169,51 @@ 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)) - { - // avail_race_q contains information not about specific ships, - // but about a race. - EXTENDED_SHIP_FRAGMENT *ExtFragPtr = - (EXTENDED_SHIP_FRAGMENT*) FragPtr; + UnlockStarShip (pQueue, hStarShip); + } +} - cread_16 (fh, &ExtFragPtr->ShipInfo.actual_strength); - cread_16 (fh, &ExtFragPtr->ShipInfo.known_strength); - cread_16 (fh, &ExtFragPtr->ShipInfo.known_loc.x); - cread_16 (fh, &ExtFragPtr->ShipInfo.known_loc.y); - cread_8 (fh, &ExtFragPtr->ShipInfo.growth_err_term); - cread_8 (fh, &ExtFragPtr->ShipInfo.func_index); - cread_16 (fh, &ExtFragPtr->ShipInfo.dest_loc.x); - cread_16 (fh, &ExtFragPtr->ShipInfo.dest_loc.y); - cread_16 (fh, NULL); /* alignment padding */ - } +static void +LoadRaceQueue (DECODE_REF fh, QUEUE *pQueue) +{ + 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); + cread_16 (fh, &ExtFragPtr->ShipInfo.known_loc.x); + cread_16 (fh, &ExtFragPtr->ShipInfo.known_loc.y); + cread_8 (fh, &ExtFragPtr->ShipInfo.growth_err_term); + cread_8 (fh, &ExtFragPtr->ShipInfo.func_index); + 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); diff --git a/sc2/src/sc2code/save.c b/sc2/src/sc2code/save.c index 5ab3b3bfd..d7fe5c437 100644 --- a/sc2/src/sc2code/save.c +++ b/sc2/src/sc2code/save.c @@ -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); - + 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_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,23 +149,55 @@ 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)) - { - // avail_race_q contains information not about specific ships, - // but about a race. - EXTENDED_SHIP_FRAGMENT *ExtFragPtr = - (EXTENDED_SHIP_FRAGMENT*) FragPtr; + UnlockStarShip (pQueue, hStarShip); + hStarShip = hNextShip; + } +} - cwrite_16 (fh, ExtFragPtr->ShipInfo.actual_strength); - cwrite_16 (fh, ExtFragPtr->ShipInfo.known_strength); - cwrite_16 (fh, ExtFragPtr->ShipInfo.known_loc.x); - cwrite_16 (fh, ExtFragPtr->ShipInfo.known_loc.y); - cwrite_8 (fh, ExtFragPtr->ShipInfo.growth_err_term); - cwrite_8 (fh, ExtFragPtr->ShipInfo.func_index); - cwrite_16 (fh, ExtFragPtr->ShipInfo.dest_loc.x); - cwrite_16 (fh, ExtFragPtr->ShipInfo.dest_loc.y); - cwrite_16 (fh, 0); /* alignment padding */ - } +static void +SaveRaceQueue (DECODE_REF fh, QUEUE *pQueue) +{ + 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); + cwrite_16 (fh, ExtFragPtr->ShipInfo.known_loc.x); + cwrite_16 (fh, ExtFragPtr->ShipInfo.known_loc.y); + cwrite_8 (fh, ExtFragPtr->ShipInfo.growth_err_term); + cwrite_8 (fh, ExtFragPtr->ShipInfo.func_index); + 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));