Skip to content

Commit

Permalink
Add Player Pawn Serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 committed Dec 10, 2024
1 parent fb660fa commit 45d5a75
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/common/engine/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,12 +1263,15 @@ FSerializer& Serialize(FSerializer& arc, const char* key, FTranslationID& value,
//
//==========================================================================


bool SerializerAllowWhenSerializingOnlyPlayer(FSerializer &arc, DObject * obj);

FSerializer &Serialize(FSerializer &arc, const char *key, DObject *&value, DObject ** /*defval*/, bool *retcode)
{
if (retcode) *retcode = true;
if (arc.isWriting())
{
if (value != nullptr && !(value->ObjectFlags & (OF_EuthanizeMe | OF_Transient)))
if (value != nullptr && !(value->ObjectFlags & (OF_EuthanizeMe | OF_Transient)) && SerializerAllowWhenSerializingOnlyPlayer(arc, value))
{
int ndx;
if (value == WP_NOCHANGE)
Expand Down
2 changes: 2 additions & 0 deletions src/common/engine/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class FSerializer
int mErrors = 0;
int mObjectErrors = 0;
FString mLumpName;

bool serializeOnlyPlayer = false;
};

FSerializer& Serialize(FSerializer& arc, const char* key, char& value, char* defval);
Expand Down
2 changes: 2 additions & 0 deletions src/g_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ void G_DoSaveGame (bool okForQuicksave, bool forceQuicksave, FString filename, c
void G_DoAutoSave ();
void G_DoQuickSave ();

FString G_SnapshotPlayer(AActor * pawn);

void STAT_Serialize(FSerializer &file);

CVARD_NAMED(Int, gameskill, skill, 2, CVAR_SERVERINFO|CVAR_LATCH, "sets the skill for the next newly started game")
Expand Down
11 changes: 11 additions & 0 deletions src/g_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,17 @@ DEFINE_ACTION_FUNCTION(FLevelLocals, WorldDone)
//
//==========================================================================

FString G_SnapshotPlayer(AActor * pawn)
{
FSerializer s;
s.serializeOnlyPlayer = true;
s.OpenWriter(false);
s("Player", pawn);
unsigned len;
const char * str = s.GetOutput(&len);
return FString(str, len);
}

void G_DoWorldDone (void)
{
gamestate = GS_LEVEL;
Expand Down
23 changes: 23 additions & 0 deletions src/playsim/dthinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ void FThinkerCollection::Link(DThinker *thinker, int statnum)
list->AddTail(thinker);
}

bool FThinkerCollection::IsTravelling(DThinker *thinker)
{
return Thinkers[STAT_TRAVELLING].HasThinker(thinker);
}

//==========================================================================
//
//
Expand Down Expand Up @@ -625,6 +630,24 @@ int FThinkerList::TickThinkers(FThinkerList *dest)
return count;
}

bool FThinkerList::HasThinker(DThinker * t)
{
DThinker *node = GetHead();

if (node == nullptr)
{
return false;
}

while (node != Sentinel)
{
if(node == t) return true;
node = node->NextThinker;
}

return false;
}

//==========================================================================
//
//
Expand Down
4 changes: 4 additions & 0 deletions src/playsim/dthinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct FThinkerList
int ProfileThinkers(FThinkerList *dest);
void SaveList(FSerializer &arc);

bool HasThinker(DThinker *);

private:
DThinker *Sentinel = nullptr;

Expand All @@ -85,6 +87,8 @@ struct FThinkerCollection
DThinker *FirstThinker(int statnum);
void Link(DThinker *thinker, int statnum);

bool IsTravelling(DThinker *thinker);

private:
FThinkerList Thinkers[MAX_STATNUM + 2];
FThinkerList FreshThinkers[MAX_STATNUM + 1];
Expand Down
12 changes: 12 additions & 0 deletions src/serializer_doom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,24 +225,34 @@ FSerializer& FDoomSerializer::StatePointer(const char* key, void* ptraddr, bool
return *this;
}

bool SerializerAllowWhenSerializingOnlyPlayer(FSerializer &arc, DObject * obj)
{
return (!arc.serializeOnlyPlayer
|| (!obj->GetClass()->IsDescendantOf(RUNTIME_CLASS(DThinker)))
|| static_cast<DThinker*>(obj)->Level->Thinkers.IsTravelling(static_cast<DThinker*>(obj)));
}



template<> FSerializer &Serialize(FSerializer &ar, const char *key, FPolyObj *&value, FPolyObj **defval)
{
if(ar.isWriting() && ar.serializeOnlyPlayer) return ar;
auto arc = dynamic_cast<FDoomSerializer*>(&ar);
if (!arc || arc->Level == nullptr) I_Error("Trying to serialize polyobject without a valid level");
return SerializePointer(*arc, key, value, defval, arc->Level->Polyobjects);
}

template<> FSerializer &Serialize(FSerializer &ar, const char *key, side_t *&value, side_t **defval)
{
if(ar.isWriting() && ar.serializeOnlyPlayer) return ar;
auto arc = dynamic_cast<FDoomSerializer*>(&ar);
if (!arc || arc->Level == nullptr) I_Error("Trying to serialize SIDEDEF without a valid level");
return SerializePointer(*arc, key, value, defval, arc->Level->sides);
}

template<> FSerializer &Serialize(FSerializer &ar, const char *key, sector_t *&value, sector_t **defval)
{
if(ar.isWriting() && ar.serializeOnlyPlayer) return ar;
auto arc = dynamic_cast<FDoomSerializer*>(&ar);
if (!arc || arc->Level == nullptr) I_Error("Trying to serialize sector without a valid level");
return SerializePointer(*arc, key, value, defval, arc->Level->sectors);
Expand All @@ -255,13 +265,15 @@ template<> FSerializer &Serialize(FSerializer &arc, const char *key, player_t *&

template<> FSerializer &Serialize(FSerializer &ar, const char *key, line_t *&value, line_t **defval)
{
if(ar.isWriting() && ar.serializeOnlyPlayer) return ar;
auto arc = dynamic_cast<FDoomSerializer*>(&ar);
if (!arc || arc->Level == nullptr) I_Error("Trying to serialize linedef without a valid level");
return SerializePointer(*arc, key, value, defval, arc->Level->lines);
}

template<> FSerializer &Serialize(FSerializer &ar, const char *key, vertex_t *&value, vertex_t **defval)
{
if(ar.isWriting() && ar.serializeOnlyPlayer) return ar;
auto arc = dynamic_cast<FDoomSerializer*>(&ar);
if (!arc || arc->Level == nullptr) I_Error("Trying to serialize vertex without a valid level");
return SerializePointer(*arc, key, value, defval, arc->Level->vertexes);
Expand Down

0 comments on commit 45d5a75

Please sign in to comment.