// Fast forwards to the next event. // If skipHEE is set, HYPERSPACE_ENCOUNTER_EVENTs are skipped. // Must be called from the Starcon2Main thread. // TODO: LockGameClock may be removed since it is only // supposed to be called synchronously wrt the game logic thread. void forwardToNextEvent (BOOLEAN skipHEE) { HEVENT hEvent; EVENT *EventPtr; COUNT year, month, day; // time of next event BOOLEAN done; if (!GameClockRunning ()) return; LockGameClock (); done = !skipHEE; do { hEvent = GetHeadEvent (); if (hEvent == 0) return; LockEvent (hEvent, &EventPtr); if (EventPtr->func_index != HYPERSPACE_ENCOUNTER_EVENT) done = TRUE; year = EventPtr->year_index; month = EventPtr->month_index; day = EventPtr->day_index; UnlockEvent (hEvent); for (;;) { if (GLOBAL (GameClock.year_index) > year || (GLOBAL (GameClock.year_index) == year && (GLOBAL (GameClock.month_index) > month || (GLOBAL (GameClock.month_index) == month && GLOBAL (GameClock.day_index) >= day)))) break; MoveGameClockDays (1); } } while (!done); UnlockGameClock (); }
BOOLEAN LoadGame (COUNT which_game, SUMMARY_DESC *SummPtr) { uio_Stream *in_fp; char file[PATH_MAX]; char buf[256]; SUMMARY_DESC loc_sd; GAME_STATE_FILE *fp; DECODE_REF fh; COUNT num_links; STAR_DESC SD; ACTIVITY Activity; sprintf (file, "starcon2.%02u", which_game); in_fp = res_OpenResFile (saveDir, file, "rb"); if (!in_fp) return FALSE; if (!LoadSummary (&loc_sd, in_fp)) { log_add (log_Error, "Warning: Savegame is corrupt"); res_CloseResFile (in_fp); return FALSE; } if (!SummPtr) { SummPtr = &loc_sd; } else { // only need summary for displaying to user memcpy (SummPtr, &loc_sd, sizeof (*SummPtr)); res_CloseResFile (in_fp); return TRUE; } // Crude check for big-endian/little-endian incompatibilities. // year_index is suitable as it's a multi-byte value within // a specific recognisable range. if (SummPtr->year_index < START_YEAR || SummPtr->year_index >= START_YEAR + YEARS_TO_KOHRAH_VICTORY + 1 /* Utwig intervention */ + 1 /* time to destroy all races, plenty */ + 25 /* for cheaters */) { log_add (log_Error, "Warning: Savegame corrupt or from " "an incompatible platform."); res_CloseResFile (in_fp); return FALSE; } GlobData.SIS_state = SummPtr->SS; if ((fh = copen (in_fp, FILE_STREAM, STREAM_READ)) == 0) { res_CloseResFile (in_fp); return FALSE; } ReinitQueue (&GLOBAL (GameClock.event_q)); ReinitQueue (&GLOBAL (encounter_q)); ReinitQueue (&GLOBAL (ip_group_q)); ReinitQueue (&GLOBAL (npc_built_ship_q)); ReinitQueue (&GLOBAL (built_ship_q)); memset (&GLOBAL (GameState[0]), 0, sizeof (GLOBAL (GameState))); Activity = GLOBAL (CurrentActivity); LoadGameState (&GlobData.Game_state, fh); NextActivity = GLOBAL (CurrentActivity); GLOBAL (CurrentActivity) = Activity; LoadRaceQueue (fh, &GLOBAL (avail_race_q)); // START_INTERPLANETARY is only set when saving from Homeworld // encounter screen. When the game is loaded, the // GenerateOrbitalFunction for the current star system will // create the encounter anew and populate the npc queue. if (!(NextActivity & START_INTERPLANETARY)) { if (NextActivity & START_ENCOUNTER) LoadShipQueue (fh, &GLOBAL (npc_built_ship_q)); else if (LOBYTE (NextActivity) == IN_INTERPLANETARY) // XXX: Technically, this queue does not need to be // saved/loaded at all. IP groups will be reloaded // from group state files. But the original code did, // and so will we until we can prove we do not need to. LoadGroupQueue (fh, &GLOBAL (ip_group_q)); else // XXX: The empty queue read is only needed to maintain // the savegame compatibility LoadEmptyQueue (fh); } LoadShipQueue (fh, &GLOBAL (built_ship_q)); // Load the game events (compressed) cread_16 (fh, &num_links); { #ifdef DEBUG_LOAD log_add (log_Debug, "EVENTS:"); #endif /* DEBUG_LOAD */ while (num_links--) { HEVENT hEvent; EVENT *EventPtr; hEvent = AllocEvent (); LockEvent (hEvent, &EventPtr); LoadEvent (EventPtr, fh); #ifdef DEBUG_LOAD log_add (log_Debug, "\t%u/%u/%u -- %u", EventPtr->month_index, EventPtr->day_index, EventPtr->year_index, EventPtr->func_index); #endif /* DEBUG_LOAD */ UnlockEvent (hEvent); PutEvent (hEvent); } } // Load the encounters (black globes in HS/QS (compressed)) cread_16 (fh, &num_links); { while (num_links--) { HENCOUNTER hEncounter; ENCOUNTER *EncounterPtr; hEncounter = AllocEncounter (); LockEncounter (hEncounter, &EncounterPtr); LoadEncounter (EncounterPtr, fh); UnlockEncounter (hEncounter); PutEncounter (hEncounter); } } // Copy the star info file from the compressed stream fp = OpenStateFile (STARINFO_FILE, "wb"); if (fp) { DWORD flen; cread_32 (fh, &flen); while (flen) { COUNT num_bytes; num_bytes = flen >= sizeof (buf) ? sizeof (buf) : (COUNT)flen; cread (buf, num_bytes, 1, fh); WriteStateFile (buf, num_bytes, 1, fp); flen -= num_bytes; } CloseStateFile (fp); } // Copy the defined groupinfo file from the compressed stream fp = OpenStateFile (DEFGRPINFO_FILE, "wb"); if (fp) { DWORD flen; cread_32 (fh, &flen); while (flen) { COUNT num_bytes; num_bytes = flen >= sizeof (buf) ? sizeof (buf) : (COUNT)flen; cread (buf, num_bytes, 1, fh); WriteStateFile (buf, num_bytes, 1, fp); flen -= num_bytes; } CloseStateFile (fp); } // Copy the random groupinfo file from the compressed stream fp = OpenStateFile (RANDGRPINFO_FILE, "wb"); if (fp) { DWORD flen; cread_32 (fh, &flen); while (flen) { COUNT num_bytes; num_bytes = flen >= sizeof (buf) ? sizeof (buf) : (COUNT)flen; cread (buf, num_bytes, 1, fh); WriteStateFile (buf, num_bytes, 1, fp); flen -= num_bytes; } CloseStateFile (fp); } LoadStarDesc (&SD, fh); cclose (fh); res_CloseResFile (in_fp); EncounterGroup = 0; EncounterRace = -1; ReinitQueue (&race_q[0]); ReinitQueue (&race_q[1]); CurStarDescPtr = FindStar (NULL, &SD.star_pt, 0, 0); if (!(NextActivity & START_ENCOUNTER) && LOBYTE (NextActivity) == IN_INTERPLANETARY) NextActivity |= START_INTERPLANETARY; return TRUE; }