Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / sv_main.qc
1 #include "_all.qh"
2
3 #include "anticheat.qh"
4 #include "g_hook.qh"
5 #include "g_world.qh"
6
7 #include "bot/bot.qh"
8 #include "bot/waypoints.qh"
9
10 #include "command/common.qh"
11
12 #include "mutators/mutators_include.qh"
13 #include "weapons/csqcprojectile.qh"
14
15 #include "../common/constants.qh"
16 #include "../common/deathtypes.qh"
17 #include "../common/mapinfo.qh"
18 #include "../common/util.qh"
19
20 #include "../common/vehicles/sv_vehicles.qh"
21 #include "../common/weapons/all.qh"
22
23 #include "../csqcmodellib/sv_model.qh"
24
25 #include "../warpzonelib/common.qh"
26 #include "../warpzonelib/server.qh"
27
28 .float lastground;
29
30 void CreatureFrame (void)
31 {
32         entity oldself;
33         float dm;
34
35         oldself = self;
36         for(self = world; (self = findfloat(self, damagedbycontents, true)); )
37         {
38                 if (self.movetype == MOVETYPE_NOCLIP) { continue; }
39
40                 float vehic = IS_VEHICLE(self);
41                 float projectile = (self.flags & FL_PROJECTILE);
42                 float monster = IS_MONSTER(self);
43
44                 if (self.watertype <= CONTENT_WATER && self.waterlevel > 0) // workaround a retarded bug made by id software :P (yes, it's that old of a bug)
45                 {
46                         if (!(self.flags & FL_INWATER))
47                         {
48                                 self.flags |= FL_INWATER;
49                                 self.dmgtime = 0;
50                         }
51
52                         if(!vehic && !projectile && !monster) // vehicles, monsters and projectiles don't drown
53                         {
54                                 if (self.waterlevel != WATERLEVEL_SUBMERGED)
55                                 {
56                                         if(self.air_finished < time)
57                                                 PlayerSound(playersound_gasp, CH_PLAYER, VOICETYPE_PLAYERSOUND);
58                                         self.air_finished = time + autocvar_g_balance_contents_drowndelay;
59                                         self.dmg = 2;
60                                 }
61                                 else if (self.air_finished < time)
62                                 {       // drown!
63                                         if (!self.deadflag)
64                                         if (self.pain_finished < time)
65                                         {
66                                                 Damage (self, world, world, autocvar_g_balance_contents_playerdamage_drowning * autocvar_g_balance_contents_damagerate, DEATH_DROWN, self.origin, '0 0 0');
67                                                 self.pain_finished = time + 0.5;
68                                         }
69                                 }
70                         }
71
72                         if (self.dmgtime < time)
73                         {
74                                 self.dmgtime = time + autocvar_g_balance_contents_damagerate;
75
76                                 if (projectile)
77                                 {
78                                         if (self.watertype == CONTENT_LAVA)
79                                         {
80                                                 Damage (self, world, world, autocvar_g_balance_contents_projectiledamage * autocvar_g_balance_contents_damagerate * self.waterlevel, DEATH_LAVA, self.origin, '0 0 0');
81                                         }
82                                         else if (self.watertype == CONTENT_SLIME)
83                                         {
84                                                 Damage (self, world, world, autocvar_g_balance_contents_projectiledamage * autocvar_g_balance_contents_damagerate * self.waterlevel, DEATH_SLIME, self.origin, '0 0 0');
85                                         }
86                                 }
87                                 else
88                                 {
89                                         if (self.watertype == CONTENT_LAVA)
90                                         {
91                                                 if (self.watersound_finished < time)
92                                                 {
93                                                         self.watersound_finished = time + 0.5;
94                                                         sound (self, CH_PLAYER_SINGLE, "player/lava.wav", VOL_BASE, ATTEN_NORM);
95                                                 }
96                                                 Damage (self, world, world, autocvar_g_balance_contents_playerdamage_lava * autocvar_g_balance_contents_damagerate * self.waterlevel, DEATH_LAVA, self.origin, '0 0 0');
97                                         }
98                                         else if (self.watertype == CONTENT_SLIME)
99                                         {
100                                                 if (self.watersound_finished < time)
101                                                 {
102                                                         self.watersound_finished = time + 0.5;
103                                                         sound (self, CH_PLAYER_SINGLE, "player/slime.wav", VOL_BASE, ATTEN_NORM);
104                                                 }
105                                                 Damage (self, world, world, autocvar_g_balance_contents_playerdamage_slime * autocvar_g_balance_contents_damagerate * self.waterlevel, DEATH_SLIME, self.origin, '0 0 0');
106                                         }
107                                 }
108                         }
109                 }
110                 else
111                 {
112                         if (self.flags & FL_INWATER)
113                         {
114                                 // play leave water sound
115                                 self.flags &= ~FL_INWATER;
116                                 self.dmgtime = 0;
117                         }
118                         self.air_finished = time + 12;
119                         self.dmg = 2;
120                 }
121
122                 if(!vehic && !projectile) // vehicles don't get falling damage
123                 {
124                         // check for falling damage
125                         float velocity_len = vlen(self.velocity);
126                         if(!self.hook.state)
127                         {
128                                 dm = vlen(self.oldvelocity) - velocity_len; // dm is now the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
129                                 if (self.deadflag)
130                                         dm = (dm - autocvar_g_balance_falldamage_deadminspeed) * autocvar_g_balance_falldamage_factor;
131                                 else
132                                         dm = min((dm - autocvar_g_balance_falldamage_minspeed) * autocvar_g_balance_falldamage_factor, autocvar_g_balance_falldamage_maxdamage);
133                                 if (dm > 0)
134                                         Damage (self, world, world, dm, DEATH_FALL, self.origin, '0 0 0');
135                         }
136
137                         if(autocvar_g_maxspeed > 0 && velocity_len > autocvar_g_maxspeed)
138                                 Damage (self, world, world, 100000, DEATH_SHOOTING_STAR, self.origin, '0 0 0');
139                         // play stupid sounds
140                         if (g_footsteps)
141                         if (!gameover)
142                         if (self.flags & FL_ONGROUND)
143                         if (!self.crouch)
144                         if (velocity_len > autocvar_sv_maxspeed * 0.6)
145                         if (!self.deadflag)
146                         if (time < self.lastground + 0.2)
147                         {
148                                 if((time > self.nextstep) || (time < (self.nextstep - 10.0)))
149                                 {
150                                         self.nextstep = time + 0.3 + random() * 0.1;
151                                         trace_dphitq3surfaceflags = 0;
152                                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
153                                         /*
154                                         if(trace_fraction == 1)
155                                                 dprint("nohit\n");
156                                         else
157                                                 dprint(ftos(trace_dphitq3surfaceflags), "\n");
158                                         */
159                                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
160                                         {
161                                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
162                                                         GlobalSound(globalsound_metalstep, CH_PLAYER, VOICETYPE_PLAYERSOUND);
163                                                 else
164                                                         GlobalSound(globalsound_step, CH_PLAYER, VOICETYPE_PLAYERSOUND);
165                                         }
166                                 }
167                         }
168                 }
169
170         self.oldvelocity = self.velocity;
171         }
172         self = oldself;
173 }
174
175
176 /*
177 =============
178 StartFrame
179
180 Called before each frame by the server
181 =============
182 */
183
184 float game_delay;
185 float game_delay_last;
186
187 float RedirectionThink();
188 void StartFrame (void)
189 {
190         execute_next_frame();
191
192         remove = remove_unsafely; // not during spawning!
193         serverprevtime = servertime;
194         servertime = time;
195         serverframetime = frametime;
196
197 #ifdef PROFILING
198         if(time > client_cefc_accumulatortime + 1)
199         {
200                 float t, pp, c_seeing, c_seen;
201                 entity cl;
202                 t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
203                 print("CEFC time: ", ftos(t * 1000), "ms; ");
204                 c_seeing = 0;
205                 c_seen = 0;
206                 FOR_EACH_CLIENT(cl)
207                 {
208                         if(IS_REAL_CLIENT(cl))
209                                 ++c_seeing;
210                         if(IS_PLAYER(cl))
211                                 ++c_seen;
212                 }
213                 print("CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ");
214                 print("CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0'), "\n");
215
216                 client_cefc_accumulatortime = time;
217                 client_cefc_accumulator = 0;
218         }
219 #endif
220
221         entity e;
222         for(e = world; (e = findfloat(e, csqcprojectile_clientanimate, 1)); )
223                 CSQCProjectile_Check(e);
224
225         if(RedirectionThink())
226                 return;
227
228         UncustomizeEntitiesRun();
229         InitializeEntitiesRun();
230
231         WarpZone_StartFrame();
232
233         sys_frametime = autocvar_sys_ticrate * autocvar_slowmo;
234         if(sys_frametime <= 0)
235                 sys_frametime = 1.0 / 60.0; // somewhat safe fallback
236
237         if (timeout_status == TIMEOUT_LEADTIME) // just before the timeout (when timeout_status will be TIMEOUT_ACTIVE)
238                 orig_slowmo = autocvar_slowmo; // slowmo will be restored after the timeout
239
240         skill = autocvar_skill;
241
242         // detect when the pre-game countdown (if any) has ended and the game has started
243         game_delay = (time < game_starttime) ? true : false;
244
245         if(game_delay_last == true)
246         if(game_delay == false)
247         if(autocvar_sv_eventlog)
248                 GameLogEcho(":startdelay_ended");
249
250         game_delay_last = game_delay;
251
252         CreatureFrame ();
253         CheckRules_World ();
254
255         // if in warmup stage and limit for warmup is hit start match
256         if(warmup_stage)
257         if(!gameover)
258         if((g_warmup_limit > 0 && time >= g_warmup_limit)
259          || (g_warmup_limit == 0 && autocvar_timelimit != 0 && time >= autocvar_timelimit * 60))
260         {
261                 ReadyRestart();
262                 return;
263         }
264
265         bot_serverframe();
266
267         FOR_EACH_PLAYER(self)
268                 self.porto_forbidden = max(0, self.porto_forbidden - 1);
269
270         anticheat_startframe();
271
272         MUTATOR_CALLHOOK(SV_StartFrame);
273 }
274
275 .vector originjitter;
276 .vector anglesjitter;
277 .float anglejitter;
278 .string gametypefilter;
279 .string cvarfilter;
280 float DoesQ3ARemoveThisEntity();
281 void SV_OnEntityPreSpawnFunction()
282 {
283         if (self)
284         if (self.gametypefilter != "")
285         if (!isGametypeInFilter(MapInfo_LoadedGametype, teamplay, have_team_spawns, self.gametypefilter))
286         {
287                 remove(self);
288                 return;
289         }
290         if(self.cvarfilter != "")
291         {
292                 float n, i, o, inv;
293                 string s, k, v;
294                 inv = 0;
295
296                 s = self.cvarfilter;
297                 if(substring(s, 0, 1) == "+")
298                 {
299                         s = substring(s, 1, -1);
300                 }
301                 else if(substring(s, 0, 1) == "-")
302                 {
303                         inv = 1;
304                         s = substring(s, 1, -1);
305                 }
306
307                 n = tokenize_console(s);
308                 for(i = 0; i < n; ++i)
309                 {
310                         s = argv(i);
311                         // syntax:
312                         // var>x
313                         // var<x
314                         // var>=x
315                         // var<=x
316                         // var==x
317                         // var!=x
318                         // var===x
319                         // var!==x
320                         if((o = strstrofs(s, ">=", 0)) >= 0)
321                         {
322                                 k = substring(s, 0, o);
323                                 v = substring(s, o+2, -1);
324                                 if(cvar(k) < stof(v))
325                                         goto cvar_fail;
326                         }
327                         else if((o = strstrofs(s, "<=", 0)) >= 0)
328                         {
329                                 k = substring(s, 0, o);
330                                 v = substring(s, o+2, -1);
331                                 if(cvar(k) > stof(v))
332                                         goto cvar_fail;
333                         }
334                         else if((o = strstrofs(s, ">", 0)) >= 0)
335                         {
336                                 k = substring(s, 0, o);
337                                 v = substring(s, o+1, -1);
338                                 if(cvar(k) <= stof(v))
339                                         goto cvar_fail;
340                         }
341                         else if((o = strstrofs(s, "<", 0)) >= 0)
342                         {
343                                 k = substring(s, 0, o);
344                                 v = substring(s, o+1, -1);
345                                 if(cvar(k) >= stof(v))
346                                         goto cvar_fail;
347                         }
348                         else if((o = strstrofs(s, "==", 0)) >= 0)
349                         {
350                                 k = substring(s, 0, o);
351                                 v = substring(s, o+2, -1);
352                                 if(cvar(k) != stof(v))
353                                         goto cvar_fail;
354                         }
355                         else if((o = strstrofs(s, "!=", 0)) >= 0)
356                         {
357                                 k = substring(s, 0, o);
358                                 v = substring(s, o+2, -1);
359                                 if(cvar(k) == stof(v))
360                                         goto cvar_fail;
361                         }
362                         else if((o = strstrofs(s, "===", 0)) >= 0)
363                         {
364                                 k = substring(s, 0, o);
365                                 v = substring(s, o+2, -1);
366                                 if(cvar_string(k) != v)
367                                         goto cvar_fail;
368                         }
369                         else if((o = strstrofs(s, "!==", 0)) >= 0)
370                         {
371                                 k = substring(s, 0, o);
372                                 v = substring(s, o+2, -1);
373                                 if(cvar_string(k) == v)
374                                         goto cvar_fail;
375                         }
376                         else if(substring(s, 0, 1) == "!")
377                         {
378                                 k = substring(s, 1, -1);
379                                 if(cvar(k))
380                                         goto cvar_fail;
381                         }
382                         else
383                         {
384                                 k = s;
385                                 if (!cvar(k))
386                                         goto cvar_fail;
387                         }
388                 }
389                 inv = !inv;
390 :cvar_fail
391                 // now inv is 1 if we want to keep the item, and 0 if we want to get rid of it
392                 if (!inv)
393                 {
394                         //print("cvarfilter fail\n");
395                         remove(self);
396                         return;
397                 }
398         }
399
400         if(DoesQ3ARemoveThisEntity())
401         {
402                 remove(self);
403                 return;
404         }
405
406         // support special -1 and -2 angle from radiant
407         if (self.angles == '0 -1 0')
408                 self.angles = '-90 0 0';
409         else if (self.angles == '0 -2 0')
410                 self.angles = '+90 0 0';
411
412         if(self.originjitter.x != 0)
413                 self.origin_x = self.origin.x + (random() * 2 - 1) * self.originjitter.x;
414         if(self.originjitter.y != 0)
415                 self.origin_y = self.origin.y + (random() * 2 - 1) * self.originjitter.y;
416         if(self.originjitter.z != 0)
417                 self.origin_z = self.origin.z + (random() * 2 - 1) * self.originjitter.z;
418         if(self.anglesjitter.x != 0)
419                 self.angles_x = self.angles.x + (random() * 2 - 1) * self.anglesjitter.x;
420         if(self.anglesjitter.y != 0)
421                 self.angles_y = self.angles.y + (random() * 2 - 1) * self.anglesjitter.y;
422         if(self.anglesjitter.z != 0)
423                 self.angles_z = self.angles.z + (random() * 2 - 1) * self.anglesjitter.z;
424         if(self.anglejitter != 0)
425                 self.angles_y = self.angles.y + (random() * 2 - 1) * self.anglejitter;
426
427         if(MUTATOR_CALLHOOK(OnEntityPreSpawn))
428         {
429                 remove(self);
430                 return;
431         }
432 }
433
434 void WarpZone_PostInitialize_Callback(void)
435 {
436         // create waypoint links for warpzones
437         entity e;
438         for(e = world; (e = find(e, classname, "trigger_warpzone")); )
439         {
440                 vector src, dst;
441                 src = (e.absmin + e.absmax) * 0.5;
442                 makevectors(e.warpzone_angles);
443                 src = src + ((e.warpzone_origin - src) * v_forward) * v_forward + 16 * v_right;
444                 dst = (e.enemy.absmin + e.enemy.absmax) * 0.5;
445                 makevectors(e.enemy.warpzone_angles);
446                 dst = dst + ((e.enemy.warpzone_origin - dst) * v_forward) * v_forward - 16 * v_right;
447                 waypoint_spawnforteleporter_v(e, src, dst, 0);
448         }
449 }