]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/sv_main.qc
531a1537bf14a6efa183f66fc82a60bbe84cc5a4
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / sv_main.qc
1 #include "sv_main.qh"
2
3 #include "anticheat.qh"
4 #include "g_hook.qh"
5 #include "g_damage.qh"
6 #include "g_world.qh"
7
8 #include "bot/api.qh"
9
10 #include "command/common.qh"
11
12 #include <server/mutators/_mod.qh>
13 #include "weapons/csqcprojectile.qh"
14 #include <server/compat/quake3.qh>
15
16 #include "../common/constants.qh"
17 #include "../common/deathtypes/all.qh"
18 #include "../common/debug.qh"
19 #include "../common/mapinfo.qh"
20 #include "../common/util.qh"
21
22 #include "../common/vehicles/all.qh"
23 #include <common/monsters/sv_monsters.qh>
24 #include <common/weapons/_all.qh>
25
26 #include "../lib/csqcmodel/sv_model.qh"
27
28 #include "../lib/warpzone/common.qh"
29 #include "../lib/warpzone/server.qh"
30
31 void CreatureFrame_hotliquids(entity this)
32 {
33         if (this.dmgtime >= time)
34         {
35                 return;
36         }
37
38         this.dmgtime = time + autocvar_g_balance_contents_damagerate;
39
40         if (this.flags & FL_PROJECTILE)
41         {
42                 if (this.watertype == CONTENT_LAVA)
43                         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');
44                 else if (this.watertype == CONTENT_SLIME)
45                         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');
46         }
47         else
48         {
49                 if (STAT(FROZEN, this))
50                 {
51                         if (this.watertype == CONTENT_LAVA)
52                                 Damage(this, NULL, NULL, 10000, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
53                         else if (this.watertype == CONTENT_SLIME)
54                                 Damage(this, NULL, NULL, 10000, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
55                 }
56                 else if (this.watertype == CONTENT_LAVA)
57                 {
58                         if (this.watersound_finished < time)
59                         {
60                                 this.watersound_finished = time + 0.5;
61                                 sound (this, CH_PLAYER_SINGLE, SND_LAVA, VOL_BASE, ATTEN_NORM);
62                         }
63                         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');
64                         if(autocvar_g_balance_contents_playerdamage_lava_burn)
65                                 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);
66                 }
67                 else if (this.watertype == CONTENT_SLIME)
68                 {
69                         if (this.watersound_finished < time)
70                         {
71                                 this.watersound_finished = time + 0.5;
72                                 sound (this, CH_PLAYER_SINGLE, SND_SLIME, VOL_BASE, ATTEN_NORM);
73                         }
74                         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');
75                 }
76         }
77 }
78
79 void CreatureFrame_Liquids(entity this)
80 {
81         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)
82         {
83                 if (!(this.flags & FL_INWATER))
84                 {
85                         this.flags |= FL_INWATER;
86                         this.dmgtime = 0;
87                 }
88
89                 CreatureFrame_hotliquids(this);
90         }
91         else
92         {
93                 if (this.flags & FL_INWATER)
94                 {
95                         // play leave water sound
96                         this.flags &= ~FL_INWATER;
97                         this.dmgtime = 0;
98                 }
99         }
100 }
101
102 void CreatureFrame_FallDamage(entity this)
103 {
104         if(IS_VEHICLE(this) || (this.flags & FL_PROJECTILE))
105                 return; // vehicles and projectiles don't receive fall damage
106         if(!(this.velocity || this.oldvelocity))
107                 return; // if the entity hasn't moved and isn't moving, then don't do anything
108
109         // check for falling damage
110         bool have_hook = false;
111         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
112         {
113             .entity weaponentity = weaponentities[slot];
114             if(this.(weaponentity).hook && this.(weaponentity).hook.state)
115             {
116                 have_hook = true;
117                 break;
118             }
119         }
120         if(!have_hook)
121         {
122                 float dm; // dm is the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
123                 if(autocvar_g_balance_falldamage_onlyvertical)
124                         dm = fabs(this.oldvelocity.z) - vlen(this.velocity);
125                 else
126                         dm = vlen(this.oldvelocity) - vlen(this.velocity);
127                 if (IS_DEAD(this))
128                         dm = (dm - autocvar_g_balance_falldamage_deadminspeed) * autocvar_g_balance_falldamage_factor;
129                 else
130                         dm = min((dm - autocvar_g_balance_falldamage_minspeed) * autocvar_g_balance_falldamage_factor, autocvar_g_balance_falldamage_maxdamage);
131                 if (dm > 0)
132                 {
133                         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
134                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODAMAGE))
135                                 Damage (this, NULL, NULL, dm, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
136                 }
137         }
138
139         if(autocvar_g_maxspeed > 0 && vdist(this.velocity, >, autocvar_g_maxspeed))
140                 Damage (this, NULL, NULL, 100000, DEATH_SHOOTING_STAR.m_id, DMG_NOWEP, this.origin, '0 0 0');
141 }
142
143 void CreatureFrame_All()
144 {
145         if(game_stopped || time < game_starttime)
146                 return;
147
148         IL_EACH(g_damagedbycontents, it.damagedbycontents,
149         {
150                 if (it.move_movetype == MOVETYPE_NOCLIP) continue;
151                 CreatureFrame_Liquids(it);
152                 CreatureFrame_FallDamage(it);
153                 it.oldvelocity = it.velocity;
154         });
155 }
156
157 void Pause_TryPause(bool ispaused)
158 {
159         int n = 0;
160         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
161                 if (PHYS_INPUT_BUTTON_CHAT(it) != ispaused) return;
162                 ++n;
163         });
164         if (!n) return;
165         setpause(ispaused);
166 }
167
168 void SV_PausedTic(float elapsedtime)
169 {
170         if (!server_is_dedicated) Pause_TryPause(false);
171 }
172
173 /*
174 =============
175 StartFrame
176
177 Called before each frame by the server
178 =============
179 */
180
181 bool game_delay_last;
182
183 bool autocvar_sv_autopause = false;
184 void systems_update();
185 void sys_phys_update(entity this, float dt);
186 void StartFrame()
187 {
188     // TODO: if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
189     IL_EACH(g_players, IS_FAKE_CLIENT(it), sys_phys_update(it, frametime));
190     IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPreThink(it));
191
192         execute_next_frame();
193         if (autocvar_sv_autopause && !server_is_dedicated) Pause_TryPause(true);
194
195         delete_fn = remove_unsafely; // not during spawning!
196         serverprevtime = servertime;
197         servertime = time;
198         serverframetime = frametime;
199
200 #ifdef PROFILING
201         if(time > client_cefc_accumulatortime + 1)
202         {
203                 float t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
204                 int c_seeing = 0;
205                 int c_seen = 0;
206                 FOREACH_CLIENT(true, {
207                         if(IS_REAL_CLIENT(it))
208                                 ++c_seeing;
209                         if(IS_PLAYER(it))
210                                 ++c_seen;
211                 });
212                 LOG_INFO(
213                     "CEFC time: ", ftos(t * 1000), "ms; ",
214             "CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ",
215             "CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0')
216         );
217                 client_cefc_accumulatortime = time;
218                 client_cefc_accumulator = 0;
219         }
220 #endif
221
222         IL_EACH(g_projectiles, it.csqcprojectile_clientanimate, CSQCProjectile_Check(it));
223
224         if (RedirectionThink()) return;
225
226         UncustomizeEntitiesRun();
227         InitializeEntitiesRun();
228
229         WarpZone_StartFrame();
230
231         sys_frametime = autocvar_sys_ticrate * autocvar_slowmo;
232         if (sys_frametime <= 0) sys_frametime = 1.0 / 60.0; // somewhat safe fallback
233
234         if (timeout_status == TIMEOUT_LEADTIME) // just before the timeout (when timeout_status will be TIMEOUT_ACTIVE)
235                 orig_slowmo = autocvar_slowmo; // slowmo will be restored after the timeout
236
237         // detect when the pre-game countdown (if any) has ended and the game has started
238         bool game_delay = (time < game_starttime);
239         if (autocvar_sv_eventlog && game_delay_last && !game_delay)
240                 GameLogEcho(":startdelay_ended");
241         game_delay_last = game_delay;
242
243         CreatureFrame_All();
244         CheckRules_World();
245
246         if (warmup_stage && !game_stopped && warmup_limit > 0 && time >= warmup_limit) {
247                 ReadyRestart();
248                 return;
249         }
250
251         bot_serverframe();
252         anticheat_startframe();
253         MUTATOR_CALLHOOK(SV_StartFrame);
254
255         GlobalStats_updateglobal();
256     FOREACH_CLIENT(true, GlobalStats_update(it));
257     IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPostThink(it));
258 }
259
260 .vector originjitter;
261 .vector anglesjitter;
262 .float anglejitter;
263 .string gametypefilter;
264 .string cvarfilter;
265
266 /**
267  * Evaluate an expression of the form: [+ | -]? [var[op]val | [op]var | val | var] ...
268  * +: all must match. this is the default
269  * -: one must NOT match
270  *
271  * var>x
272  * var<x
273  * var>=x
274  * var<=x
275  * var==x
276  * var!=x
277  * var===x
278  * var!==x
279  */
280 bool expr_evaluate(string s)
281 {
282     bool ret = false;
283     if (str2chr(s, 0) == '+') {
284         s = substring(s, 1, -1);
285     } else if (str2chr(s, 0) == '-') {
286         ret = true;
287         s = substring(s, 1, -1);
288     }
289     bool expr_fail = false;
290     for (int i = 0, n = tokenize_console(s); i < n; ++i) {
291         int o;
292         string k, v;
293         s = argv(i);
294         #define X(expr) \
295             if (expr) \
296                 continue; \
297             expr_fail = true; \
298             break;
299
300         #define BINOP(op, len, expr) \
301             if ((o = strstrofs(s, op, 0)) >= 0) { \
302                 k = substring(s, 0, o); \
303                 v = substring(s, o + len, -1); \
304                 X(expr); \
305             }
306         BINOP(">=", 2, cvar(k) >= stof(v));
307         BINOP("<=", 2, cvar(k) <= stof(v));
308         BINOP(">",  1, cvar(k) >  stof(v));
309         BINOP("<",  1, cvar(k) <  stof(v));
310         BINOP("==", 2, cvar(k) == stof(v));
311         BINOP("!=", 2, cvar(k) != stof(v));
312         BINOP("===", 3, cvar_string(k) == v);
313         BINOP("!==", 3, cvar_string(k) != v);
314         {
315             k = s;
316             bool b = true;
317             if (str2chr(k, 0) == '!') {
318                 k = substring(s, 1, -1);
319                 b = false;
320             }
321             float f = stof(k);
322             bool isnum = ftos(f) == k;
323             X(boolean(isnum ? f : cvar(k)) == b);
324         }
325         #undef BINOP
326         #undef X
327     }
328     if (!expr_fail) {
329         ret = !ret;
330     }
331     // now ret is true if we want to keep the item, and false if we want to get rid of it
332     return ret;
333 }
334
335 void SV_OnEntityPreSpawnFunction(entity this)
336 {
337         if (this)
338         if (this.gametypefilter != "")
339         if (!isGametypeInFilter(MapInfo_LoadedGametype, teamplay, have_team_spawns, this.gametypefilter))
340         {
341                 delete(this);
342                 return;
343         }
344         if (this.cvarfilter != "" && !expr_evaluate(this.cvarfilter)) {
345                 delete(this);
346                 return;
347         }
348
349         if (DoesQ3ARemoveThisEntity(this)) {
350                 delete(this);
351                 return;
352         }
353
354         set_movetype(this, this.movetype);
355
356         if (this.monster_attack) {
357                 IL_PUSH(g_monster_targets, this);
358     }
359
360         // support special -1 and -2 angle from radiant
361         if (this.angles == '0 -1 0') {
362                 this.angles = '-90 0 0';
363         } else if (this.angles == '0 -2 0') {
364                 this.angles = '+90 0 0';
365     }
366
367     #define X(out, in) MACRO_BEGIN \
368         if (in != 0) { out = out + (random() * 2 - 1) * in; } \
369     MACRO_END
370     X(this.origin.x, this.originjitter.x); X(this.origin.y, this.originjitter.y); X(this.origin.z, this.originjitter.z);
371     X(this.angles.x, this.anglesjitter.x); X(this.angles.y, this.anglesjitter.y); X(this.angles.z, this.anglesjitter.z);
372     X(this.angles.y, this.anglejitter);
373     #undef X
374
375         if (MUTATOR_CALLHOOK(OnEntityPreSpawn, this)) {
376                 delete(this);
377                 return;
378         }
379 }
380
381 void WarpZone_PostInitialize_Callback()
382 {
383         // create waypoint links for warpzones
384         entity tracetest_ent = spawn();
385         setsize(tracetest_ent, PL_MIN_CONST, PL_MAX_CONST);
386         tracetest_ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
387         //for(entity e = warpzone_first; e; e = e.warpzone_next)
388         for(entity e = NULL; (e = find(e, classname, "trigger_warpzone")); )
389                 waypoint_spawnforteleporter_wz(e, tracetest_ent);
390         delete(tracetest_ent);
391 }
392
393 /*
394 ==================
395 main
396
397 unused but required by the engine
398 ==================
399 */
400 void main ()
401 {
402
403 }