From 244a0c1a5e2071823922d64c3601e2c7950a4bd8 Mon Sep 17 00:00:00 2001 From: terminx Date: Fri, 18 Jul 2008 02:46:24 +0000 Subject: [PATCH] Gamevars in savestates (in savegames) Still needs debugging git-svn-id: https://svn.eduke32.com/eduke32@862 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/duke3d.h | 2 ++ polymer/eduke32/source/funct.h | 2 ++ polymer/eduke32/source/game.c | 2 +- polymer/eduke32/source/gameexec.c | 33 ++++++++++++++++++++++++ polymer/eduke32/source/gamevars.c | 43 ++++++++++++++++++++++++++----- polymer/eduke32/source/premap.c | 29 +++++++++++++++++++++ 6 files changed, 103 insertions(+), 8 deletions(-) diff --git a/polymer/eduke32/source/duke3d.h b/polymer/eduke32/source/duke3d.h index 9316e77c5..771506681 100644 --- a/polymer/eduke32/source/duke3d.h +++ b/polymer/eduke32/source/duke3d.h @@ -831,6 +831,7 @@ enum gamevarflags { GAMEVAR_FLAG_SYNCCHECK = 16384, // check event sync when translating GAMEVAR_FLAG_SHORTPTR = 32768, // plValue is a pointer to a short GAMEVAR_FLAG_CHARPTR = 65536, // plValue is a pointer to a char + GAMEVAR_FLAG_NORESET = 131072, // var values are not reset when restoring map state }; enum gamearrayflags { @@ -1031,6 +1032,7 @@ typedef struct { int lockclock; int randomseed, global_random; char scriptptrs[MAXSPRITES]; + intptr_t *vars[MAXGAMEVARS]; } mapstate_t; typedef struct { diff --git a/polymer/eduke32/source/funct.h b/polymer/eduke32/source/funct.h index eb33c519d..62e7b78ad 100644 --- a/polymer/eduke32/source/funct.h +++ b/polymer/eduke32/source/funct.h @@ -246,4 +246,6 @@ extern int getteampal(int team); extern void se40code(int x,int y,int z,int a,int h, int smoothratio); +extern void FreeMapState(int mapnum); + #endif // __funct_h__ diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 3ec003242..a488eaafa 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -9560,7 +9560,7 @@ static void freeconmem(void) if (map[i].musicfn1 != NULL) Bfree(map[i].musicfn1); if (map[i].savedstate != NULL) - Bfree(map[i].savedstate); + FreeMapState(i); } for (i=MAXQUOTES-1;i>=0;i--) diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index c9372a502..e88a5467d 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -7828,10 +7828,31 @@ void savemapstate(mapstate_t *save) Bmemcpy(&save->lockclock,&lockclock,sizeof(lockclock)); Bmemcpy(&save->randomseed,&randomseed,sizeof(randomseed)); Bmemcpy(&save->global_random,&global_random,sizeof(global_random)); + + for (i = 0; ivars[i]) + save->vars[i] = Bcalloc(MAXPLAYERS,sizeof(intptr_t)); + Bmemcpy(&save->vars[i][0],&aGameVars[i].plValues[0],sizeof(intptr_t) * MAXPLAYERS); + } + else if (aGameVars[i].dwFlags & GAMEVAR_FLAG_PERACTOR) + { + if (!save->vars[i]) + save->vars[i] = Bcalloc(MAXSPRITES,sizeof(intptr_t)); + Bmemcpy(&save->vars[i][0],&aGameVars[i].plValues[0],sizeof(intptr_t) * MAXSPRITES); + } + else save->vars[i] = (intptr_t *)aGameVars[i].lValue; + } + ototalclock = totalclock; } } +extern void ResetPointerVars(void); + void restoremapstate(mapstate_t *save) { if (save != NULL) @@ -7896,6 +7917,18 @@ void restoremapstate(mapstate_t *save) Bmemcpy(&randomseed,&save->randomseed,sizeof(randomseed)); Bmemcpy(&global_random,&save->global_random,sizeof(global_random)); + for (i = 0; ivars[i][0],sizeof(intptr_t) * MAXPLAYERS); + else if (aGameVars[i].dwFlags & GAMEVAR_FLAG_PERACTOR) + Bmemcpy(&aGameVars[i].plValues[0],&save->vars[i][0],sizeof(intptr_t) * MAXSPRITES); + else aGameVars[i].lValue = (intptr_t)save->vars[i]; + } + + ResetPointerVars(); + if (g_player[myconnectindex].ps->over_shoulder_on != 0) { cameradist = 0; diff --git a/polymer/eduke32/source/gamevars.c b/polymer/eduke32/source/gamevars.c index 07d81e718..ecbd35a78 100644 --- a/polymer/eduke32/source/gamevars.c +++ b/polymer/eduke32/source/gamevars.c @@ -28,8 +28,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern int g_i,g_p; -static void ResetPointerVars(void); - +void ResetPointerVars(void); +extern void FreeMapState(int mapnum); static void FreeGameVars(void) /* called from ReadGameVars() and ResetGameVars() */ { // call this function as many times as needed. @@ -98,7 +98,7 @@ static void ClearGameVars(void) int ReadGameVars(int fil) { - int i; + int i, j; intptr_t l; char savedstate[MAXVOLUMES*MAXLEVELS]; @@ -200,11 +200,26 @@ int ReadGameVars(int fil) if (map[i].savedstate == NULL) map[i].savedstate = Bcalloc(1,sizeof(mapstate_t)); if (kdfread(map[i].savedstate,sizeof(mapstate_t),1,fil) != sizeof(mapstate_t)) goto corrupt; + for (j=0;jvars[j]) + map[i].savedstate->vars[j] = Bcalloc(MAXPLAYERS,sizeof(intptr_t)); + if (kdfread(&map[i].savedstate->vars[j][0],sizeof(intptr_t) * MAXPLAYERS, 1, fil) != 1) goto corrupt; + } + else if (aGameVars[j].dwFlags & GAMEVAR_FLAG_PERACTOR) + { + if (!map[i].savedstate->vars[j]) + map[i].savedstate->vars[j] = Bcalloc(MAXSPRITES,sizeof(intptr_t)); + if (kdfread(&map[i].savedstate->vars[j][0],sizeof(intptr_t), MAXSPRITES, fil) != MAXSPRITES) goto corrupt; + } + } } else if (map[i].savedstate) { - Bfree(map[i].savedstate); - map[i].savedstate = NULL; + FreeMapState(i); } } @@ -233,7 +248,7 @@ corrupt: void SaveGameVars(FILE *fil) { - int i; + int i, j; intptr_t l; char savedstate[MAXVOLUMES*MAXLEVELS]; @@ -304,7 +319,21 @@ void SaveGameVars(FILE *fil) for (i=0;i<(MAXVOLUMES*MAXLEVELS);i++) if (map[i].savedstate) + { dfwrite(map[i].savedstate,sizeof(mapstate_t),1,fil); + for (j=0;jvars[j][0],sizeof(intptr_t) * MAXPLAYERS, 1, fil); + } + else if (aGameVars[j].dwFlags & GAMEVAR_FLAG_PERACTOR) + { + dfwrite(&map[i].savedstate->vars[j][0],sizeof(intptr_t) * MAXSPRITES, 1, fil); + } + } + } Bsprintf(g_szBuf,"EOF: EDuke32"); l=strlen(g_szBuf); @@ -1425,7 +1454,7 @@ void InitGameVarPointers(void) } } -static void ResetPointerVars(void) +void ResetPointerVars(void) { aGameVars[GetGameID("RESPAWN_MONSTERS")].lValue = (intptr_t)&ud.respawn_monsters; aGameVars[GetGameID("RESPAWN_ITEMS")].lValue = (intptr_t)&ud.respawn_items; diff --git a/polymer/eduke32/source/premap.c b/polymer/eduke32/source/premap.c index 3db556c62..2253a7f07 100644 --- a/polymer/eduke32/source/premap.c +++ b/polymer/eduke32/source/premap.c @@ -1236,6 +1236,13 @@ void newgame(int vn,int ln,int sk) ResetSystemDefaults(); + for (i=0;i<(MAXVOLUMES*MAXLEVELS);i++) + if (map[i].savedstate) + { + Bfree(map[i].savedstate); + map[i].savedstate = NULL; + } + if (ud.m_coop != 1) { for (i=0;ivars[j]) + Bfree(map[mapnum].savedstate->vars[j]); + } + else if (aGameVars[j].dwFlags & GAMEVAR_FLAG_PERACTOR) + { + if (map[mapnum].savedstate->vars[j]) + Bfree(map[mapnum].savedstate->vars[j]); + } + } + Bfree(map[mapnum].savedstate); + map[mapnum].savedstate = NULL; +} \ No newline at end of file