]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/main.qc
Banning system: prevent a bug where any redundant space in a ban list bans all player...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / main.qc
1 #include "main.qh"
2
3 #include <common/command/generic.qh>
4 #include <common/constants.qh>
5 #include <common/deathtypes/all.qh>
6 #include <common/debug.qh>
7 #include <common/mapinfo.qh>
8 #include <common/monsters/sv_monsters.qh>
9 #include <common/util.qh>
10 #include <common/vehicles/all.qh>
11 #include <common/weapons/_all.qh>
12 #include <lib/csqcmodel/sv_model.qh>
13 #include <lib/warpzone/common.qh>
14 #include <lib/warpzone/server.qh>
15 #include <server/anticheat.qh>
16 #include <server/bot/api.qh>
17 #include <server/command/common.qh>
18 #include <server/compat/quake3.qh>
19 #include <server/damage.qh>
20 #include <server/gamelog.qh>
21 #include <server/hook.qh>
22 #include <server/ipban.qh>
23 #include <server/mutators/_mod.qh>
24 #include <server/spawnpoints.qh>
25 #include <server/weapons/common.qh>
26 #include <server/weapons/csqcprojectile.qh>
27 #include <server/world.qh>
28
29 void dropclient_do(entity this)
30 {
31         if (this.owner)
32                 dropclient(this.owner);
33         delete(this);
34 }
35 /**
36  * Schedules dropclient for a player and returns true;
37  * if dropclient is already scheduled (for that player) it does nothing and returns false.
38  *
39  * NOTE: this function exists only to allow sending a message to the kicked player with
40  * Send_Notification, which doesn't work if called together with dropclient
41  */
42 bool dropclient_schedule(entity this)
43 {
44         bool scheduled = false;
45         FOREACH_ENTITY_CLASS("dropclient_handler", true,
46         {
47                 if(it.owner == this)
48                 {
49                         scheduled = true;
50                         break; // can't use return here, compiler shows a warning
51                 }
52         });
53         if (scheduled)
54                 return false;
55
56         entity e = new_pure(dropclient_handler);
57         setthink(e, dropclient_do);
58         e.owner = this;
59         e.nextthink = time + 0.1;
60         return true;
61 }
62
63 void CreatureFrame_hotliquids(entity this)
64 {
65         if (this.contents_damagetime >= time)
66         {
67                 return;
68         }
69
70         this.contents_damagetime = time + autocvar_g_balance_contents_damagerate;
71
72         if (this.flags & FL_PROJECTILE)
73         {
74                 if (this.watertype == CONTENT_LAVA)
75                         Damage (this, NULL, NULL, autocvar_g_balance_contents_projectiledamage * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
76                 else if (this.watertype == CONTENT_SLIME)
77                         Damage (this, NULL, NULL, autocvar_g_balance_contents_projectiledamage * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
78         }
79         else
80         {
81                 if (STAT(FROZEN, this))
82                 {
83                         if (this.watertype == CONTENT_LAVA)
84                                 Damage(this, NULL, NULL, 10000, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
85                         else if (this.watertype == CONTENT_SLIME)
86                                 Damage(this, NULL, NULL, 10000, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
87                 }
88                 else if (this.watertype == CONTENT_LAVA)
89                 {
90                         if (this.watersound_finished < time)
91                         {
92                                 this.watersound_finished = time + 0.5;
93                                 sound (this, CH_PLAYER_SINGLE, SND_LAVA, VOL_BASE, ATTEN_NORM);
94                         }
95                         Damage (this, NULL, NULL, autocvar_g_balance_contents_playerdamage_lava * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
96                         if(autocvar_g_balance_contents_playerdamage_lava_burn)
97                                 Fire_AddDamage(this, NULL, autocvar_g_balance_contents_playerdamage_lava_burn * this.waterlevel, autocvar_g_balance_contents_playerdamage_lava_burn_time * this.waterlevel, DEATH_LAVA.m_id);
98                 }
99                 else if (this.watertype == CONTENT_SLIME)
100                 {
101                         if (this.watersound_finished < time)
102                         {
103                                 this.watersound_finished = time + 0.5;
104                                 sound (this, CH_PLAYER_SINGLE, SND_SLIME, VOL_BASE, ATTEN_NORM);
105                         }
106                         Damage (this, NULL, NULL, autocvar_g_balance_contents_playerdamage_slime * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
107                 }
108         }
109 }
110
111 void CreatureFrame_Liquids(entity this)
112 {
113         if (this.watertype <= CONTENT_WATER && this.waterlevel > 0) // workaround a retarded bug made by id software :P (yes, it's that old of a bug)
114         {
115                 if (!(this.flags & FL_INWATER))
116                 {
117                         this.flags |= FL_INWATER;
118                         this.contents_damagetime = 0;
119                 }
120
121                 CreatureFrame_hotliquids(this);
122         }
123         else
124         {
125                 if (this.flags & FL_INWATER)
126                 {
127                         // play leave water sound
128                         this.flags &= ~FL_INWATER;
129                         this.contents_damagetime = 0;
130                 }
131         }
132 }
133
134 void CreatureFrame_FallDamage(entity this)
135 {
136         if(IS_VEHICLE(this) || (this.flags & FL_PROJECTILE))
137                 return; // vehicles and projectiles don't receive fall damage
138         if(!(this.velocity || this.oldvelocity))
139                 return; // if the entity hasn't moved and isn't moving, then don't do anything
140
141         // check for falling damage
142         bool have_hook = false;
143         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
144         {
145             .entity weaponentity = weaponentities[slot];
146             if(this.(weaponentity).hook && this.(weaponentity).hook.state)
147             {
148                 have_hook = true;
149                 break;
150             }
151         }
152         if(!have_hook)
153         {
154                 float dm; // dm is the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
155                 if(autocvar_g_balance_falldamage_onlyvertical)
156                         dm = fabs(this.oldvelocity.z) - vlen(this.velocity);
157                 else
158                         dm = vlen(this.oldvelocity) - vlen(this.velocity);
159                 if (IS_DEAD(this))
160                         dm = (dm - autocvar_g_balance_falldamage_deadminspeed) * autocvar_g_balance_falldamage_factor;
161                 else
162                         dm = min((dm - autocvar_g_balance_falldamage_minspeed) * autocvar_g_balance_falldamage_factor, autocvar_g_balance_falldamage_maxdamage);
163                 if (dm > 0)
164                 {
165                         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
166                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODAMAGE))
167                                 Damage (this, NULL, NULL, dm, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
168                 }
169         }
170
171         if(autocvar_g_maxspeed > 0 && vdist(this.velocity, >, autocvar_g_maxspeed))
172                 Damage (this, NULL, NULL, 100000, DEATH_SHOOTING_STAR.m_id, DMG_NOWEP, this.origin, '0 0 0');
173 }
174
175 void CreatureFrame_All()
176 {
177         if(game_stopped || time < game_starttime)
178                 return;
179
180         IL_EACH(g_damagedbycontents, it.damagedbycontents,
181         {
182                 if (it.move_movetype == MOVETYPE_NOCLIP) continue;
183                 CreatureFrame_Liquids(it);
184                 CreatureFrame_FallDamage(it);
185                 it.oldvelocity = it.velocity;
186         });
187 }
188
189 void Pause_TryPause_Dedicated(entity this)
190 {
191         if (player_count == 0)
192                 setpause(1);
193 }
194
195 void Pause_TryPause()
196 {
197         int n = 0, p = 0;
198         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
199                 if (PHYS_INPUT_BUTTON_CHAT(it)) ++p;
200                 ++n;
201         });
202         if (!n) return;
203         if (n == p)
204                 setpause(1);
205         else
206                 setpause(0);
207 }
208
209 void SV_PausedTic(float elapsedtime)
210 {
211         if (!server_is_dedicated)
212         {
213                 if (autocvar_sv_autopause)
214                         Pause_TryPause();
215                 else
216                         setpause(0);
217         }
218 }
219
220 void dedicated_print(string input)
221 {
222         if (server_is_dedicated) print(input);
223 }
224
225 void make_safe_for_remove(entity e)
226 {
227     if (e.initialize_entity)
228     {
229         entity ent, prev = NULL;
230         for (ent = initialize_entity_first; ent; )
231         {
232             if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
233             {
234                 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
235                 // skip it in linked list
236                 if (prev)
237                 {
238                     prev.initialize_entity_next = ent.initialize_entity_next;
239                     ent = prev.initialize_entity_next;
240                 }
241                 else
242                 {
243                     initialize_entity_first = ent.initialize_entity_next;
244                     ent = initialize_entity_first;
245                 }
246             }
247             else
248             {
249                 prev = ent;
250                 ent = ent.initialize_entity_next;
251             }
252         }
253     }
254 }
255
256 void remove_except_protected(entity e)
257 {
258         if(e.remove_except_protected_forbidden)
259                 error("not allowed to remove this at this point");
260         builtin_remove(e);
261 }
262
263 void remove_unsafely(entity e)
264 {
265     if(e.classname == "spike")
266         error("Removing spikes is forbidden (crylink bug), please report");
267     builtin_remove(e);
268 }
269
270 void remove_safely(entity e)
271 {
272     make_safe_for_remove(e);
273     builtin_remove(e);
274 }
275
276 /*
277 =============
278 StartFrame
279
280 Called before each frame by the server
281 =============
282 */
283
284 bool game_delay_last;
285
286 void systems_update();
287 void sys_phys_update(entity this, float dt);
288 void StartFrame()
289 {
290     // TODO: if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
291     IL_EACH(g_players, IS_FAKE_CLIENT(it), sys_phys_update(it, frametime));
292     IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPreThink(it));
293
294         execute_next_frame();
295         if (autocvar_sv_autopause && !server_is_dedicated) Pause_TryPause();
296
297         delete_fn = remove_unsafely; // not during spawning!
298         serverprevtime = servertime;
299         servertime = time;
300         serverframetime = frametime;
301
302 #ifdef PROFILING
303         if(time > client_cefc_accumulatortime + 1)
304         {
305                 float t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
306                 int c_seeing = 0;
307                 int c_seen = 0;
308                 FOREACH_CLIENT(true, {
309                         if(IS_REAL_CLIENT(it))
310                                 ++c_seeing;
311                         if(IS_PLAYER(it))
312                                 ++c_seen;
313                 });
314                 LOG_INFO(
315                     "CEFC time: ", ftos(t * 1000), "ms; ",
316             "CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ",
317             "CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0')
318         );
319                 client_cefc_accumulatortime = time;
320                 client_cefc_accumulator = 0;
321         }
322 #endif
323
324         IL_EACH(g_projectiles, it.csqcprojectile_clientanimate, CSQCProjectile_Check(it));
325
326         if (RedirectionThink()) return;
327
328         UncustomizeEntitiesRun();
329         InitializeEntitiesRun();
330
331         WarpZone_StartFrame();
332
333         sys_frametime = autocvar_sys_ticrate * autocvar_slowmo;
334         if (sys_frametime <= 0) sys_frametime = 1.0 / 60.0; // somewhat safe fallback
335
336         if (timeout_status == TIMEOUT_LEADTIME) // just before the timeout (when timeout_status will be TIMEOUT_ACTIVE)
337                 orig_slowmo = autocvar_slowmo; // slowmo will be restored after the timeout
338
339         // detect when the pre-game countdown (if any) has ended and the game has started
340         bool game_delay = (time < game_starttime);
341         if (autocvar_sv_eventlog && game_delay_last && !game_delay)
342                 GameLogEcho(":startdelay_ended");
343         game_delay_last = game_delay;
344
345         CreatureFrame_All();
346         CheckRules_World();
347
348         if (warmup_stage && !game_stopped && warmup_limit > 0 && time - game_starttime >= warmup_limit) {
349                 ReadyRestart(true);
350                 return;
351         }
352
353         bot_serverframe();
354         anticheat_startframe();
355         MUTATOR_CALLHOOK(SV_StartFrame);
356
357         GlobalStats_updateglobal();
358     FOREACH_CLIENT(true, GlobalStats_update(it));
359     IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPostThink(it));
360 }
361
362 .vector originjitter;
363 .vector anglesjitter;
364 .float anglejitter;
365 .string gametypefilter;
366 .string cvarfilter;
367
368 void SV_OnEntityPreSpawnFunction(entity this)
369 {
370         if (this)
371         if (this.gametypefilter != "")
372         if (!isGametypeInFilter(MapInfo_LoadedGametype, teamplay, have_team_spawns, this.gametypefilter))
373         {
374                 delete(this);
375                 return;
376         }
377         if (this.cvarfilter != "" && !expr_evaluate(this.cvarfilter)) {
378                 delete(this);
379                 return;
380         }
381
382         if (q3compat && DoesQ3ARemoveThisEntity(this)) {
383                 delete(this);
384                 return;
385         }
386
387         set_movetype(this, this.movetype);
388
389         if (this.monster_attack) {
390                 IL_PUSH(g_monster_targets, this);
391     }
392
393         // support special -1 and -2 angle from radiant
394         if (this.angles == '0 -1 0') {
395                 this.angles = '-90 0 0';
396         } else if (this.angles == '0 -2 0') {
397                 this.angles = '+90 0 0';
398     }
399
400     #define X(out, in) MACRO_BEGIN \
401         if (in != 0) { out = out + (random() * 2 - 1) * in; } \
402     MACRO_END
403     X(this.origin.x, this.originjitter.x); X(this.origin.y, this.originjitter.y); X(this.origin.z, this.originjitter.z);
404     X(this.angles.x, this.anglesjitter.x); X(this.angles.y, this.anglesjitter.y); X(this.angles.z, this.anglesjitter.z);
405     X(this.angles.y, this.anglejitter);
406     #undef X
407
408         if (MUTATOR_CALLHOOK(OnEntityPreSpawn, this)) {
409                 delete(this);
410                 return;
411         }
412 }
413
414 string GetField_fullspawndata(entity e, string f, ...)
415 /* Retrieves the value of a map entity field from fullspawndata
416  * This bypasses field value changes made by the engine,
417  * eg string-to-float and escape sequence substitution.
418  *
419  * Avoids the need to declare fields just to read them once :)
420  *
421  * Returns the last instance of the field to match DarkPlaces behaviour.
422  * Path support: converts \ to / and tests the file if a third (bool, true) arg is passed.
423  * Returns string_null if the entity does not have the field, or the file is not in the VFS.
424  *
425  * FIXME: entities with //comments are not supported.
426  */
427 {
428         string v = string_null;
429
430         if (!e.fullspawndata)
431         {
432                 //LOG_WARNF("^1EDICT %s (classname %s) has no fullspawndata, engine lacks support?", ftos(num_for_edict(e)), e.classname);
433                 return v;
434         }
435
436         if (strstrofs(e.fullspawndata, "//", 0) >= 0)
437         {
438                 // tokenize and tokenize_console return early if "//" is reached,
439                 // which can leave an odd number of tokens and break key:value pairing.
440                 LOG_WARNF("^1EDICT %s fullspawndata contains unsupported //comment^7%s", ftos(num_for_edict(e)), e.fullspawndata);
441                 return v;
442         }
443
444         //print(sprintf("%s(EDICT %s, FIELD %s)\n", __FUNC__, ftos(num_for_edict(e)), f));
445         //print(strcat("FULLSPAWNDATA:", e.fullspawndata, "\n"));
446
447         // tokenize treats \ as an escape, but tokenize_console returns the required literal
448         for (int t = tokenize_console(e.fullspawndata) - 3; t > 0; t -= 2)
449         {
450                 //print(sprintf("\tTOKEN %s:%s\t%s:%s\n", ftos(t), ftos(t + 1), argv(t), argv(t + 1)));
451                 if (argv(t) == f)
452                 {
453                         v = argv(t + 1);
454                         break;
455                 }
456         }
457
458         //print(strcat("RESULT: ", v, "\n\n"));
459
460         if (v && ...(0, bool) == true)
461         {
462                 v = strreplace("\\", "/", v);
463                 if (whichpack(v) == "")
464                         return string_null;
465         }
466
467         return v;
468 }
469
470 void WarpZone_PostInitialize_Callback()
471 {
472         // create waypoint links for warpzones
473         entity tracetest_ent = spawn();
474         setsize(tracetest_ent, PL_MIN_CONST, PL_MAX_CONST);
475         tracetest_ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
476         //for(entity e = warpzone_first; e; e = e.warpzone_next)
477         for(entity e = NULL; (e = find(e, classname, "trigger_warpzone")); )
478                 waypoint_spawnforteleporter_wz(e, tracetest_ent);
479         delete(tracetest_ent);
480 }
481
482 /** engine callback */
483 void URI_Get_Callback(float id, float status, string data)
484 {
485         if(url_URI_Get_Callback(id, status, data))
486         {
487                 // handled
488         }
489         else if (id == URI_GET_DISCARD)
490         {
491                 // discard
492         }
493         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
494         {
495                 // sv_cmd curl
496                 Curl_URI_Get_Callback(id, status, data);
497         }
498         else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
499         {
500                 // online ban list
501                 OnlineBanList_URI_Get_Callback(id, status, data);
502         }
503         else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
504         {
505                 // handled by a mutator
506         }
507         else
508         {
509                 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".");
510         }
511 }
512
513 /*
514 ==================
515 main
516
517 unused but required by the engine
518 ==================
519 */
520 void main ()
521 {
522
523 }