1 #include "miscfunctions.qh"
3 #include "command/common.qh"
4 #include "constants.qh"
7 #include "mutators/_mod.qh"
8 #include "../common/t_items.qh"
9 #include "weapons/accuracy.qh"
10 #include "weapons/csqcprojectile.qh"
11 #include "weapons/selection.qh"
12 #include "../common/command/_mod.qh"
13 #include "../common/constants.qh"
14 #include <common/net_linked.qh>
15 #include "../common/deathtypes/all.qh"
16 #include "../common/mapinfo.qh"
17 #include "../common/notifications/all.qh"
18 #include "../common/playerstats.qh"
19 #include "../common/teams.qh"
20 #include "../common/triggers/subs.qh"
21 #include "../common/util.qh"
22 #include "../common/turrets/sv_turrets.qh"
23 #include <common/weapons/_all.qh>
24 #include "../common/vehicles/sv_vehicles.qh"
25 #include "../common/vehicles/vehicle.qh"
26 #include "../common/items/_mod.qh"
27 #include "../common/state.qh"
28 #include "../common/effects/qc/globalsound.qh"
29 #include "../common/wepent.qh"
30 #include "../lib/csqcmodel/sv_model.qh"
31 #include "../lib/warpzone/anglestransform.qh"
32 #include "../lib/warpzone/server.qh"
34 void crosshair_trace(entity pl)
36 traceline_antilag(pl, pl.cursor_trace_start, pl.cursor_trace_start + normalize(pl.cursor_trace_endpos - pl.cursor_trace_start) * MAX_SHOT_DISTANCE, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
38 .bool ctrace_solidchanged;
39 void crosshair_trace_plusvisibletriggers(entity pl)
41 FOREACH_ENTITY_FLOAT(solid, SOLID_TRIGGER,
46 it.ctrace_solidchanged = true;
52 FOREACH_ENTITY_FLOAT(ctrace_solidchanged, true,
54 it.solid = SOLID_TRIGGER;
55 it.ctrace_solidchanged = false;
58 void WarpZone_crosshair_trace(entity pl)
60 WarpZone_traceline_antilag(pl, pl.cursor_trace_start, pl.cursor_trace_start + normalize(pl.cursor_trace_endpos - pl.cursor_trace_start) * MAX_SHOT_DISTANCE, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
66 if(autocvar_sv_adminnick != "")
67 return autocvar_sv_adminnick;
69 return "SERVER ADMIN";
73 void GameLogEcho(string s)
78 if (autocvar_sv_eventlog_files)
83 matches = autocvar_sv_eventlog_files_counter + 1;
84 cvar_set("sv_eventlog_files_counter", itos(matches));
87 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
88 fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
89 logfile = fopen(fn, FILE_APPEND);
90 fputs(logfile, ":logversion:3\n");
94 if (autocvar_sv_eventlog_files_timestamps)
95 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
97 fputs(logfile, strcat(s, "\n"));
100 if (autocvar_sv_eventlog_console)
102 dedicated_print(strcat(s, "\n"));
109 // will be opened later
114 if (logfile_open && logfile >= 0)
121 entity findnearest(vector point, .string field, string value, vector axismod)
132 localhead = find(NULL, field, value);
135 if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
136 dist = localhead.oldorigin;
138 dist = localhead.origin;
140 dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
143 for (i = 0; i < num_nearest; ++i)
145 if (len < nearest_length[i])
149 // now i tells us where to insert at
150 // INSERTION SORT! YOU'VE SEEN IT! RUN!
151 if (i < NUM_NEAREST_ENTITIES)
153 for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
155 nearest_length[j + 1] = nearest_length[j];
156 nearest_entity[j + 1] = nearest_entity[j];
158 nearest_length[i] = len;
159 nearest_entity[i] = localhead;
160 if (num_nearest < NUM_NEAREST_ENTITIES)
161 num_nearest = num_nearest + 1;
164 localhead = find(localhead, field, value);
167 // now use the first one from our list that we can see
168 for (i = 0; i < num_nearest; ++i)
170 traceline(point, nearest_entity[i].origin, true, NULL);
171 if (trace_fraction == 1)
175 LOG_TRACE("Nearest point (");
176 LOG_TRACE(nearest_entity[0].netname);
177 LOG_TRACE(") is not visible, using a visible one.");
179 return nearest_entity[i];
183 if (num_nearest == 0)
186 LOG_TRACE("Not seeing any location point, using nearest as fallback.");
188 dprint("Candidates were: ");
189 for(j = 0; j < num_nearest; ++j)
193 dprint(nearest_entity[j].netname);
198 return nearest_entity[0];
201 string NearestLocation(vector p)
206 loc = findnearest(p, classname, "target_location", '1 1 1');
213 loc = findnearest(p, target, "###item###", '1 1 4');
220 string formatmessage(entity this, string msg)
232 ammoitems = "batteries";
233 if(this.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
234 if(this.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
235 if(this.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
236 if(this.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
238 WarpZone_crosshair_trace(this);
239 cursor = trace_endpos;
240 cursor_ent = trace_ent;
244 break; // too many replacements
247 p1 = strstrofs(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
248 p2 = strstrofs(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
261 replacement = substring(msg, p, 2);
262 escape = substring(msg, p + 1, 1);
264 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
268 case "%": replacement = "%"; break;
269 case "\\":replacement = "\\"; break;
270 case "n": replacement = "\n"; break;
271 case "a": replacement = ftos(floor(this.armorvalue)); break;
272 case "h": replacement = ftos(floor(this.health)); break;
273 case "l": replacement = NearestLocation(this.origin); break;
274 case "y": replacement = NearestLocation(cursor); break;
275 case "d": replacement = NearestLocation(this.death_origin); break;
276 case "w": replacement = ((this.(weaponentity).m_weapon == WEP_Null) ? ((this.(weaponentity).m_switchweapon == WEP_Null) ? Weapons_from(this.(weaponentity).cnt) : this.(weaponentity).m_switchweapon) : this.(weaponentity).m_weapon).m_name; break;
277 case "W": replacement = ammoitems; break;
278 case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
279 case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
280 case "S": replacement = ftos(vlen(this.velocity)); break;
281 case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
282 case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
285 MUTATOR_CALLHOOK(FormatMessage, this, escape, replacement, msg);
286 replacement = M_ARGV(2, string);
291 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
292 p = p + strlen(replacement);
303 >0: receives a cvar from name=argv(f) value=argv(f+1)
305 void GetCvars_handleString(entity this, string thisname, float f, .string field, string name)
310 strunzone(this.(field));
311 this.(field) = string_null;
315 if (thisname == name)
318 strunzone(this.(field));
319 this.(field) = strzone(argv(f + 1));
323 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
325 void GetCvars_handleString_Fixup(entity this, string thisname, float f, .string field, string name, string(entity, string) func)
327 GetCvars_handleString(this, thisname, f, field, name);
328 if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
329 if (thisname == name)
331 string s = func(this, strcat1(this.(field)));
332 if (s != this.(field))
334 strunzone(this.(field));
335 this.(field) = strzone(s);
339 void GetCvars_handleFloat(entity this, string thisname, float f, .float field, string name)
346 if (thisname == name)
347 this.(field) = stof(argv(f + 1));
350 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
352 void GetCvars_handleFloatOnce(entity this, string thisname, float f, .float field, string name)
359 if (thisname == name)
363 this.(field) = stof(argv(f + 1));
372 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
375 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
378 o = W_FixWeaponOrder_ForceComplete(wo);
379 if(this.weaponorder_byimpulse)
381 strunzone(this.weaponorder_byimpulse);
382 this.weaponorder_byimpulse = string_null;
384 this.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
388 REPLICATE(autoswitch, bool, "cl_autoswitch");
390 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
392 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
394 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
396 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
398 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
400 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
402 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
404 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
406 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
408 REPLICATE(cvar_cl_physics, string, "cl_physics");
410 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
412 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
414 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
416 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
419 * @param f -1: cleanup, 0: request, 1: receive
421 void GetCvars(entity this, int f)
423 string s = string_null;
426 s = strcat1(argv(f));
430 MUTATOR_CALLHOOK(GetCvars);
432 Notification_GetCvars(this);
434 ReplicateVars(this, s, f);
436 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
437 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
438 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
439 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
440 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
441 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
442 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
443 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
444 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
445 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
446 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
448 GetCvars_handleFloat(this, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
450 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
453 if (s == "cl_weaponpriority")
455 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
457 .entity weaponentity = weaponentities[slot];
458 if (this.(weaponentity) && (this.(weaponentity).m_weapon != WEP_Null || slot == 0))
459 this.(weaponentity).m_switchweapon = w_getbestweapon(this, weaponentity);
462 if (s == "cl_allow_uidtracking")
463 PlayerStats_GameReport_AddPlayer(this);
467 // decolorizes and team colors the player name when needed
468 string playername(entity p)
471 if (teamplay && !intermission_running && IS_PLAYER(p))
473 t = Team_ColorCode(p.team);
474 return strcat(t, strdecolorize(p.netname));
480 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
482 int i = weaponinfo.m_id;
484 bool allow_mutatorblocked = false;
489 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
490 d = M_ARGV(1, float);
491 allguns = M_ARGV(2, bool);
492 allow_mutatorblocked = M_ARGV(3, bool);
496 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
501 else if(!mutator_returnvalue)
502 d = !(!weaponinfo.weaponstart);
504 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
507 float t = weaponinfo.weaponstartoverride;
509 //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
514 // 4: is set by default?
523 void readplayerstartcvars()
528 // initialize starting values for players
529 start_weapons = '0 0 0';
530 start_weapons_default = '0 0 0';
531 start_weapons_defaultmask = '0 0 0';
533 start_ammo_shells = 0;
534 start_ammo_nails = 0;
535 start_ammo_rockets = 0;
536 start_ammo_cells = 0;
537 start_ammo_plasma = 0;
538 start_health = cvar("g_balance_health_start");
539 start_armorvalue = cvar("g_balance_armor_start");
542 g_weaponarena_weapons = '0 0 0';
544 s = cvar_string("g_weaponarena");
546 MUTATOR_CALLHOOK(SetWeaponArena, s);
547 s = M_ARGV(0, string);
549 if (s == "0" || s == "")
555 // forcibly turn off weaponarena
557 else if (s == "all" || s == "1")
560 g_weaponarena_list = "All Weapons";
561 FOREACH(Weapons, it != WEP_Null, LAMBDA(
562 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
563 g_weaponarena_weapons |= (it.m_wepset);
566 else if (s == "most")
569 g_weaponarena_list = "Most Weapons";
570 FOREACH(Weapons, it != WEP_Null, LAMBDA(
571 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
572 if(it.spawnflags & WEP_FLAG_NORMAL)
573 g_weaponarena_weapons |= (it.m_wepset);
576 else if (s == "none")
579 g_weaponarena_list = "No Weapons";
584 t = tokenize_console(s);
585 g_weaponarena_list = "";
586 for (i = 0; i < t; ++i)
589 FOREACH(Weapons, it != WEP_Null, LAMBDA(
592 g_weaponarena_weapons |= (it.m_wepset);
593 g_weaponarena_list = strcat(g_weaponarena_list, it.m_name, " & ");
598 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
602 g_weaponarena_random = cvar("g_weaponarena_random");
604 g_weaponarena_random = 0;
605 g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
609 g_weapon_stay = 0; // incompatible
610 start_weapons = g_weaponarena_weapons;
611 start_items |= IT_UNLIMITED_AMMO;
615 FOREACH(Weapons, it != WEP_Null, LAMBDA(
616 int w = want_weapon(it, false);
617 WepSet s = it.m_wepset;
621 start_weapons_default |= s;
623 start_weapons_defaultmask |= s;
627 if(!cvar("g_use_ammunition"))
628 start_items |= IT_UNLIMITED_AMMO;
630 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
632 start_ammo_shells = 999;
633 start_ammo_nails = 999;
634 start_ammo_rockets = 999;
635 start_ammo_cells = 999;
636 start_ammo_plasma = 999;
637 start_ammo_fuel = 999;
641 start_ammo_shells = cvar("g_start_ammo_shells");
642 start_ammo_nails = cvar("g_start_ammo_nails");
643 start_ammo_rockets = cvar("g_start_ammo_rockets");
644 start_ammo_cells = cvar("g_start_ammo_cells");
645 start_ammo_plasma = cvar("g_start_ammo_plasma");
646 start_ammo_fuel = cvar("g_start_ammo_fuel");
651 warmup_start_ammo_shells = start_ammo_shells;
652 warmup_start_ammo_nails = start_ammo_nails;
653 warmup_start_ammo_rockets = start_ammo_rockets;
654 warmup_start_ammo_cells = start_ammo_cells;
655 warmup_start_ammo_plasma = start_ammo_plasma;
656 warmup_start_ammo_fuel = start_ammo_fuel;
657 warmup_start_health = start_health;
658 warmup_start_armorvalue = start_armorvalue;
659 warmup_start_weapons = start_weapons;
660 warmup_start_weapons_default = start_weapons_default;
661 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
663 if (!g_weaponarena && !g_ca && !g_freezetag)
665 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
666 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
667 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
668 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
669 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
670 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
671 warmup_start_health = cvar("g_warmup_start_health");
672 warmup_start_armorvalue = cvar("g_warmup_start_armor");
673 warmup_start_weapons = '0 0 0';
674 warmup_start_weapons_default = '0 0 0';
675 warmup_start_weapons_defaultmask = '0 0 0';
676 FOREACH(Weapons, it != WEP_Null, LAMBDA(
677 int w = want_weapon(it, g_warmup_allguns);
678 WepSet s = (it.m_wepset);
680 warmup_start_weapons |= s;
682 warmup_start_weapons_default |= s;
684 warmup_start_weapons_defaultmask |= s;
690 start_items |= ITEM_Jetpack.m_itemid;
692 MUTATOR_CALLHOOK(SetStartItems);
694 if (start_items & ITEM_Jetpack.m_itemid)
696 start_items |= ITEM_JetpackRegen.m_itemid;
697 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
698 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
701 WepSet precache_weapons = start_weapons;
702 if (g_warmup_allguns != 1)
703 precache_weapons |= warmup_start_weapons;
704 FOREACH(Weapons, it != WEP_Null, LAMBDA(
705 if(precache_weapons & (it.m_wepset))
709 start_ammo_shells = max(0, start_ammo_shells);
710 start_ammo_nails = max(0, start_ammo_nails);
711 start_ammo_rockets = max(0, start_ammo_rockets);
712 start_ammo_cells = max(0, start_ammo_cells);
713 start_ammo_plasma = max(0, start_ammo_plasma);
714 start_ammo_fuel = max(0, start_ammo_fuel);
716 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
717 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
718 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
719 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
720 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
721 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
724 void precache_playermodel(string m)
726 float globhandle, i, n;
729 if(substring(m, -9,5) == "_lod1")
731 if(substring(m, -9,5) == "_lod2")
734 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
737 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
741 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
744 n = search_getsize(globhandle);
745 for (i = 0; i < n; ++i)
747 //print(search_getfilename(globhandle, i), "\n");
748 f = search_getfilename(globhandle, i);
749 PrecachePlayerSounds(f);
751 search_end(globhandle);
753 void precache_all_playermodels(string pattern)
755 int globhandle = search_begin(pattern, true, false);
756 if (globhandle < 0) return;
757 int n = search_getsize(globhandle);
758 for (int i = 0; i < n; ++i)
760 string s = search_getfilename(globhandle, i);
761 precache_playermodel(s);
763 search_end(globhandle);
766 void precache_playermodels(string s)
768 FOREACH_WORD(s, true, LAMBDA(precache_playermodel(it)));
773 // gamemode related things
775 // Precache all player models if desired
776 if (autocvar_sv_precacheplayermodels)
778 PrecachePlayerSounds("sound/player/default.sounds");
779 precache_all_playermodels("models/player/*.zym");
780 precache_all_playermodels("models/player/*.dpm");
781 precache_all_playermodels("models/player/*.md3");
782 precache_all_playermodels("models/player/*.psk");
783 precache_all_playermodels("models/player/*.iqm");
786 if (autocvar_sv_defaultcharacter)
788 precache_playermodels(autocvar_sv_defaultplayermodel_red);
789 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
790 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
791 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
792 precache_playermodels(autocvar_sv_defaultplayermodel);
796 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
798 if (!this.noise && this.music) // quake 3 uses the music field
799 this.noise = this.music;
801 // plays music for the level if there is any
804 precache_sound (this.noise);
805 ambientsound ('0 0 0', this.noise, VOL_BASE, ATTEN_NONE);
811 void make_safe_for_remove(entity e)
813 if (e.initialize_entity)
815 entity ent, prev = NULL;
816 for (ent = initialize_entity_first; ent; )
818 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
820 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
821 // skip it in linked list
824 prev.initialize_entity_next = ent.initialize_entity_next;
825 ent = prev.initialize_entity_next;
829 initialize_entity_first = ent.initialize_entity_next;
830 ent = initialize_entity_first;
836 ent = ent.initialize_entity_next;
842 .float remove_except_protected_forbidden;
843 void remove_except_protected(entity e)
845 if(e.remove_except_protected_forbidden)
846 error("not allowed to remove this at this point");
850 void remove_unsafely(entity e)
852 if(e.classname == "spike")
853 error("Removing spikes is forbidden (crylink bug), please report");
857 void remove_safely(entity e)
859 make_safe_for_remove(e);
863 void InitializeEntity(entity e, void(entity this) func, float order)
867 if (!e || e.initialize_entity)
869 // make a proxy initializer entity
871 e = new(initialize_entity);
875 e.initialize_entity = func;
876 e.initialize_entity_order = order;
878 cur = initialize_entity_first;
882 if (!cur || cur.initialize_entity_order > order)
884 // insert between prev and cur
886 prev.initialize_entity_next = e;
888 initialize_entity_first = e;
889 e.initialize_entity_next = cur;
893 cur = cur.initialize_entity_next;
896 void InitializeEntitiesRun()
898 entity startoflist = initialize_entity_first;
899 initialize_entity_first = NULL;
900 delete_fn = remove_except_protected;
901 for (entity e = startoflist; e; e = e.initialize_entity_next)
903 e.remove_except_protected_forbidden = 1;
905 for (entity e = startoflist; e; )
907 e.remove_except_protected_forbidden = 0;
908 e.initialize_entity_order = 0;
909 entity next = e.initialize_entity_next;
910 e.initialize_entity_next = NULL;
911 var void(entity this) func = e.initialize_entity;
912 e.initialize_entity = func_null;
913 if (e.classname == "initialize_entity")
915 entity wrappee = e.enemy;
919 //dprint("Delayed initialization: ", e.classname, "\n");
927 backtrace(strcat("Null function in: ", e.classname, "\n"));
931 delete_fn = remove_unsafely;
934 .float(entity) isEliminated;
935 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
939 WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
940 WriteByte(MSG_ENTITY, sendflags);
944 for(i = 1; i <= maxclients; i += 8)
946 for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
948 if(eliminatedPlayers.isEliminated(e))
951 WriteByte(MSG_ENTITY, f);
958 void EliminatedPlayers_Init(float(entity) isEliminated_func)
960 if(eliminatedPlayers)
962 backtrace("Can't spawn eliminatedPlayers again!");
965 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
966 eliminatedPlayers.isEliminated = isEliminated_func;
972 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
974 if(!(IS_ONGROUND(this))) // if onground, we ARE touching something, but HITTYPE_SPLASH is to be networked if the damage causing projectile is not touching ANYTHING
975 this.projectiledeathtype |= HITTYPE_SPLASH;
976 adaptor_think2use(this);
980 void DropToFloor_Handler(entity this)
982 WITHSELF(this, builtin_droptofloor());
983 this.dropped_origin = this.origin;
986 void droptofloor(entity this)
988 InitializeEntity(this, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
993 float trace_hits_box_a0, trace_hits_box_a1;
995 float trace_hits_box_1d(float end, float thmi, float thma)
999 // just check if x is in range
1007 // do the trace with respect to x
1008 // 0 -> end has to stay in thmi -> thma
1009 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1010 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1011 if (trace_hits_box_a0 > trace_hits_box_a1)
1017 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1022 // now it is a trace from 0 to end
1024 trace_hits_box_a0 = 0;
1025 trace_hits_box_a1 = 1;
1027 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1029 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1031 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1037 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1039 return trace_hits_box(start, end, thmi - ma, thma - mi);
1042 bool SUB_NoImpactCheck(entity this, entity toucher)
1044 // zero hitcontents = this is not the real impact, but either the
1045 // mirror-impact of something hitting the projectile instead of the
1046 // projectile hitting the something, or a touchareagrid one. Neither of
1047 // these stop the projectile from moving, so...
1048 if(trace_dphitcontents == 0)
1050 LOG_TRACEF("A hit from a projectile happened with no hit contents! DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct. (edict: %i, classname: %s, origin: %v)", this, this.classname, this.origin);
1053 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1055 if (toucher == NULL && this.size != '0 0 0')
1058 tic = this.velocity * sys_frametime;
1059 tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
1060 traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
1061 if (trace_fraction >= 1)
1063 LOG_TRACE("Odd... did not hit...?");
1065 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1067 LOG_TRACE("Detected and prevented the sky-grapple bug.");
1075 #define SUB_OwnerCheck(ent,oth) ((oth) && ((oth) == (ent).owner))
1077 void W_Crylink_Dequeue(entity e);
1078 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
1080 if(SUB_OwnerCheck(this, toucher))
1082 if(SUB_NoImpactCheck(this, toucher))
1084 if(this.classname == "nade")
1085 return false; // no checks here
1086 else if(this.classname == "grapplinghook")
1088 else if(this.classname == "spike")
1090 W_Crylink_Dequeue(this);
1097 if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1098 UpdateCSQCProjectile(this);
1102 /** engine callback */
1103 void URI_Get_Callback(float id, float status, string data)
1105 if(url_URI_Get_Callback(id, status, data))
1109 else if (id == URI_GET_DISCARD)
1113 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1116 Curl_URI_Get_Callback(id, status, data);
1118 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1121 OnlineBanList_URI_Get_Callback(id, status, data);
1123 else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
1125 // handled by a mutator
1129 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1133 string uid2name(string myuid) {
1135 s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1137 // FIXME remove this later after 0.6 release
1138 // convert old style broken records to correct style
1141 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1144 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1145 db_remove(ServerProgsDB, strcat("uid2name", myuid));
1150 s = "^1Unregistered Player";
1154 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1157 vector start, org, delta, end, enddown, mstart;
1159 m = e.dphitcontentsmask;
1160 e.dphitcontentsmask = goodcontents | badcontents;
1163 delta = boundmax - boundmin;
1167 for (i = 0; i < attempts; ++i)
1169 start.x = org.x + random() * delta.x;
1170 start.y = org.y + random() * delta.y;
1171 start.z = org.z + random() * delta.z;
1173 // rule 1: start inside world bounds, and outside
1174 // solid, and don't start from somewhere where you can
1175 // fall down to evil
1176 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1177 if (trace_fraction >= 1)
1179 if (trace_startsolid)
1181 if (trace_dphitcontents & badcontents)
1183 if (trace_dphitq3surfaceflags & badsurfaceflags)
1186 // rule 2: if we are too high, lower the point
1187 if (trace_fraction * delta.z > maxaboveground)
1188 start = trace_endpos + '0 0 1' * maxaboveground;
1189 enddown = trace_endpos;
1191 // rule 3: make sure we aren't outside the map. This only works
1192 // for somewhat well formed maps. A good rule of thumb is that
1193 // the map should have a convex outside hull.
1194 // these can be traceLINES as we already verified the starting box
1195 mstart = start + 0.5 * (e.mins + e.maxs);
1196 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1197 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1199 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1200 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1202 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1203 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1205 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1206 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1208 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1209 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1212 // rule 4: we must "see" some spawnpoint or item
1214 IL_EACH(g_spawnpoints, checkpvs(mstart, it),
1216 if((traceline(mstart, it.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1224 IL_EACH(g_items, checkpvs(mstart, it),
1226 if((traceline(mstart, it.origin + (it.mins + it.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1237 // find a random vector to "look at"
1238 end.x = org.x + random() * delta.x;
1239 end.y = org.y + random() * delta.y;
1240 end.z = org.z + random() * delta.z;
1241 end = start + normalize(end - start) * vlen(delta);
1243 // rule 4: start TO end must not be too short
1244 tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1245 if (trace_startsolid)
1247 if (trace_fraction < minviewdistance / vlen(delta))
1250 // rule 5: don't want to look at sky
1251 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1254 // rule 6: we must not end up in trigger_hurt
1255 if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1261 e.dphitcontentsmask = m;
1265 setorigin(e, start);
1266 e.angles = vectoangles(end - start);
1267 LOG_TRACE("Needed ", ftos(i + 1), " attempts");
1274 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1276 return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1279 void write_recordmarker(entity pl, float tstart, float dt)
1281 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1283 // also write a marker into demo files for demotc-race-record-extractor to find
1286 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1287 " ", ftos(tstart), " ", ftos(dt), "\n"));
1290 void attach_sameorigin(entity e, entity to, string tag)
1292 vector org, t_forward, t_left, t_up, e_forward, e_up;
1295 org = e.origin - gettaginfo(to, gettagindex(to, tag));
1296 tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1297 t_forward = v_forward * tagscale;
1298 t_left = v_right * -tagscale;
1299 t_up = v_up * tagscale;
1301 e.origin_x = org * t_forward;
1302 e.origin_y = org * t_left;
1303 e.origin_z = org * t_up;
1305 // current forward and up directions
1306 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1307 e.angles = AnglesTransform_FromVAngles(e.angles);
1309 e.angles = AnglesTransform_FromAngles(e.angles);
1310 fixedmakevectors(e.angles);
1312 // untransform forward, up!
1313 e_forward.x = v_forward * t_forward;
1314 e_forward.y = v_forward * t_left;
1315 e_forward.z = v_forward * t_up;
1316 e_up.x = v_up * t_forward;
1317 e_up.y = v_up * t_left;
1318 e_up.z = v_up * t_up;
1320 e.angles = fixedvectoangles2(e_forward, e_up);
1321 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1322 e.angles = AnglesTransform_ToVAngles(e.angles);
1324 e.angles = AnglesTransform_ToAngles(e.angles);
1326 setattachment(e, to, tag);
1327 setorigin(e, e.origin);
1330 void detach_sameorigin(entity e)
1333 org = gettaginfo(e, 0);
1334 e.angles = fixedvectoangles2(v_forward, v_up);
1335 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1336 e.angles = AnglesTransform_ToVAngles(e.angles);
1338 e.angles = AnglesTransform_ToAngles(e.angles);
1340 setattachment(e, NULL, "");
1341 setorigin(e, e.origin);
1344 void follow_sameorigin(entity e, entity to)
1346 set_movetype(e, MOVETYPE_FOLLOW); // make the hole follow
1347 e.aiment = to; // make the hole follow bmodel
1348 e.punchangle = to.angles; // the original angles of bmodel
1349 e.view_ofs = e.origin - to.origin; // relative origin
1350 e.v_angle = e.angles - to.angles; // relative angles
1353 void unfollow_sameorigin(entity e)
1355 set_movetype(e, MOVETYPE_NONE);
1358 entity gettaginfo_relative_ent;
1359 vector gettaginfo_relative(entity e, float tag)
1361 if (!gettaginfo_relative_ent)
1363 gettaginfo_relative_ent = spawn();
1364 gettaginfo_relative_ent.effects = EF_NODRAW;
1366 gettaginfo_relative_ent.model = e.model;
1367 gettaginfo_relative_ent.modelindex = e.modelindex;
1368 gettaginfo_relative_ent.frame = e.frame;
1369 return gettaginfo(gettaginfo_relative_ent, tag);
1372 .string aiment_classname;
1373 .float aiment_deadflag;
1374 void SetMovetypeFollow(entity ent, entity e)
1376 // FIXME this may not be warpzone aware
1377 set_movetype(ent, MOVETYPE_FOLLOW); // make the hole follow
1378 ent.solid = SOLID_NOT; // MOVETYPE_FOLLOW is always non-solid - this means this cannot be teleported by warpzones any more! Instead, we must notice when our owner gets teleported.
1379 ent.aiment = e; // make the hole follow bmodel
1380 ent.punchangle = e.angles; // the original angles of bmodel
1381 ent.view_ofs = ent.origin - e.origin; // relative origin
1382 ent.v_angle = ent.angles - e.angles; // relative angles
1383 ent.aiment_classname = strzone(e.classname);
1384 ent.aiment_deadflag = e.deadflag;
1386 void UnsetMovetypeFollow(entity ent)
1388 set_movetype(ent, MOVETYPE_FLY);
1389 PROJECTILE_MAKETRIGGER(ent);
1392 float LostMovetypeFollow(entity ent)
1395 if(ent.move_movetype != MOVETYPE_FOLLOW)
1401 if(ent.aiment.classname != ent.aiment_classname)
1403 if(ent.aiment.deadflag != ent.aiment_deadflag)
1410 bool isPushable(entity e)
1421 case "droppedweapon":
1423 case "bullet": // antilagged bullets can't hit this either
1426 if (e.projectiledeathtype)