Skip to content

Commit a938e9c

Browse files
committed
moved the NetworkEntityManager into the backend code.
1 parent f6c02d4 commit a938e9c

File tree

7 files changed

+186
-183
lines changed

7 files changed

+186
-183
lines changed

src/common/objects/dobject.cpp

+161
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,164 @@ void *DObject::ScriptVar(FName field, PType *type)
622622
// This is only for internal use so I_Error is fine.
623623
I_Error("Variable %s not found in %s\n", field.GetChars(), cls->TypeName.GetChars());
624624
}
625+
626+
627+
//==========================================================================
628+
//
629+
//
630+
//
631+
//==========================================================================
632+
633+
void NetworkEntityManager::InitializeNetworkEntities()
634+
{
635+
if (!s_netEntities.Size())
636+
s_netEntities.AppendFill(nullptr, NetIDStart); // Allocate the first 0-8 slots for the world and clients.
637+
}
638+
639+
// Clients need special handling since they always go in slots 1 - MAXPLAYERS.
640+
void NetworkEntityManager::SetClientNetworkEntity(DObject* mo, const uint32_t id)
641+
{
642+
// If resurrecting, we need to swap the corpse's position with the new pawn's
643+
// position so it's no longer considered the client's body.
644+
DObject* const oldBody = s_netEntities[id];
645+
if (oldBody != nullptr)
646+
{
647+
if (oldBody == mo)
648+
return;
649+
650+
const uint32_t curID = mo->GetNetworkID();
651+
652+
s_netEntities[curID] = oldBody;
653+
oldBody->ClearNetworkID();
654+
oldBody->SetNetworkID(curID);
655+
656+
mo->ClearNetworkID();
657+
}
658+
else
659+
{
660+
RemoveNetworkEntity(mo); // Free up its current id.
661+
}
662+
663+
s_netEntities[id] = mo;
664+
mo->SetNetworkID(ClientNetIDStart + id);
665+
}
666+
667+
void NetworkEntityManager::AddNetworkEntity(DObject* const ent)
668+
{
669+
if (ent->IsNetworked())
670+
return;
671+
672+
// Slot 0 is reserved for the world.
673+
// Clients go in the first 1 - MAXPLAYERS slots
674+
// Everything else is first come first serve.
675+
uint32_t id = WorldNetID;
676+
if (s_openNetIDs.Size())
677+
{
678+
s_openNetIDs.Pop(id);
679+
s_netEntities[id] = ent;
680+
}
681+
else
682+
{
683+
id = s_netEntities.Push(ent);
684+
}
685+
686+
ent->SetNetworkID(id);
687+
}
688+
689+
void NetworkEntityManager::RemoveNetworkEntity(DObject* const ent)
690+
{
691+
if (!ent->IsNetworked())
692+
return;
693+
694+
const uint32_t id = ent->GetNetworkID();
695+
if (id == WorldNetID)
696+
return;
697+
698+
assert(s_netEntities[id] == ent);
699+
if (id >= NetIDStart)
700+
s_openNetIDs.Push(id);
701+
s_netEntities[id] = nullptr;
702+
ent->ClearNetworkID();
703+
}
704+
705+
DObject* NetworkEntityManager::GetNetworkEntity(const uint32_t id)
706+
{
707+
if (id == WorldNetID || id >= s_netEntities.Size())
708+
return nullptr;
709+
710+
return s_netEntities[id];
711+
}
712+
713+
//==========================================================================
714+
//
715+
//
716+
//
717+
//==========================================================================
718+
719+
void DObject::SetNetworkID(const uint32_t id)
720+
{
721+
if (!IsNetworked())
722+
{
723+
ObjectFlags |= OF_Networked;
724+
_networkID = id;
725+
}
726+
}
727+
728+
void DObject::ClearNetworkID()
729+
{
730+
ObjectFlags &= ~OF_Networked;
731+
_networkID = NetworkEntityManager::WorldNetID;
732+
}
733+
734+
void DObject::EnableNetworking(const bool enable)
735+
{
736+
if (enable)
737+
NetworkEntityManager::AddNetworkEntity(this);
738+
else
739+
NetworkEntityManager::RemoveNetworkEntity(this);
740+
}
741+
742+
void DObject::RemoveFromNetwork()
743+
{
744+
NetworkEntityManager::RemoveNetworkEntity(this);
745+
}
746+
747+
static unsigned int GetNetworkID(DObject* const self)
748+
{
749+
return self->GetNetworkID();
750+
}
751+
752+
DEFINE_ACTION_FUNCTION_NATIVE(DObject, GetNetworkID, GetNetworkID)
753+
{
754+
PARAM_SELF_PROLOGUE(DObject);
755+
756+
ACTION_RETURN_INT(self->GetNetworkID());
757+
}
758+
759+
static void EnableNetworking(DObject* const self, const bool enable)
760+
{
761+
self->EnableNetworking(enable);
762+
}
763+
764+
DEFINE_ACTION_FUNCTION_NATIVE(DObject, EnableNetworking, EnableNetworking)
765+
{
766+
PARAM_SELF_PROLOGUE(DObject);
767+
PARAM_BOOL(enable);
768+
769+
self->EnableNetworking(enable);
770+
return 0;
771+
}
772+
773+
static DObject* GetNetworkEntity(const unsigned int id)
774+
{
775+
return NetworkEntityManager::GetNetworkEntity(id);
776+
}
777+
778+
DEFINE_ACTION_FUNCTION_NATIVE(DObject, GetNetworkEntity, GetNetworkEntity)
779+
{
780+
PARAM_PROLOGUE;
781+
PARAM_UINT(id);
782+
783+
ACTION_RETURN_OBJECT(NetworkEntityManager::GetNetworkEntity(id));
784+
}
785+

