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