From 212b3ae0f9fd56043c95bdf68d25c62a2b764dc6 Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sun, 23 Aug 2020 12:59:01 +0200
Subject: [PATCH] - Exhumed: fixed map startuo

---
 source/exhumed/src/2d.cpp       |  4 +++-
 source/exhumed/src/gameloop.cpp | 36 ++++++++++++++++-----------------
 source/exhumed/src/init.cpp     |  1 +
 3 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/source/exhumed/src/2d.cpp b/source/exhumed/src/2d.cpp
index 896ba6ac8..3df1e5044 100644
--- a/source/exhumed/src/2d.cpp
+++ b/source/exhumed/src/2d.cpp
@@ -813,7 +813,9 @@ public:
 	 {
 		 return;
 	 }
+#ifdef _DEBUG
      nLevelBest = kMap20;
+#endif
 
 	 if (nLevel < 1) nLevel = 1;
 	 if (nLevelNew < 1) nLevelNew = nLevel;
@@ -848,7 +850,7 @@ void TextOverlay::ComputeCinemaText()
 
 void TextOverlay::ReadyCinemaText(uint16_t nVal)
 {
-    FStringf label("TXT_EX_LASTLEVEL%d", nVal + 1);
+    FStringf label("TXT_EX_CINEMA%d", nVal + 1);
     label = GStrings(label);
     screentext = label.Split("\n");
     ComputeCinemaText();
diff --git a/source/exhumed/src/gameloop.cpp b/source/exhumed/src/gameloop.cpp
index 40d5ecf86..86bf0c5ef 100644
--- a/source/exhumed/src/gameloop.cpp
+++ b/source/exhumed/src/gameloop.cpp
@@ -65,35 +65,37 @@ void DrawClock();
 int32_t calc_smoothratio(ClockTicks totalclk, ClockTicks ototalclk);
 void DoTitle(CompletionFunc completion);
 
-int levelnew = -1;
-
-void FinishLevel(TArray<JobDesc> jobs)
+static int FinishLevel(TArray<JobDesc> &jobs)
 {
     if (levelnum > nBestLevel) {
         nBestLevel = levelnum - 1;
     }
 
-    levelnew = levelnum + 1;
 
     StopAllSounds();
 
     bCamera = false;
     nMapMode = 0;
 
-    if (levelnum != kMap20 && EndLevel != 2)
+    STAT_Update(levelnum == kMap20);
+    if (levelnum != kMap20)
     {
-        // There's really no choice but to enter an active wait loop here to make the sound play out.
-        PlayLocalSound(StaticSound[59], 0, true, CHANF_UI);
-        int nTicks = totalclock + 12;
-        while (nTicks > (int)totalclock) { HandleAsync(); }
+        if (EndLevel != 2)
+        {
+            // There's really no choice but to enter an active wait loop here to make the sound play out.
+            PlayLocalSound(StaticSound[59], 0, true, CHANF_UI);
+            int nTicks = totalclock + 12;
+            while (nTicks > (int)totalclock) { HandleAsync(); }
+        }
     }
     else nPlayerLives[0] = 0;
 
     DoAfterCinemaScene(levelnum, jobs);
+    return levelnum == kMap20? -1 : levelnum + 1;
 }
 
 
-void showmap(short nLevel, short nLevelNew, short nLevelBest, TArray<JobDesc> jobs)
+static void showmap(short nLevel, short nLevelNew, short nLevelBest, TArray<JobDesc> &jobs)
 {
     if (nLevelNew == 5 && !(nCinemaSeen & 1)) {
         nCinemaSeen |= 1;
@@ -175,18 +177,17 @@ void CheckProgression()
 {
     TArray<JobDesc> jobs;
     bool startlevel = false;
-    int mylevelnew = levelnew;
-
-    levelnew = -1;
+    int mylevelnew = -1;
 
     if (GameAction >= 0)
     {
         if (GameAction < 1000)
         {
             // start a new game on the given level
-            GameAction = -1;
             mylevelnew = GameAction;
+            GameAction = -1;
             InitNewGame();
+            if (mylevelnew > 0) STAT_StartNewGame("Exhumed", 1);
             if (mylevelnew != 0) nBestLevel = mylevelnew - 1;
         }
         else
@@ -200,7 +201,7 @@ void CheckProgression()
     else if (EndLevel)
     {
         if (levelnum == 0) startmainmenu();
-        else FinishLevel(jobs);
+        else mylevelnew = FinishLevel(jobs);
         EndLevel = false;
     }
     if (mylevelnew > -1 && mylevelnew < kMap20)
@@ -239,6 +240,7 @@ void CheckProgression()
 
                     if (selectedlevelnew == 11) nCinemaSeen |= 2;
                     if (mylevelnew != selectedlevelnew) STAT_Cancel();
+                    else STAT_NewLevel(currentLevel->labelName);
                 }
             });
     }
@@ -284,14 +286,13 @@ int GameInterface::app_main()
             case GS_STARTUP:
                 totalclock = 0;
                 ototalclock = 0;
-                levelnew = -1;
                 GameAction = -1;
                 EndLevel = false;
 
                 if (userConfig.CommandMap.IsNotEmpty())
                 {
                     auto map = FindMapByName(userConfig.CommandMap);
-                    if (map) levelnew = map->levelNumber;
+                    if (map) GameAction = map->levelNumber;
                     userConfig.CommandMap = "";
                     continue;
                 }
@@ -322,7 +323,6 @@ int GameInterface::app_main()
         catch (CRecoverableError& err)
         {
             // Clear all progression sensitive variables here.
-            levelnew = -1;
             GameAction = -1;
             EndLevel = false;
             C_FullConsole();
diff --git a/source/exhumed/src/init.cpp b/source/exhumed/src/init.cpp
index 91ae9f476..6e44d87dd 100644
--- a/source/exhumed/src/init.cpp
+++ b/source/exhumed/src/init.cpp
@@ -184,6 +184,7 @@ void InitLevel(int level)
     if (!LoadLevel(level)) {
         I_Error("Can't load level %d...\n", level);
     }
+    currentLevel = FindMapByLevelNum(level);
 
     for (int i = 0; i < nTotalPlayers; i++)
     {