Maelstrom: Use the game ID to match scores in the high score list and the last game

From d3a782c5ca45eab23b2673ff4d387fc1e7a24492 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 6 Apr 2026 16:12:35 -0700
Subject: [PATCH] Use the game ID to match scores in the high score list and
 the last game

---
 game/Maelstrom_Globals.h |  2 +-
 game/gameover.cpp        |  4 +---
 game/init.cpp            |  4 ++--
 game/main.cpp            |  2 +-
 game/replay.cpp          |  3 +++
 game/scores.cpp          | 14 +++++++-------
 game/scores.h            |  1 +
 7 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/game/Maelstrom_Globals.h b/game/Maelstrom_Globals.h
index 6f13fb09..37051d99 100644
--- a/game/Maelstrom_Globals.h
+++ b/game/Maelstrom_Globals.h
@@ -103,7 +103,7 @@ struct Resolution {
 extern array<Resolution> gResolutions;
 extern int	gResolutionIndex;
 extern char    *gReplayFile;
-extern Sint32	gLastHigh;
+extern Uint32	gLastGameID;
 extern SDL_Rect gScrnRect;
 extern MPoint	gShotOrigins[SHIP_FRAMES];
 extern MPoint	gThrustOrigins[SHIP_FRAMES];
diff --git a/game/gameover.cpp b/game/gameover.cpp
index 778abd3f..fd1ef6fd 100644
--- a/game/gameover.cpp
+++ b/game/gameover.cpp
@@ -127,13 +127,10 @@ void GameOverPanelDelegate::OnShow()
 	    TheShip->GetScore() > 0) {
 		for ( i = 0; i<NUM_SCORES; ++i ) {
 			if ( TheShip->GetScore() >= (int)hScores[i].score ) {
-				gLastHigh = i;
 				BeginEnterName();
 				break;
 			}
 		}
-	} else {
-		gLastHigh = -1;
 	}
 
 	m_showTime = SDL_GetTicks();
@@ -144,6 +141,7 @@ void GameOverPanelDelegate::OnHide()
 	if (gReplay.IsRecording()) {
 		// Save this as the last game
 		gReplay.Save(LAST_REPLAY);
+		gLastGameID = gReplay.GetGameInfo().gameID;
 	}
 	gReplay.SetMode(REPLAY_IDLE);
 
diff --git a/game/init.cpp b/game/init.cpp
index c6a15be4..1d44560f 100644
--- a/game/init.cpp
+++ b/game/init.cpp
@@ -49,7 +49,7 @@ UIManager *ui = nullptr;
 array<Resolution> gResolutions;
 int	gResolutionIndex;
 char   *gReplayFile = nullptr;
-Sint32	gLastHigh;
+Uint32	gLastGameID;
 Uint64	gLastDrawn;
 int     gNumSprites;
 SDL_Rect gScrnRect;
@@ -855,7 +855,7 @@ bool StartInitialization(int window_width, int window_height, Uint32 window_flag
 	gNetworkAvailable = NET_Init();
 
 	// -- Initialize some variables
-	gLastHigh = -1;
+	gLastGameID = 0;
 
 	// -- Load our preferences files
 	prefs = new Prefs(GAME_PREFS_FILE);
diff --git a/game/main.cpp b/game/main.cpp
index 4fe8e237..55c89be2 100644
--- a/game/main.cpp
+++ b/game/main.cpp
@@ -361,7 +361,7 @@ MainPanelDelegate::OnTick()
 	for (int index = 0; index < NUM_SCORES; index++) {
 		Uint8 R, G, B;
 
-		if ( gLastHigh == index ) {
+		if ( gLastGameID && gLastGameID == hScores[index].gameID ) {
 			R = 0xFF;
 			G = 0xFF;
 			B = 0xFF;
diff --git a/game/replay.cpp b/game/replay.cpp
index 8b737757..a52baf78 100644
--- a/game/replay.cpp
+++ b/game/replay.cpp
@@ -183,6 +183,9 @@ Replay::Save(const char *file)
 		SDL_WriteU8(fp, m_finalScore[i].Frags);
 	}
 
+	// Use the seed as the unique game ID
+	m_game.gameID = m_game.seed;
+
 	data.Reset();
 	m_game.WriteToPacket(data);
 	SDL_WriteU32LE(fp, data.Size());
diff --git a/game/scores.cpp b/game/scores.cpp
index 12dead8f..5b57cb09 100644
--- a/game/scores.cpp
+++ b/game/scores.cpp
@@ -51,19 +51,20 @@ static SDL_EnumerationResult SDLCALL LoadScoresCallback(void *userdata, const ch
 	Replay replay;
 	Scores score;
 
-	if (SDL_strcmp(fname, LAST_REPLAY) == 0) {
-		return SDL_ENUM_CONTINUE;
-	}
 	if (replay.Load(fname, true)) {
 		SDL_strlcpy(score.name, replay.GetDisplayName(), sizeof(score.name));
 		score.wave = replay.GetFinalWave();
 		score.score = replay.GetFinalScore();
+		score.gameID = replay.GetGameInfo().gameID;
 	} else {
 		SDL_zero(score);
 	}
-	score.file = SDL_strdup(fname);
-	scores->add(score);
-
+	if (SDL_strcmp(fname, LAST_REPLAY) == 0) {
+		gLastGameID = score.gameID;
+	} else {
+		score.file = SDL_strdup(fname);
+		scores->add(score);
+	}
 	return SDL_ENUM_CONTINUE;
 }
 
@@ -136,6 +137,5 @@ void ZapHighScores()
 	SDL_CloseStorage(storage);
 
 	FreeScores();
-	gLastHigh = -1;
 }
 
diff --git a/game/scores.h b/game/scores.h
index bfb00649..7cfcb475 100644
--- a/game/scores.h
+++ b/game/scores.h
@@ -32,6 +32,7 @@ typedef	struct {
 	char name[20];
 	Uint32 wave;
 	Uint32 score;	
+	Uint32 gameID;
 	char *file;
 } Scores;