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