diff --git a/src/d_clisrv.c b/src/d_clisrv.c index bc88519db..9f7d96ca1 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -5302,10 +5302,18 @@ void TryRunTics(tic_t realtics) } // record the actual local controls - boolean canSimulate = (gamestate == GS_LEVEL) && cv_simulate.value && !server; + boolean canSimulate = (gamestate == GS_LEVEL) && leveltime >= 10 && (cv_simulate.value && !server); + boolean recordingStates = (gamestate == GS_LEVEL) && leveltime >= 10; tic_t numNewTics = neededtic - gametic; static int controlDelay = -1; + if (recordingStates && rewindingWow && rewindingTarget > 0) + { + save_p = gameStateBuffer[(gametic - rewindingTarget) % BACKUPTICS]; + P_LoadNetGame(true); + rewindingWow = false; + } + // record the actual local controls for this frame liveTic = I_GetTime(); simulatedTicBuffer[liveTic % BACKUPTICS] = localcmds; @@ -5340,9 +5348,7 @@ void TryRunTics(tic_t realtics) consistancy[gametic%BACKUPTICS] = Consistancy(); - - if (canSimulate) { - + if (recordingStates) { // store this real state save_p = gameStateBuffer[gametic % BACKUPTICS]; P_SaveNetGame(); @@ -5396,7 +5402,7 @@ void TryRunTics(tic_t realtics) // this must be done after smoothedTic is set RefreshNetDetections(); - numToSimulate = cv_simulatetics.value; + numToSimulate = estimatedRTT;//cv_simulatetics.value; // simulate the rest o da future for (int i = 0; i < numToSimulate; i++) { @@ -5404,7 +5410,7 @@ void TryRunTics(tic_t realtics) for (int j = 0; j < MAXPLAYERS; j++) { if (playeringame[j] && j != consoleplayer) { // just use their last control for now - netcmds[gametic % BACKUPTICS][j] = netcmds[(gametic - 1 + BACKUPTICS) % BACKUPTICS][j]; + netcmds[gametic % BACKUPTICS][j] = netcmds[(min(smoothedTic + i + 1, gametic) - 1 + BACKUPTICS) % BACKUPTICS][j]; } } @@ -5480,7 +5486,7 @@ void MakeNetDebugString() netDebugText[0] = 0; - for (int i = 15; i >= 0; i--) + for (int i = 8; i >= 0; i--) { if ((tic_t)i >= simTic - gametic) { // show tics and matches diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 12cf3dca6..a9a31442f 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -119,6 +119,7 @@ static void KartEliminateLast_OnChange(void); static void Command_Savestate(void); static void Command_Loadstate(void); static void Command_Rewind(void); +static void Command_Saveloadtest(void); #ifdef NETGAME_DEVMODE static void Fishcake_OnChange(void); @@ -637,6 +638,7 @@ void D_RegisterServerCommands(void) COM_AddCommand("savestate", Command_Savestate); COM_AddCommand("loadstate", Command_Loadstate); COM_AddCommand("rewind", Command_Rewind); + COM_AddCommand("saveloadtest", Command_Saveloadtest); CV_RegisterVar(&cv_simulate); CV_RegisterVar(&cv_simulatetics); @@ -780,6 +782,7 @@ static void Command_Loadstate(void) extern boolean rewindingWow; extern int rewindingTarget; +extern boolean memleak; static void Command_Rewind(void) { @@ -787,6 +790,39 @@ static void Command_Rewind(void) rewindingTarget = COM_Argc() > 1 ? atoi(COM_Argv(1)) : 0; } +void MemShow() { + Z_CheckHeap(-1); + CONS_Printf("\x82%s", M_GetText("Memory Info\n")); + CONS_Printf(M_GetText("Total heap used : %7s KB\n"), sizeu1(Z_TotalUsage() >> 10)); + CONS_Printf(M_GetText("Static : %7s KB\n"), sizeu1(Z_TagUsage(PU_STATIC) >> 10)); + CONS_Printf(M_GetText("Static (sound) : %7s KB\n"), sizeu1(Z_TagUsage(PU_SOUND) >> 10)); + CONS_Printf(M_GetText("Static (music) : %7s KB\n"), sizeu1(Z_TagUsage(PU_MUSIC) >> 10)); + CONS_Printf(M_GetText("Locked cache : %7s KB\n"), sizeu1(Z_TagUsage(PU_CACHE) >> 10)); + CONS_Printf(M_GetText("Level : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVEL) >> 10)); + CONS_Printf(M_GetText("Special thinker : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVSPEC) >> 10)); + CONS_Printf(M_GetText("All purgable : %7s KB\n"), + sizeu1(Z_TagsUsage(PU_PURGELEVEL, INT32_MAX) >> 10)); +} + +char buffer[1024 * 768]; +static void Command_Saveloadtest(void) +{ + memleak = true; + + MemShow(); + + for (int i = 0; i < 100; i++) { + save_p = buffer; + P_SaveNetGame(); + save_p = buffer; + P_LoadNetGame(true); + } + + MemShow(); + + memleak = false; +} + // ========================================================================= // CLIENT STARTUP // ========================================================================= diff --git a/src/p_saveg.c b/src/p_saveg.c index eba1c61a5..13cb55b47 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -46,6 +46,8 @@ UINT8 *save_p; #define ARCHIVEBLOCK_THINKERS 0x7F37037C #define ARCHIVEBLOCK_SPECIALS 0x7F228378 +boolean memleak = false; + // Note: This cannot be bigger // than an UINT16 typedef enum @@ -493,6 +495,7 @@ static void P_NetArchiveWorld(void) UINT8 *put; // reload the map just to see difference + UINT8* wadData = NULL; mapsector_t *ms; mapsidedef_t *msd; maplinedef_t *mld; @@ -504,22 +507,21 @@ static void P_NetArchiveWorld(void) if (W_IsLumpWad(lastloadedmaplumpnum)) // welp it's a map wad in a pk3 { // HACK: Open wad file rather quickly so we can get the data from the relevant lumps - UINT8 *wadData = W_CacheLumpNum(lastloadedmaplumpnum, PU_STATIC); + wadData = W_CacheLumpNum(lastloadedmaplumpnum, PU_STATIC); filelump_t *fileinfo = (filelump_t *)(wadData + ((wadinfo_t *)wadData)->infotableofs); #define retrieve_mapdata(d, f)\ - d = Z_Malloc((f)->size, PU_CACHE, NULL); \ - M_Memcpy(d, wadData + (f)->filepos, (f)->size) + d = (void*)(wadData + (f)->filepos); + retrieve_mapdata(ms, fileinfo + ML_SECTORS); retrieve_mapdata(mld, fileinfo + ML_LINEDEFS); retrieve_mapdata(msd, fileinfo + ML_SIDEDEFS); #undef retrieve_mapdata - Z_Free(wadData); // we're done with this now } else // phew it's just a WAD { - ms = W_CacheLumpNum(lastloadedmaplumpnum+ML_SECTORS, PU_CACHE); - mld = W_CacheLumpNum(lastloadedmaplumpnum+ML_LINEDEFS, PU_CACHE); - msd = W_CacheLumpNum(lastloadedmaplumpnum+ML_SIDEDEFS, PU_CACHE); + ms = W_CacheLumpNum(lastloadedmaplumpnum+ML_SECTORS, PU_CACHE); + mld = W_CacheLumpNum(lastloadedmaplumpnum+ML_LINEDEFS, PU_CACHE); + msd = W_CacheLumpNum(lastloadedmaplumpnum+ML_SIDEDEFS, PU_CACHE); } for (i = 0; i < numsectors; i++, ss++, ms++) @@ -734,6 +736,12 @@ static void P_NetArchiveWorld(void) WRITEUINT16(put, 0xffff); R_ClearTextureNumCache(false); + if (wadData) + { + // memory leak fix + Z_Free(wadData); + } + save_p = put; } @@ -2674,6 +2682,23 @@ static void P_NetUnArchiveThinkers(void) I_Error("Bad $$$.sav at archive block Thinkers"); // remove all the current thinkers + /*thinker_t* debugThinkerLists[NUM_THINKERLISTS][1024]; + mobj_t* debugMobjLists[NUM_THINKERLISTS][1024]; + int numDebugThinkers[NUM_THINKERLISTS]; + + for (i = 0; i < NUM_THINKERLISTS; i++) { + int count = 0; + thinker_t* test; + thinker_t* testNext; + for (test = thlist[i].next; test != &thlist[i]; test = testNext) { + testNext = test->next; + debugThinkerLists[i][count] = test; + debugMobjLists[i][count] = (mobj_t*)test; + count++; + } + numDebugThinkers[i] = count; + }*/ + for (i = 0; i < NUM_THINKERLISTS; i++) { currentthinker = thlist[i].next; @@ -2681,16 +2706,22 @@ static void P_NetUnArchiveThinkers(void) { next = currentthinker->next; + currentthinker->references = 0; + if (currentthinker->function.acp1 == (actionf_p1)P_MobjThinker) P_RemoveSavegameMobj((mobj_t *)currentthinker); // item isn't saved, don't remove it - else + else { + currentthinker->prev->next = currentthinker->next; + currentthinker->next->prev = currentthinker->prev; Z_Free(currentthinker); + } } } // we don't want the removed mobjs to come back iquetail = iquehead = 0; - P_InitThinkers(); + + //P_InitThinkers(); caused a memory leak with delayed-removed mobjs // clear sector thinker pointers so they don't point to non-existant thinkers for all of eternity for (i = 0; i < numsectors; i++) @@ -2880,7 +2911,7 @@ static void P_NetUnArchiveThinkers(void) executor_t *delay = NULL; UINT32 mobjnum; for (currentthinker = thlist[THINK_MAIN].next; currentthinker != &thlist[THINK_MAIN]; - currentthinker = currentthinker->next) + currentthinker = currentthinker->next) { if (currentthinker->function.acp1 != (actionf_p1)T_ExecutorDelay) continue; @@ -3505,6 +3536,7 @@ boolean P_LoadNetGame(boolean preserveLevel) CV_LoadNetVars(&save_p); if (!P_NetUnArchiveMisc(preserveLevel)) return false; + P_NetUnArchivePlayers(); if (gamestate == GS_LEVEL) {