From a62ff684e0c8014fbf023ac8f514749d22af0962 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 6 Mar 2024 03:31:49 +0100 Subject: [PATCH] port some crap over from newest netplus --- src/Makefile | 1 + src/d_clisrv.c | 23 ++- src/d_netcmd.c | 4 +- src/hashtable.c | 374 +++++++++++++++++++++++++++++++++++++++++++++++ src/hashtable.h | 56 +++++++ src/lua_script.c | 3 + src/p_saveg.c | 215 +++++++++++++++++++++------ src/p_saveg.h | 2 +- src/p_tick.c | 2 +- 9 files changed, 629 insertions(+), 51 deletions(-) create mode 100644 src/hashtable.c create mode 100644 src/hashtable.h diff --git a/src/Makefile b/src/Makefile index bed91c98c..995de0753 100644 --- a/src/Makefile +++ b/src/Makefile @@ -533,6 +533,7 @@ OBJS:=$(i_main_o) \ $(OBJDIR)/i_time.o \ $(OBJDIR)/lzf.o \ $(OBJDIR)/b_bot.o \ + $(OBJDIR)/hashtable.o \ $(i_net_o) \ $(i_system_o) \ $(i_sound_o) \ diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 9a1b61079..db570d419 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -6268,7 +6268,7 @@ boolean TryRunTics(tic_t realtics) simtic = gametic; // CONS_Printf("Not simulating, clearing savestates...\n"); DEBFILE("NETPLUS: Not simulating, clearing savestates...\n"); - //InvalidateSavestates(); + InvalidateSavestates(); } // see if we have enough latency to simulate lots of sims @@ -6725,6 +6725,27 @@ int rttBuffer[20]; int rttBufferIndex = 0; int rttBufferMax = 20; +void InvalidateSavestates() +{ + if (simtic > gametic) + DEBFILE("NETPLUS: Savestates were invalidated during a simulation\n"); + // CONS_Printf("Warning: Savestates were invalidated during a simulation!!\n"); + + for (int i = 0; i < MAXLOCALSAVESTATES; i++) + { + if (gameStateBufferIsValid[i] == true) + if (&gameStateBuffer[i].buffer != NULL) + { + //we don't want to store old states because a client might not want to simulate + //the game anymore or saved games are not valid anymore due to resynch + P_GameStateFreeMemory(&gameStateBuffer[i]); + } + gameStateBufferIsValid[i] = false; + } + SavestatesClearedTic = gametic; +} + + void DetermineNetConditions() { // Refresh the time offset between real time and server time diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 84ce4f4c4..848467f6c 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -871,8 +871,8 @@ void Command_Autotimefudge(void) return; } - lastTimeReceivedPacket = (double)SDL_GetPerformanceCounter() / tic_frequency; - while ((abs((double)SDL_GetPerformanceCounter() / tic_frequency - startTime) < numSampleTics) || numReceivedPackets < 10) + lastTimeReceivedPacket = (double)I_GetPreciseTime() / tic_frequency; + while ((abs((double)I_GetPreciseTime() / tic_frequency - startTime) < numSampleTics) || numReceivedPackets < 10) { I_NetGet(); if ((doomcom->remotenode != -1)) // Packet received diff --git a/src/hashtable.c b/src/hashtable.c new file mode 100644 index 000000000..01e6069cb --- /dev/null +++ b/src/hashtable.c @@ -0,0 +1,374 @@ +// SONIC ROBO BLAST 2 +//----------------------------------------------------------------------------- +// John FrostFox (john.frostfox@gmail.com) +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file hashtable.c +/// \brief Hashtable abstraction implementation + +#include "hashtable.h" + +#include +#include +#include +#include + +#define HT_NUMLISTS UINT8_MAX + +typedef struct mobjnum_linkedList_s{ + thinker_t* thinker; + struct mobjnum_linkedList_s* prev; //the first element must always be null + struct mobjnum_linkedList_s* next; //the last element must always be null +} +mobjnum_linkedList; + +mobjnum_linkedList mobjnum_Hashtable[HT_NUMLISTS]; //dumb and idiotic, there are up to UINT8_MAX linked lists. +//Wish I could just simply make a mobj_t array with UINT64_MAX entries, LOL + +void mobjnum_ht_linkedList_Wipe(); +/** Initializes simple mobjnum/thinker hashtable linked list.*/ +void mobjnum_ht_linkedList_Init() +{ + mobjnum_ht_linkedList_Wipe(); + for (uint8_t i = 0; i < HT_NUMLISTS; i++) + { + mobjnum_Hashtable[i].thinker = NULL; + mobjnum_Hashtable[i].prev = NULL; + mobjnum_Hashtable[i].next = NULL; + } + +} + +/** Adds a thinker's address, determines in which list to add automatically + * + * \param mobj Which mobj's memory address to add + */ +void mobjnum_ht_linkedList_AddEntry (thinker_t* thinker) +{ + + mobjnum_linkedList* currentEntry; // = &mobjnum_Hashtable[(UINT8)(mobj->mobjnum % HT_NUMLISTS)]; + mobjnum_linkedList* next; + + currentEntry = &mobjnum_Hashtable[(UINT8)(((mobj_t*)thinker)->mobjnum % HT_NUMLISTS)]; + + if (!currentEntry->next) // check for the first entry + { + currentEntry->thinker = thinker; + // CONS_Printf("next not exists, create\n"); + currentEntry->next = malloc(sizeof(mobjnum_linkedList)); + currentEntry->next->thinker = NULL; + currentEntry->next->next = NULL; + currentEntry->next->prev = currentEntry; + return; + } + + for (currentEntry = &mobjnum_Hashtable[(UINT8)(((mobj_t*)thinker)->mobjnum % HT_NUMLISTS)]; currentEntry != NULL; currentEntry = next) + { + if (!currentEntry->thinker) + { + currentEntry->thinker = thinker; + break; + } + else + { + if (!currentEntry->next) + { + currentEntry->next = malloc(sizeof(mobjnum_linkedList)); + currentEntry->next->thinker = thinker; + currentEntry->next->next = NULL; + currentEntry->next->prev = currentEntry; + next = NULL; + } + else + next = currentEntry->next; + } + } +} + +/** Removes a thinker's address by its mobjnum, determines from which list to remove automatically + * + * \param mobjnum Which mobj's memory address to remove by itsmobjnum + */ +// void mobjnum_ht_linkedList_RemoveEntry (uint8_t mobjnumber) +// { +// mobjnum_linkedList* currentEntry = &mobjnum_Hashtable[(UINT8)(mobjnumber % HT_NUMLISTS)]; +// if (currentEntry->next == ¤tEntry) +// currentEntry->mobj = &mobj; +// else +// { +// currentEntry->next = malloc(sizeof(mobjnum_linkedList)); + +// } +// } + +/** Looks for a thinker by its mobjnum. + * + * \param mobjnumber mobj's mobjnum + * \return thinker_t* if an object is found, otherwise NULL. Cast it to mobj_t* to get the mobj. + */ +thinker_t* mobjnum_ht_linkedList_Find (uint32_t mobjnumber) +{ + // mobjnum_linkedList* currentEntry = &mobjnum_Hashtable[(UINT8)(mobjnumber % HT_NUMLISTS)]; + + mobjnum_linkedList* currentEntry; // = &mobjnum_Hashtable[(UINT8)(mobj->mobjnum % HT_NUMLISTS)]; + mobjnum_linkedList* next; + + if (!mobjnumber) + return NULL; + + currentEntry = &mobjnum_Hashtable[(UINT8)(mobjnumber % HT_NUMLISTS)]; + + if (!currentEntry->next) // check for the first entry + { + if (!currentEntry->thinker) + return NULL; + + if (((mobj_t *)currentEntry->thinker)->mobjnum == mobjnumber) + return currentEntry->thinker; + } + + for (currentEntry = &mobjnum_Hashtable[(UINT8)(mobjnumber % HT_NUMLISTS)]; currentEntry != NULL; currentEntry = next) + { + if (!currentEntry->thinker) + return NULL; + + if (((mobj_t *)currentEntry->thinker)->mobjnum == mobjnumber) + return currentEntry->thinker; + if (currentEntry->next) + next = currentEntry->next; + else + next = NULL; + } + return NULL; +} + +/** Wipes the entire list by iterating everything it has*/ +void mobjnum_ht_linkedList_Wipe() +{ + mobjnum_linkedList* currentEntry; // = &mobjnum_Hashtable[(UINT8)(mobj->mobjnum % HT_NUMLISTS)]; + mobjnum_linkedList* next; + + for (UINT8 i = 0; i < HT_NUMLISTS; i++) + { + if (!mobjnum_Hashtable[i].next) + continue; + for (currentEntry = &mobjnum_Hashtable[i]; currentEntry != NULL; currentEntry = next) + { + // bye! + if (currentEntry->next) + next = currentEntry->next; + else + next = NULL; + if (currentEntry->next) + { + currentEntry->next = NULL; + } + if (currentEntry->thinker) + { + currentEntry->thinker = NULL; + } + if (currentEntry->prev) //prevents freeing the first element + { + currentEntry->prev = NULL; + free(currentEntry); + } + } + } + return; +} + +// Simple hash table implemented in C. + +// Hash table entry (slot may be filled or empty). +typedef struct { + const char* key; // key is NULL if this slot is empty + void* value; +} ht_entry; + +// Hash table structure: create with ht_create, free with ht_destroy. +struct hashtable { + ht_entry* entries; // hash slots + size_t capacity; // size of _entries array + size_t length; // number of items in hash table +}; + +#define INITIAL_CAPACITY 16 // must not be zero + +hashtable* hashtable_Create(void) { + // Allocate space for hash table struct. + hashtable* table = malloc(sizeof(hashtable)); + if (table == NULL) { + return NULL; + } + table->length = 0; + table->capacity = INITIAL_CAPACITY; + + // Allocate (zero'd) space for entry buckets. + table->entries = calloc(table->capacity, sizeof(ht_entry)); + if (table->entries == NULL) { + free(table); // error, free table before we return! + return NULL; + } + return table; +} + +void hashtable_Destroy(hashtable* table) { + // First free allocated keys. + for (size_t i = 0; i < table->capacity; i++) { + if (table->entries[i].key != NULL) { + free((void*)table->entries[i].key); + } + } + + // Then free entries array and table itself. + free(table->entries); + free(table); +} + +#define FNV_OFFSET 14695981039346656037UL +#define FNV_PRIME 1099511628211UL + +// Return 64-bit FNV-1a hash for key (NUL-terminated). See description: +// https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function +static uint64_t hash_key(const char* key) { + uint64_t hash = FNV_OFFSET; + for (const char* p = key; *p; p++) { + hash ^= (uint64_t)(unsigned char)(*p); + hash *= FNV_PRIME; + } + return hash; +} + +void* hashtable_Get(hashtable* table, const UINT32* key) { + // AND hash with capacity-1 to ensure it's within entries array. + uint64_t hash = hash_key(key); + size_t index = (size_t)(hash & (uint64_t)(table->capacity - 1)); + + // Loop till we find an empty entry. + while (table->entries[index].key != NULL) { + if (strcmp(key, table->entries[index].key) == 0) { + // Found key, return value. + return table->entries[index].value; + } + // Key wasn't in this slot, move to next (linear probing). + index++; + if (index >= table->capacity) { + // At end of entries array, wrap around. + index = 0; + } + } + return NULL; +} + +// Internal function to set an entry (without expanding table). +static const char* ht_set_entry(ht_entry* entries, size_t capacity, + const char* key, void* value, size_t* plength) { + // AND hash with capacity-1 to ensure it's within entries array. + uint64_t hash = hash_key(key); + size_t index = (size_t)(hash & (uint64_t)(capacity - 1)); + + // Loop till we find an empty entry. + while (entries[index].key != NULL) { + if (strcmp(key, entries[index].key) == 0) { + // Found key (it already exists), update value. + entries[index].value = value; + return entries[index].key; + } + // Key wasn't in this slot, move to next (linear probing). + index++; + if (index >= capacity) { + // At end of entries array, wrap around. + index = 0; + } + } + + // Didn't find key, allocate+copy if needed, then insert it. + if (plength != NULL) { + key = strdup(key); + if (key == NULL) { + return NULL; + } + (*plength)++; + } + entries[index].key = (char*)key; + entries[index].value = value; + return key; +} + +// Expand hash table to twice its current size. Return true on success, +// false if out of memory. +static boolean ht_expand(hashtable* table) { + // Allocate new entries array. + size_t new_capacity = table->capacity * 2; + if (new_capacity < table->capacity) { + return false; // overflow (capacity would be too big) + } + ht_entry* new_entries = calloc(new_capacity, sizeof(ht_entry)); + if (new_entries == NULL) { + return false; + } + + // Iterate entries, move all non-empty ones to new table's entries. + for (size_t i = 0; i < table->capacity; i++) { + ht_entry entry = table->entries[i]; + if (entry.key != NULL) { + ht_set_entry(new_entries, new_capacity, entry.key, + entry.value, NULL); + } + } + + // Free old entries array and update this table's details. + free(table->entries); + table->entries = new_entries; + table->capacity = new_capacity; + return true; +} + +const char* hashtable_Set(hashtable* table, const UINT32* key, void* value) { + assert(value != NULL); + if (value == NULL) { + return NULL; + } + + // If length will exceed half of current capacity, expand it. + if (table->length >= table->capacity / 2) { + if (!ht_expand(table)) { + return NULL; + } + } + + // Set entry and update length. + return ht_set_entry(table->entries, table->capacity, key, value, + &table->length); +} + +size_t hashtable_Length(hashtable* table) { + return table->length; +} + +hashtable_iterator ht_iterator(hashtable* table) { + hashtable_iterator it; + it._table = table; + it._index = 0; + return it; +} + +boolean hashtable_Next(hashtable_iterator* it) { + // Loop till we've hit end of entries array. + hashtable* table = it->_table; + while (it->_index < table->capacity) { + size_t i = it->_index; + it->_index++; + if (table->entries[i].key != NULL) { + // Found next non-empty item, update iterator key and value. + ht_entry entry = table->entries[i]; + it->key = entry.key; + it->value = entry.value; + return true; + } + } + return false; +} \ No newline at end of file diff --git a/src/hashtable.h b/src/hashtable.h new file mode 100644 index 000000000..c32a19fb0 --- /dev/null +++ b/src/hashtable.h @@ -0,0 +1,56 @@ +// SONIC ROBO BLAST 2 +//----------------------------------------------------------------------------- +// John FrostFox (john.frostfox@gmail.com) +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file hashtable.h +/// \brief Hashtable abstraction + +#include "doomdef.h" +#include "p_mobj.h" +#include "d_think.h" + +// extern uint16_t hashHits; +// extern uint16_t hashMiss; + +void mobjnum_ht_linkedList_Init(); +void mobjnum_ht_linkedList_AddEntry (thinker_t* thinker); +thinker_t* mobjnum_ht_linkedList_Find (uint32_t mobjnumber); +// void mobjnum_ht_linkedList_Wipe(); + +// Hash table iterator: create with ht_iterator, iterate with ht_next. +typedef struct hashtable hashtable; + +typedef struct { + const UINT32* key; + void* value; + + hashtable* _table; // reference to hash table being iterated + size_t _index; // current index into ht._entries +} hashtable_iterator; + +hashtable* hashtable_Create(void); //creates a hashtable and returns the pointer +void* hashtable_Get(hashtable* table, const UINT32* key); + +// Set item with given key (NUL-terminated) to value (which must not +// be NULL). If not already present in table, key is copied to newly +// allocated memory (keys are freed automatically when ht_destroy is +// called). Return address of copied key, or NULL if out of memory. +// const char* hashtable_Set(hashtable* table, UINT32* key, void* value); +const char* hashtable_Set(hashtable* table, const UINT32* key, void* value); + +size_t hashtable_Length(hashtable* table); + +// Free memory allocated for hash table, including allocated keys. +void hashtable_Destroy(hashtable* table); + +// Return new hash table iterator (for use with ht_next). +hashtable_iterator hashtable_Iterator(hashtable_iterator* table); + +// Move iterator to next item in hash table, update iterator's key +// and value to current item, and return true. If there are no more +// items, return false. Don't call ht_set during iteration. +boolean hashtable_Next(hashtable_iterator* it); \ No newline at end of file diff --git a/src/lua_script.c b/src/lua_script.c index 047e0a162..d1d1ae351 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -26,6 +26,8 @@ #include "d_netfil.h" // for LUA_DumpFile #endif +#include "hashtable.h" + #include "lua_script.h" #include "lua_libs.h" #include "lua_glib.h" @@ -1622,6 +1624,7 @@ void LUA_UnArchive(void) do { mobjnum = READUINT32(save_p); // read a mobjnum + th = mobjnum_ht_linkedList_Find(mobjnum); for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next) { if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed) diff --git a/src/p_saveg.c b/src/p_saveg.c index ac73e672a..9cb83356a 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -34,6 +34,8 @@ #include "p_slopes.h" #include "console.h" +#include "hashtable.h" + savedata_t savedata; UINT8 *save_p; @@ -63,6 +65,34 @@ typedef enum SECONDAXIS = 0x20, } player_saveflags; + +// Now save the pointers, tracer and target, but at load time we must +// relink to this; the savegame contains the old position in the pointer +// field copyed in the info field temporarily, but finally we just search +// for the old position and relink to it. +mobj_t *P_FindNewPosition_Hashtable(UINT32 oldposition) +{ + thinker_t *th; + mobj_t *mobj; + th = mobjnum_ht_linkedList_Find(oldposition); + if (th && ((mobj_t *)th)->mobjnum == oldposition && !(th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed)) + return (mobj_t *)th; + for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next) + { + if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed) + continue; + + mobj = (mobj_t *)th; + if (mobj->mobjnum != oldposition) + continue; + + return mobj; + } + CONS_Debug(DBG_GAMELOGIC, "mobj not found\n"); + return NULL; +} + + // // P_ArchivePlayer // @@ -3714,56 +3744,32 @@ static void P_NetUnArchiveThinkers(boolean preserveLevel) default: CONS_Alert(CONS_ERROR, "Found mobj with unknown map thing type NULL\n"); } - if (th) - P_AddThinker(thinkerlist, th); + if (th) + { + P_AddThinker(thinkerlist, th); + if (thinkerlist == THINK_MOBJ && tclass == tc_mobj) + mobjnum_ht_linkedList_AddEntry(th); + } } if (restoreNum) { executor_t *delay = NULL; UINT32 mobjnum; - for (currentthinker = thlist[THINK_MAIN].next; currentthinker != &thlist[THINK_MAIN]; - currentthinker = currentthinker->next) + for (currentthinker = thlist[THINK_MAIN].next; currentthinker != &thlist[THINK_MAIN]; currentthinker = currentthinker->next) { if (currentthinker->function.acp1 != (actionf_p1)T_ExecutorDelay) continue; delay = (void *)currentthinker; if (!(mobjnum = (UINT32)(size_t)delay->caller)) continue; - delay->caller = P_FindNewPosition(mobjnum); + delay->caller = P_FindNewPosition_Hashtable(mobjnum); } } // restore skyboxes, if applicable for (i = 0; i < sizeof(skyboxmo) / sizeof(skyboxmo[0]); i++) skyboxmo[i] = NULL; - - /* - for (i = 0; i < sizeof(skyboxcenterpnts) / sizeof(skyboxcenterpnts[0]); i++) - { - skyboxcenterpnts[i] = NULL; - skyboxviewpnts[i] = NULL; - } - - - for (currentmobj = (mobj_t*)thlist[THINK_MOBJ].next; currentmobj != (mobj_t*)&thlist[THINK_MOBJ]; currentmobj = (mobj_t*)currentmobj->thinker.next) - { - if (currentmobj->type == MT_SKYBOX) - { - if ((currentmobj->extravalue2 >> 16) == 1) - { - skyboxcenterpnts[currentmobj->extravalue2 & 0xFFFF] = currentmobj; - } - else - { - skyboxviewpnts[currentmobj->extravalue2 & 0xFFFF] = currentmobj; - } - } - } - - skyboxmo[0] = skyboxviewpnts[(skyviewid >= 0) ? skyviewid : 0]; - skyboxmo[1] = skyboxcenterpnts[(skycenterid >= 0) ? skycenterid : 0]; - */ } /////////////////////////////////////////////////////////////////////////////// @@ -3925,56 +3931,56 @@ static void P_RelinkPointers(void) { temp = (UINT32)(size_t)mobj->tracer; mobj->tracer = NULL; - if (!P_SetTarget(&mobj->tracer, P_FindNewPosition(temp))) + if (!P_SetTarget(&mobj->tracer, P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "tracer not found on %d\n", mobj->type); } if (mobj->target) { temp = (UINT32)(size_t)mobj->target; mobj->target = NULL; - if (!P_SetTarget(&mobj->target, P_FindNewPosition(temp))) + if (!P_SetTarget(&mobj->target, P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "target not found on %d\n", mobj->type); } if (mobj->hnext) { temp = (UINT32)(size_t)mobj->hnext; mobj->hnext = NULL; - if (!(mobj->hnext = P_FindNewPosition(temp))) + if (!(mobj->hnext = P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "hnext not found on %d\n", mobj->type); } if (mobj->hprev) { temp = (UINT32)(size_t)mobj->hprev; mobj->hprev = NULL; - if (!(mobj->hprev = P_FindNewPosition(temp))) + if (!(mobj->hprev = P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "hprev not found on %d\n", mobj->type); } if (mobj->player && mobj->player->capsule) { temp = (UINT32)(size_t)mobj->player->capsule; mobj->player->capsule = NULL; - if (!P_SetTarget(&mobj->player->capsule, P_FindNewPosition(temp))) + if (!P_SetTarget(&mobj->player->capsule, P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "capsule not found on %d\n", mobj->type); } if (mobj->player && mobj->player->axis1) { temp = (UINT32)(size_t)mobj->player->axis1; mobj->player->axis1 = NULL; - if (!P_SetTarget(&mobj->player->axis1, P_FindNewPosition(temp))) + if (!P_SetTarget(&mobj->player->axis1, P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "axis1 not found on %d\n", mobj->type); } if (mobj->player && mobj->player->axis2) { temp = (UINT32)(size_t)mobj->player->axis2; mobj->player->axis2 = NULL; - if (!P_SetTarget(&mobj->player->axis2, P_FindNewPosition(temp))) + if (!P_SetTarget(&mobj->player->axis2, P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "axis2 not found on %d\n", mobj->type); } if (mobj->player && mobj->player->awayviewmobj) { temp = (UINT32)(size_t)mobj->player->awayviewmobj; mobj->player->awayviewmobj = NULL; - if (!P_SetTarget(&mobj->player->awayviewmobj, P_FindNewPosition(temp))) + if (!P_SetTarget(&mobj->player->awayviewmobj, P_FindNewPosition_Hashtable(temp))) CONS_Debug(DBG_GAMELOGIC, "awayviewmobj not found on %d\n", mobj->type); } } @@ -4418,8 +4424,20 @@ boolean P_LoadGame(INT16 mapoverride) return true; } +void P_GameStateFreeMemory(savestate_t* savestate) +{ + // if (gamestate != GS_LEVEL) + Z_Free(savestate->buffer); + savestate->buffer = NULL; //a hacky way to invalidate the memory +} + + boolean P_LoadNetGame(boolean preserveLevel) { + mobjnum_ht_linkedList_Init(); //clean up hashtables to avoid lua stuff using them + //this is temporary and will be rewritten to use in vanilla code + //once rollback netcode will be stable + CV_LoadNetVars(&save_p, false); if (!P_NetUnArchiveMisc(preserveLevel)) return false; @@ -4459,8 +4477,13 @@ void P_SaveGameState(savestate_t* savestate) thinker_t* th; size_t s = 0; int mobjnum = 1; + + if (savestate->buffer == NULL) + { + savestate->buffer = Z_Malloc(10 * 1024 * 1024, PU_STATIC, NULL); //ten megabytes? + } - save_p = ((unsigned char*)savestate->buffer); + save_p = savestate->buffer; WRITEINT16(save_p, gamemap); WRITEUINT32(save_p, globalmobjnum); @@ -4507,22 +4530,67 @@ void P_SaveGameState(savestate_t* savestate) LUA_Archive(); } +/*void P_SaveGameState(savestate_t* savestate) +{ + thinker_t *th; + mobj_t *mobj; + INT32 i = 1; // don't start from 0, it'd be confused with a blank pointer otherwise + + if (savestate->buffer == NULL) + { + savestate->buffer = Z_Malloc(10 * 1024 * 1024, PU_STATIC, NULL); //ten megabytes? + } + + save_p = savestate->buffer; + + WRITEINT16(save_p, gamemap); + WRITEUINT32(save_p, globalmobjnum); + + CV_SaveNetVars(&save_p, false); + P_NetArchiveMisc(); + + // Assign the mobjnumber for pointer tracking + for (th = thlist[THINK_MOBJ].next; th != &thlist[THINK_MOBJ]; th = th->next) + { + if (th->function.acp1 == (actionf_p1)P_RemoveThinkerDelayed) + continue; + + mobj = (mobj_t *)th; + if (mobj->type == MT_HOOP || mobj->type == MT_HOOPCOLLIDE || mobj->type == MT_HOOPCENTER) + continue; + mobj->mobjnum = i++; + } + + P_NetArchivePlayers(); + if (gamestate == GS_LEVEL) + { + // P_NetArchiveWorld(); + P_LocalArchiveWorld(); + P_ArchivePolyObjects(); + P_NetArchiveThinkers(); + P_NetArchiveSpecials(); + } + LUA_Archive(); +}*/ + + // P_LoadGameState is a within-level-only mechanism for loading the game state. It must not be used cross level. Used for simulation backtracking. // It uses a mixture of existing NetUnArchive functions and faster LocalUnArchive functions to do the job boolean P_LoadGameState(const savestate_t* savestate) { INT16 savedGameMap; + mobjnum_ht_linkedList_Init(); - save_p = savestate->buffer; + if (savestate->buffer == NULL) + return false; + + save_p = ((unsigned char*)savestate->buffer); savedGameMap = READINT16(save_p); globalmobjnum = READUINT32(save_p); if (savedGameMap != gamemap) - { - // savestates do not work cross-level return false; - } con_muted = true; CV_LoadNetVars(&save_p, true); @@ -4547,4 +4615,59 @@ boolean P_LoadGameState(const savestate_t* savestate) //P_SetRandSeed(P_GetInitSeed()); return true; -} \ No newline at end of file +} + +/*boolean P_LoadGameState(const savestate_t* savestate) +{ + INT16 savedGameMap; + mobjnum_ht_linkedList_Init(); + + if (savestate->buffer == NULL) + return false; + + save_p = ((unsigned char*)savestate->buffer); + + savedGameMap = READINT16(save_p); + + if (savedGameMap != gamemap) + return false; + + globalmobjnum = READUINT32(save_p); + + con_muted = true; + CV_LoadNetVars(&save_p, true); + con_muted = false; + + if (!P_NetUnArchiveMisc(true)) + { + return false; + } + + P_NetUnArchivePlayers(); + if (gamestate == GS_LEVEL) + { + // P_NetUnArchiveWorld(); + P_LocalUnArchiveWorld(); + P_UnArchivePolyObjects(); + P_NetUnArchiveThinkers(true); + P_NetUnArchiveSpecials(); + P_RelinkPointers(); //candidate for optimization + P_FinishMobjs(); + } + con_muted = true; + LUA_UnArchive(); //candidate for optimization + con_muted = false; + + // This is stupid and hacky, but maybe it'll work! + P_SetRandSeed(P_GetInitSeed()); + + // The precipitation would normally be spawned in P_SetupLevel, which is called by + // P_NetUnArchiveMisc above. However, that would place it up before P_NetUnArchiveThinkers, + // so the thinkers would be deleted later. Therefore, P_SetupLevel will *not* spawn + // precipitation when loading a netgame save. Instead, precip has to be spawned here. + // This is done in P_NetUnArchiveSpecials now. + //P_UnArchiveLuabanksAndConsistency(); + // save_p = NULL; //invalidate it//why?? + return 0; +}*/ + diff --git a/src/p_saveg.h b/src/p_saveg.h index 17c127bce..a4e3158a3 100644 --- a/src/p_saveg.h +++ b/src/p_saveg.h @@ -32,7 +32,7 @@ typedef struct typedef struct { - UINT8 buffer[16 * 1024 * 1024]; // 16 megs per frame should do + UINT8* buffer; } savestate_t; // Persistent storage/archiving. diff --git a/src/p_tick.c b/src/p_tick.c index 608d86316..bed526e67 100644 --- a/src/p_tick.c +++ b/src/p_tick.c @@ -338,7 +338,7 @@ static inline void P_RunThinkers(void) #ifdef PARANOIA I_Assert(currentthinker->function.acp1 != NULL); #endif - if (currentthinker->function.acp1 == P_MobjThinker && issimulation && ((mobj_t*)currentthinker)->isculled) + if (currentthinker->function.acp1 == (actionf_p1)P_MobjThinker && issimulation && ((mobj_t*)currentthinker)->isculled) continue; // apply distance culling currentthinker->function.acp1(currentthinker);