src/common/objects/dobject.h

+21
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,25 @@ inline T *&DObject::PointerVar(FName field)
487487
return *(T**)ScriptVar(field, nullptr); // pointer check is more tricky and for the handful of uses in the DECORATE parser not worth the hassle.
488488
}
489489

490+
491+
class NetworkEntityManager
492+
{
493+
private:
494+
inline static TArray<DObject*> s_netEntities = {};
495+
inline static TArray<uint32_t> s_openNetIDs = {};
496+
497+
public:
498+
NetworkEntityManager() = delete;
499+
500+
static constexpr uint32_t WorldNetID = 0u;
501+
static constexpr uint32_t ClientNetIDStart = 1u;
502+
inline static uint32_t NetIDStart;// = MAXPLAYERS + 1u;
503+
504+
static void InitializeNetworkEntities();
505+
static void SetClientNetworkEntity(DObject* mo, const uint32_t id);
506+
static void AddNetworkEntity(DObject* const ent);
507+
static void RemoveNetworkEntity(DObject* const ent);
508+
static DObject* GetNetworkEntity(const uint32_t id);
509+
};
510+
490511
#endif //__DOBJECT_H__

src/d_main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//-----------------------------------------------------------------------------
2+
//-----------------------------------------------------------------------------
23
// Copyright 1993-1996 id Software
34
// Copyright 1999-2016 Randy Heit
45
// Copyright 2002-2016 Christoph Oelckers
@@ -3617,6 +3618,7 @@ static int D_DoomMain_Internal (void)
36173618
const char *wad;
36183619
FIWadManager *iwad_man;
36193620

3621+
NetworkEntityManager::NetIDStart = MAXPLAYERS + 1;
36203622
GC::AddMarkerFunc(GC_MarkGameRoots);
36213623
VM_CastSpriteIDToString = Doom_CastSpriteIDToString;
36223624

src/d_net.cpp

-162
Original file line numberDiff line numberDiff line change
@@ -2971,168 +2971,6 @@ int Net_GetLatency(int *ld, int *ad)
29712971
//
29722972
//==========================================================================
29732973

