]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/sv_main.qc
Globalsound: assert source is a player
[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/debug.qh"
17 #include "../common/mapinfo.qh"
18 #include "../common/util.qh"
19
20 #include "../common/vehicles/all.qh"
21 #include "../common/weapons/all.qh"
22
23 #include "../lib/csqcmodel/sv_model.qh"
24
25 #include "../lib/warpzone/common.qh"
26 #include "../lib/warpzone/server.qh"
27
28 .float lastground;
29 .int state;
30
31 void CreatureFrame ()
32 {SELFPARAM();
33         float dm;
34
35         for(entity e = world; (e = findfloat(e, damagedbycontents, true)); )
36         {
37                 setself(e);
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(self, 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.m_id, 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.m_id, 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.m_id, 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, SND_LAVA, 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.m_id, self.origin, '0 0 0');
97                                                 if(autocvar_g_balance_contents_playerdamage_lava_burn)
98                                                         Fire_AddDamage(self, world, autocvar_g_balance_contents_playerdamage_lava_burn * self.waterlevel, autocvar_g_balance_contents_playerdamage_lava_burn_time * self.waterlevel, DEATH_LAVA.m_id);
99                                         }
100                                         else if (self.watertype == CONTENT_SLIME)
101                                         {
102                                                 if (self.watersound_finished < time)
103                                                 {
104                                                         self.watersound_finished = time + 0.5;
105                                                         sound (self, CH_PLAYER_SINGLE, SND_SLIME, VOL_BASE, ATTEN_NORM);
106                                                 }
107                                                 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');
108                                         }
109                                 }
110                         }
111                 }
112                 else
113                 {
114                         if (self.flags & FL_INWATER)
115                         {
116                                 // play leave water sound
117                                 self.flags &= ~FL_INWATER;
118                                 self.dmgtime = 0;
119                         }
120                         self.air_finished = time + 12;
121                         self.dmg = 2;
122                 }
123
124                 if(!vehic && !projectile) // vehicles don't get falling damage
125                 {
126                         // check for falling damage
127                         float velocity_len = vlen(self.velocity);
128                         if(!self.hook.state)
129                         {
130                                 dm = vlen(self.oldvelocity) - velocity_len; // dm is now the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
131                                 if (self.deadflag)
132                                         dm = (dm - autocvar_g_balance_falldamage_deadminspeed) * autocvar_g_balance_falldamage_factor;
133                                 else
134                                         dm = min((dm - autocvar_g_balance_falldamage_minspeed) * autocvar_g_balance_falldamage_factor, autocvar_g_balance_falldamage_maxdamage);
135                                 if (dm > 0)
136                                         Damage (self, world, world, dm, DEATH_FALL.m_id, self.origin, '0 0 0');
137                         }
138
139                         if(autocvar_g_maxspeed > 0 && velocity_len > autocvar_g_maxspeed)
140                                 Damage (self, world, world, 100000, DEATH_SHOOTING_STAR.m_id, self.origin, '0 0 0');
141                         // play stupid sounds
142                         if (g_footsteps)
143                         if (!gameover)
144                         if (self.flags & FL_ONGROUND)
145                         if (!self.crouch)
146                         if (velocity_len > autocvar_sv_maxspeed * 0.6)
147                         if (!self.deadflag)
148                         if (time < self.lastground + 0.2)
149                         {
150                                 if((time > self.nextstep) || (time < (self.nextstep - 10.0)))
151                                 {
152                                         self.nextstep = time + 0.3 + random() * 0.1;
153                                         trace_dphitq3surfaceflags = 0;
154                                         tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
155                                         /*
156                                         if(trace_fraction == 1)
157                                                 dprint("nohit\n");
158                                         else
159                                                 dprint(ftos(trace_dphitq3surfaceflags), "\n");
160                                         */
161                                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
162                                         {
163                                                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
164                                                         GlobalSound(self, GS_STEP_METAL, CH_PLAYER, VOICETYPE_PLAYERSOUND);
165                                                 else
166                                                         GlobalSound(self, GS_STEP, CH_PLAYER, VOICETYPE_PLAYERSOUND);
167                                         }
168                                 }
169                         }
170                 }
171
172         self.oldvelocity = self.velocity;
173         }
174         setself(this);
175 }
176
177
178 /*
179 =============
180 StartFrame
181
182 Called before each frame by the server
183 =============
184 */
185
186 float game_delay;
187 float game_delay_last;
188
189 float RedirectionThink();
190 void StartFrame()
191 {
192         SELFPARAM();
193         execute_next_frame();
194
195         remove = remove_unsafely; // not during spawning!
196         serverprevtime = servertime;
197         servertime = time;
198         serverframetime = frametime;
199
200 #ifdef PROFILING
201         if(time > client_cefc_accumulatortime + 1)
202         {
203                 float t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
204                 LOG_INFO("CEFC time: ", ftos(t * 1000), "ms; ");
205                 int c_seeing = 0;
206                 int c_seen = 0;
207                 entity cl;
208                 FOR_EACH_CLIENT(cl)
209                 {
210                         if(IS_REAL_CLIENT(cl))
211                                 ++c_seeing;
212                         if(IS_PLAYER(cl))
213                                 ++c_seen;
214                 }
215                 LOG_INFO("CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ");
216                 LOG_INFO("CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0'), "\n");
217
218                 client_cefc_accumulatortime = time;
219                 client_cefc_accumulator = 0;
220         }
221 #endif
222
223         for(entity e = world; (e = findfloat(e, csqcprojectile_clientanimate, 1)); )
224                 CSQCProjectile_Check(e);
225
226         if(RedirectionThink())
227                 return;
228
229         UncustomizeEntitiesRun();
230         InitializeEntitiesRun();
231
232         WarpZone_StartFrame();
233
234         sys_frametime = autocvar_sys_ticrate * autocvar_slowmo;
235         if(sys_frametime <= 0)
236                 sys_frametime = 1.0 / 60.0; // somewhat safe fallback
237
238         if (timeout_status == TIMEOUT_LEADTIME) // just before the timeout (when timeout_status will be TIMEOUT_ACTIVE)
239                 orig_slowmo = autocvar_slowmo; // slowmo will be restored after the timeout
240
241         skill = autocvar_skill;
242
243         // detect when the pre-game countdown (if any) has ended and the game has started
244         game_delay = (time < game_starttime);
245
246         if(autocvar_sv_eventlog && game_delay_last && !game_delay)
247                 GameLogEcho(":startdelay_ended");
248
249         game_delay_last = game_delay;
250
251         CreatureFrame();
252         CheckRules_World();
253
254         // if in warmup stage and limit for warmup is hit start match
255         if(warmup_stage)
256         if(!gameover)
257         if((g_warmup_limit > 0 && time >= g_warmup_limit)
258          || (g_warmup_limit == 0 && autocvar_timelimit != 0 && time >= autocvar_timelimit * 60))
259         {
260                 ReadyRestart();
261                 return;
262         }
263
264         bot_serverframe();
265         anticheat_startframe();
266         MUTATOR_CALLHOOK(SV_StartFrame);
267         {
268         entity e;
269         FOR_EACH_CLIENT(e)
270         {
271             GlobalStats_update(e);
272         }
273     }
274 }
275
276 .vector originjitter;
277 .vector anglesjitter;
278 .float anglejitter;
279 .string gametypefilter;
280 .string cvarfilter;
281 float DoesQ3ARemoveThisEntity();
282 void SV_OnEntityPreSpawnFunction()
283 {SELFPARAM();
284         if (self)
285         if (self.gametypefilter != "")
286         if (!isGametypeInFilter(MapInfo_LoadedGametype, teamplay, have_team_spawns, self.gametypefilter))
287         {
288                 remove(self);
289                 return;
290         }
291         if(self.cvarfilter != "")
292         {
293                 float n, i, o, inv;
294                 string s, k, v;
295                 inv = 0;
296
297                 s = self.cvarfilter;
298                 if(substring(s, 0, 1) == "+")
299                 {
300                         s = substring(s, 1, -1);
301                 }
302                 else if(substring(s, 0, 1) == "-")
303                 {
304                         inv = 1;
305                         s = substring(s, 1, -1);
306                 }
307
308                 n = tokenize_console(s);
309                 for(i = 0; i < n; ++i)
310                 {
311                         s = argv(i);
312                         // syntax:
313                         // var>x
314                         // var<x
315                         // var>=x
316                         // var<=x
317                         // var==x
318                         // var!=x
319                         // var===x
320                         // var!==x
321                         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+2, -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+1, -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(k) == stof(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((o = strstrofs(s, "!==", 0)) >= 0)
371                         {
372                                 k = substring(s, 0, o);
373                                 v = substring(s, o+2, -1);
374                                 if(cvar_string(k) == v)
375                                         goto cvar_fail;
376                         }
377                         else if(substring(s, 0, 1) == "!")
378                         {
379                                 k = substring(s, 1, -1);
380                                 if(cvar(k))
381                                         goto cvar_fail;
382                         }
383                         else
384                         {
385                                 k = s;
386                                 if (!cvar(k))
387                                         goto cvar_fail;
388                         }
389                 }
390                 inv = !inv;
391 :cvar_fail
392                 // now inv is 1 if we want to keep the item, and 0 if we want to get rid of it
393                 if (!inv)
394                 {
395                         //print("cvarfilter fail\n");
396                         remove(self);
397                         return;
398                 }
399         }
400
401         if(DoesQ3ARemoveThisEntity())
402         {
403                 remove(self);
404                 return;
405         }
406
407         // support special -1 and -2 angle from radiant
408         if (self.angles == '0 -1 0')
409                 self.angles = '-90 0 0';
410         else if (self.angles == '0 -2 0')
411                 self.angles = '+90 0 0';
412
413         if(self.originjitter.x != 0)
414                 self.origin_x = self.origin.x + (random() * 2 - 1) * self.originjitter.x;
415         if(self.originjitter.y != 0)
416                 self.origin_y = self.origin.y + (random() * 2 - 1) * self.originjitter.y;
417         if(self.originjitter.z != 0)
418                 self.origin_z = self.origin.z + (random() * 2 - 1) * self.originjitter.z;
419         if(self.anglesjitter.x != 0)
420                 self.angles_x = self.angles.x + (random() * 2 - 1) * self.anglesjitter.x;
421         if(self.anglesjitter.y != 0)
422                 self.angles_y = self.angles.y + (random() * 2 - 1) * self.anglesjitter.y;
423         if(self.anglesjitter.z != 0)
424                 self.angles_z = self.angles.z + (random() * 2 - 1) * self.anglesjitter.z;
425         if(self.anglejitter != 0)
426                 self.angles_y = self.angles.y + (random() * 2 - 1) * self.anglejitter;
427
428         if(MUTATOR_CALLHOOK(OnEntityPreSpawn))
429         {
430                 remove(self);
431                 return;
432         }
433 }
434
435 void WarpZone_PostInitialize_Callback()
436 {
437         // create waypoint links for warpzones
438         entity e;
439         for(e = world; (e = find(e, classname, "trigger_warpzone")); )
440         {
441                 vector src, dst;
442                 src = (e.absmin + e.absmax) * 0.5;
443                 makevectors(e.warpzone_angles);
444                 src = src + ((e.warpzone_origin - src) * v_forward) * v_forward + 16 * v_right;
445                 dst = (e.enemy.absmin + e.enemy.absmax) * 0.5;
446                 makevectors(e.enemy.warpzone_angles);
447                 dst = dst + ((e.enemy.warpzone_origin - dst) * v_forward) * v_forward - 16 * v_right;
448                 waypoint_spawnforteleporter_v(e, src, dst, 0);
449         }
450 }