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