]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/main.qc
Merge branch 'master' into bones_was_here/q3compat
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / main.qc
1 #include "main.qh"
2
3 #include <common/command/generic.qh>
4 #include <common/constants.qh>
5 #include <common/deathtypes/all.qh>
6 #include <common/debug.qh>
7 #include <common/mapinfo.qh>
8 #include <common/monsters/sv_monsters.qh>
9 #include <common/util.qh>
10 #include <common/vehicles/all.qh>
11 #include <common/weapons/_all.qh>
12 #include <lib/csqcmodel/sv_model.qh>
13 #include <lib/warpzone/common.qh>
14 #include <lib/warpzone/server.qh>
15 #include <server/anticheat.qh>
16 #include <server/bot/api.qh>
17 #include <server/command/common.qh>
18 #include <server/compat/quake3.qh>
19 #include <server/damage.qh>
20 #include <server/gamelog.qh>
21 #include <server/hook.qh>
22 #include <server/ipban.qh>
23 #include <server/mutators/_mod.qh>
24 #include <server/spawnpoints.qh>
25 #include <server/weapons/common.qh>
26 #include <server/weapons/csqcprojectile.qh>
27 #include <server/world.qh>
28
29 void CreatureFrame_hotliquids(entity this)
30 {
31         if (this.contents_damagetime >= time)
32         {
33                 return;
34         }
35
36         this.contents_damagetime = time + autocvar_g_balance_contents_damagerate;
37
38         if (this.flags & FL_PROJECTILE)
39         {
40                 if (this.watertype == CONTENT_LAVA)
41                         Damage (this, NULL, NULL, autocvar_g_balance_contents_projectiledamage * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
42                 else if (this.watertype == CONTENT_SLIME)
43                         Damage (this, NULL, NULL, autocvar_g_balance_contents_projectiledamage * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
44         }
45         else
46         {
47                 if (STAT(FROZEN, this))
48                 {
49                         if (this.watertype == CONTENT_LAVA)
50                                 Damage(this, NULL, NULL, 10000, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
51                         else if (this.watertype == CONTENT_SLIME)
52                                 Damage(this, NULL, NULL, 10000, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
53                 }
54                 else if (this.watertype == CONTENT_LAVA)
55                 {
56                         if (this.watersound_finished < time)
57                         {
58                                 this.watersound_finished = time + 0.5;
59                                 sound (this, CH_PLAYER_SINGLE, SND_LAVA, VOL_BASE, ATTEN_NORM);
60                         }
61                         Damage (this, NULL, NULL, autocvar_g_balance_contents_playerdamage_lava * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
62                         if(autocvar_g_balance_contents_playerdamage_lava_burn)
63                                 Fire_AddDamage(this, NULL, autocvar_g_balance_contents_playerdamage_lava_burn * this.waterlevel, autocvar_g_balance_contents_playerdamage_lava_burn_time * this.waterlevel, DEATH_LAVA.m_id);
64                 }
65                 else if (this.watertype == CONTENT_SLIME)
66                 {
67                         if (this.watersound_finished < time)
68                         {
69                                 this.watersound_finished = time + 0.5;
70                                 sound (this, CH_PLAYER_SINGLE, SND_SLIME, VOL_BASE, ATTEN_NORM);
71                         }
72                         Damage (this, NULL, NULL, autocvar_g_balance_contents_playerdamage_slime * autocvar_g_balance_contents_damagerate * this.waterlevel, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
73                 }
74         }
75 }
76
77 void CreatureFrame_Liquids(entity this)
78 {
79         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)
80         {
81                 if (!(this.flags & FL_INWATER))
82                 {
83                         this.flags |= FL_INWATER;
84                         this.contents_damagetime = 0;
85                 }
86
87                 CreatureFrame_hotliquids(this);
88         }
89         else
90         {
91                 if (this.flags & FL_INWATER)
92                 {
93                         // play leave water sound
94                         this.flags &= ~FL_INWATER;
95                         this.contents_damagetime = 0;
96                 }
97         }
98 }
99
100 void CreatureFrame_FallDamage(entity this)
101 {
102         if(IS_VEHICLE(this) || (this.flags & FL_PROJECTILE))
103                 return; // vehicles and projectiles don't receive fall damage
104         if(!(this.velocity || this.oldvelocity))
105                 return; // if the entity hasn't moved and isn't moving, then don't do anything
106
107         // check for falling damage
108         bool have_hook = false;
109         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
110         {
111             .entity weaponentity = weaponentities[slot];
112             if(this.(weaponentity).hook && this.(weaponentity).hook.state)
113             {
114                 have_hook = true;
115                 break;
116             }
117         }
118         if(!have_hook)
119         {
120                 float dm; // dm is the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
121                 if(autocvar_g_balance_falldamage_onlyvertical)
122                         dm = fabs(this.oldvelocity.z) - vlen(this.velocity);
123                 else
124                         dm = vlen(this.oldvelocity) - vlen(this.velocity);
125                 if (IS_DEAD(this))
126                         dm = (dm - autocvar_g_balance_falldamage_deadminspeed) * autocvar_g_balance_falldamage_factor;
127                 else
128                         dm = min((dm - autocvar_g_balance_falldamage_minspeed) * autocvar_g_balance_falldamage_factor, autocvar_g_balance_falldamage_maxdamage);
129                 if (dm > 0)
130                 {
131                         tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
132                         if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODAMAGE))
133                                 Damage (this, NULL, NULL, dm, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
134                 }
135         }
136
137         if(autocvar_g_maxspeed > 0 && vdist(this.velocity, >, autocvar_g_maxspeed))
138                 Damage (this, NULL, NULL, 100000, DEATH_SHOOTING_STAR.m_id, DMG_NOWEP, this.origin, '0 0 0');
139 }
140
141 void CreatureFrame_All()
142 {
143         if(game_stopped || time < game_starttime)
144                 return;
145
146         IL_EACH(g_damagedbycontents, it.damagedbycontents,
147         {
148                 if (it.move_movetype == MOVETYPE_NOCLIP) continue;
149                 CreatureFrame_Liquids(it);
150                 CreatureFrame_FallDamage(it);
151                 it.oldvelocity = it.velocity;
152         });
153 }
154
155 void Pause_TryPause(bool ispaused)
156 {
157         int n = 0;
158         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
159                 if (PHYS_INPUT_BUTTON_CHAT(it) != ispaused) return;
160                 ++n;
161         });
162         if (!n) return;
163         setpause(ispaused);
164 }
165
166 void SV_PausedTic(float elapsedtime)
167 {
168         if (!server_is_dedicated) Pause_TryPause(false);
169 }
170
171 void dedicated_print(string input)
172 {
173         if (server_is_dedicated) print(input);
174 }
175
176 void make_safe_for_remove(entity e)
177 {
178     if (e.initialize_entity)
179     {
180         entity ent, prev = NULL;
181         for (ent = initialize_entity_first; ent; )
182         {
183             if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
184             {
185                 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
186                 // skip it in linked list
187                 if (prev)
188                 {
189                     prev.initialize_entity_next = ent.initialize_entity_next;
190                     ent = prev.initialize_entity_next;
191                 }
192                 else
193                 {
194                     initialize_entity_first = ent.initialize_entity_next;
195                     ent = initialize_entity_first;
196                 }
197             }
198             else
199             {
200                 prev = ent;
201                 ent = ent.initialize_entity_next;
202             }
203         }
204     }
205 }
206
207 void remove_except_protected(entity e)
208 {
209         if(e.remove_except_protected_forbidden)
210                 error("not allowed to remove this at this point");
211         builtin_remove(e);
212 }
213
214 void remove_unsafely(entity e)
215 {
216     if(e.classname == "spike")
217         error("Removing spikes is forbidden (crylink bug), please report");
218     builtin_remove(e);
219 }
220
221 void remove_safely(entity e)
222 {
223     make_safe_for_remove(e);
224     builtin_remove(e);
225 }
226
227 /*
228 =============
229 StartFrame
230
231 Called before each frame by the server
232 =============
233 */
234
235 bool game_delay_last;
236
237 bool autocvar_sv_autopause = false;
238 void systems_update();
239 void sys_phys_update(entity this, float dt);
240 void StartFrame()
241 {
242     // TODO: if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
243     IL_EACH(g_players, IS_FAKE_CLIENT(it), sys_phys_update(it, frametime));
244     IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPreThink(it));
245
246         execute_next_frame();
247         if (autocvar_sv_autopause && !server_is_dedicated) Pause_TryPause(true);
248
249         delete_fn = remove_unsafely; // not during spawning!
250         serverprevtime = servertime;
251         servertime = time;
252         serverframetime = frametime;
253
254 #ifdef PROFILING
255         if(time > client_cefc_accumulatortime + 1)
256         {
257                 float t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
258                 int c_seeing = 0;
259                 int c_seen = 0;
260                 FOREACH_CLIENT(true, {
261                         if(IS_REAL_CLIENT(it))
262                                 ++c_seeing;
263                         if(IS_PLAYER(it))
264                                 ++c_seen;
265                 });
266                 LOG_INFO(
267                     "CEFC time: ", ftos(t * 1000), "ms; ",
268             "CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ",
269             "CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0')
270         );
271                 client_cefc_accumulatortime = time;
272                 client_cefc_accumulator = 0;
273         }
274 #endif
275
276         IL_EACH(g_projectiles, it.csqcprojectile_clientanimate, CSQCProjectile_Check(it));
277
278         if (RedirectionThink()) return;
279
280         UncustomizeEntitiesRun();
281         InitializeEntitiesRun();
282
283         WarpZone_StartFrame();
284
285         sys_frametime = autocvar_sys_ticrate * autocvar_slowmo;
286         if (sys_frametime <= 0) sys_frametime = 1.0 / 60.0; // somewhat safe fallback
287
288         if (timeout_status == TIMEOUT_LEADTIME) // just before the timeout (when timeout_status will be TIMEOUT_ACTIVE)
289                 orig_slowmo = autocvar_slowmo; // slowmo will be restored after the timeout
290
291         // detect when the pre-game countdown (if any) has ended and the game has started
292         bool game_delay = (time < game_starttime);
293         if (autocvar_sv_eventlog && game_delay_last && !game_delay)
294                 GameLogEcho(":startdelay_ended");
295         game_delay_last = game_delay;
296
297         CreatureFrame_All();
298         CheckRules_World();
299
300         if (warmup_stage && !game_stopped && warmup_limit > 0 && time >= warmup_limit) {
301                 ReadyRestart();
302                 return;
303         }
304
305         bot_serverframe();
306         anticheat_startframe();
307         MUTATOR_CALLHOOK(SV_StartFrame);
308
309         GlobalStats_updateglobal();
310     FOREACH_CLIENT(true, GlobalStats_update(it));
311     IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPostThink(it));
312 }
313
314 .vector originjitter;
315 .vector anglesjitter;
316 .float anglejitter;
317 .string gametypefilter;
318 .string cvarfilter;
319
320 void SV_OnEntityPreSpawnFunction(entity this)
321 {
322         if (this)
323         if (this.gametypefilter != "")
324         if (!isGametypeInFilter(MapInfo_LoadedGametype, teamplay, have_team_spawns, this.gametypefilter))
325         {
326                 delete(this);
327                 return;
328         }
329         if (this.cvarfilter != "" && !expr_evaluate(this.cvarfilter)) {
330                 delete(this);
331                 return;
332         }
333
334         if (q3compat && DoesQ3ARemoveThisEntity(this)) {
335                 delete(this);
336                 return;
337         }
338
339         set_movetype(this, this.movetype);
340
341         if (this.monster_attack) {
342                 IL_PUSH(g_monster_targets, this);
343     }
344
345         // support special -1 and -2 angle from radiant
346         if (this.angles == '0 -1 0') {
347                 this.angles = '-90 0 0';
348         } else if (this.angles == '0 -2 0') {
349                 this.angles = '+90 0 0';
350     }
351
352     #define X(out, in) MACRO_BEGIN \
353         if (in != 0) { out = out + (random() * 2 - 1) * in; } \
354     MACRO_END
355     X(this.origin.x, this.originjitter.x); X(this.origin.y, this.originjitter.y); X(this.origin.z, this.originjitter.z);
356     X(this.angles.x, this.anglesjitter.x); X(this.angles.y, this.anglesjitter.y); X(this.angles.z, this.anglesjitter.z);
357     X(this.angles.y, this.anglejitter);
358     #undef X
359
360         if (MUTATOR_CALLHOOK(OnEntityPreSpawn, this)) {
361                 delete(this);
362                 return;
363         }
364 }
365
366 string GetField_fullspawndata(entity e, string f, ...)
367 /* Retrieves the value of a map entity field from fullspawndata
368  * This bypasses field value changes made by the engine,
369  * eg string-to-float and escape sequence substitution.
370  *
371  * Avoids the need to declare fields just to read them once :)
372  *
373  * Returns the last instance of the field to match DarkPlaces behaviour.
374  * Path support: converts \ to / and tests the file if a third (bool, true) arg is passed.
375  * Returns string_null if the entity does not have the field, or the file is not in the VFS.
376  *
377  * FIXME: entities with //comments are not supported.
378  */
379 {
380         string v = string_null;
381
382         if (!e.fullspawndata)
383         {
384                 LOG_WARNF("^1EDICT %s (classname %s) has no fullspawndata, engine lacks support?", ftos(num_for_edict(e)), e.classname);
385                 return v;
386         }
387
388         if (strstrofs(e.fullspawndata, "//", 0) >= 0)
389         {
390                 // tokenize and tokenize_console return early if "//" is reached,
391                 // which can leave an odd number of tokens and break key:value pairing.
392                 LOG_WARNF("^1EDICT %s fullspawndata contains unsupported //comment^7%s", ftos(num_for_edict(e)), e.fullspawndata);
393                 return v;
394         }
395
396         //print(sprintf("%s(EDICT %s, FIELD %s)\n", __FUNC__, ftos(num_for_edict(e)), f));
397         //print(strcat("FULLSPAWNDATA:", e.fullspawndata, "\n"));
398
399         // tokenize treats \ as an escape, but tokenize_console returns the required literal
400         for (int t = tokenize_console(e.fullspawndata) - 3; t > 0; t -= 2)
401         {
402                 //print(sprintf("\tTOKEN %s:%s\t%s:%s\n", ftos(t), ftos(t + 1), argv(t), argv(t + 1)));
403                 if (argv(t) == f)
404                 {
405                         v = argv(t + 1);
406                         break;
407                 }
408         }
409
410         //print(strcat("RESULT: ", v, "\n\n"));
411
412         if (v && ...(0, bool) == true)
413         {
414                 v = strreplace("\\", "/", v);
415                 if (whichpack(v) == "")
416                         return string_null;
417         }
418
419         return v;
420 }
421
422 void WarpZone_PostInitialize_Callback()
423 {
424         // create waypoint links for warpzones
425         entity tracetest_ent = spawn();
426         setsize(tracetest_ent, PL_MIN_CONST, PL_MAX_CONST);
427         tracetest_ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
428         //for(entity e = warpzone_first; e; e = e.warpzone_next)
429         for(entity e = NULL; (e = find(e, classname, "trigger_warpzone")); )
430                 waypoint_spawnforteleporter_wz(e, tracetest_ent);
431         delete(tracetest_ent);
432 }
433
434 /** engine callback */
435 void URI_Get_Callback(float id, float status, string data)
436 {
437         if(url_URI_Get_Callback(id, status, data))
438         {
439                 // handled
440         }
441         else if (id == URI_GET_DISCARD)
442         {
443                 // discard
444         }
445         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
446         {
447                 // sv_cmd curl
448                 Curl_URI_Get_Callback(id, status, data);
449         }
450         else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
451         {
452                 // online ban list
453                 OnlineBanList_URI_Get_Callback(id, status, data);
454         }
455         else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
456         {
457                 // handled by a mutator
458         }
459         else
460         {
461                 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".");
462         }
463 }
464
465 /*
466 ==================
467 main
468
469 unused but required by the engine
470 ==================
471 */
472 void main ()
473 {
474
475 }