script, ui: added "sv_min_startmap_health" cvar
[k8vavoom.git] / vccrun / vcc_netobj.cpp
blob83432ea6abbe44d852a4c8de516bb21ac1c1ea74
1 //**************************************************************************
2 //**
3 //** ## ## ## ## ## #### #### ### ###
4 //** ## ## ## ## ## ## ## ## ## ## #### ####
5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ##
6 //** ## ## ######## ## ## ## ## ## ## ## ### ##
7 //** ### ## ## ### ## ## ## ## ## ##
8 //** # ## ## # #### #### ## ##
9 //**
10 //** Copyright (C) 1999-2006 Jānis Legzdiņš
11 //** Copyright (C) 2018-2023 Ketmar Dark
12 //**
13 //** This program is free software: you can redistribute it and/or modify
14 //** it under the terms of the GNU General Public License as published by
15 //** the Free Software Foundation, version 3 of the License ONLY.
16 //**
17 //** This program is distributed in the hope that it will be useful,
18 //** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 //** GNU General Public License for more details.
21 //**
22 //** You should have received a copy of the GNU General Public License
23 //** along with this program. If not, see <http://www.gnu.org/licenses/>.
24 //**
25 //**************************************************************************
26 #include "vcc_run.h"
27 //#include "gamedefs.h"
28 //#include "network.h"
31 //==========================================================================
33 // VNetObjectsMap::VNetObjectsMap
35 //==========================================================================
36 VNetObjectsMap::VNetObjectsMap () : Connection(nullptr) {
40 //==========================================================================
42 // VNetObjectsMap::VNetObjectsMap
44 //==========================================================================
45 VNetObjectsMap::VNetObjectsMap (VNetConnection *AConnection) : Connection(AConnection) {
49 //==========================================================================
51 // VNetObjectsMap::SetUpClassLookup
53 //==========================================================================
54 void VNetObjectsMap::SetUpClassLookup () {
55 NameMap.setLength(VName::GetNumNames());
56 NameLookup.setLength(VName::GetNumNames());
57 for (int i = 0; i < VName::GetNumNames(); ++i) {
58 NameMap[i] = i;
59 NameLookup[i] = *(VName *)&i;
62 ClassLookup.clear();
63 ClassLookup.append(nullptr);
64 for (int i = 0; i < VMemberBase::GMembers.length(); ++i) {
65 if (VMemberBase::GMembers[i]->MemberType == MEMBER_Class) {
66 VClass *cls = static_cast<VClass *>(VMemberBase::GMembers[i]);
67 if (cls->IsChildOf(/*VThinker*/VObject::StaticClass())) {
68 ClassMap.put(cls, ClassLookup.length());
69 ClassLookup.Append(cls);
76 //==========================================================================
78 // VNetObjectsMap::CanSerialiseObject
80 //==========================================================================
81 bool VNetObjectsMap::CanSerialiseObject (VObject *obj) {
82 //VThinker *Thinker = Cast<VThinker>(Obj);
83 VObject *Thinker = obj;
84 if (Thinker) {
85 // thinker can be serialised only if it has an open channel
86 //return !!Connection->ThinkerChannels.FindPtr(Thinker);
87 return true; //FIXME
88 } else {
89 // we can always serialise nullptr object
90 return !obj;
95 //==========================================================================
97 // VNetObjectsMap::SerialiseName
99 //==========================================================================
100 bool VNetObjectsMap::SerialiseName (VStream &Strm, VName &Name) {
101 if (Strm.IsLoading()) {
102 vuint32 NameIndex;
103 Strm.SerialiseInt(NameIndex/*, NameLookup.length()+1*/);
104 if ((vint32)NameIndex == NameLookup.length()) {
105 Name = NAME_None;
106 } else {
107 Name = NameLookup[NameIndex];
109 return true;
110 } else {
111 vuint32 NameIndex = (Name.GetIndex() < NameMap.length() ? NameMap[Name.GetIndex()] : NameLookup.length());
112 Strm.SerialiseInt(NameIndex/*, NameLookup.length()+1*/);
113 return ((vint32)NameIndex != NameLookup.length());
118 //==========================================================================
120 // VNetObjectsMap::SerialiseObject
122 //==========================================================================
123 bool VNetObjectsMap::SerialiseObject (VStream &Strm, VObject *&obj) {
124 #if 0
125 if (Strm.IsLoading()) {
126 obj = nullptr;
127 vuint8 IsThinker = 0;
128 Strm.SerialiseBits(&IsThinker, 1);
129 if (IsThinker) {
130 // it's a thinker that has an open channel
131 vuint32 Index;
132 Strm.SerialiseInt(Index, MAX_CHANNELS);
133 VChannel *Chan = Connection->Channels[Index];
134 if (Chan && Chan->Type == CHANNEL_Thinker && !Chan->Closing) {
135 obj = ((VThinkerChannel *)Chan)->Thinker;
138 return true;
139 } else {
140 VThinker *Thinker = Cast<VThinker>(obj);
141 vuint8 IsThinker = !!Thinker;
142 Strm.SerialiseBits(&IsThinker, 1);
143 if (Thinker) {
144 // it's a thinker. if it has an open channel we can use it's
145 // channel number to identify it, otherwise we can't serialise it.
146 bool Ret = false;
147 vuint32 Index = 0;
148 VThinkerChannel *Chan = Connection->ThinkerChannels.FindPtr(Thinker);
149 if (Chan) {
150 Index = Chan->Index;
151 Ret = (Chan->OpenAcked ? true : false);
153 Strm.SerialiseInt(Index, MAX_CHANNELS);
154 return Ret;
156 return !obj;
158 #else
159 obj = nullptr;
160 return false;
161 #endif
165 //==========================================================================
167 // VNetObjectsMap::SerialiseClass
169 //==========================================================================
170 bool VNetObjectsMap::SerialiseClass (VStream &Strm, VClass *&Class) {
171 if (Strm.IsLoading()) {
172 vuint32 ClassId;
173 Strm.SerialiseInt(ClassId/*, ClassLookup.length()*/);
174 if (ClassId) {
175 Class = ClassLookup[ClassId];
176 } else {
177 Class = nullptr;
179 } else {
180 if (Class) {
181 vuint32 *ClassId = ClassMap.get(Class);
182 Strm.SerialiseInt(*ClassId/*, ClassLookup.length()*/);
183 } else {
184 vuint32 NoClass = 0;
185 Strm.SerialiseInt(NoClass/*, ClassLookup.length()*/);
188 return true;
192 //==========================================================================
194 // VNetObjectsMap::SerialiseState
196 //==========================================================================
197 bool VNetObjectsMap::SerialiseState (VStream &Strm, VState *&State) {
198 if (Strm.IsLoading()) {
199 vuint32 ClassId;
200 Strm.SerialiseInt(ClassId/*, ClassLookup.length()*/);
201 if (ClassId) {
202 vuint32 StateId;
203 Strm.SerialiseInt(StateId/*, ClassLookup[ClassId]->StatesLookup.length()*/);
204 State = ClassLookup[ClassId]->StatesLookup[StateId];
205 } else {
206 State = nullptr;
208 } else {
209 if (State) {
210 vuint32 *ClassId = ClassMap.get((VClass*)State->Outer);
211 vuint32 StateId = State->NetId;
212 vensure(ClassId);
213 Strm.SerialiseInt(*ClassId/*, ClassLookup.length()*/);
214 Strm.SerialiseInt(StateId/*, ((VClass *)State->Outer)->StatesLookup.length()*/);
215 } else {
216 vuint32 NoClass = 0;
217 Strm.SerialiseInt(NoClass/*, ClassLookup.length()*/);
220 return true;