Registry: use BITS everywhere
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / net.qh
1 #ifndef NET_H
2 #define NET_H
3
4 #ifdef SVQC
5         .int Version;  // deprecated, use SendFlags
6         .int SendFlags;
7         .bool(entity to, int sendflags)SendEntity;
8         .bool(entity this, entity to, int sendflags)SendEntity3;
9
10         bool SendEntity_self(entity to, int sendflags) { return self.SendEntity3(self, to, sendflags); }
11
12         void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags)sendfunc)
13         {
14                 if (e.classname == "") e.classname = "net_linked";
15
16                 if (e.model == "" || self.modelindex == 0)
17                 {
18                         vector mi = e.mins;
19                         vector ma = e.maxs;
20                         _setmodel(e, "null");
21                         setsize(e, mi, ma);
22                 }
23
24                 e.SendEntity = SendEntity_self;
25                 e.SendEntity3 = sendfunc;
26                 e.SendFlags = 0xFFFFFF;
27
28                 if (!docull) e.effects |= EF_NODEPTHTEST;
29
30                 if (dt)
31                 {
32                         e.nextthink = time + dt;
33                         e.think = SUB_Remove;
34                 }
35         }
36
37         .void() uncustomizeentityforclient;
38         .float uncustomizeentityforclient_set;
39
40         void SetCustomizer(entity e, float(void)customizer, void(void)uncustomizer)
41         {
42                 e.customizeentityforclient = customizer;
43                 e.uncustomizeentityforclient = uncustomizer;
44                 e.uncustomizeentityforclient_set = !!uncustomizer;
45         }
46
47         void UncustomizeEntitiesRun()
48         {
49                 for (entity e = NULL; (e = findfloat(e, uncustomizeentityforclient_set, 1)); )
50                         WITH(entity, self, e, e.uncustomizeentityforclient());
51         }
52
53 #endif
54
55 #include "registry.qh"
56 #include "sort.qh"
57
58 .string netname;
59 .int m_id;
60 .void(entity this, bool isNew)m_read;
61
62 #ifdef CSQC
63         #define Net_Accept() \
64                 do \
65                 { \
66                         if (!this)    this = spawn(); \
67                 } \
68                 while (0)
69         #define Net_Reject() \
70                 do \
71                 { \
72                         if (this)     remove(this); \
73                 } \
74                 while (0)
75 #else
76         #define WriteHeader(to, id) \
77                 do \
78                 { \
79                         if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
80                         WriteByte(to, NET_##id.m_id); \
81                 } \
82                 while (0)
83 #endif
84
85 #ifdef CSQC
86         #define REGISTER_NET_LINKED(id, param) \
87                 void Ent_Read##id(entity this, param) \
88                 { \
89                         this = self; \
90                 } \
91                 REGISTER(RegisterLinkedEntities, NET, LinkedEntities, id, m_id, spawn()) \
92                 { \
93                         this.netname = #id; \
94                         this.m_read = Ent_Read##id; \
95                 } \
96                 [[accumulate]] void Ent_Read##id(entity this, param)
97 #else
98         #define REGISTER_NET_LINKED(id, param) \
99                 const bool NET_##id##_istemp = false; \
100                 REGISTER(RegisterLinkedEntities, NET, LinkedEntities, id, m_id, spawn()) \
101                 { \
102                         this.netname = #id; \
103                 }
104 #endif
105
106 REGISTRY(LinkedEntities, BITS(4))
107 REGISTER_REGISTRY(RegisterLinkedEntities)
108 REGISTRY_SORT(LinkedEntities, netname, 0)
109 STATIC_INIT(RegisterLinkedEntities_renumber)
110 {
111         for (int i = 0; i < LinkedEntities_COUNT; ++i)
112                 LinkedEntities[i].m_id = 100 + i;
113 }
114
115 #ifdef CSQC
116         #define REGISTER_NET_TEMP(id, param) \
117                 void Net_Read##id(entity this, param); \
118                 REGISTER(RegisterTempEntities, NET, TempEntities, id, m_id, spawn()) \
119                 { \
120                         this.netname = #id; \
121                         this.m_read = Net_Read##id; \
122                 } \
123                 void Net_Read##id(entity this, param)
124 #else
125         #define REGISTER_NET_TEMP(id, param) \
126                 const bool NET_##id##_istemp = true; \
127                 REGISTER(RegisterTempEntities, NET, TempEntities, id, m_id, spawn()) \
128                 { \
129                         this.netname = #id; \
130                 }
131 #endif
132
133 REGISTRY(TempEntities, BITS(4))
134 REGISTER_REGISTRY(RegisterTempEntities)
135 REGISTRY_SORT(TempEntities, netname, 0)
136 STATIC_INIT(RegisterTempEntities_renumber)
137 {
138         for (int i = 0; i < TempEntities_COUNT; ++i)
139                 TempEntities[i].m_id = 115 + i;
140 }
141
142 #ifndef MENUQC
143         #ifdef CSQC
144                 int ReadInt24_t()
145                 {
146                         int v = ReadShort() << 8; // note: this is signed
147                         v += ReadByte();          // note: this is unsigned
148                         return v;
149                 }
150                 vector ReadInt48_t()
151                 {
152                         vector v;
153                         v.x = ReadInt24_t();
154                         v.y = ReadInt24_t();
155                         v.z = 0;
156                         return v;
157                 }
158                 vector ReadInt72_t()
159                 {
160                         vector v;
161                         v.x = ReadInt24_t();
162                         v.y = ReadInt24_t();
163                         v.z = ReadInt24_t();
164                         return v;
165                 }
166         #else
167                 void WriteInt24_t(float dst, float val)
168                 {
169                         float v;
170                         WriteShort(dst, (v = floor(val >> 8)));
171                         WriteByte(dst, val - (v << 8));  // 0..255
172                 }
173                 void WriteInt48_t(float dst, vector val)
174                 {
175                         WriteInt24_t(dst, val.x);
176                         WriteInt24_t(dst, val.y);
177                 }
178                 void WriteInt72_t(float dst, vector val)
179                 {
180                         WriteInt24_t(dst, val.x);
181                         WriteInt24_t(dst, val.y);
182                         WriteInt24_t(dst, val.z);
183                 }
184         #endif
185 #endif
186
187 #endif