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