shithub: choc

Download patch

ref: 5d82119ccad9476d5db479f81952e56cef0e1751
parent: 9ff63977237ba7d7edb7f863c9a6ad4dd5b07cbe
author: Simon Howard <[email protected]>
date: Mon Sep 5 18:18:16 EDT 2011

Store Heretic savegames in the configuration directory, as with Doom
savegames.

Subversion-branch: /branches/raven-branch
Subversion-revision: 2361

--- a/src/heretic/d_main.c
+++ b/src/heretic/d_main.c
@@ -421,25 +421,19 @@
 void D_CheckRecordFrom(void)
 {
     int p;
-    char file[256];
+    char *filename;
 
     p = M_CheckParm("-recordfrom");
     if (!p || p > myargc - 2)
         return;
 
-    if (cdrom)
-    {
-        sprintf(file, SAVEGAMENAMECD "%c.hsg", myargv[p + 1][0]);
-    }
-    else
-    {
-        sprintf(file, SAVEGAMENAME "%c.hsg", myargv[p + 1][0]);
-    }
-    G_LoadGame(file);
+    filename = SV_Filename(myargv[p + 1][0] - '0');
+    G_LoadGame(filename);
     G_DoLoadGame();             // load the gameskill etc info from savegame
 
     G_RecordDemo(gameskill, 1, gameepisode, gamemap, myargv[p + 2]);
     D_DoomLoop();               // never returns
+    free(filename);
 }
 
 /*
@@ -940,6 +934,8 @@
         gamedescription = "Heretic (registered)";
     }
 
+    savegamedir = M_GetSaveGameDir("heretic.wad");
+
     I_PrintStartupBanner(gamedescription);
 
     // haleyjd: removed WATCOMC
@@ -1033,15 +1029,11 @@
     p = M_CheckParm("-loadgame");
     if (p && p < myargc - 1)
     {
-        if (cdrom)
-        {
-            sprintf(file, SAVEGAMENAMECD "%c.hsg", myargv[p + 1][0]);
-        }
-        else
-        {
-            sprintf(file, SAVEGAMENAME "%c.hsg", myargv[p + 1][0]);
-        }
-        G_LoadGame(file);
+        char *filename;
+
+	filename = SV_Filename(myargv[p + 1][0] - '0');
+        G_LoadGame(filename);
+	free(filename);
     }
 
     // Check valid episode and map
--- a/src/heretic/doomdef.h
+++ b/src/heretic/doomdef.h
@@ -66,7 +66,6 @@
 #include "d_ticcmd.h"
 
 #define	SAVEGAMENAME "hticsav"
-#define SAVEGAMENAMECD "c:\\heretic.cd\\hticsav"
 
 /*
 ===============================================================================
@@ -740,6 +739,7 @@
 // called by M_Responder
 
 // Support routines for saving games
+char *SV_Filename(int slot);
 void SV_Open(char *fileName);
 void SV_Close(char *fileName);
 void SV_Write(void *buffer, int size);
@@ -746,6 +746,8 @@
 void SV_WriteByte(byte val);
 void SV_WriteWord(unsigned short val);
 void SV_WriteLong(unsigned int val);
+
+extern char *savegamedir;
 
 void G_RecordDemo(skill_t skill, int numplayers, int episode, int map,
                   char *name);
--- a/src/heretic/g_game.c
+++ b/src/heretic/g_game.c
@@ -25,6 +25,7 @@
 // G_game.c
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include "doomdef.h"
 #include "doomkeys.h"
@@ -133,6 +134,7 @@
 
 short consistancy[MAXPLAYERS][BACKUPTICS];
 
+char *savegamedir;
 byte *savebuffer, *save_p;
 
 
@@ -1294,11 +1296,11 @@
 //
 //---------------------------------------------------------------------------
 
-char savename[256];
+static char *savename = NULL;
 
 void G_LoadGame(char *name)
 {
-    strcpy(savename, name);
+    savename = strdup(name);
     gameaction = ga_loadgame;
 }
 
@@ -1322,6 +1324,9 @@
     gameaction = ga_nothing;
 
     length = M_ReadFile(savename, &savebuffer);
+    free(savename);
+    savename = NULL;
+
     save_p = savebuffer + SAVESTRINGSIZE;
     // Skip the description field
     memset(vcheck, 0, sizeof(vcheck));
@@ -1684,21 +1689,15 @@
 void G_DoSaveGame(void)
 {
     int i;
-    char name[100];
+    char *filename;
     char verString[VERSIONSIZE];
     char *description;
 
-    if (cdrom)
-    {
-        sprintf(name, SAVEGAMENAMECD "%d.hsg", savegameslot);
-    }
-    else
-    {
-        sprintf(name, SAVEGAMENAME "%d.hsg", savegameslot);
-    }
+    filename = SV_Filename(savegameslot);
+
     description = savedescription;
 
-    SV_Open(name);
+    SV_Open(filename);
     SV_Write(description, SAVESTRINGSIZE);
     memset(verString, 0, sizeof(verString));
     DEH_snprintf(verString, VERSIONSIZE, "version %i", HERETIC_VERSION);
@@ -1717,11 +1716,32 @@
     P_ArchiveWorld();
     P_ArchiveThinkers();
     P_ArchiveSpecials();
-    SV_Close(name);
+    SV_Close(filename);
 
     gameaction = ga_nothing;
     savedescription[0] = 0;
     P_SetMessage(&players[consoleplayer], DEH_String(TXT_GAMESAVED), true);
+
+    free(filename);
+}
+
+//==========================================================================
+//
+// SV_Filename
+//
+// Generate the filename to use for a particular savegame slot.
+// Returns a malloc()'d buffer that must be freed by the caller.
+//
+//==========================================================================
+
+char *SV_Filename(int slot)
+{
+    char *filename;
+
+    filename = malloc(strlen(savegamedir) + strlen(SAVEGAMENAME) + 8);
+    sprintf(filename, "%s" SAVEGAMENAME "%d.hsg", savegamedir, slot);
+
+    return filename;
 }
 
 //==========================================================================
--- a/src/heretic/mn_menu.c
+++ b/src/heretic/mn_menu.c
@@ -645,19 +645,14 @@
     FILE *fp;
     int count;
     int i;
-    char name[256];
+    char *filename;
 
     for (i = 0; i < 6; i++)
     {
-        if (cdrom)
-        {
-            sprintf(name, SAVEGAMENAMECD "%d.hsg", i);
-        }
-        else
-        {
-            sprintf(name, SAVEGAMENAME "%d.hsg", i);
-        }
-        fp = fopen(name, "rb+");
+        filename = SV_Filename(i);
+        fp = fopen(filename, "rb+");
+	free(filename);
+
         if (!fp)
         {
             SlotText[i][0] = 0; // empty the string
@@ -826,21 +821,17 @@
 
 static boolean SCLoadGame(int option)
 {
-    char name[256];
+    char *filename;
 
     if (!SlotStatus[option])
     {                           // slot's empty...don't try and load
         return false;
     }
-    if (cdrom)
-    {
-        sprintf(name, SAVEGAMENAMECD "%d.hsg", option);
-    }
-    else
-    {
-        sprintf(name, SAVEGAMENAME "%d.hsg", option);
-    }
-    G_LoadGame(name);
+
+    filename = SV_Filename(option);
+    G_LoadGame(filename);
+    free(filename);
+
     MN_DeactivateMenu();
     BorderNeedRefresh = true;
     if (quickload == -1)