From a8eb9b2485a8c8e5ab3134fa44f6b49e61ae26c1 Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 22 Dec 2019 15:13:58 +0000 Subject: [PATCH] Polyobjects are now synced with gratuitous awkwardness Fixed executor crash During savestates, console vars will be stealthy --- src/command.c | 6 +- src/command.h | 2 +- src/d_clisrv.c | 2 +- src/d_netcmd.c | 2 + src/g_game.c | 2 +- src/p_polyobj.c | 5 ++ src/p_polyobj.h | 4 + src/p_saveg.c | 192 +++++++++++++++++++++++++++++++++++------------- src/p_saveg.h | 2 +- 9 files changed, 160 insertions(+), 57 deletions(-) diff --git a/src/command.c b/src/command.c index 3eebe32d1..354d984b8 100644 --- a/src/command.c +++ b/src/command.c @@ -1364,6 +1364,7 @@ badinput: // static boolean serverloading = false; +static boolean serverloadingstealth = false; static void Got_NetVar(UINT8 **p, INT32 playernum) { @@ -1391,7 +1392,7 @@ static void Got_NetVar(UINT8 **p, INT32 playernum) cvar = CV_FindNetVar(netid); svalue = (char *)*p; SKIPSTRING(*p); - stealth = READUINT8(*p); + stealth = READUINT8(*p) || (serverloading && serverloadingstealth); if (!cvar) { @@ -1448,13 +1449,14 @@ void CV_SaveNetVars(UINT8 **p, boolean isdemorecording) WRITEUINT16(count_p, count); } -void CV_LoadNetVars(UINT8 **p) +void CV_LoadNetVars(UINT8 **p, boolean onlyIfChanged) { consvar_t *cvar; UINT16 count; // prevent "invalid command received" serverloading = true; + serverloadingstealth = onlyIfChanged; for (cvar = consvar_vars; cvar; cvar = cvar->next) if (cvar->flags & CV_NETVAR) diff --git a/src/command.h b/src/command.h index 6b5d513ef..2ef276fe0 100644 --- a/src/command.h +++ b/src/command.h @@ -165,7 +165,7 @@ void CV_SaveVariables(FILE *f); // load/save gamesate (load and save option and for network join in game) void CV_SaveNetVars(UINT8 **p, boolean isdemorecording); -void CV_LoadNetVars(UINT8 **p); +void CV_LoadNetVars(UINT8 **p, boolean onlyIfChanged); // reset cheat netvars after cheats is deactivated void CV_ResetCheatNetVars(void); diff --git a/src/d_clisrv.c b/src/d_clisrv.c index e438bf965..0d5705e31 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -4151,7 +4151,7 @@ static void HandlePacketFromAwayNode(SINT8 node) scp = netbuffer->u.servercfg.varlengthinputs; CV_LoadPlayerNames(&scp); - CV_LoadNetVars(&scp); + CV_LoadNetVars(&scp, false); #ifdef JOININGAME /// \note Wait. What if a Lua script uses some global custom variables synched with the NetVars hook? /// Shouldn't them be downloaded even at intermission time? diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 3d88c9024..80c63ed01 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -847,12 +847,14 @@ static void Command_Saveloadtest(void) } extern boolean autotimefudge; +extern UINT8 autotimefudgetime; static void Command_Autotimefudge(void) { if (!autotimefudge) { CONS_Printf("Searching for the best time fudge...\n"); autotimefudge = true; + autotimefudgetime = 0; } } diff --git a/src/g_game.c b/src/g_game.c index b0f18d98e..284a69ba5 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -7357,7 +7357,7 @@ void G_DoPlayDemo(char *defdemoname) #endif // net var data - CV_LoadNetVars(&demo_p); + CV_LoadNetVars(&demo_p, false); // Sigh ... it's an empty demo. if (*demo_p == DEMOMARKER) diff --git a/src/p_polyobj.c b/src/p_polyobj.c index 25a7122ad..b08bdb6a7 100644 --- a/src/p_polyobj.c +++ b/src/p_polyobj.c @@ -1538,6 +1538,11 @@ void Polyobj_InitLevel(void) qitem = (mobjqitem_t *)M_QueueIterator(&spawnqueue); Polyobj_spawnPolyObj(i, qitem->mo, qitem->mo->spawnpoint->angle); + + PolyObjects[i].origx = qitem->mo->x; + PolyObjects[i].origy = qitem->mo->y; + PolyObjects[i].origz = qitem->mo->z; + PolyObjects[i].origangle = qitem->mo->spawnpoint->angle; } // move polyobjects to spawn points diff --git a/src/p_polyobj.h b/src/p_polyobj.h index 60e996cae..ee8ff6e45 100644 --- a/src/p_polyobj.h +++ b/src/p_polyobj.h @@ -104,6 +104,10 @@ typedef struct polyobj_s // these are saved for netgames, so do not let Lua touch these! INT32 spawnflags; // Flags the polyobject originally spawned with + INT32 spawntrans; // Translucency the polyobject originally spawned with + + fixed_t origx, origy, origz; + INT16 origangle; } polyobj_t; // diff --git a/src/p_saveg.c b/src/p_saveg.c index 88993ef4f..991d24013 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -2202,6 +2202,7 @@ static void P_LocalArchiveThinkers(void) int i, j; const thinker_t* thinker; mobj_t* savedMobj; + executor_t* savedExecutor; UINT8* original = save_p; WRITEUINT32(save_p, ARCHIVEBLOCK_THINKERS); @@ -2234,6 +2235,12 @@ static void P_LocalArchiveThinkers(void) RELINK(savedMobj->hnext); RELINK(savedMobj->hprev); } + + if (j == tc_executor) + { + savedExecutor = &((executor_t*)save_p)[-1]; + RELINK(savedExecutor->caller); + } } #undef RELINK @@ -3246,6 +3253,15 @@ static void CollectDebugObjectList(void) { void S_DetachChannelsFromOrigin(void* origin); +typedef struct +{ + mobj_t* mobj; + int thinkerListIndex; +} mobjloclink_t; + +#define HASHLOC(x, y, z) (((((x<<3)^(y>>6)*(z>>3)+(x>>15))^(y<<20)-(z>>17)+(x>>6))^x) & 0x7FFF) +#define MAXNUMMOBJSBYLOC 5 + static void P_LocalUnArchiveThinkers() { thinker_t *thinker; @@ -3257,11 +3273,15 @@ static void P_LocalUnArchiveThinkers() int skycenterid = -1; thinker_t newthinkers[NUM_THINKERLISTS] = { 0 }; static thinker_t* thinkersbytype[tc_end][16384]; - static UINT8 thinkerlistbytype[tc_end][16384]; static int numthinkersbytype[tc_end]; + static mobjloclink_t mobjByLoc[32768][MAXNUMMOBJSBYLOC]; + static int numMobjsByLoc[32768]; static mobj_t* mobjByNum[16384]; memset(mobjByNum, 0, sizeof(mobjByNum)); + memset(mobjByLoc, 0, sizeof(mobjByLoc)); + memset(numMobjsByLoc, 0, sizeof(numMobjsByLoc)); + memset(numthinkersbytype, 0, sizeof(numthinkersbytype)); if (READUINT32(save_p) != ARCHIVEBLOCK_THINKERS) I_Error("Bad SaveState at archive block Thinkers"); @@ -3278,10 +3298,7 @@ static void P_LocalUnArchiveThinkers() } skyboxmo[0] = skyboxmo[1] = NULL; - // sort objects by type - for (i = 0; i < tc_end; i++) - numthinkersbytype[i] = 0; - + // sort thinkers into maps to speed up replacement searches for (i = 0; i < NUM_THINKERLISTS; i++) { newthinkers[i].next = &newthinkers[i]; @@ -3289,14 +3306,27 @@ static void P_LocalUnArchiveThinkers() for (thinker = thlist[i].next; thinker != &thlist[i]; thinker = thinker->next) { + if (thinker->function.acp1 == P_RemoveThinkerDelayed) + continue; for (j = 0; j < tc_end; j++) { if (thinker->function.acp1 == specialDefs[j].action) - { - thinkersbytype[j][numthinkersbytype[j]] = thinker; - thinkerlistbytype[j][numthinkersbytype[j]] = i; - numthinkersbytype[j]++; break; + } + + // insert the thinker into the appropriate maps + thinkersbytype[j][numthinkersbytype[j]] = thinker; + numthinkersbytype[j]++; + + if (thinker->function.acp1 == P_MobjThinker) + { + UINT16 locHash = HASHLOC(((mobj_t*)thinker)->x, ((mobj_t*)thinker)->y, ((mobj_t*)thinker)->z); + + if (numMobjsByLoc[locHash] < MAXNUMMOBJSBYLOC) + { + mobjByLoc[locHash][numMobjsByLoc[locHash]].mobj = (mobj_t*)thinker; + mobjByLoc[locHash][numMobjsByLoc[locHash]].thinkerListIndex = numthinkersbytype[j] - 1; + numMobjsByLoc[locHash]++; } } } @@ -3323,47 +3353,69 @@ static void P_LocalUnArchiveThinkers() break; // next list thinker_t* newthinker = NULL; + boolean preservePositions = false; - if (numthinkersbytype[tclass] > 0) + // find the existing thinker(s) at the same position, if possible + if (tclass == tc_mobj) + { + mobj_t* savedMobj = (mobj_t*)save_p; + UINT16 locHash = HASHLOC(savedMobj->x, savedMobj->y, savedMobj->z); + + for (i = 0; i < numMobjsByLoc[locHash]; i++) + { + if (mobjByLoc[locHash][i].mobj->x == savedMobj->x && mobjByLoc[locHash][i].mobj->y == savedMobj->y && mobjByLoc[locHash][i].mobj->z == savedMobj->z && + (mobjByLoc[locHash][i].mobj->flags & (MF_NOSECTOR | MF_NOBLOCKMAP)) == (savedMobj->flags & (MF_NOSECTOR | MF_NOBLOCKMAP))) + { + newthinker = (thinker_t*)mobjByLoc[locHash][i].mobj; + thinkersbytype[tc_mobj][mobjByLoc[locHash][i].thinkerListIndex] = NULL; + mobjByLoc[locHash][i] = mobjByLoc[locHash][numMobjsByLoc[locHash] - 1]; + numMobjsByLoc[locHash]--; + preservePositions = true; + break; + } + } + } + + // if that didn't work, find an existing thinker of the same type + if (newthinker == NULL && numthinkersbytype[tclass] > 0) { for (j = numthinkersbytype[tclass] - 1; j >= 0; j--) { - UINT8 thinkerlist = thinkerlistbytype[tclass][j]; - - if (thinkerlist != list) - continue; // don't replace something from a different list actually it doesn't matter lol + if (thinkersbytype[tclass][j] == NULL) + { + numthinkersbytype[tclass]--; + continue; + } // we found an object to replace! newthinker = thinkersbytype[tclass][j]; numthinkersbytype[tclass]--; - - if (tclass == tc_mobj) - { - P_UnsetThingPosition((mobj_t*)newthinker); - if (sector_list != NULL) { - P_DelSeclist(sector_list); - sector_list = NULL; - } - } - - // detach any playing sounds from this object if we can (still need to work on doing this properly!) - S_DetachChannelsFromOrigin((mobj_t*)newthinker); - - // allocate it to the new object - for (int k = j; k < numthinkersbytype[tclass]; k++) - { - thinkersbytype[tclass][k] = thinkersbytype[tclass][k + 1]; - thinkerlistbytype[tclass][k] = thinkerlistbytype[tclass][k + 1]; - } - - // unlink it from the old list - newthinker->prev->next = newthinker->next; - newthinker->next->prev = newthinker->prev; break; } } - if (newthinker == NULL) + if (newthinker != NULL) + { + // we found a thinker! we just need to set it up to be replaced + if (tclass == tc_mobj && !preservePositions) + { + // remove it from its old position + P_UnsetThingPosition((mobj_t*)newthinker); + if (sector_list != NULL) + { + P_DelSeclist(sector_list); + sector_list = NULL; + } + } + + // detach any playing sounds from this object if we can (still need to work on doing this properly!) + S_DetachChannelsFromOrigin((mobj_t*)newthinker); + + // unlink it from the old list + newthinker->prev->next = newthinker->next; + newthinker->next->prev = newthinker->prev; + } + else { // we couldn't find an object to replace so we gotta create it if (tclass == tc_mobj) @@ -3373,9 +3425,9 @@ static void P_LocalUnArchiveThinkers() } // preserve vital positioning stuff (cleanup...) - mobj_t preserveMobj; + mobj_t preservedMobj; if (tclass == tc_mobj) - preserveMobj = *(mobj_t*)newthinker; + preservedMobj = *(mobj_t*)newthinker; // read in the data READMEM(save_p, newthinker, specialDefs[tclass].size); @@ -3414,10 +3466,10 @@ static void P_LocalUnArchiveThinkers() // clear sector links etc mobj->bnext = NULL; mobj->bprev = NULL; - mobj->snext = preserveMobj.snext; - mobj->sprev = preserveMobj.sprev; - mobj->subsector = preserveMobj.subsector; - mobj->touching_sectorlist = preserveMobj.touching_sectorlist; + mobj->snext = preservedMobj.snext; + mobj->sprev = preservedMobj.sprev; + mobj->subsector = preservedMobj.subsector; + mobj->touching_sectorlist = preservedMobj.touching_sectorlist; mobjByNum[mobj->mobjnum] = mobj; break; // i dream of a programming language where breaking is the default and fallthrough is the optional (and so much more rarely used) specifier } @@ -3448,7 +3500,15 @@ static void P_LocalUnArchiveThinkers() newthinker->next->prev = newthinker; if (tclass == tc_mobj) - P_SetThingPosition((mobj_t*)newthinker); + { + if (!preservePositions) + P_SetThingPosition((mobj_t*)newthinker); + else + { + ((mobj_t*)newthinker)->bnext = preservedMobj.bnext; + ((mobj_t*)newthinker)->bprev = preservedMobj.bprev; + } + } } } @@ -3885,7 +3945,7 @@ static inline void P_UnArchivePolyObj(polyobj_t *po) return; // rotate and translate polyobject - Polyobj_MoveOnLoad(po, angle, x, y); + Polyobj_MoveOnLoad(po, angle + (po->origangle - po->angle), x + (po->origx - po->spawnSpot.x), y + (po->origy - po->spawnSpot.y)); } static inline void P_ArchivePolyObjects(void) @@ -3916,6 +3976,36 @@ static inline void P_UnArchivePolyObjects(void) for (i = 0; i < numSavedPolys; ++i) P_UnArchivePolyObj(&PolyObjects[i]); } + +static void P_LocalArchivePolyObjects(void) +{ + P_ArchivePolyObjects(); + /*WRITEUINT32(save_p, ARCHIVEBLOCK_POBJS); + + WRITEINT32(save_p, numPolyObjects); + + WRITEMEM(save_p, PolyObjects, numPolyObjects * sizeof(PolyObjects[0]));*/ +} + +static void P_LocalUnArchivePolyObjects(void) +{ + P_UnArchivePolyObjects(); + /*if (READUINT32(save_p) != ARCHIVEBLOCK_POBJS) + I_Error("Bad savestate at archive block polyobjects"); + + numPolyObjects = READINT32(save_p); + + READMEM(save_p, PolyObjects, numPolyObjects * sizeof(PolyObjects[0])); + + bmap_freelist = NULL; + + for (int i = 0; i < numPolyObjects; i++) + { + PolyObjects[i].thinker = NULL; + PolyObjects[i].linked = false; + Polyobj_linkToBlockmap(&PolyObjects[i]); + }*/ +} #endif // // P_FinishMobjs @@ -4445,7 +4535,7 @@ boolean P_LoadGame(INT16 mapoverride) boolean P_LoadNetGame(boolean preserveLevel) { - CV_LoadNetVars(&save_p); + CV_LoadNetVars(&save_p, false); loadTimes[0] = I_GetTimeUs(); if (!P_NetUnArchiveMisc(preserveLevel)) return false; @@ -4522,7 +4612,7 @@ void P_SaveGameState(savestate_t* savestate) P_LocalArchivePlayers(); P_LocalArchiveWorld(); #ifdef POLYOBJECTS - P_ArchivePolyObjects(); + P_LocalArchivePolyObjects(); #endif P_LocalArchiveThinkers(); P_NetArchiveSpecials(); @@ -4547,13 +4637,13 @@ boolean P_LoadGameState(const savestate_t* savestate) globalmobjnum = READUINT32(save_p); - CV_LoadNetVars(&save_p); + CV_LoadNetVars(&save_p, true); P_NetUnArchiveMisc(true); P_LocalUnArchivePlayers(); P_LocalUnArchiveWorld(); #ifdef POLYOBJECTS - P_UnArchivePolyObjects(); + P_LocalUnArchivePolyObjects(); #endif P_LocalUnArchiveThinkers(); P_NetUnArchiveSpecials(); @@ -4566,7 +4656,7 @@ boolean P_LoadGameState(const savestate_t* savestate) //P_SetRandSeed(P_GetInitSeed()); P_UnArchiveLuabanksAndConsistency(); - loadStateBenchmark = I_GetTimeUs() - time; + return true; } \ No newline at end of file diff --git a/src/p_saveg.h b/src/p_saveg.h index 7aba45188..24ca4821f 100644 --- a/src/p_saveg.h +++ b/src/p_saveg.h @@ -32,7 +32,7 @@ typedef struct typedef struct { - UINT8 buffer[4 * 1024 * 1024]; // 4 megs per frame should do + UINT8 buffer[16 * 1024 * 1024]; // 16 megs per frame should do } savestate_t; // Persistent storage/archiving.