2974-
void NetworkEntityManager::InitializeNetworkEntities()
2975-
{
2976-
if (!s_netEntities.Size())
2977-
s_netEntities.AppendFill(nullptr, NetIDStart); // Allocate the first 0-8 slots for the world and clients.
2978-
}
2979-
2980-
// Clients need special handling since they always go in slots 1 - MAXPLAYERS.
2981-
void NetworkEntityManager::SetClientNetworkEntity(player_t* const client)
2982-
{
2983-
AActor* const mo = client->mo;
2984-
const uint32_t id = ClientNetIDStart + mo->Level->PlayerNum(client);
2985-
2986-
// If resurrecting, we need to swap the corpse's position with the new pawn's
2987-
// position so it's no longer considered the client's body.
2988-
DObject* const oldBody = s_netEntities[id];
2989-
if (oldBody != nullptr)
2990-
{
2991-
if (oldBody == mo)
2992-
return;
2993-
2994-
const uint32_t curID = mo->GetNetworkID();
2995-
2996-
s_netEntities[curID] = oldBody;
2997-
oldBody->ClearNetworkID();
2998-
oldBody->SetNetworkID(curID);
2999-
3000-
mo->ClearNetworkID();
3001-
}
3002-
else
3003-
{
3004-
RemoveNetworkEntity(mo); // Free up its current id.
3005-
}
3006-
3007-
s_netEntities[id] = mo;
3008-
mo->SetNetworkID(id);
3009-
}
3010-
3011-
void NetworkEntityManager::AddNetworkEntity(DObject* const ent)
3012-
{
3013-
if (ent->IsNetworked())
3014-
return;
3015-
3016-
// Slot 0 is reserved for the world.
3017-
// Clients go in the first 1 - MAXPLAYERS slots
3018-
// Everything else is first come first serve.
3019-
uint32_t id = WorldNetID;
3020-
if (s_openNetIDs.Size())
3021-
{
3022-
s_openNetIDs.Pop(id);
3023-
s_netEntities[id] = ent;
3024-
}
3025-
else
3026-
{
3027-
id = s_netEntities.Push(ent);
3028-
}
3029-
3030-
ent->SetNetworkID(id);
3031-
}
3032-
3033-
void NetworkEntityManager::RemoveNetworkEntity(DObject* const ent)
3034-
{
3035-
if (!ent->IsNetworked())
3036-
return;
3037-
3038-
const uint32_t id = ent->GetNetworkID();
3039-
if (id == WorldNetID)
3040-
return;
3041-
3042-
assert(s_netEntities[id] == ent);
3043-
if (id >= NetIDStart)
3044-
s_openNetIDs.Push(id);
3045-
s_netEntities[id] = nullptr;
3046-
ent->ClearNetworkID();
3047-
}
3048-
3049-
DObject* NetworkEntityManager::GetNetworkEntity(const uint32_t id)
3050-
{
3051-
if (id == WorldNetID || id >= s_netEntities.Size())
3052-
return nullptr;
3053-
3054-
return s_netEntities[id];
3055-
}
3056-
3057-
//==========================================================================
3058-
//
3059-
//
3060-
//
3061-
//==========================================================================
3062-
3063-
void DObject::SetNetworkID(const uint32_t id)
3064-
{
3065-
if (!IsNetworked())
3066-
{
3067-
ObjectFlags |= OF_Networked;
3068-
_networkID = id;
3069-
}
3070-
}
3071-
3072-
void DObject::ClearNetworkID()
3073-
{
3074-
ObjectFlags &= ~OF_Networked;
3075-
_networkID = NetworkEntityManager::WorldNetID;
3076-
}
3077-
3078-
void DObject::EnableNetworking(const bool enable)
3079-
{
3080-
if (enable)
3081-
NetworkEntityManager::AddNetworkEntity(this);
3082-
else
3083-
NetworkEntityManager::RemoveNetworkEntity(this);
3084-
}
3085-
3086-
void DObject::RemoveFromNetwork()
3087-
{
3088-
NetworkEntityManager::RemoveNetworkEntity(this);
3089-
}
3090-
3091-
static unsigned int GetNetworkID(DObject* const self)
3092-
{
3093-
return self->GetNetworkID();
3094-
}
3095-
3096-
DEFINE_ACTION_FUNCTION_NATIVE(DObject, GetNetworkID, GetNetworkID)
3097-
{
3098-
PARAM_SELF_PROLOGUE(DObject);
3099-
3100-
ACTION_RETURN_INT(self->GetNetworkID());
3101-
}
3102-
3103-
static void EnableNetworking(DObject* const self, const bool enable)
3104-
{
3105-
self->EnableNetworking(enable);
3106-
}
3107-
3108-
DEFINE_ACTION_FUNCTION_NATIVE(DObject, EnableNetworking, EnableNetworking)
3109-
{
3110-
PARAM_SELF_PROLOGUE(DObject);
3111-
PARAM_BOOL(enable);
3112-
3113-
self->EnableNetworking(enable);
3114-
return 0;
3115-
}
3116-
3117-
static DObject* GetNetworkEntity(const unsigned int id)
3118-
{
3119-
return NetworkEntityManager::GetNetworkEntity(id);
3120-
}
3121-
3122-
DEFINE_ACTION_FUNCTION_NATIVE(DObject, GetNetworkEntity, GetNetworkEntity)
3123-
{
3124-
PARAM_PROLOGUE;
3125-
PARAM_UINT(id);
3126-
3127-
ACTION_RETURN_OBJECT(NetworkEntityManager::GetNetworkEntity(id));
3128-
}
3129-
3130-
//==========================================================================
3131-
//
3132-
//
3133-
//
3134-
//==========================================================================
3135-
31362974
// [RH] List "ping" times
31372975
CCMD (pings)
31382976
{

src/d_net.h

-19
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,6 @@ extern int ticdup;
9898
class player_t;
9999
class DObject;
100100

101-
class NetworkEntityManager
102-
{
103-
private:
104-
inline static TArray<DObject*> s_netEntities = {};
105-
inline static TArray<uint32_t> s_openNetIDs = {};
106-
107-
public:
108-
NetworkEntityManager() = delete;
109-
110-
inline static uint32_t WorldNetID = 0u;
111-
inline static uint32_t ClientNetIDStart = 1u;
112-
inline static uint32_t NetIDStart = MAXPLAYERS + 1u;
113-
114-
static void InitializeNetworkEntities();
115-
static void SetClientNetworkEntity(player_t* const client);
116-
static void AddNetworkEntity(DObject* const ent);
117-
static void RemoveNetworkEntity(DObject* const ent);
118-
static DObject* GetNetworkEntity(const uint32_t id);
119-
};
120101

121102
// [RH]
122103
// New generic packet structure:

0 commit comments

Comments
 (0)