DEADSOFTWARE

restarting the same map will not reload textures (yay, quickload!); don't spam log...
[d2df-sdl.git] / src / game / g_saveload.pas
index c77147f87537902a514173bbfe081821576d6a96..a54e26be05338c02430c418a9954762f1091a213 100644 (file)
@@ -13,7 +13,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
-{$MODE DELPHI}
+{$INCLUDE ../shared/a_modes.inc}
 unit g_saveload;
 
 interface
@@ -21,27 +21,30 @@ interface
 uses
   e_graphics, g_phys, g_textures, BinEditor;
 
+
 function g_GetSaveName(n: Integer): String;
 function g_SaveGame(n: Integer; Name: String): Boolean;
 function g_LoadGame(n: Integer): Boolean;
 procedure Obj_SaveState(o: PObj; var Mem: TBinMemoryWriter);
 procedure Obj_LoadState(o: PObj; var Mem: TBinMemoryReader);
 
+
 implementation
 
 uses
   g_game, g_items, g_map, g_monsters, g_triggers,
   g_basic, g_main, SysUtils, Math, wadreader,
-  MAPSTRUCT, MAPDEF, g_weapons, g_player, g_console,
+  MAPDEF, g_weapons, g_player, g_console,
   e_log, g_language;
 
 const
   SAVE_SIGNATURE = $56534644; // 'DFSV'
-  SAVE_VERSION = $02;
+  SAVE_VERSION = $04;
   END_MARKER_STRING = 'END';
   PLAYER_VIEW_SIGNATURE = $57564C50; // 'PLVW'
   OBJ_SIGNATURE = $4A424F5F; // '_OBJ'
 
+
 procedure Obj_SaveState(o: PObj; var Mem: TBinMemoryWriter);
 var
   sig: DWORD;
@@ -163,6 +166,9 @@ begin
     bMem := TBinMemoryWriter.Create(256);
   // Èìÿ èãðû:
     bMem.WriteString(Name, 32);
+  // Ïîëíûé ïóòü ê âàäó è êàðòà
+    if (Length(gCurrentMapFileName) <> 0) then e_LogWritefln('SAVE: current map is ''%s''...', [gCurrentMapFileName]);
+    bMem.WriteString(gCurrentMapFileName);
   // Ïóòü ê êàðòå:
     str := gGameSettings.WAD;
     bMem.WriteString(str, 128);
@@ -337,6 +343,8 @@ var
   Game_CoopTotalSecrets,
   PID1, PID2: Word;
   i: Integer;
+  gameCleared: Boolean = false;
+  curmapfile: AnsiString = '';
 begin
   Result := False;
   bMem := nil;
@@ -353,7 +361,7 @@ begin
     end;
 
     e_WriteLog('Loading saved game...', MSG_NOTIFY);
-    g_Game_Free();
+    //g_Game_Free(false); // don't free textures for the same map
 
     g_Game_ClearLoading();
     g_Game_SetLoadingText(_lc[I_LOAD_SAVE_FILE], 0, False);
@@ -364,6 +372,16 @@ begin
     bFile.ReadMemory(bMem);
   // Èìÿ èãðû:
     bMem.ReadString(str);
+
+  // Ïîëíûé ïóòü ê âàäó è êàðòà
+    bMem.ReadString(curmapfile);
+
+    if (Length(gCurrentMapFileName) <> 0) then e_LogWritefln('LOAD: previous map was ''%s''...', [gCurrentMapFileName]);
+    if (Length(curmapfile) <> 0) then e_LogWritefln('LOAD: new map is ''%s''...', [curmapfile]);
+  // À âîò òóò, íàêîíåö, ÷èñòèì ðåñóðñû
+    g_Game_Free(curmapfile <> gCurrentMapFileName); // don't free textures for the same map
+    gameCleared := true;
+
   // Ïóòü ê êàðòå:
     bMem.ReadString(WAD_Path);
   // Èìÿ êàðòû:
@@ -441,7 +459,7 @@ begin
     g_Game_SetupScreenSize();
 
   // Çàãðóçêà è çàïóñê êàðòû:
-    if not g_Game_StartMap(WAD_Path + ':\' + Map_Name, True) then
+    if not g_Game_StartMap(WAD_Path + ':\' + Map_Name, True, curmapfile) then
     begin
       g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [WAD_Path + ':\' + Map_Name]));
       Exit;
@@ -565,7 +583,7 @@ begin
   ///// /////
 
   // Èùåì òðèããåðû ñ óñëîâèåì ñìåðòè ìîíñòðîâ:
-    if (gMonsters <> nil) and (gTriggers <> nil) then
+    if {(gMonsters <> nil) and} (gTriggers <> nil) then
       g_Map_ReAdd_DieTriggers();
 
   // Çàêðûâàåì ôàéë çàãðóçêè:
@@ -578,11 +596,13 @@ begin
       begin
         g_Console_Add(_lc[I_GAME_ERROR_LOAD]);
         e_WriteLog('LoadState I/O Error: '+E1.Message, MSG_WARNING);
+        if not gameCleared then g_Game_Free();
       end;
     on E2: EBinSizeError do
       begin
         g_Console_Add(_lc[I_GAME_ERROR_LOAD]);
         e_WriteLog('LoadState Size Error: '+E2.Message, MSG_WARNING);
+        if not gameCleared then g_Game_Free();
       end;
   end;