a5f35aa89ae3bc04a0e8d51d9ab5587dea084c1d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / sv_main.qc
1
2 void CreatureFrame (void)
3 {
4         local entity oldself;
5         local float dm, maxspeed;
6         oldself = self;
7         self = findfloat(world, iscreature, TRUE);
8         while (self)
9         {
10                 if (self.movetype != MOVETYPE_NOCLIP)
11                 {
12                         if (self.waterlevel)
13                         {
14                                 if (!(self.flags & FL_INWATER))
15                                 {
16                                         self.flags |= FL_INWATER;
17                                         self.dmgtime = 0;
18                                 }
19                                 if (self.waterlevel != WATERLEVEL_SUBMERGED)
20                                 {
21                                         if(self.air_finished < time + 9)
22                                                 PlayerSound(playersound_gasp, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
23                                         self.air_finished = time + 12;
24                                         self.dmg = 2;
25                                 }
26                                 else if (self.air_finished < time)
27                                 {       // drown!
28                                         if (!self.deadflag)
29                                         if (self.pain_finished < time)
30                                         {
31                                                 Damage (self, world, world, 5, DEATH_DROWN, self.origin, '0 0 0');
32                                                 self.pain_finished = time + 0.5;
33                                         }
34                                 }
35                                 if (self.dmgtime < time)
36                                 {
37                                         self.dmgtime = time + 0.2;
38                                         if (self.watertype == CONTENT_LAVA)
39                                         {
40                                                 if (self.watersound_finished < time)
41                                                 {
42                                                         self.watersound_finished = time + 0.5;
43                                                         sound (self, CHAN_PLAYER, "player/lava.wav", VOL_BASE, ATTN_NORM);
44                                                 }
45                                                 Damage (self, world, world, 6 * self.waterlevel, DEATH_LAVA, self.origin, '0 0 0');
46                                         }
47                                         else if (self.watertype == CONTENT_SLIME)
48                                         {
49                                                 if (self.watersound_finished < time)
50                                                 {
51                                                         self.watersound_finished = time + 0.5;
52                                                         sound (self, CHAN_PLAYER, "player/slime.wav", VOL_BASE, ATTN_NORM);
53                                                 }
54                                                 Damage (self, world, world, 2 * self.waterlevel, DEATH_SLIME, self.origin, '0 0 0');
55                                         }
56                                 }
57                         }
58                         else
59                         {
60                                 if (self.flags & FL_INWATER)
61                                 {
62                                         // play leave water sound
63                                         self.flags &~= FL_INWATER;
64                                         self.dmgtime = 0;
65                                 }
66                                 self.air_finished = time + 12;
67                                 self.dmg = 2;
68                         }
69                         // check for falling damage
70                         if(!self.hook.state && !g_ca && !(g_cts && !cvar("g_cts_selfdamage")))
71                         {
72                                 dm = vlen(self.oldvelocity) - vlen(self.velocity); // dm is now the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
73                                 if (self.deadflag)
74                                         dm = (dm - cvar("g_balance_falldamage_deadminspeed")) * cvar("g_balance_falldamage_factor");
75                                 else
76                                         dm = min((dm - cvar("g_balance_falldamage_minspeed")) * cvar("g_balance_falldamage_factor"), cvar("g_balance_falldamage_maxdamage"));
77                                 if (dm > 0)
78                                         Damage (self, world, world, dm, DEATH_FALL, self.origin, '0 0 0');
79                         }
80
81                         maxspeed = cvar("g_maxspeed");
82                         if(maxspeed > 0 && vlen(self.velocity) > maxspeed)
83                                 Damage (self, world, world, 100000, DEATH_SHOOTING_STAR, self.origin, '0 0 0');
84
85                         // play stupid sounds
86                         if (g_footsteps)
87                         if (!gameover)
88                         if (self.flags & FL_ONGROUND)
89                         if (vlen(self.velocity) > sv_maxspeed * 0.6)
90                         if (!self.deadflag)
91                         if (time < self.lastground + 0.2)
92                         {
93                                 if((time > self.nextstep) || (time < (self.nextstep - 10.0)))
94                                 {
95                                         self.nextstep = time + 0.3 + random() * 0.1;
96                                         trace_dphitq3surfaceflags = 0;
97                                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
98                                         /*
99                                         if(trace_fraction == 1)
100                                                 dprint("nohit\n");
101                                         else
102                                                 dprint(ftos(trace_dphitq3surfaceflags), "\n");
103                                         */
104                                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS)
105                                         {
106                                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
107                                                         GlobalSound(globalsound_metalstep, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
108                                                 else
109                                                         GlobalSound(globalsound_step, CHAN_PLAYER, VOICETYPE_PLAYERSOUND);
110                                         }
111                                 }
112                         }
113                         self.oldvelocity = self.velocity;
114                 }
115                 self = findfloat(self, iscreature, TRUE);
116         }
117         self = oldself;
118 }
119
120
121 /*
122 =============
123 StartFrame
124
125 Called before each frame by the server
126 =============
127 */
128
129 float game_delay;
130 float game_delay_last;
131
132 void RuneMatchGivePoints();
133 float RedirectionThink();
134 entity SelectSpawnPoint (float anypoint);
135 void StartFrame (void)
136 {
137         remove = remove_unsafely; // not during spawning!
138         serverprevtime = servertime;
139         servertime = time;
140         serverframetime = frametime;
141
142 #ifdef PROFILING
143         if(time > client_cefc_accumulatortime + 1)
144         {
145                 float t, pp, c_seeing, c_seen;
146                 entity cl;
147                 t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
148                 print("CEFC time: ", ftos(t * 1000), "ms; ");
149                 c_seeing = 0;
150                 c_seen = 0;
151                 FOR_EACH_CLIENT(cl)
152                 {
153                         if(clienttype(cl) == CLIENTTYPE_REAL)
154                                 ++c_seeing;
155                         if(cl.classname == "player")
156                                 ++c_seen;
157                 }
158                 print("CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ");
159                 print("CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0'), "\n");
160
161                 client_cefc_accumulatortime = time;
162                 client_cefc_accumulator = 0;
163         }
164 #endif
165
166         entity e;
167         for(e = world; (e = findfloat(e, csqcprojectile_clientanimate, 1)); )
168                 CSQCProjectile_Check(e);
169
170         if(RedirectionThink())
171                 return;
172
173         UncustomizeEntitiesRun();
174         InitializeEntitiesRun();
175
176         WarpZone_StartFrame();
177
178         sv_gravity = cvar("sv_gravity");
179         sv_maxairspeed = cvar("sv_maxairspeed");
180         sv_maxspeed = cvar ("sv_maxspeed");
181         sv_friction = cvar ("sv_friction");
182         sv_accelerate = cvar ("sv_accelerate");
183         sv_airaccelerate = cvar("sv_airaccelerate");
184         sv_airaccel_sideways_friction = cvar("sv_airaccel_sideways_friction");
185         sv_airaccel_qw = cvar("sv_airaccel_qw");
186         sv_stopspeed = cvar ("sv_stopspeed");
187         sv_airstopaccelerate = cvar("sv_airstopaccelerate");
188         sv_airstrafeaccelerate = cvar("sv_airstrafeaccelerate");
189         sv_maxairstrafespeed = cvar("sv_maxairstrafespeed");
190         sv_airstrafeaccel_qw = cvar("sv_airstrafeaccel_qw");
191         sv_aircontrol = cvar("sv_aircontrol");
192         sv_aircontrol_penalty = cvar("sv_aircontrol_penalty");
193         sv_aircontrol_power = cvar("sv_aircontrol_power");
194         sv_warsowbunny_airforwardaccel = cvar("sv_warsowbunny_airforwardaccel");
195         sv_warsowbunny_accel = cvar("sv_warsowbunny_accel");
196         sv_warsowbunny_topspeed = cvar("sv_warsowbunny_topspeed");
197         sv_warsowbunny_turnaccel = cvar("sv_warsowbunny_turnaccel");
198         sv_warsowbunny_backtosideratio = cvar("sv_warsowbunny_backtosideratio");
199         sv_airspeedlimit_nonqw = cvar("sv_airspeedlimit_nonqw");
200         teamplay = cvar ("teamplay");
201         sys_frametime = cvar("sys_ticrate") * cvar("slowmo");
202         if(sys_frametime <= 0)
203                 sys_frametime = 1.0 / 60.0; // somewhat safe fallback
204         sv_doublejump = cvar("sv_doublejump");
205
206         if (timeoutStatus == 1) // just before the timeout (when timeoutStatus will be 2)
207                 orig_slowmo = cvar("slowmo"); // slowmo will be restored after the timeout
208
209         if(teams_matter && !teamplay)
210                 teamplay = 3;
211         if(!teams_matter && teamplay)
212                 teamplay = 0;
213
214         skill = cvar("skill");
215
216         Spawnqueue_Check();
217
218
219         // detect when the pre-game countdown (if any) has ended and the game has started
220         game_delay = (time < game_starttime) ? TRUE : FALSE;
221
222         if(game_delay_last == TRUE)
223         if(game_delay == FALSE)
224         if(cvar("sv_eventlog"))
225                         GameLogEcho(":startdelay_ended");
226
227         game_delay_last = game_delay;
228
229         // if in warmup stage and limit for warmup is hit start match
230         if (inWarmupStage)
231         if ((g_warmup_limit > 0 && time >= g_warmup_limit)
232          || (g_warmup_limit == 0 && cvar("timelimit") != 0 && time >= cvar("timelimit") * 60))
233         {
234                 ReadyRestart();
235                 return;
236         }
237
238         CreatureFrame ();
239         CheckRules_World ();
240
241         AuditTeams();
242
243         RuneMatchGivePoints();
244         bot_serverframe();
245
246         if(cvar("spawn_debugview"))
247         {
248                 RandomSelection_Init();
249                 for(self = world; (self = find(self, classname, "player")); )
250                         RandomSelection_Add(self, 0, string_null, 1, 0);
251                 self = RandomSelection_chosen_ent;
252                 SelectSpawnPoint(0);
253         }
254
255         FOR_EACH_PLAYER(self)
256                 self.porto_forbidden = max(0, self.porto_forbidden - 1);
257 }
258
259 .vector originjitter;
260 .vector anglesjitter;
261 .float anglejitter;
262 .string gametypefilter;
263 .string cvarfilter;
264 void SV_OnEntityPreSpawnFunction()
265 {
266         if(self.gametypefilter != "")
267         if not(isGametypeInFilter(game, teams_matter, self.gametypefilter))
268         {
269                 remove(self);
270                 return;
271         }
272         if(self.cvarfilter != "")
273         {
274                 float n, i, o, inv;
275                 string s, k, v;
276                 inv = 0;
277
278                 s = self.cvarfilter;
279                 if(substring(s, 0, 1) == "+")
280                 {
281                         s = substring(s, 1, -1);
282                 }
283                 else if(substring(s, 0, 1) == "-")
284                 {
285                         inv = 1;
286                         s = substring(s, 1, -1);
287                 }
288
289                 n = tokenize(s);
290                 for(i = 0; i < n; ++i)
291                 {
292                         s = argv(i);
293                         // syntax:
294                         // var>x
295                         // var<x
296                         // var>=x
297                         // var<=x
298                         // var==x
299                         // var!=x
300                         // var===x
301                         // var!==x
302                         if((o = strstrofs(s, ">=", 0)) >= 0)
303                         {
304                                 k = substring(s, 0, o);
305                                 v = substring(s, o+2, -1);
306                                 if(cvar(k) < stof(v))
307                                         goto cvar_fail;
308                         }
309                         else if((o = strstrofs(s, "<=", 0)) >= 0)
310                         {
311                                 k = substring(s, 0, o);
312                                 v = substring(s, o+2, -1);
313                                 if(cvar(k) > stof(v))
314                                         goto cvar_fail;
315                         }
316                         else if((o = strstrofs(s, ">", 0)) >= 0)
317                         {
318                                 k = substring(s, 0, o);
319                                 v = substring(s, o+1, -1);
320                                 if(cvar(k) <= stof(v))
321                                         goto cvar_fail;
322                         }
323                         else if((o = strstrofs(s, "<", 0)) >= 0)
324                         {
325                                 k = substring(s, 0, o);
326                                 v = substring(s, o+1, -1);
327                                 if(cvar(k) >= stof(v))
328                                         goto cvar_fail;
329                         }
330                         else if((o = strstrofs(s, "==", 0)) >= 0)
331                         {
332                                 k = substring(s, 0, o);
333                                 v = substring(s, o+2, -1);
334                                 if(cvar(k) != stof(v))
335                                         goto cvar_fail;
336                         }
337                         else if((o = strstrofs(s, "!=", 0)) >= 0)
338                         {
339                                 k = substring(s, 0, o);
340                                 v = substring(s, o+2, -1);
341                                 if(cvar(k) == stof(v))
342                                         goto cvar_fail;
343                         }
344                         else if((o = strstrofs(s, "===", 0)) >= 0)
345                         {
346                                 k = substring(s, 0, o);
347                                 v = substring(s, o+2, -1);
348                                 if(cvar_string(k) != v)
349                                         goto cvar_fail;
350                         }
351                         else if((o = strstrofs(s, "!==", 0)) >= 0)
352                         {
353                                 k = substring(s, 0, o);
354                                 v = substring(s, o+2, -1);
355                                 if(cvar_string(k) == v)
356                                         goto cvar_fail;
357                         }
358                         else if(substring(s, 0, 1) == "!")
359                         {
360                                 k = substring(s, 1, -1);
361                                 if(cvar(k))
362                                         goto cvar_fail;
363                         }
364                         else
365                         {
366                                 k = s;
367                                 if not(cvar(k))
368                                         goto cvar_fail;
369                         }
370                 }
371                 inv = !inv;
372 :cvar_fail
373                 // now inv is 1 if we want to keep the item, and 0 if we want to get rid of it
374                 if not(inv)
375                 {
376                         //print("cvarfilter fail\n");
377                         remove(self);
378                         return;
379                 }
380         }
381
382         // support special -1 and -2 angle from radiant
383         if (self.angles == '0 -1 0')
384                 self.angles = '-90 0 0';
385         else if (self.angles == '0 -2 0')
386                 self.angles = '+90 0 0';
387
388         if(self.originjitter_x != 0)
389                 self.origin_x = self.origin_x + (random() * 2 - 1) * self.originjitter_x;
390         if(self.originjitter_y != 0)
391                 self.origin_y = self.origin_y + (random() * 2 - 1) * self.originjitter_y;
392         if(self.originjitter_z != 0)
393                 self.origin_z = self.origin_z + (random() * 2 - 1) * self.originjitter_z;
394         if(self.anglesjitter_x != 0)
395                 self.angles_x = self.angles_x + (random() * 2 - 1) * self.anglesjitter_x;
396         if(self.anglesjitter_y != 0)
397                 self.angles_y = self.angles_y + (random() * 2 - 1) * self.anglesjitter_y;
398         if(self.anglesjitter_z != 0)
399                 self.angles_z = self.angles_z + (random() * 2 - 1) * self.anglesjitter_z;
400         if(self.anglejitter != 0)
401                 self.angles_y = self.angles_y + (random() * 2 - 1) * self.anglejitter;
402
403         if(MUTATOR_CALLHOOK(OnEntityPreSpawn))
404         {
405                 remove(self);
406                 return;
407         }
408 }