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/mapobjects/_mod.qh>
9 #include <common/monsters/sv_monsters.qh>
10 #include <common/util.qh>
11 #include <common/vehicles/all.qh>
12 #include <common/weapons/_all.qh>
13 #include <lib/csqcmodel/sv_model.qh>
14 #include <lib/warpzone/common.qh>
15 #include <lib/warpzone/server.qh>
16 #include <server/anticheat.qh>
17 #include <server/bot/api.qh>
18 #include <server/command/common.qh>
19 #include <server/compat/quake3.qh>
20 #include <server/damage.qh>
21 #include <server/gamelog.qh>
22 #include <server/hook.qh>
23 #include <server/ipban.qh>
24 #include <server/mutators/_mod.qh>
25 #include <server/spawnpoints.qh>
26 #include <server/weapons/common.qh>
27 #include <server/weapons/csqcprojectile.qh>
28 #include <server/world.qh>
30 void dropclient_do(entity this)
33 dropclient(this.owner);
38 * Schedules dropclient for a player and returns true;
39 * if dropclient is already scheduled (for that player) it does nothing and returns false.
41 * NOTE: this function exists only to allow sending a message to the kicked player with
42 * Send_Notification, which doesn't work if called together with dropclient
44 bool dropclient_schedule(entity this)
46 bool scheduled = false;
47 FOREACH_ENTITY_CLASS("dropclient_handler", true,
52 break; // can't use return here, compiler shows a warning
58 entity e = new_pure(dropclient_handler);
59 setthink(e, dropclient_do);
61 e.nextthink = time + 0.1;
65 void CreatureFrame_hotliquids(entity this)
67 if (this.contents_damagetime >= time)
72 this.contents_damagetime = time + autocvar_g_balance_contents_damagerate;
74 if (this.flags & FL_PROJECTILE)
76 if (this.watertype == CONTENT_LAVA)
77 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');
78 else if (this.watertype == CONTENT_SLIME)
79 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');
83 if (STAT(FROZEN, this))
85 if (this.watertype == CONTENT_LAVA)
86 Damage(this, NULL, NULL, 10000, DEATH_LAVA.m_id, DMG_NOWEP, this.origin, '0 0 0');
87 else if (this.watertype == CONTENT_SLIME)
88 Damage(this, NULL, NULL, 10000, DEATH_SLIME.m_id, DMG_NOWEP, this.origin, '0 0 0');
90 else if (this.watertype == CONTENT_LAVA)
92 if (this.watersound_finished < time)
94 this.watersound_finished = time + 0.5;
95 sound (this, CH_PLAYER_SINGLE, SND_LAVA, VOL_BASE, ATTEN_NORM);
97 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');
98 if(autocvar_g_balance_contents_playerdamage_lava_burn)
99 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);
101 else if (this.watertype == CONTENT_SLIME)
103 if (this.watersound_finished < time)
105 this.watersound_finished = time + 0.5;
106 sound (this, CH_PLAYER_SINGLE, SND_SLIME, VOL_BASE, ATTEN_NORM);
108 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');
113 void CreatureFrame_Liquids(entity this)
115 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)
117 if (!(this.flags & FL_INWATER))
119 this.flags |= FL_INWATER;
120 this.contents_damagetime = 0;
123 CreatureFrame_hotliquids(this);
127 if (this.flags & FL_INWATER)
129 // play leave water sound
130 this.flags &= ~FL_INWATER;
131 this.contents_damagetime = 0;
136 void CreatureFrame_FallDamage(entity this)
138 if(IS_VEHICLE(this) || (this.flags & FL_PROJECTILE))
139 return; // vehicles and projectiles don't receive fall damage
140 if(!(this.velocity || this.oldvelocity))
141 return; // if the entity hasn't moved and isn't moving, then don't do anything
143 // check for falling damage
144 bool have_hook = false;
145 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
147 .entity weaponentity = weaponentities[slot];
148 if(this.(weaponentity).hook && this.(weaponentity).hook.state)
156 float dm; // dm is the velocity DECREASE. Velocity INCREASE should never cause a sound or any damage.
157 if(autocvar_g_balance_falldamage_onlyvertical)
158 dm = fabs(this.oldvelocity.z) - vlen(this.velocity);
160 dm = vlen(this.oldvelocity) - vlen(this.velocity);
162 dm = (dm - autocvar_g_balance_falldamage_deadminspeed) * autocvar_g_balance_falldamage_factor;
164 dm = min((dm - autocvar_g_balance_falldamage_minspeed) * autocvar_g_balance_falldamage_factor, autocvar_g_balance_falldamage_maxdamage);
167 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
168 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODAMAGE))
169 Damage (this, NULL, NULL, dm, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
173 if(autocvar_g_maxspeed > 0 && vdist(this.velocity, >, autocvar_g_maxspeed))
174 Damage (this, NULL, NULL, 100000, DEATH_SHOOTING_STAR.m_id, DMG_NOWEP, this.origin, '0 0 0');
177 void CreatureFrame_All()
179 if(game_stopped || time < game_starttime)
182 IL_EACH(g_damagedbycontents, it.damagedbycontents,
184 if (it.move_movetype == MOVETYPE_NOCLIP) continue;
185 CreatureFrame_Liquids(it);
186 CreatureFrame_FallDamage(it);
187 it.oldvelocity = it.velocity;
191 // called shortly after map change in dedicated
192 void Pause_TryPause_Dedicated(entity this)
194 if (player_count == 0 && !intermission_running && !autocvar__endmatch)
198 // called every normal frame in singleplayer/listen
199 void Pause_TryPause()
202 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
203 if (PHYS_INPUT_BUTTON_CHAT(it)) ++p;
213 // called every paused frame by DP
214 void SV_PausedTic(float elapsedtime)
216 if (autocvar__endmatch) // `endmatch` while paused
217 setpause(0); // proceed to intermission
218 else if (!server_is_dedicated)
220 if (autocvar_sv_autopause)
227 void dedicated_print(string input)
229 if (server_is_dedicated) print(input);
232 void make_safe_for_remove(entity e)
234 if (e.initialize_entity)
236 entity ent, prev = NULL;
237 for (ent = initialize_entity_first; ent; )
239 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
241 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
242 // skip it in linked list
245 prev.initialize_entity_next = ent.initialize_entity_next;
246 ent = prev.initialize_entity_next;
250 initialize_entity_first = ent.initialize_entity_next;
251 ent = initialize_entity_first;
257 ent = ent.initialize_entity_next;
263 void remove_except_protected(entity e)
265 if(e.remove_except_protected_forbidden)
266 error("not allowed to remove this at this point");
270 void remove_unsafely(entity e)
272 if(e.classname == "spike")
273 error("Removing spikes is forbidden (crylink bug), please report");
277 void remove_safely(entity e)
279 make_safe_for_remove(e);
287 Called before each frame by the server
291 bool game_delay_last;
293 void systems_update();
294 void sys_phys_update(entity this, float dt);
297 // TODO: if move is more than 50ms, split it into two moves (this matches QWSV behavior and the client prediction)
298 IL_EACH(g_players, IS_FAKE_CLIENT(it), sys_phys_update(it, frametime));
299 IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPreThink(it));
301 execute_next_frame();
303 delete_fn = remove_unsafely; // not during spawning!
304 serverprevtime = servertime;
306 serverframetime = frametime;
309 if(time > client_cefc_accumulatortime + 1)
311 float t = client_cefc_accumulator / (time - client_cefc_accumulatortime);
314 FOREACH_CLIENT(true, {
315 if(IS_REAL_CLIENT(it))
321 "CEFC time: ", ftos(t * 1000), "ms; ",
322 "CEFC calls per second: ", ftos(c_seeing * (c_seen - 1) / t), "; ",
323 "CEFC 100% load at: ", ftos(solve_quadratic(t, -t, -1) * '0 1 0')
325 client_cefc_accumulatortime = time;
326 client_cefc_accumulator = 0;
330 IL_EACH(g_projectiles, it.csqcprojectile_clientanimate, CSQCProjectile_Check(it));
332 if (RedirectionThink()) return;
334 UncustomizeEntitiesRun();
335 InitializeEntitiesRun();
337 WarpZone_StartFrame();
339 sys_frametime = autocvar_sys_ticrate * autocvar_slowmo;
340 if (sys_frametime <= 0) sys_frametime = 1.0 / 60.0; // somewhat safe fallback
342 if (timeout_status == TIMEOUT_LEADTIME) // just before the timeout (when timeout_status will be TIMEOUT_ACTIVE)
343 orig_slowmo = autocvar_slowmo; // slowmo will be restored after the timeout
345 // detect when the pre-game countdown (if any) has ended and the game has started
346 bool game_delay = (time < game_starttime);
347 if (autocvar_sv_eventlog && game_delay_last && !game_delay)
348 GameLogEcho(":startdelay_ended");
349 game_delay_last = game_delay;
354 // after CheckRules_World() as it may set intermission_running, and after RedirectionThink() in case listen server is closing
355 if (autocvar_sv_autopause && !server_is_dedicated && !intermission_running)
358 if (warmup_stage && !game_stopped && warmup_limit > 0 && time - game_starttime >= warmup_limit) {
364 anticheat_startframe();
365 MUTATOR_CALLHOOK(SV_StartFrame);
367 GlobalStats_updateglobal();
368 FOREACH_CLIENT(true, GlobalStats_update(it));
369 IL_EACH(g_players, IS_FAKE_CLIENT(it), PlayerPostThink(it));
372 .vector originjitter;
373 .vector anglesjitter;
375 .string gametypefilter;
378 void SV_OnEntityPreSpawnFunction(entity this)
381 if (this.gametypefilter != "")
382 if (!isGametypeInFilter(MapInfo_LoadedGametype, teamplay, have_team_spawns, this.gametypefilter))
387 if (this.cvarfilter != "" && !expr_evaluate(this.cvarfilter)) {
392 if (q3compat && DoesQ3ARemoveThisEntity(this)) {
397 set_movetype(this, this.movetype);
399 if (this.monster_attack) {
400 IL_PUSH(g_monster_targets, this);
403 // support special -1 and -2 angle from radiant
404 if (this.angles == '0 -1 0') {
405 this.angles = '-90 0 0';
406 } else if (this.angles == '0 -2 0') {
407 this.angles = '+90 0 0';
410 #define X(out, in) MACRO_BEGIN \
411 if (in != 0) { out = out + (random() * 2 - 1) * in; } \
413 X(this.origin.x, this.originjitter.x); X(this.origin.y, this.originjitter.y); X(this.origin.z, this.originjitter.z);
414 X(this.angles.x, this.anglesjitter.x); X(this.angles.y, this.anglesjitter.y); X(this.angles.z, this.anglesjitter.z);
415 X(this.angles.y, this.anglejitter);
418 if (MUTATOR_CALLHOOK(OnEntityPreSpawn, this)) {
424 string GetField_fullspawndata(entity e, string f, ...)
425 /* Retrieves the value of a map entity field from fullspawndata
426 * This bypasses field value changes made by the engine,
427 * eg string-to-float and escape sequence substitution.
429 * Avoids the need to declare fields just to read them once :)
431 * Returns the last instance of the field to match DarkPlaces behaviour.
432 * Path support: converts \ to / and tests the file if a third (bool, true) arg is passed.
433 * Returns string_null if the entity does not have the field, or the file is not in the VFS.
435 * FIXME: entities with //comments are not supported.
438 string v = string_null;
440 if (!e.fullspawndata)
442 //LOG_WARNF("^1EDICT %s (classname %s) has no fullspawndata, engine lacks support?", ftos(num_for_edict(e)), e.classname);
446 if (strstrofs(e.fullspawndata, "//", 0) >= 0)
448 // tokenize and tokenize_console return early if "//" is reached,
449 // which can leave an odd number of tokens and break key:value pairing.
450 LOG_WARNF("^1EDICT %s fullspawndata contains unsupported //comment^7%s", ftos(num_for_edict(e)), e.fullspawndata);
454 //print(sprintf("%s(EDICT %s, FIELD %s)\n", __FUNC__, ftos(num_for_edict(e)), f));
455 //print(strcat("FULLSPAWNDATA:", e.fullspawndata, "\n"));
457 // tokenize treats \ as an escape, but tokenize_console returns the required literal
458 for (int t = tokenize_console(e.fullspawndata) - 3; t > 0; t -= 2)
460 //print(sprintf("\tTOKEN %s:%s\t%s:%s\n", ftos(t), ftos(t + 1), argv(t), argv(t + 1)));
468 //print(strcat("RESULT: ", v, "\n\n"));
470 if (v && ...(0, bool) == true)
472 v = strreplace("\\", "/", v);
473 if (whichpack(v) == "")
480 void WarpZone_PostInitialize_Callback()
482 // create waypoint links for warpzones
483 entity tracetest_ent = spawn();
484 setsize(tracetest_ent, PL_MIN_CONST, PL_MAX_CONST);
485 tracetest_ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
486 //for(entity e = warpzone_first; e; e = e.warpzone_next)
487 for(entity e = NULL; (e = find(e, classname, "trigger_warpzone")); )
488 waypoint_spawnforteleporter_wz(e, tracetest_ent);
489 delete(tracetest_ent);
492 /** engine callback */
493 void URI_Get_Callback(float id, float status, string data)
495 if(url_URI_Get_Callback(id, status, data))
499 else if (id == URI_GET_DISCARD)
503 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
506 Curl_URI_Get_Callback(id, status, data);
508 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
511 OnlineBanList_URI_Get_Callback(id, status, data);
513 else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
515 // handled by a mutator
519 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".");
527 unused but required by the engine