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