1 #include "miscfunctions.qh"
3 #include "command/common.qh"
4 #include "constants.qh"
7 #include "mutators/all.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/generic.qh"
13 #include "../common/constants.qh"
14 #include "../common/deathtypes/all.qh"
15 #include "../common/mapinfo.qh"
16 #include "../common/notifications/all.qh"
17 #include "../common/playerstats.qh"
18 #include "../common/teams.qh"
19 #include "../common/triggers/subs.qh"
20 #include "../common/util.qh"
21 #include "../common/turrets/sv_turrets.qh"
22 #include "../common/weapons/all.qh"
23 #include "../common/vehicles/sv_vehicles.qh"
24 #include "../common/vehicles/vehicle.qh"
25 #include "../common/items/all.qc"
26 #include "../common/state.qh"
27 #include "../common/effects/qc/globalsound.qh"
28 #include "../lib/csqcmodel/sv_model.qh"
29 #include "../lib/warpzone/anglestransform.qh"
30 #include "../lib/warpzone/server.qh"
32 void crosshair_trace(entity pl)
34 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));
36 void crosshair_trace_plusvisibletriggers(entity pl)
40 first = findchainfloat(solid, SOLID_TRIGGER);
42 for (e = first; e; e = e.chain)
48 for (e = first; e; e = e.chain)
49 e.solid = SOLID_TRIGGER;
51 void WarpZone_crosshair_trace(entity pl)
53 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));
59 if(autocvar_sv_adminnick != "")
60 return autocvar_sv_adminnick;
62 return "SERVER ADMIN";
66 void GameLogEcho(string s)
71 if (autocvar_sv_eventlog_files)
76 matches = autocvar_sv_eventlog_files_counter + 1;
77 cvar_set("sv_eventlog_files_counter", itos(matches));
80 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
81 fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
82 logfile = fopen(fn, FILE_APPEND);
83 fputs(logfile, ":logversion:3\n");
87 if (autocvar_sv_eventlog_files_timestamps)
88 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
90 fputs(logfile, strcat(s, "\n"));
93 if (autocvar_sv_eventlog_console)
102 // will be opened later
107 if (logfile_open && logfile >= 0)
114 entity findnearest(vector point, .string field, string value, vector axismod)
125 localhead = find(world, field, value);
128 if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
129 dist = localhead.oldorigin;
131 dist = localhead.origin;
133 dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
136 for (i = 0; i < num_nearest; ++i)
138 if (len < nearest_length[i])
142 // now i tells us where to insert at
143 // INSERTION SORT! YOU'VE SEEN IT! RUN!
144 if (i < NUM_NEAREST_ENTITIES)
146 for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
148 nearest_length[j + 1] = nearest_length[j];
149 nearest_entity[j + 1] = nearest_entity[j];
151 nearest_length[i] = len;
152 nearest_entity[i] = localhead;
153 if (num_nearest < NUM_NEAREST_ENTITIES)
154 num_nearest = num_nearest + 1;
157 localhead = find(localhead, field, value);
160 // now use the first one from our list that we can see
161 for (i = 0; i < num_nearest; ++i)
163 traceline(point, nearest_entity[i].origin, true, world);
164 if (trace_fraction == 1)
168 LOG_TRACE("Nearest point (");
169 LOG_TRACE(nearest_entity[0].netname);
170 LOG_TRACE(") is not visible, using a visible one.\n");
172 return nearest_entity[i];
176 if (num_nearest == 0)
179 LOG_TRACE("Not seeing any location point, using nearest as fallback.\n");
181 dprint("Candidates were: ");
182 for(j = 0; j < num_nearest; ++j)
186 dprint(nearest_entity[j].netname);
191 return nearest_entity[0];
194 string NearestLocation(vector p)
199 loc = findnearest(p, classname, "target_location", '1 1 1');
206 loc = findnearest(p, target, "###item###", '1 1 4');
213 string formatmessage(entity this, string msg)
225 ammoitems = "batteries";
226 if(this.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
227 if(this.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
228 if(this.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
229 if(this.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
231 WarpZone_crosshair_trace(this);
232 cursor = trace_endpos;
233 cursor_ent = trace_ent;
237 break; // too many replacements
240 p1 = strstrofs(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
241 p2 = strstrofs(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
254 replacement = substring(msg, p, 2);
255 escape = substring(msg, p + 1, 1);
259 case "%": replacement = "%"; break;
260 case "\\":replacement = "\\"; break;
261 case "n": replacement = "\n"; break;
262 case "a": replacement = ftos(floor(this.armorvalue)); break;
263 case "h": replacement = ftos(floor(this.health)); break;
264 case "l": replacement = NearestLocation(this.origin); break;
265 case "y": replacement = NearestLocation(cursor); break;
266 case "d": replacement = NearestLocation(this.death_origin); break;
267 case "w": replacement = ((PS(this).m_weapon == WEP_Null) ? ((PS(this).m_switchweapon == WEP_Null) ? Weapons_from(this.cnt) : PS(this).m_switchweapon) : PS(this).m_weapon).m_name; break;
268 case "W": replacement = ammoitems; break;
269 case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
270 case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
271 case "S": replacement = ftos(vlen(this.velocity)); break;
272 case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
273 case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
276 MUTATOR_CALLHOOK(FormatMessage, this, escape, replacement, msg);
277 escape = format_escape;
278 replacement = format_replacement;
283 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
284 p = p + strlen(replacement);
295 >0: receives a cvar from name=argv(f) value=argv(f+1)
297 void GetCvars_handleString(entity this, string thisname, float f, .string field, string name)
302 strunzone(this.(field));
303 this.(field) = string_null;
307 if (thisname == name)
310 strunzone(this.(field));
311 this.(field) = strzone(argv(f + 1));
315 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
317 void GetCvars_handleString_Fixup(entity this, string thisname, float f, .string field, string name, string(entity, string) func)
319 GetCvars_handleString(this, thisname, f, field, name);
320 if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
321 if (thisname == name)
323 string s = func(this, strcat1(this.(field)));
324 if (s != this.(field))
326 strunzone(this.(field));
327 this.(field) = strzone(s);
331 void GetCvars_handleFloat(entity this, string thisname, float f, .float field, string name)
338 if (thisname == name)
339 this.(field) = stof(argv(f + 1));
342 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
344 void GetCvars_handleFloatOnce(entity this, string thisname, float f, .float field, string name)
351 if (thisname == name)
355 this.(field) = stof(argv(f + 1));
364 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
367 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
370 o = W_FixWeaponOrder_ForceComplete(wo);
371 if(this.weaponorder_byimpulse)
373 strunzone(this.weaponorder_byimpulse);
374 this.weaponorder_byimpulse = string_null;
376 this.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
380 REPLICATE(autoswitch, bool, "cl_autoswitch");
382 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
384 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
386 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
388 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
390 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
392 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
394 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
396 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
398 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
400 REPLICATE(cvar_cl_physics, string, "cl_physics");
402 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
404 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
406 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
408 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
411 * @param f -1: cleanup, 0: request, 1: receive
413 void GetCvars(entity this, int f)
415 string s = string_null;
418 s = strcat1(argv(f));
422 MUTATOR_CALLHOOK(GetCvars);
424 Notification_GetCvars(this);
426 ReplicateVars(this, s, f);
428 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
429 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
430 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
431 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
432 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
433 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
434 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
435 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
436 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
437 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
438 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
440 GetCvars_handleFloat(this, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
442 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
445 if (s == "cl_weaponpriority")
446 if (PS(self)) PS(self).m_switchweapon = w_getbestweapon(self);
447 if (s == "cl_allow_uidtracking")
448 PlayerStats_GameReport_AddPlayer(self);
452 // decolorizes and team colors the player name when needed
453 string playername(entity p)
456 if (teamplay && !intermission_running && IS_PLAYER(p))
458 t = Team_ColorCode(p.team);
459 return strcat(t, strdecolorize(p.netname));
465 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
467 int i = weaponinfo.m_id;
469 bool allow_mutatorblocked = false;
474 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
476 allguns = want_allguns;
477 allow_mutatorblocked = false;
481 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
486 else if(!mutator_returnvalue)
487 d = !(!weaponinfo.weaponstart);
489 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
492 float t = weaponinfo.weaponstartoverride;
494 //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
499 // 4: is set by default?
508 void readplayerstartcvars()
513 // initialize starting values for players
514 start_weapons = '0 0 0';
515 start_weapons_default = '0 0 0';
516 start_weapons_defaultmask = '0 0 0';
518 start_ammo_shells = 0;
519 start_ammo_nails = 0;
520 start_ammo_rockets = 0;
521 start_ammo_cells = 0;
522 start_ammo_plasma = 0;
523 start_health = cvar("g_balance_health_start");
524 start_armorvalue = cvar("g_balance_armor_start");
527 g_weaponarena_weapons = '0 0 0';
529 s = cvar_string("g_weaponarena");
531 MUTATOR_CALLHOOK(SetWeaponArena, s);
534 if (s == "0" || s == "")
540 // forcibly turn off weaponarena
542 else if (s == "all" || s == "1")
545 g_weaponarena_list = "All Weapons";
546 FOREACH(Weapons, it != WEP_Null, LAMBDA(
547 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
548 g_weaponarena_weapons |= (it.m_wepset);
551 else if (s == "most")
554 g_weaponarena_list = "Most Weapons";
555 FOREACH(Weapons, it != WEP_Null, LAMBDA(
556 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
557 if(it.spawnflags & WEP_FLAG_NORMAL)
558 g_weaponarena_weapons |= (it.m_wepset);
561 else if (s == "none")
564 g_weaponarena_list = "No Weapons";
569 t = tokenize_console(s);
570 g_weaponarena_list = "";
571 for (i = 0; i < t; ++i)
574 FOREACH(Weapons, it != WEP_Null, LAMBDA(
577 g_weaponarena_weapons |= (it.m_wepset);
578 g_weaponarena_list = strcat(g_weaponarena_list, it.m_name, " & ");
583 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
587 g_weaponarena_random = cvar("g_weaponarena_random");
589 g_weaponarena_random = 0;
590 g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
594 g_weapon_stay = 0; // incompatible
595 start_weapons = g_weaponarena_weapons;
596 start_items |= IT_UNLIMITED_AMMO;
600 FOREACH(Weapons, it != WEP_Null, LAMBDA(
601 int w = want_weapon(it, false);
602 WepSet s = it.m_wepset;
606 start_weapons_default |= s;
608 start_weapons_defaultmask |= s;
612 if(!cvar("g_use_ammunition"))
613 start_items |= IT_UNLIMITED_AMMO;
615 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
617 start_ammo_shells = 999;
618 start_ammo_nails = 999;
619 start_ammo_rockets = 999;
620 start_ammo_cells = 999;
621 start_ammo_plasma = 999;
622 start_ammo_fuel = 999;
626 start_ammo_shells = cvar("g_start_ammo_shells");
627 start_ammo_nails = cvar("g_start_ammo_nails");
628 start_ammo_rockets = cvar("g_start_ammo_rockets");
629 start_ammo_cells = cvar("g_start_ammo_cells");
630 start_ammo_plasma = cvar("g_start_ammo_plasma");
631 start_ammo_fuel = cvar("g_start_ammo_fuel");
636 warmup_start_ammo_shells = start_ammo_shells;
637 warmup_start_ammo_nails = start_ammo_nails;
638 warmup_start_ammo_rockets = start_ammo_rockets;
639 warmup_start_ammo_cells = start_ammo_cells;
640 warmup_start_ammo_plasma = start_ammo_plasma;
641 warmup_start_ammo_fuel = start_ammo_fuel;
642 warmup_start_health = start_health;
643 warmup_start_armorvalue = start_armorvalue;
644 warmup_start_weapons = start_weapons;
645 warmup_start_weapons_default = start_weapons_default;
646 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
648 if (!g_weaponarena && !g_ca && !g_freezetag)
650 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
651 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
652 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
653 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
654 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
655 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
656 warmup_start_health = cvar("g_warmup_start_health");
657 warmup_start_armorvalue = cvar("g_warmup_start_armor");
658 warmup_start_weapons = '0 0 0';
659 warmup_start_weapons_default = '0 0 0';
660 warmup_start_weapons_defaultmask = '0 0 0';
661 FOREACH(Weapons, it != WEP_Null, LAMBDA(
662 int w = want_weapon(it, g_warmup_allguns);
663 WepSet s = (it.m_wepset);
665 warmup_start_weapons |= s;
667 warmup_start_weapons_default |= s;
669 warmup_start_weapons_defaultmask |= s;
675 start_items |= ITEM_Jetpack.m_itemid;
677 MUTATOR_CALLHOOK(SetStartItems);
679 if (start_items & ITEM_Jetpack.m_itemid)
681 start_items |= ITEM_JetpackRegen.m_itemid;
682 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
683 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
686 WepSet precache_weapons = start_weapons;
687 if (g_warmup_allguns != 1)
688 precache_weapons |= warmup_start_weapons;
689 FOREACH(Weapons, it != WEP_Null, LAMBDA(
690 if(precache_weapons & (it.m_wepset))
694 start_ammo_shells = max(0, start_ammo_shells);
695 start_ammo_nails = max(0, start_ammo_nails);
696 start_ammo_rockets = max(0, start_ammo_rockets);
697 start_ammo_cells = max(0, start_ammo_cells);
698 start_ammo_plasma = max(0, start_ammo_plasma);
699 start_ammo_fuel = max(0, start_ammo_fuel);
701 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
702 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
703 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
704 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
705 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
706 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
709 void precache_playermodel(string m)
711 float globhandle, i, n;
714 if(substring(m, -9,5) == "_lod1")
716 if(substring(m, -9,5) == "_lod2")
719 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
722 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
726 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
729 n = search_getsize(globhandle);
730 for (i = 0; i < n; ++i)
732 //print(search_getfilename(globhandle, i), "\n");
733 f = search_getfilename(globhandle, i);
734 PrecachePlayerSounds(f);
736 search_end(globhandle);
738 void precache_all_playermodels(string pattern)
740 int globhandle = search_begin(pattern, true, false);
741 if (globhandle < 0) return;
742 int n = search_getsize(globhandle);
743 for (int i = 0; i < n; ++i)
745 string s = search_getfilename(globhandle, i);
746 precache_playermodel(s);
748 search_end(globhandle);
751 void precache_playermodels(string s)
753 FOREACH_WORD(s, true, LAMBDA(precache_playermodel(it)));
758 // gamemode related things
760 // Precache all player models if desired
761 if (autocvar_sv_precacheplayermodels)
763 PrecachePlayerSounds("sound/player/default.sounds");
764 precache_all_playermodels("models/player/*.zym");
765 precache_all_playermodels("models/player/*.dpm");
766 precache_all_playermodels("models/player/*.md3");
767 precache_all_playermodels("models/player/*.psk");
768 precache_all_playermodels("models/player/*.iqm");
771 if (autocvar_sv_defaultcharacter)
773 precache_playermodels(autocvar_sv_defaultplayermodel_red);
774 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
775 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
776 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
777 precache_playermodels(autocvar_sv_defaultplayermodel);
781 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
783 if (!self.noise && self.music) // quake 3 uses the music field
784 self.noise = self.music;
786 // plays music for the level if there is any
789 precache_sound (self.noise);
790 ambientsound ('0 0 0', self.noise, VOL_BASE, ATTEN_NONE);
796 void make_safe_for_remove(entity e)
798 if (e.initialize_entity)
800 entity ent, prev = world;
801 for (ent = initialize_entity_first; ent; )
803 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
805 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
806 // skip it in linked list
809 prev.initialize_entity_next = ent.initialize_entity_next;
810 ent = prev.initialize_entity_next;
814 initialize_entity_first = ent.initialize_entity_next;
815 ent = initialize_entity_first;
821 ent = ent.initialize_entity_next;
827 void objerror(string s)
829 make_safe_for_remove(self);
833 .float remove_except_protected_forbidden;
834 void remove_except_protected(entity e)
836 if(e.remove_except_protected_forbidden)
837 error("not allowed to remove this at this point");
841 void remove_unsafely(entity e)
843 if(e.classname == "spike")
844 error("Removing spikes is forbidden (crylink bug), please report");
848 void remove_safely(entity e)
850 make_safe_for_remove(e);
854 void InitializeEntity(entity e, void(entity this) func, float order)
858 if (!e || e.initialize_entity)
860 // make a proxy initializer entity
862 e = new(initialize_entity);
866 e.initialize_entity = func;
867 e.initialize_entity_order = order;
869 cur = initialize_entity_first;
873 if (!cur || cur.initialize_entity_order > order)
875 // insert between prev and cur
877 prev.initialize_entity_next = e;
879 initialize_entity_first = e;
880 e.initialize_entity_next = cur;
884 cur = cur.initialize_entity_next;
887 void InitializeEntitiesRun()
889 entity startoflist = initialize_entity_first;
890 initialize_entity_first = NULL;
891 remove = remove_except_protected;
892 for (entity e = startoflist; e; e = e.initialize_entity_next)
894 e.remove_except_protected_forbidden = 1;
896 for (entity e = startoflist; e; )
898 e.remove_except_protected_forbidden = 0;
899 e.initialize_entity_order = 0;
900 entity next = e.initialize_entity_next;
901 e.initialize_entity_next = NULL;
902 var void(entity this) func = e.initialize_entity;
903 e.initialize_entity = func_null;
904 if (e.classname == "initialize_entity")
906 entity wrappee = e.enemy;
910 //dprint("Delayed initialization: ", e.classname, "\n");
913 WITHSELF(e, func(e));
918 backtrace(strcat("Null function in: ", e.classname, "\n"));
922 remove = remove_unsafely;
925 .float(entity) isEliminated;
926 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
930 WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
931 WriteByte(MSG_ENTITY, sendflags);
935 for(i = 1; i <= maxclients; i += 8)
937 for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
939 if(eliminatedPlayers.isEliminated(e))
942 WriteByte(MSG_ENTITY, f);
949 void EliminatedPlayers_Init(float(entity) isEliminated_func)
951 if(eliminatedPlayers)
953 backtrace("Can't spawn eliminatedPlayers again!");
956 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
957 eliminatedPlayers.isEliminated = isEliminated_func;
963 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
965 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
966 this.projectiledeathtype |= HITTYPE_SPLASH;
967 WITHSELF(this, adaptor_think2use(this));
971 void DropToFloor_Handler(entity this)
973 WITHSELF(this, builtin_droptofloor());
974 this.dropped_origin = this.origin;
977 void droptofloor(entity this)
979 InitializeEntity(this, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
984 float trace_hits_box_a0, trace_hits_box_a1;
986 float trace_hits_box_1d(float end, float thmi, float thma)
990 // just check if x is in range
998 // do the trace with respect to x
999 // 0 -> end has to stay in thmi -> thma
1000 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1001 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1002 if (trace_hits_box_a0 > trace_hits_box_a1)
1008 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1013 // now it is a trace from 0 to end
1015 trace_hits_box_a0 = 0;
1016 trace_hits_box_a1 = 1;
1018 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1020 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1022 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1028 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1030 return trace_hits_box(start, end, thmi - ma, thma - mi);
1033 bool SUB_NoImpactCheck(entity this, entity toucher)
1035 // zero hitcontents = this is not the real impact, but either the
1036 // mirror-impact of something hitting the projectile instead of the
1037 // projectile hitting the something, or a touchareagrid one. Neither of
1038 // these stop the projectile from moving, so...
1039 if(trace_dphitcontents == 0)
1041 //dprint("A hit happened with zero hit contents... DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct.\n");
1042 LOG_TRACEF("A hit from a projectile happened with no hit contents! DEBUG THIS, this should never happen for projectiles! Profectile will self-destruct. (edict: %d, classname: %s, origin: %s)\n", etof(this), this.classname, vtos(this.origin));
1043 WITHSELF(this, checkclient());
1045 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1047 if (toucher == world && this.size != '0 0 0')
1050 tic = this.velocity * sys_frametime;
1051 tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
1052 traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
1053 if (trace_fraction >= 1)
1055 LOG_TRACE("Odd... did not hit...?\n");
1057 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1059 LOG_TRACE("Detected and prevented the sky-grapple bug.\n");
1067 #define SUB_OwnerCheck(ent,oth) ((oth) && ((oth) == (ent).owner))
1069 void W_Crylink_Dequeue(entity e);
1070 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
1072 if(SUB_OwnerCheck(this, toucher))
1074 if(SUB_NoImpactCheck(this, toucher))
1076 if(this.classname == "nade")
1077 return false; // no checks here
1078 else if(this.classname == "grapplinghook")
1079 RemoveGrapplingHook(this.realowner);
1080 else if(this.classname == "spike")
1082 W_Crylink_Dequeue(this);
1089 if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1090 UpdateCSQCProjectile(this);
1094 /** engine callback */
1095 void URI_Get_Callback(float id, float status, string data)
1097 if(url_URI_Get_Callback(id, status, data))
1101 else if (id == URI_GET_DISCARD)
1105 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1108 Curl_URI_Get_Callback(id, status, data);
1110 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1113 OnlineBanList_URI_Get_Callback(id, status, data);
1115 else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
1117 // handled by a mutator
1121 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1125 string uid2name(string myuid) {
1127 s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1129 // FIXME remove this later after 0.6 release
1130 // convert old style broken records to correct style
1133 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1136 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1137 db_remove(ServerProgsDB, strcat("uid2name", myuid));
1142 s = "^1Unregistered Player";
1146 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1149 vector start, org, delta, end, enddown, mstart;
1152 m = e.dphitcontentsmask;
1153 e.dphitcontentsmask = goodcontents | badcontents;
1156 delta = boundmax - boundmin;
1160 for (i = 0; i < attempts; ++i)
1162 start.x = org.x + random() * delta.x;
1163 start.y = org.y + random() * delta.y;
1164 start.z = org.z + random() * delta.z;
1166 // rule 1: start inside world bounds, and outside
1167 // solid, and don't start from somewhere where you can
1168 // fall down to evil
1169 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1170 if (trace_fraction >= 1)
1172 if (trace_startsolid)
1174 if (trace_dphitcontents & badcontents)
1176 if (trace_dphitq3surfaceflags & badsurfaceflags)
1179 // rule 2: if we are too high, lower the point
1180 if (trace_fraction * delta.z > maxaboveground)
1181 start = trace_endpos + '0 0 1' * maxaboveground;
1182 enddown = trace_endpos;
1184 // rule 3: make sure we aren't outside the map. This only works
1185 // for somewhat well formed maps. A good rule of thumb is that
1186 // the map should have a convex outside hull.
1187 // these can be traceLINES as we already verified the starting box
1188 mstart = start + 0.5 * (e.mins + e.maxs);
1189 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1190 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1192 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1193 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1195 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1196 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1198 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1199 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1201 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1202 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1205 // rule 4: we must "see" some spawnpoint or item
1206 for(sp = world; (sp = find(sp, classname, "info_player_deathmatch")); )
1207 if(checkpvs(mstart, sp))
1208 if((traceline(mstart, sp.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1212 for(sp = world; (sp = findflags(sp, flags, FL_ITEM)); )
1213 if(checkpvs(mstart, sp))
1214 if((traceline(mstart, sp.origin + (sp.mins + sp.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1220 // find a random vector to "look at"
1221 end.x = org.x + random() * delta.x;
1222 end.y = org.y + random() * delta.y;
1223 end.z = org.z + random() * delta.z;
1224 end = start + normalize(end - start) * vlen(delta);
1226 // rule 4: start TO end must not be too short
1227 tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1228 if (trace_startsolid)
1230 if (trace_fraction < minviewdistance / vlen(delta))
1233 // rule 5: don't want to look at sky
1234 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1237 // rule 6: we must not end up in trigger_hurt
1238 if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1244 e.dphitcontentsmask = m;
1248 setorigin(e, start);
1249 e.angles = vectoangles(end - start);
1250 LOG_TRACE("Needed ", ftos(i + 1), " attempts\n");
1257 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1259 return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1262 void write_recordmarker(entity pl, float tstart, float dt)
1264 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1266 // also write a marker into demo files for demotc-race-record-extractor to find
1269 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1270 " ", ftos(tstart), " ", ftos(dt), "\n"));
1273 void attach_sameorigin(entity e, entity to, string tag)
1275 vector org, t_forward, t_left, t_up, e_forward, e_up;
1278 org = e.origin - gettaginfo(to, gettagindex(to, tag));
1279 tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1280 t_forward = v_forward * tagscale;
1281 t_left = v_right * -tagscale;
1282 t_up = v_up * tagscale;
1284 e.origin_x = org * t_forward;
1285 e.origin_y = org * t_left;
1286 e.origin_z = org * t_up;
1288 // current forward and up directions
1289 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1290 e.angles = AnglesTransform_FromVAngles(e.angles);
1292 e.angles = AnglesTransform_FromAngles(e.angles);
1293 fixedmakevectors(e.angles);
1295 // untransform forward, up!
1296 e_forward.x = v_forward * t_forward;
1297 e_forward.y = v_forward * t_left;
1298 e_forward.z = v_forward * t_up;
1299 e_up.x = v_up * t_forward;
1300 e_up.y = v_up * t_left;
1301 e_up.z = v_up * t_up;
1303 e.angles = fixedvectoangles2(e_forward, e_up);
1304 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1305 e.angles = AnglesTransform_ToVAngles(e.angles);
1307 e.angles = AnglesTransform_ToAngles(e.angles);
1309 setattachment(e, to, tag);
1310 setorigin(e, e.origin);
1313 void detach_sameorigin(entity e)
1316 org = gettaginfo(e, 0);
1317 e.angles = fixedvectoangles2(v_forward, v_up);
1318 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1319 e.angles = AnglesTransform_ToVAngles(e.angles);
1321 e.angles = AnglesTransform_ToAngles(e.angles);
1323 setattachment(e, world, "");
1324 setorigin(e, e.origin);
1327 void follow_sameorigin(entity e, entity to)
1329 e.movetype = MOVETYPE_FOLLOW; // make the hole follow
1330 e.aiment = to; // make the hole follow bmodel
1331 e.punchangle = to.angles; // the original angles of bmodel
1332 e.view_ofs = e.origin - to.origin; // relative origin
1333 e.v_angle = e.angles - to.angles; // relative angles
1336 void unfollow_sameorigin(entity e)
1338 e.movetype = MOVETYPE_NONE;
1341 entity gettaginfo_relative_ent;
1342 vector gettaginfo_relative(entity e, float tag)
1344 if (!gettaginfo_relative_ent)
1346 gettaginfo_relative_ent = spawn();
1347 gettaginfo_relative_ent.effects = EF_NODRAW;
1349 gettaginfo_relative_ent.model = e.model;
1350 gettaginfo_relative_ent.modelindex = e.modelindex;
1351 gettaginfo_relative_ent.frame = e.frame;
1352 return gettaginfo(gettaginfo_relative_ent, tag);
1355 .string aiment_classname;
1356 .float aiment_deadflag;
1357 void SetMovetypeFollow(entity ent, entity e)
1359 // FIXME this may not be warpzone aware
1360 ent.movetype = MOVETYPE_FOLLOW; // make the hole follow
1361 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.
1362 ent.aiment = e; // make the hole follow bmodel
1363 ent.punchangle = e.angles; // the original angles of bmodel
1364 ent.view_ofs = ent.origin - e.origin; // relative origin
1365 ent.v_angle = ent.angles - e.angles; // relative angles
1366 ent.aiment_classname = strzone(e.classname);
1367 ent.aiment_deadflag = e.deadflag;
1369 void UnsetMovetypeFollow(entity ent)
1371 ent.movetype = MOVETYPE_FLY;
1372 PROJECTILE_MAKETRIGGER(ent);
1375 float LostMovetypeFollow(entity ent)
1378 if(ent.movetype != MOVETYPE_FOLLOW)
1384 if(ent.aiment.classname != ent.aiment_classname)
1386 if(ent.aiment.deadflag != ent.aiment_deadflag)
1393 bool isPushable(entity e)
1404 case "droppedweapon":
1406 case "bullet": // antilagged bullets can't hit this either
1409 if (e.projectiledeathtype)