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