DEADSOFTWARE

moving platforms experiment (DO NOT USE YET!)
[d2df-sdl.git] / src / game / g_saveload.pas
index 3a1cecdc6e0782df8ef69912b81b17284c78ba18..bf56c6eeb0afb6b227e1dcbee88595b806f49489 100644 (file)
@@ -1,4 +1,19 @@
-{$MODE DELPHI}
+(* Copyright (C)  DooM 2D:Forever Developers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *)
+{$INCLUDE ../shared/a_modes.inc}
 unit g_saveload;
 
 interface
@@ -6,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 = $05;
   END_MARKER_STRING = 'END';
   PLAYER_VIEW_SIGNATURE = $57564C50; // 'PLVW'
   OBJ_SIGNATURE = $4A424F5F; // '_OBJ'
 
+
 procedure Obj_SaveState(o: PObj; var Mem: TBinMemoryWriter);
 var
   sig: DWORD;
@@ -148,11 +166,14 @@ 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);
   // Èìÿ êàðòû:
-    g_ProcessResourceStr(gMapInfo.Map, nil, nil, @str);
+    str := g_ExtractFileName(gMapInfo.Map);
     bMem.WriteString(str, 32);
   // Êîëè÷åñòâî èãðîêîâ:
     nPlayers := g_Player_GetCount();
@@ -322,6 +343,8 @@ var
   Game_CoopTotalSecrets,
   PID1, PID2: Word;
   i: Integer;
+  gameCleared: Boolean = false;
+  curmapfile: AnsiString = '';
 begin
   Result := False;
   bMem := nil;
@@ -338,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);
@@ -349,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);
   // Èìÿ êàðòû:
@@ -426,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;
@@ -550,7 +583,7 @@ begin
   ///// /////
 
   // Èùåì òðèããåðû ñ óñëîâèåì ñìåðòè ìîíñòðîâ:
-    if (gMonsters <> nil) and (gTriggers <> nil) then
+    if {(gMonsters <> nil) and} (gTriggers <> nil) then
       g_Map_ReAdd_DieTriggers();
 
   // Çàêðûâàåì ôàéë çàãðóçêè:
@@ -563,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;