//------------------------------------------------------------------------- /* Copyright (C) 1996, 2003 - 3D Realms Entertainment Copyright (C) 2000, 2003 - Matt Saettler (EDuke Enhancements) Copyright (C) 2004, 2007 - EDuke32 developers This file is part of EDuke32 EDuke32 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ //------------------------------------------------------------------------- #include "duke3d.h" #include "gamedef.h" #include "osd.h" extern int g_i,g_p; 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. int i=(MAXGAMEVARS-1); // AddLog("FreeGameVars"); for (;i>=0;i--) { if (aGameVars[i].plValues) Bfree(aGameVars[i].plValues); aGameVars[i].plValues=NULL; aGameVars[i].bReset=1; if (i >= MAXGAMEARRAYS) continue; if (aGameArrays[i].plValues) Bfree(aGameArrays[i].plValues); aGameArrays[i].plValues=NULL; aGameArrays[i].bReset=1; } iGameVarCount=iGameArrayCount=0; return; } static void ClearGameVars(void) { // only call this function ONCE... int i=(MAXGAMEVARS-1); //AddLog("ClearGameVars"); for (;i>=0;i--) { aGameVars[i].lValue=0; if (aGameVars[i].szLabel) Bfree(aGameVars[i].szLabel); aGameVars[i].szLabel=NULL; aGameVars[i].dwFlags=0; if (aGameVars[i].plValues) Bfree(aGameVars[i].plValues); aGameVars[i].plValues=NULL; aGameVars[i].bReset=1; if (i >= MAXGAMEARRAYS) continue; if (aGameArrays[i].szLabel) Bfree(aGameArrays[i].szLabel); aGameArrays[i].szLabel=NULL; if (aGameArrays[i].plValues) Bfree(aGameArrays[i].plValues); aGameArrays[i].plValues=NULL; aGameArrays[i].bReset=1; } iGameVarCount=iGameArrayCount=0; return; } int ReadGameVars(int fil) { int i, j; intptr_t l; char savedstate[MAXVOLUMES*MAXLEVELS]; Bmemset(&savedstate,0,sizeof(savedstate)); // AddLog("Reading gamevars from savegame"); FreeGameVars(); // nuke 'em from orbit, it's the only way to be sure... // Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__); // AddLog(g_szBuf); if (kdfread(&iGameVarCount,sizeof(iGameVarCount),1,fil) != 1) goto corrupt; for (i=0;ivars[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) { FreeMapState(i); } } if (kdfread(&l,sizeof(l),1,fil) != 1) goto corrupt; if (kdfread(g_szBuf,l,1,fil) != 1) goto corrupt; g_szBuf[l]=0; OSD_Printf("%s\n",g_szBuf); #if 0 { FILE *fp; AddLog("Dumping Vars..."); fp=fopen("xxx.txt","w"); if (fp) { DumpGameVars(fp); fclose(fp); } AddLog("Done Dumping..."); } #endif return(0); corrupt: return(1); } void SaveGameVars(FILE *fil) { int i, j; intptr_t l; char savedstate[MAXVOLUMES*MAXLEVELS]; Bmemset(&savedstate,0,sizeof(savedstate)); // AddLog("Saving Game Vars to File"); dfwrite(&iGameVarCount,sizeof(iGameVarCount),1,fil); for (i=0;ivars[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); dfwrite(&l,sizeof(l),1,fil); dfwrite(g_szBuf,l,1,fil); } void DumpGameVars(FILE *fp) { int i; if (!fp) { return; } fprintf(fp,"// Current Game Definitions\n\n"); for (i=0;i (MAXARRAYLABEL-1)) { error++; ReportError(-1); initprintf("%s:%d: error: array name `%s' exceeds limit of %d characters.\n",compilefile,line_number,pszLabel, MAXARRAYLABEL); return 0; } for (i=0;i (MAXVARLABEL-1)) { error++; ReportError(-1); initprintf("%s:%d: error: variable name `%s' exceeds limit of %d characters.\n",compilefile,line_number,pszLabel, MAXVARLABEL); return 0; } for (i=0;i=0;i--) if ((aGameVars[i].dwFlags & GAMEVAR_FLAG_PERACTOR) && !(aGameVars[i].dwFlags & GAMEVAR_FLAG_NODEFAULT)) { // OSD_Printf("reset %s (%d) to %s (%d)\n",aGameVars[i].szLabel,aGameVars[i].plValues[iActor],aDefaultGameVars[i].szLabel,aDefaultGameVars[i].lValue); aGameVars[i].plValues[iActor]=aGameVars[i].lDefault; } } static int GetGameID(const char *szGameLabel) { int i; for (i=0;i= iGameVarCount || id<0) { if (id&(MAXGAMEVARS<<2)) { int index=GetGameVarID(*insptr++,iActor,iPlayer); id &= ~(MAXGAMEVARS<<2); if (id&(MAXGAMEVARS<<1)) // negative array access { m = -m; id &= ~(MAXGAMEVARS<<1); } // OSD_Printf("GetGameVarID(): reading from array\n"); if (index >= aGameArrays[id].size || index < 0) { OSD_Printf(OSD_ERROR "GetGameVarID(): invalid array index (%s[%d])\n",aGameArrays[id].szLabel,index); return -1; } return(m * aGameArrays[id].plValues[index]); } if ((id&(MAXGAMEVARS<<1)) == 0) { OSD_Printf(OSD_ERROR "GetGameVarID(): invalid gamevar ID (%d)\n",id); return -1; } m = -m; id &= ~(MAXGAMEVARS<<1); } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_PERPLAYER) { // for the current player if (iPlayer < 0 || iPlayer >= MAXPLAYERS) { OSD_Printf(OSD_ERROR "GetGameVarID(): invalid player ID (%d)\n",iPlayer); return -1; } return(m * aGameVars[id].plValues[iPlayer]); } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_PERACTOR) { // for the current actor if (iActor < 0 || iActor >= MAXSPRITES) { OSD_Printf(OSD_ERROR "GetGameVarID(): invalid sprite ID (%d)\n",iActor); return -1; } return(m * aGameVars[id].plValues[iActor]); } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_INTPTR) return(m * (*((int*)aGameVars[id].lValue))); if (aGameVars[id].dwFlags & GAMEVAR_FLAG_SHORTPTR) return(m * (*((short*)aGameVars[id].lValue))); if (aGameVars[id].dwFlags & GAMEVAR_FLAG_CHARPTR) return(m * (*((char*)aGameVars[id].lValue))); return(m * aGameVars[id].lValue); } } void SetGameArrayID(int id,int index, int lValue) { if (id<0 || id >= iGameArrayCount || !((index < aGameArrays[id].size)&&(index>=0))) { OSD_Printf(OSD_ERROR "SetGameVarID(): tried to set invalid array ID (%d) or index out of bounds from sprite %d (%d), player %d\n",id,g_i,sprite[g_i].picnum,g_p); return; } aGameArrays[id].plValues[index]=lValue; } void SetGameVarID(int id, int lValue, int iActor, int iPlayer) { if (id<0 || id >= iGameVarCount) { OSD_Printf(OSD_ERROR "SetGameVarID(): tried to set invalid gamevar ID (%d) from sprite %d (%d), player %d\n",id,g_i,sprite[g_i].picnum,g_p); return; } //Bsprintf(g_szBuf,"SGVI: %d ('%s') to %d for %d %d",id,aGameVars[id].szLabel,lValue,iActor,iPlayer); //AddLog(g_szBuf); if (aGameVars[id].dwFlags & GAMEVAR_FLAG_PERPLAYER) { if (iPlayer < 0 || iPlayer > MAXPLAYERS-1) { OSD_Printf(OSD_ERROR "SetGameVarID(): invalid player (%d) for per-player gamevar %s from sprite %d (%d), player %d\n",iPlayer,aGameVars[id].szLabel,g_i,sprite[g_i].picnum,g_p); return; } // for the current player aGameVars[id].plValues[iPlayer]=lValue; return; } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_PERACTOR) { if (iActor < 0 || iActor > MAXSPRITES-1) { OSD_Printf(OSD_ERROR "SetGameVarID(): invalid sprite (%d) for per-actor gamevar %s from sprite %d (%d), player %d\n",iActor,aGameVars[id].szLabel,g_i,sprite[g_i].picnum,g_p); return; } // for the current actor aGameVars[id].plValues[iActor]=lValue; return; } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_INTPTR) { // set the value at pointer *((int*)aGameVars[id].lValue)=(int)lValue; return; } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_SHORTPTR) { // set the value at pointer *((short*)aGameVars[id].lValue)=(short)lValue; return; } if (aGameVars[id].dwFlags & GAMEVAR_FLAG_CHARPTR) { // set the value at pointer *((char*)aGameVars[id].lValue)=(char)lValue; return; } aGameVars[id].lValue=lValue; } int GetGameVar(const char *szGameLabel, int lDefault, int iActor, int iPlayer) { int i=0; for (;i=0;i--) { Bsprintf(aszBuf,"WEAPON%d_CLIP",i); aplWeaponClip[i]=GetGameValuePtr(aszBuf); if (!aplWeaponClip[i]) { initprintf("ERROR: NULL Weapon\n"); exit(0); } Bsprintf(aszBuf,"WEAPON%d_RELOAD",i); aplWeaponReload[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_FIREDELAY",i); aplWeaponFireDelay[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_TOTALTIME",i); aplWeaponTotalTime[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_HOLDDELAY",i); aplWeaponHoldDelay[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_FLAGS",i); aplWeaponFlags[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_SHOOTS",i); aplWeaponShoots[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_SPAWNTIME",i); aplWeaponSpawnTime[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_SPAWN",i); aplWeaponSpawn[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_SHOTSPERBURST",i); aplWeaponShotsPerBurst[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_WORKSLIKE",i); aplWeaponWorksLike[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_INITIALSOUND",i); aplWeaponInitialSound[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_FIRESOUND",i); aplWeaponFireSound[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_SOUND2TIME",i); aplWeaponSound2Time[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_SOUND2SOUND",i); aplWeaponSound2Sound[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_RELOADSOUND1",i); aplWeaponReloadSound1[i]=GetGameValuePtr(aszBuf); Bsprintf(aszBuf,"WEAPON%d_RELOADSOUND2",i); aplWeaponReloadSound2[i]=GetGameValuePtr(aszBuf); } } void ResetPointerVars(void) { aGameVars[GetGameID("RESPAWN_MONSTERS")].lValue = (intptr_t)&ud.respawn_monsters; aGameVars[GetGameID("RESPAWN_ITEMS")].lValue = (intptr_t)&ud.respawn_items; aGameVars[GetGameID("RESPAWN_INVENTORY")].lValue = (intptr_t)&ud.respawn_inventory; aGameVars[GetGameID("MONSTERS_OFF")].lValue = (intptr_t)&ud.monsters_off; aGameVars[GetGameID("MARKER")].lValue = (intptr_t)&ud.marker; aGameVars[GetGameID("FFIRE")].lValue = (intptr_t)&ud.ffire; aGameVars[GetGameID("LEVEL")].lValue = (intptr_t)&ud.level_number; aGameVars[GetGameID("VOLUME")].lValue = (intptr_t)&ud.volume_number; aGameVars[GetGameID("COOP")].lValue = (intptr_t)&ud.coop; aGameVars[GetGameID("MULTIMODE")].lValue = (intptr_t)&ud.multimode; aGameVars[GetGameID("myconnectindex")].lValue = (intptr_t)&myconnectindex; aGameVars[GetGameID("screenpeek")].lValue = (intptr_t)&screenpeek; aGameVars[GetGameID("currentweapon")].lValue = (intptr_t)&g_currentweapon; aGameVars[GetGameID("gs")].lValue = (intptr_t)&g_gs; aGameVars[GetGameID("looking_arc")].lValue = (intptr_t)&g_looking_arc; aGameVars[GetGameID("gun_pos")].lValue = (intptr_t)&g_gun_pos; aGameVars[GetGameID("weapon_xoffset")].lValue = (intptr_t)&g_weapon_xoffset; aGameVars[GetGameID("weaponcount")].lValue = (intptr_t)&g_kb; aGameVars[GetGameID("looking_angSR1")].lValue = (intptr_t)&g_looking_angSR1; aGameVars[GetGameID("xdim")].lValue = (intptr_t)&xdim; aGameVars[GetGameID("ydim")].lValue = (intptr_t)&ydim; aGameVars[GetGameID("windowx1")].lValue = (intptr_t)&windowx1; aGameVars[GetGameID("windowx2")].lValue = (intptr_t)&windowx2; aGameVars[GetGameID("windowy1")].lValue = (intptr_t)&windowy1; aGameVars[GetGameID("windowy2")].lValue = (intptr_t)&windowy2; aGameVars[GetGameID("totalclock")].lValue = (intptr_t)&totalclock; aGameVars[GetGameID("lastvisinc")].lValue = (intptr_t)&lastvisinc; aGameVars[GetGameID("numsectors")].lValue = (intptr_t)&numsectors; aGameVars[GetGameID("numplayers")].lValue = (intptr_t)&numplayers; aGameVars[GetGameID("current_menu")].lValue = (intptr_t)¤t_menu; aGameVars[GetGameID("viewingrange")].lValue = (intptr_t)&viewingrange; aGameVars[GetGameID("yxaspect")].lValue = (intptr_t)&yxaspect; aGameVars[GetGameID("gravitationalconstant")].lValue = (intptr_t)&gc; aGameVars[GetGameID("gametype_flags")].lValue = (intptr_t)&gametype_flags[ud.coop]; aGameVars[GetGameID("framerate")].lValue = (intptr_t)&framerate; aGameVars[GetGameID("camerax")].lValue = (intptr_t)&ud.camerax; aGameVars[GetGameID("cameray")].lValue = (intptr_t)&ud.cameray; aGameVars[GetGameID("cameraz")].lValue = (intptr_t)&ud.cameraz; aGameVars[GetGameID("cameraang")].lValue = (intptr_t)&ud.cameraang; aGameVars[GetGameID("camerahoriz")].lValue = (intptr_t)&ud.camerahoriz; aGameVars[GetGameID("camerasect")].lValue = (intptr_t)&ud.camerasect; aGameVars[GetGameID("cameradist")].lValue = (intptr_t)&cameradist; aGameVars[GetGameID("cameraclock")].lValue = (intptr_t)&cameraclock; aGameVars[GetGameID("myx")].lValue = (intptr_t)&myx; aGameVars[GetGameID("myy")].lValue = (intptr_t)&myy; aGameVars[GetGameID("myz")].lValue = (intptr_t)&myz; aGameVars[GetGameID("omyx")].lValue = (intptr_t)&omyx; aGameVars[GetGameID("omyy")].lValue = (intptr_t)&omyy; aGameVars[GetGameID("omyz")].lValue = (intptr_t)&omyz; aGameVars[GetGameID("myxvel")].lValue = (intptr_t)&myxvel; aGameVars[GetGameID("myyvel")].lValue = (intptr_t)&myyvel; aGameVars[GetGameID("myzvel")].lValue = (intptr_t)&myzvel; aGameVars[GetGameID("myhoriz")].lValue = (intptr_t)&myhoriz; aGameVars[GetGameID("myhorizoff")].lValue = (intptr_t)&myhorizoff; aGameVars[GetGameID("omyhoriz")].lValue = (intptr_t)&omyhoriz; aGameVars[GetGameID("omyhorizoff")].lValue = (intptr_t)&omyhorizoff; aGameVars[GetGameID("myang")].lValue = (intptr_t)&myang; aGameVars[GetGameID("omyang")].lValue = (intptr_t)&omyang; aGameVars[GetGameID("mycursectnum")].lValue = (intptr_t)&mycursectnum; aGameVars[GetGameID("myjumpingcounter")].lValue = (intptr_t)&myjumpingcounter; aGameVars[GetGameID("myjumpingtoggle")].lValue = (intptr_t)&myjumpingtoggle; aGameVars[GetGameID("myonground")].lValue = (intptr_t)&myonground; aGameVars[GetGameID("myhardlanding")].lValue = (intptr_t)&myhardlanding; aGameVars[GetGameID("myreturntocenter")].lValue = (intptr_t)&myreturntocenter; aGameVars[GetGameID("display_mirror")].lValue = (intptr_t)&display_mirror; aGameVars[GetGameID("randomseed")].lValue = (intptr_t)&randomseed; aGameVars[GetGameID("NUMWALLS")].lValue = (intptr_t)&numwalls; aGameVars[GetGameID("NUMSECTORS")].lValue = (intptr_t)&numsectors; }