Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
client_management.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <enet/enet.h> // for ENetPeer, ENetPa...
4 #include <limits.h> // for INT_MAX
5 #include <algorithm> // for max
6 
7 #include "inexor/fpsgame/fpsstate.hpp" // for fpsstate
8 #include "inexor/network/SharedVar.hpp" // for SharedVar
9 #include "inexor/network/legacy/administration.hpp" // for ::PRIV_NONE, ::M...
10 #include "inexor/shared/cube_loops.hpp" // for i, loopi
11 #include "inexor/shared/cube_types.hpp" // for string, uchar, uint
12 #include "inexor/shared/cube_vector.hpp" // for vector
13 #include "inexor/shared/ents.hpp" // for ::CS_ALIVE, ::CS...
14 #include "inexor/shared/geom.hpp" // for vec
15 #include "inexor/util/legacy_time.hpp" // for gamemillis
16 
17 
18 #define DEFAULTCLIENTS 16
19 
20 
21 namespace server {
22 
23 extern ENetPeer *getclientpeer(int i);
24 extern uint getclientip(int n);
25 
28 extern int reserveclients();
29 
31 struct client
32 {
33  bool connected;
34  int num;
35  ENetPeer *peer;
36  string hostname;
37  void *info;
38 };
40 
41 extern client &add_client_connection(ENetPeer *peer);
42 
44 extern void check_clients_timed_out();
45 
46 extern void disconnect_client(int n, int reason);
47 extern bool has_clients();
48 extern int get_num_clients();
49 
55 extern int numclients(int exclude = -1, bool nospec = true, bool noai = true, bool priv = false);
56 
57 static constexpr int DEATHMILLIS = 300;
58 
59 extern int nextexceeded;
60 
61 template <int N>
63 {
64  int projs[N];
65  int numprojs;
66 
68 
69  void reset() { numprojs = 0; }
70 
71  void add(int val)
72  {
73  if(numprojs>=N) numprojs = 0;
74  projs[numprojs++] = val;
75  }
76 
77  bool remove(int val)
78  {
79  loopi(numprojs) if(projs[i]==val)
80  {
81  projs[i] = projs[--numprojs];
82  return true;
83  }
84  return false;
85  }
86 };
87 
90 {
91  vec o;
94  int lastshot;
97  shotdamage, //all damage your shots could have made
98  damage, tokens;
101 
103 
104  bool isalive(int gamemillis)
105  {
106  return state==CS_ALIVE || (state==CS_DEAD && gamemillis - lastdeath <= DEATHMILLIS);
107  }
108 
110  {
111  return gamemillis - lastshot >= gunwait;
112  }
113 
114  void reset()
115  {
117  maxhealth = 100;
118  rockets.reset();
119  grenades.reset();
120  bombs.reset();
121 
122  timeplayed = 0;
123  effectiveness = 0;
125 
126  lastdeath = 0;
127 
128  respawn();
129  }
130 
131  void respawn()
132  {
134  o = vec(-1e10f, -1e10f, -1e10f);
135  deadflush = 0;
136  lastspawn = -1;
137  lastshot = 0;
138  tokens = 0;
139  }
140 
141  void reassign()
142  {
143  respawn();
144  rockets.reset();
145  grenades.reset();
146  bombs.reset();
147  }
148 
149  void setbackupweapon(int weapon)
150  {
151  fpsstate::backupweapon = weapon;
152  }
153 };
154 
155 struct clientinfo;
156 
157 // Events
158 
159 struct gameevent
160 {
161  virtual ~gameevent() {}
162 
163  virtual bool flush(clientinfo *ci, int fmillis);
164  virtual void process(clientinfo *ci) {}
165 
166  virtual bool keepable() const { return false; }
167 };
168 
170 {
171  int millis;
172 
173  bool flush(clientinfo *ci, int fmillis) override;
174 };
175 
176 struct hitinfo
177 {
178  int target;
180  int rays;
181  float dist;
183 };
184 
186 {
187  int id, gun;
190 
191  void process(clientinfo *ci) override;
192 };
193 
195 {
196  int id, gun;
198 
199  bool keepable() const override { return true; }
200 
201  void process(clientinfo *ci) override;
202 };
203 
205 {
206  void process(clientinfo *ci) override;
207 };
208 
210 {
211  int ent;
212 
213  void process(clientinfo *ci) override;
214 };
215 
216 
218 {
220  string name, tag, team, mapvote;
222  int fov;
223  int modevote;
231  int wslen;
234  string clientmap;
235  int mapcrc;
237  ENetPacket *getdemo, *getmap, *clipboard;
239 
240  clientinfo() : getdemo(nullptr), getmap(nullptr), clipboard(nullptr) { reset(); }
241  ~clientinfo() { events.deletecontents(); cleanclipboard(); }
242 
244  {
245  if(state.state==CS_SPECTATOR || events.length()>100) delete e;
246  else events.add(e);
247  }
248 
249  enum
250  {
251  PUSHMILLIS = 3000
252  };
253 
255  {
256  ENetPeer *peer = getclientpeer(ownernum);
257  return PUSHMILLIS + (peer ? peer->roundTripTime + peer->roundTripTimeVariance : ENET_PEER_DEFAULT_ROUND_TRIP_TIME);
258  }
259 
260  bool checkpushed(int millis, int range)
261  {
262  return millis >= pushed - range && millis <= pushed + range;
263  }
264 
266  {
267  if(state.state!=CS_ALIVE || !exceeded) return;
268  int range = calcpushrange();
269  if(!nextexceeded || exceeded + range < nextexceeded) nextexceeded = exceeded + range;
270  }
271 
272  void setexceeded()
273  {
276  }
277 
278  void setpushed()
279  {
282  }
283 
285  {
287  }
288 
289  void mapchange()
290  {
291  mapvote[0] = 0;
292  modevote = INT_MAX;
293  state.reset();
294  events.deletecontents();
295  overflow = 0;
296  timesync = false;
297  lastevent = 0;
298  exceeded = 0;
299  pushed = 0;
300  clientmap[0] = '\0';
301  mapcrc = 0;
302  warned = false;
303  gameclip = false;
304  }
305 
306  void reassign()
307  {
308  state.reassign();
309  events.deletecontents();
310  timesync = false;
311  lastevent = 0;
312  }
313 
314  void cleanclipboard(bool fullclean = true)
315  {
316  if(clipboard) { if(--clipboard->referenceCount <= 0) enet_packet_destroy(clipboard); clipboard = nullptr; }
317  if(fullclean) lastclipboard = 0;
318  }
319 
320  void reset()
321  {
322  name[0] = team[0] = tag[0] = 0;
323  playermodel = -1;
324  fov = 100;
326  connected = false;
327  position.setsize(0);
328  messages.setsize(0);
329  ping = 0;
330  aireinit = 0;
331  needclipboard = 0;
332  cleanclipboard();
333  mapchange();
334  }
335 
336  int geteventmillis(int servmillis, int clientmillis)
337  {
338  if(!timesync || (events.empty() && state.waitexpired(servmillis)))
339  {
340  timesync = true;
341  gameoffset = servmillis - clientmillis;
342  return servmillis;
343  } else return gameoffset + clientmillis;
344  }
345 };
346 
349 extern const char *colorname(clientinfo *ci, const char *name = nullptr);
350 
351 extern bool player_connected(clientinfo *ci, const char *password, const char *mapwish, int modewish);
352 
353 // TODO remove connects array and merge with client_connections
355 
356 extern clientinfo *get_client_info(int n, bool findbots = true);
357 
359 extern void resetdisconnectedplayerscores();
360 
361 namespace aiman
362 {
363 extern void removeai(clientinfo *ci);
364 extern void clearai();
365 extern void checkai();
366 extern void reqadd(clientinfo *ci, int skill);
367 extern void reqdel(clientinfo *ci);
368 extern void setbotlimit(clientinfo *ci, int limit);
369 extern void setbotbalance(clientinfo *ci, bool balance);
370 extern void changemap();
371 extern void addclient(clientinfo *ci);
372 extern void changeteam(clientinfo *ci);
373 }
374 
375 extern int mastermode, mastermask;
376 extern int get_mastermode_int();
377 extern bool setmaster(clientinfo *ci, bool val, const char *pass = "", bool force = false, bool trial = false);
378 extern void change_mastermode(int mm, int sendernum, clientinfo *actor);
379 
381 
382 extern void addban(uint ip, int expire);
383 
384 extern void clearbans(clientinfo *actor);
385 extern void check_bans_expired();
386 extern bool trykick(clientinfo *ci, int victim, const char *reason = nullptr, bool trial = false);
389 extern void kickclients(uint ip, clientinfo *actor = nullptr, int priv = PRIV_NONE);
390 
391 } // ns server
392 
393 #define MM_MODE 0xF
394 #define MM_AUTOAPPROVE 0x1000
395 #define MM_PRIVSERV (MM_MODE | MM_AUTOAPPROVE)
396 #define MM_PUBSERV ((1<<MM_OPEN) | (1<<MM_VETO))
397 #define MM_COOPSERV (MM_AUTOAPPROVE | MM_PUBSERV | (1<<MM_LOCKED))
398 
void kickclients(uint ip, clientinfo *actor, int priv)
Kick all clients with this IP (including bots and people in the same LAN)
Definition: client_management.cpp:153
Definition: client_management.hpp:217
Vector template.
Definition: cube_vector.hpp:22
int ownernum
Definition: client_management.hpp:219
const T & max(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:224
void process(clientinfo *ci) override
Definition: server.cpp:1282
Definition: administration.hpp:6
void process(clientinfo *ci) override
Definition: server.cpp:1287
bool connected
Definition: client_management.hpp:33
void process(clientinfo *ci) override
Definition: server.cpp:1373
static constexpr int DEATHMILLIS
Definition: client_management.hpp:57
unsigned int uint
Definition: cube_types.hpp:9
Definition: client_management.hpp:251
int mastermask
Definition: client_management.cpp:230
ENetPeer * getclientpeer(int i)
Definition: client_management.cpp:54
void reqdel(clientinfo *ci)
Definition: server.cpp:274
int gameoffset
Definition: client_management.hpp:226
void reset()
Definition: client_management.hpp:69
void checkai()
Definition: server.cpp:254
ENetPacket * getdemo
Definition: client_management.hpp:237
void setbotbalance(clientinfo *ci, bool balance)
Definition: server.cpp:291
int id
Definition: client_management.hpp:187
string tag
Definition: client_management.hpp:220
int projs[N]
Definition: client_management.hpp:64
int lastdeath
Definition: client_management.hpp:93
void disconnect_client(int n, int reason)
Definition: client_management.cpp:559
bool checkpushed(int millis, int range)
Definition: client_management.hpp:260
int lastclipboard
Definition: client_management.hpp:238
void clearbans(clientinfo *actor)
Definition: client_management.cpp:139
int deadflush
Definition: client_management.hpp:93
int id
Definition: client_management.hpp:196
bool gameclip
Definition: client_management.hpp:236
SharedVar< int > maxdupclients
int rays
Definition: client_management.hpp:180
int ping
Definition: client_management.hpp:233
ENetPeer * peer
Definition: client_management.hpp:35
int backupweapon
Definition: fpsstate.hpp:26
int mapcrc
Definition: client_management.hpp:235
int target
Definition: client_management.hpp:178
int aireinit
Definition: client_management.hpp:233
vec o
Definition: client_management.hpp:91
void reset()
Definition: client_management.hpp:114
vec from
Definition: client_management.hpp:188
void setpushed()
Definition: client_management.hpp:278
server specialisation of the fpsstate (?)
Definition: client_management.hpp:89
uchar * wsdata
Definition: client_management.hpp:230
vec to
Definition: client_management.hpp:188
string clientmap
Definition: client_management.hpp:234
int gunwait
Definition: fpsstate.hpp:23
projectilestate< 8 > rockets
Definition: client_management.hpp:95
projectilestate< 8 > bombs
Definition: client_management.hpp:95
string name
Definition: client_management.hpp:220
void check_clients_timed_out()
After some period of time without response we disconnect a client.
Definition: client_management.cpp:590
int gun
Definition: client_management.hpp:187
const char * colorname(clientinfo *ci, const char *name)
colorful version of the clients name (if two people share the same name or ci is a bot) ...
Definition: client_management.cpp:106
SharedVar< int > maxclients
int damage
Definition: client_management.hpp:96
void setbackupweapon(int weapon)
Definition: client_management.hpp:149
vector< clientinfo * > clients
Definition: client_management.cpp:52
int needclipboard
Definition: client_management.hpp:238
bool keepable() const override
Definition: client_management.hpp:199
int lastevent
Definition: client_management.hpp:226
int teamkills
Definition: client_management.hpp:96
vector< hitinfo > hits
Definition: client_management.hpp:189
int ent
Definition: client_management.hpp:211
void reassign()
Definition: client_management.hpp:141
void cleanclipboard(bool fullclean=true)
Definition: client_management.hpp:314
int shotdamage
Definition: client_management.hpp:96
bool setmaster(clientinfo *ci, bool val, const char *pass, bool force, bool trial)
Definition: client_management.cpp:268
bool flush(clientinfo *ci, int fmillis) override
Definition: server.cpp:1386
void setsize(int i)
shrink vector memory size
Definition: cube_vector.hpp:163
int frags
Definition: client_management.hpp:96
string team
Definition: client_management.hpp:220
else loopi(numargs)
Definition: command.cpp:3019
void addban(uint ip, int expire)
Definition: client_management.cpp:128
void removeai(clientinfo *ci)
Definition: server.cpp:214
int state
Definition: client_management.hpp:92
bool waitexpired(int gamemillis)
Definition: client_management.hpp:109
void mapchange()
Definition: client_management.hpp:289
void process(clientinfo *ci) override
Definition: server.cpp:1330
Definition: client_management.hpp:176
clientinfo * get_client_info(int n, bool findbots)
Definition: client_management.cpp:89
int reserveclients()
The amount of people who can connect more than the actual specified max clients limit.
Definition: client_management.cpp:36
void reqadd(clientinfo *ci, int skill)
Definition: server.cpp:267
vec dir
Definition: client_management.hpp:182
uint getclientip(int n)
Definition: client_management.cpp:59
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
vector< client * > client_connections
TODO merge with clients or connects.
Definition: client_management.cpp:50
vector< hitinfo > hits
Definition: client_management.hpp:197
int gun
Definition: client_management.hpp:196
bool isalive(int gamemillis)
Definition: client_management.hpp:104
int overflow
Definition: client_management.hpp:219
int clientnum
Definition: client_management.hpp:219
Definition: client_management.hpp:159
bool timesync
Definition: client_management.hpp:225
vector< gameevent * > events
Definition: client_management.hpp:228
int privilege
Definition: client_management.hpp:224
vector< uchar > messages
Definition: client_management.hpp:229
int nextexceeded
Definition: server.cpp:55
int get_num_clients()
Definition: client_management.cpp:73
void reset()
Definition: client_management.hpp:320
Definition: ents.hpp:120
Definition: client_management.hpp:204
int deaths
Definition: client_management.hpp:96
int sessionid
Definition: client_management.hpp:219
float dist
Definition: client_management.hpp:181
int connectmillis
Definition: client_management.hpp:219
projectilestate()
Definition: client_management.hpp:67
int get_mastermode_int()
Definition: client_management.cpp:257
void clearai()
Definition: server.cpp:247
server side version of "dynent" type
Definition: client_management.hpp:31
bool connected
Definition: client_management.hpp:225
int millis
Definition: client_management.hpp:171
virtual bool keepable() const
Definition: client_management.hpp:166
int flags
Definition: client_management.hpp:96
int timeplayed
Definition: client_management.hpp:99
int maxhealth
Definition: fpsstate.hpp:20
client & add_client_connection(ENetPeer *peer)
Definition: client_management.cpp:502
int wslen
Definition: client_management.hpp:231
Definition: client_management.hpp:62
int mastermode
Definition: client_management.cpp:230
void reassign()
Definition: client_management.hpp:306
~clientinfo()
Definition: client_management.hpp:241
projectilestate< 8 > grenades
Definition: client_management.hpp:95
void changeteam(clientinfo *ci)
Definition: server.cpp:318
gamestate state
Definition: client_management.hpp:227
int lastspawn
Definition: client_management.hpp:93
int fov
Definition: client_management.hpp:222
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
int lastshot
Definition: client_management.hpp:94
ENetPacket * clipboard
Definition: client_management.hpp:237
clientinfo()
Definition: client_management.hpp:240
void check_bans_expired()
Definition: client_management.cpp:148
ENetPacket * getmap
Definition: client_management.hpp:237
int exceeded
Definition: client_management.hpp:226
bool trykick(clientinfo *ci, int victim, const char *reason, bool trial)
Definition: client_management.cpp:164
void respawn()
reset all members when spawning
Definition: fpsstate.hpp:109
bool checkexceeded()
Definition: client_management.hpp:284
int calcpushrange()
Definition: client_management.hpp:254
virtual void process(clientinfo *ci)
Definition: client_management.hpp:164
void addevent(gameevent *e)
Definition: client_management.hpp:243
void * info
Definition: client_management.hpp:37
void setbotlimit(clientinfo *ci, int limit)
Definition: server.cpp:281
void addclient(clientinfo *ci)
Definition: server.cpp:311
int num
Definition: client_management.hpp:34
int editstate
Definition: client_management.hpp:92
Definition: ents.hpp:120
Definition: ents.hpp:120
void change_mastermode(int mm, int sendernum, clientinfo *actor)
Definition: client_management.cpp:234
void scheduleexceeded()
Definition: client_management.hpp:265
SharedVar wrapper for primitive/immutable objects.
Definition: SharedVar.hpp:55
void setexceeded()
Definition: client_management.hpp:272
float effectiveness
Definition: client_management.hpp:100
int geteventmillis(int servmillis, int clientmillis)
Definition: client_management.hpp:336
vector< uchar > position
Definition: client_management.hpp:229
int numprojs
Definition: client_management.hpp:65
Definition: client_management.hpp:194
int lifesequence
Definition: client_management.hpp:179
vector< clientinfo * > bots
Definition: client_management.cpp:52
static map entities ("entity") and dynamic entities (players/monsters, "dynent") the gamecode extends...
int lasttimeplayed
Definition: client_management.hpp:99
int tokens
Definition: client_management.hpp:96
mathmatics for vectors, matrices, quaterions and more
int numclients(int exclude, bool nospec, bool noai, bool priv)
List all connected clients (game players)
Definition: client_management.cpp:75
Definition: client_management.hpp:185
gamestate()
Definition: client_management.hpp:102
int playermodel
Definition: client_management.hpp:221
int pushed
Definition: client_management.hpp:226
vector< clientinfo * > bots
Definition: client_management.hpp:232
virtual ~gameevent()
Definition: client_management.hpp:161
bool player_connected(clientinfo *ci, const char *password, const char *mapwish, int modewish)
Definition: client_management.cpp:451
virtual bool flush(clientinfo *ci, int fmillis)
Definition: server.cpp:1380
int lifesequence
Definition: client_management.hpp:93
First person state.
Definition: fpsstate.hpp:18
bool has_clients()
Definition: client_management.cpp:70
int modevote
Definition: client_management.hpp:223
void resetdisconnectedplayerscores()
New map: we throw away the scores of disconnected players.
Definition: client_management.cpp:418
void changemap()
Definition: server.cpp:302
Definition: random.cpp:21
string hostname
Definition: client_management.hpp:36
string mapvote
Definition: client_management.hpp:220
bool warned
Definition: client_management.hpp:236
void add(int val)
Definition: client_management.hpp:71
void respawn()
Definition: client_management.hpp:131
Definition: client_management.hpp:169
int gamemillis
Definition: legacy_time.cpp:15
Definition: client_management.hpp:209
vector< clientinfo * > connects
Definition: client_management.cpp:52