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