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