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.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 "../lib/csqcmodel/sv_model.qh"
27 #include "../lib/warpzone/anglestransform.qh"
28 #include "../lib/warpzone/server.qh"
30 void crosshair_trace(entity pl)
32 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));
34 void crosshair_trace_plusvisibletriggers(entity pl)
38 first = findchainfloat(solid, SOLID_TRIGGER);
40 for (e = first; e; e = e.chain)
46 for (e = first; e; e = e.chain)
47 e.solid = SOLID_TRIGGER;
49 void WarpZone_crosshair_trace(entity pl)
51 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));
57 if(autocvar_sv_adminnick != "")
58 return autocvar_sv_adminnick;
60 return "SERVER ADMIN";
64 void GameLogEcho(string s)
69 if (autocvar_sv_eventlog_files)
74 matches = autocvar_sv_eventlog_files_counter + 1;
75 cvar_set("sv_eventlog_files_counter", itos(matches));
78 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
79 fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
80 logfile = fopen(fn, FILE_APPEND);
81 fputs(logfile, ":logversion:3\n");
85 if (autocvar_sv_eventlog_files_timestamps)
86 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
88 fputs(logfile, strcat(s, "\n"));
91 if (autocvar_sv_eventlog_console)
100 // will be opened later
105 if (logfile_open && logfile >= 0)
112 entity findnearest(vector point, .string field, string value, vector axismod)
123 localhead = find(world, field, value);
126 if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
127 dist = localhead.oldorigin;
129 dist = localhead.origin;
131 dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
134 for (i = 0; i < num_nearest; ++i)
136 if (len < nearest_length[i])
140 // now i tells us where to insert at
141 // INSERTION SORT! YOU'VE SEEN IT! RUN!
142 if (i < NUM_NEAREST_ENTITIES)
144 for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
146 nearest_length[j + 1] = nearest_length[j];
147 nearest_entity[j + 1] = nearest_entity[j];
149 nearest_length[i] = len;
150 nearest_entity[i] = localhead;
151 if (num_nearest < NUM_NEAREST_ENTITIES)
152 num_nearest = num_nearest + 1;
155 localhead = find(localhead, field, value);
158 // now use the first one from our list that we can see
159 for (i = 0; i < num_nearest; ++i)
161 traceline(point, nearest_entity[i].origin, true, world);
162 if (trace_fraction == 1)
166 LOG_TRACE("Nearest point (");
167 LOG_TRACE(nearest_entity[0].netname);
168 LOG_TRACE(") is not visible, using a visible one.\n");
170 return nearest_entity[i];
174 if (num_nearest == 0)
177 LOG_TRACE("Not seeing any location point, using nearest as fallback.\n");
179 dprint("Candidates were: ");
180 for(j = 0; j < num_nearest; ++j)
184 dprint(nearest_entity[j].netname);
189 return nearest_entity[0];
192 string NearestLocation(vector p)
197 loc = findnearest(p, classname, "target_location", '1 1 1');
204 loc = findnearest(p, target, "###item###", '1 1 4');
211 string formatmessage(string msg)
223 ammoitems = "batteries";
224 if(self.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
225 if(self.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
226 if(self.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
227 if(self.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
229 WarpZone_crosshair_trace(self);
230 cursor = trace_endpos;
231 cursor_ent = trace_ent;
235 break; // too many replacements
238 p1 = strstr(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
239 p2 = strstr(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
252 replacement = substring(msg, p, 2);
253 escape = substring(msg, p + 1, 1);
257 case "%": replacement = "%"; break;
258 case "\\":replacement = "\\"; break;
259 case "n": replacement = "\n"; break;
260 case "a": replacement = ftos(floor(self.armorvalue)); break;
261 case "h": replacement = ftos(floor(self.health)); break;
262 case "l": replacement = NearestLocation(self.origin); break;
263 case "y": replacement = NearestLocation(cursor); break;
264 case "d": replacement = NearestLocation(self.death_origin); break;
265 case "w": replacement = ((PS(self).m_weapon == WEP_Null) ? ((PS(self).m_switchweapon == WEP_Null) ? Weapons_from(self.cnt) : PS(self).m_switchweapon) : PS(self).m_weapon).m_name; break;
266 case "W": replacement = ammoitems; break;
267 case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
268 case "s": replacement = ftos(vlen(self.velocity - self.velocity_z * '0 0 1')); break;
269 case "S": replacement = ftos(vlen(self.velocity)); break;
270 case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
271 case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
274 MUTATOR_CALLHOOK(FormatMessage, escape, replacement, msg);
275 escape = format_escape;
276 replacement = format_replacement;
281 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
282 p = p + strlen(replacement);
293 >0: receives a cvar from name=argv(f) value=argv(f+1)
295 void GetCvars_handleString(string thisname, float f, .string field, string name)
300 strunzone(self.(field));
301 self.(field) = string_null;
305 if (thisname == name)
308 strunzone(self.(field));
309 self.(field) = strzone(argv(f + 1));
313 stuffcmd(self, strcat("cl_cmd sendcvar ", name, "\n"));
315 void GetCvars_handleString_Fixup(string thisname, float f, .string field, string name, string(string) func)
317 GetCvars_handleString(thisname, f, field, name);
318 if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
319 if (thisname == name)
321 string s = func(strcat1(self.(field)));
322 if (s != self.(field))
324 strunzone(self.(field));
325 self.(field) = strzone(s);
329 void GetCvars_handleFloat(string thisname, float f, .float field, string name)
336 if (thisname == name)
337 self.(field) = stof(argv(f + 1));
340 stuffcmd(self, strcat("cl_cmd sendcvar ", name, "\n"));
342 void GetCvars_handleFloatOnce(string thisname, float f, .float field, string name)
349 if (thisname == name)
353 self.(field) = stof(argv(f + 1));
362 stuffcmd(self, strcat("cl_cmd sendcvar ", name, "\n"));
365 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(string wo)
368 o = W_FixWeaponOrder_ForceComplete(wo);
369 if(self.weaponorder_byimpulse)
371 strunzone(self.weaponorder_byimpulse);
372 self.weaponorder_byimpulse = string_null;
374 self.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
378 REPLICATE(autoswitch, bool, "cl_autoswitch");
380 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
382 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
384 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
386 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
388 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
390 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
392 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
394 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
396 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
398 REPLICATE(cvar_cl_physics, string, "cl_physics");
400 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
402 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
404 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
406 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
409 * @param f -1: cleanup, 0: request, 1: receive
413 string s = string_null;
416 s = strcat1(argv(f));
420 MUTATOR_CALLHOOK(GetCvars);
422 Notification_GetCvars();
424 ReplicateVars(this, s, f);
426 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
427 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
428 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
429 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
430 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
431 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
432 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
433 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
434 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
435 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
436 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
438 GetCvars_handleFloat(s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
440 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
443 if (s == "cl_weaponpriority")
444 if (PS(self)) PS(self).m_switchweapon = w_getbestweapon(self);
445 if (s == "cl_allow_uidtracking")
446 PlayerStats_GameReport_AddPlayer(self);
450 // decolorizes and team colors the player name when needed
451 string playername(entity p)
454 if (teamplay && !intermission_running && IS_PLAYER(p))
456 t = Team_ColorCode(p.team);
457 return strcat(t, strdecolorize(p.netname));
463 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
465 int i = weaponinfo.m_id;
467 bool allow_mutatorblocked = false;
472 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
474 allguns = want_allguns;
475 allow_mutatorblocked = false;
479 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
484 else if(!mutator_returnvalue)
485 d = !(!weaponinfo.weaponstart);
487 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
490 float t = weaponinfo.weaponstartoverride;
492 //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
497 // 4: is set by default?
506 void readplayerstartcvars()
511 // initialize starting values for players
512 start_weapons = '0 0 0';
513 start_weapons_default = '0 0 0';
514 start_weapons_defaultmask = '0 0 0';
516 start_ammo_shells = 0;
517 start_ammo_nails = 0;
518 start_ammo_rockets = 0;
519 start_ammo_cells = 0;
520 start_ammo_plasma = 0;
521 start_health = cvar("g_balance_health_start");
522 start_armorvalue = cvar("g_balance_armor_start");
525 g_weaponarena_weapons = '0 0 0';
527 s = cvar_string("g_weaponarena");
529 MUTATOR_CALLHOOK(SetWeaponArena, s);
532 if (s == "0" || s == "")
538 // forcibly turn off weaponarena
540 else if (s == "all" || s == "1")
543 g_weaponarena_list = "All Weapons";
544 FOREACH(Weapons, it != WEP_Null, LAMBDA(
545 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
546 g_weaponarena_weapons |= (it.m_wepset);
549 else if (s == "most")
552 g_weaponarena_list = "Most Weapons";
553 FOREACH(Weapons, it != WEP_Null, LAMBDA(
554 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
555 if(it.spawnflags & WEP_FLAG_NORMAL)
556 g_weaponarena_weapons |= (it.m_wepset);
559 else if (s == "none")
562 g_weaponarena_list = "No Weapons";
567 t = tokenize_console(s);
568 g_weaponarena_list = "";
569 for (i = 0; i < t; ++i)
572 FOREACH(Weapons, it != WEP_Null, LAMBDA(
575 g_weaponarena_weapons |= (it.m_wepset);
576 g_weaponarena_list = strcat(g_weaponarena_list, it.m_name, " & ");
581 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
585 g_weaponarena_random = cvar("g_weaponarena_random");
587 g_weaponarena_random = 0;
588 g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
592 g_weapon_stay = 0; // incompatible
593 start_weapons = g_weaponarena_weapons;
594 start_items |= IT_UNLIMITED_AMMO;
598 FOREACH(Weapons, it != WEP_Null, LAMBDA(
599 int w = want_weapon(it, false);
600 WepSet s = it.m_wepset;
604 start_weapons_default |= s;
606 start_weapons_defaultmask |= s;
610 if(!cvar("g_use_ammunition"))
611 start_items |= IT_UNLIMITED_AMMO;
613 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
615 start_ammo_shells = 999;
616 start_ammo_nails = 999;
617 start_ammo_rockets = 999;
618 start_ammo_cells = 999;
619 start_ammo_plasma = 999;
620 start_ammo_fuel = 999;
624 start_ammo_shells = cvar("g_start_ammo_shells");
625 start_ammo_nails = cvar("g_start_ammo_nails");
626 start_ammo_rockets = cvar("g_start_ammo_rockets");
627 start_ammo_cells = cvar("g_start_ammo_cells");
628 start_ammo_plasma = cvar("g_start_ammo_plasma");
629 start_ammo_fuel = cvar("g_start_ammo_fuel");
634 warmup_start_ammo_shells = start_ammo_shells;
635 warmup_start_ammo_nails = start_ammo_nails;
636 warmup_start_ammo_rockets = start_ammo_rockets;
637 warmup_start_ammo_cells = start_ammo_cells;
638 warmup_start_ammo_plasma = start_ammo_plasma;
639 warmup_start_ammo_fuel = start_ammo_fuel;
640 warmup_start_health = start_health;
641 warmup_start_armorvalue = start_armorvalue;
642 warmup_start_weapons = start_weapons;
643 warmup_start_weapons_default = start_weapons_default;
644 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
646 if (!g_weaponarena && !g_ca && !g_freezetag)
648 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
649 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
650 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
651 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
652 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
653 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
654 warmup_start_health = cvar("g_warmup_start_health");
655 warmup_start_armorvalue = cvar("g_warmup_start_armor");
656 warmup_start_weapons = '0 0 0';
657 warmup_start_weapons_default = '0 0 0';
658 warmup_start_weapons_defaultmask = '0 0 0';
659 FOREACH(Weapons, it != WEP_Null, LAMBDA(
660 int w = want_weapon(it, g_warmup_allguns);
661 WepSet s = (it.m_wepset);
663 warmup_start_weapons |= s;
665 warmup_start_weapons_default |= s;
667 warmup_start_weapons_defaultmask |= s;
673 start_items |= ITEM_Jetpack.m_itemid;
675 MUTATOR_CALLHOOK(SetStartItems);
677 if (start_items & ITEM_Jetpack.m_itemid)
679 start_items |= ITEM_JetpackRegen.m_itemid;
680 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
681 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
684 WepSet precache_weapons = start_weapons;
685 if (g_warmup_allguns != 1)
686 precache_weapons |= warmup_start_weapons;
687 FOREACH(Weapons, it != WEP_Null, LAMBDA(
688 if(precache_weapons & (it.m_wepset))
692 start_ammo_shells = max(0, start_ammo_shells);
693 start_ammo_nails = max(0, start_ammo_nails);
694 start_ammo_rockets = max(0, start_ammo_rockets);
695 start_ammo_cells = max(0, start_ammo_cells);
696 start_ammo_plasma = max(0, start_ammo_plasma);
697 start_ammo_fuel = max(0, start_ammo_fuel);
699 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
700 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
701 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
702 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
703 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
704 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
707 void precache_playermodel(string m)
709 float globhandle, i, n;
712 if(substring(m, -9,5) == "_lod1")
714 if(substring(m, -9,5) == "_lod2")
717 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
720 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
724 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
727 n = search_getsize(globhandle);
728 for (i = 0; i < n; ++i)
730 //print(search_getfilename(globhandle, i), "\n");
731 f = search_getfilename(globhandle, i);
732 PrecachePlayerSounds(f);
734 search_end(globhandle);
736 void precache_all_playermodels(string pattern)
738 int globhandle = search_begin(pattern, true, false);
739 if (globhandle < 0) return;
740 int n = search_getsize(globhandle);
741 for (int i = 0; i < n; ++i)
743 string s = search_getfilename(globhandle, i);
744 precache_playermodel(s);
746 search_end(globhandle);
749 void precache_playermodels(string s)
751 FOREACH_WORD(s, true, LAMBDA(precache_playermodel(it)));
756 // gamemode related things
758 // Precache all player models if desired
759 if (autocvar_sv_precacheplayermodels)
761 PrecachePlayerSounds("sound/player/default.sounds");
762 precache_all_playermodels("models/player/*.zym");
763 precache_all_playermodels("models/player/*.dpm");
764 precache_all_playermodels("models/player/*.md3");
765 precache_all_playermodels("models/player/*.psk");
766 precache_all_playermodels("models/player/*.iqm");
769 if (autocvar_sv_defaultcharacter)
771 precache_playermodels(autocvar_sv_defaultplayermodel_red);
772 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
773 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
774 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
775 precache_playermodels(autocvar_sv_defaultplayermodel);
779 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
781 if (!self.noise && self.music) // quake 3 uses the music field
782 self.noise = self.music;
784 // plays music for the level if there is any
787 precache_sound (self.noise);
788 ambientsound ('0 0 0', self.noise, VOL_BASE, ATTEN_NONE);
794 void make_safe_for_remove(entity e)
796 if (e.initialize_entity)
798 entity ent, prev = world;
799 for (ent = initialize_entity_first; ent; )
801 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
803 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
804 // skip it in linked list
807 prev.initialize_entity_next = ent.initialize_entity_next;
808 ent = prev.initialize_entity_next;
812 initialize_entity_first = ent.initialize_entity_next;
813 ent = initialize_entity_first;
819 ent = ent.initialize_entity_next;
825 void objerror(string s)
827 make_safe_for_remove(self);
831 .float remove_except_protected_forbidden;
832 void remove_except_protected(entity e)
834 if(e.remove_except_protected_forbidden)
835 error("not allowed to remove this at this point");
839 void remove_unsafely(entity e)
841 if(e.classname == "spike")
842 error("Removing spikes is forbidden (crylink bug), please report");
846 void remove_safely(entity e)
848 make_safe_for_remove(e);
852 void InitializeEntity(entity e, void() func, float order)
856 if (!e || e.initialize_entity)
858 // make a proxy initializer entity
860 e = new(initialize_entity);
864 e.initialize_entity = func;
865 e.initialize_entity_order = order;
867 cur = initialize_entity_first;
871 if (!cur || cur.initialize_entity_order > order)
873 // insert between prev and cur
875 prev.initialize_entity_next = e;
877 initialize_entity_first = e;
878 e.initialize_entity_next = cur;
882 cur = cur.initialize_entity_next;
885 void InitializeEntitiesRun()
887 entity startoflist = initialize_entity_first;
888 initialize_entity_first = NULL;
889 remove = remove_except_protected;
890 for (entity e = startoflist; e; e = e.initialize_entity_next)
892 e.remove_except_protected_forbidden = 1;
894 for (entity e = startoflist; e; )
896 e.remove_except_protected_forbidden = 0;
897 e.initialize_entity_order = 0;
898 entity next = e.initialize_entity_next;
899 e.initialize_entity_next = NULL;
900 var void() func = e.initialize_entity;
901 e.initialize_entity = func_null;
902 if (e.classname == "initialize_entity")
904 entity wrappee = e.enemy;
908 //dprint("Delayed initialization: ", e.classname, "\n");
911 WITH(entity, self, e, func());
916 backtrace(strcat("Null function in: ", e.classname, "\n"));
920 remove = remove_unsafely;
923 .float(entity) isEliminated;
924 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
928 WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
929 WriteByte(MSG_ENTITY, sendflags);
933 for(i = 1; i <= maxclients; i += 8)
935 for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
937 if(eliminatedPlayers.isEliminated(e))
940 WriteByte(MSG_ENTITY, f);
947 void EliminatedPlayers_Init(float(entity) isEliminated_func)
949 if(eliminatedPlayers)
951 backtrace("Can't spawn eliminatedPlayers again!");
954 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
955 eliminatedPlayers.isEliminated = isEliminated_func;
959 void adaptor_think2touch()
968 void adaptor_think2use()
980 void adaptor_think2use_hittype_splash() // for timed projectile detonation
982 if(!(IS_ONGROUND(self))) // if onground, we ARE touching something, but HITTYPE_SPLASH is to be networked if the damage causing projectile is not touching ANYTHING
983 self.projectiledeathtype |= HITTYPE_SPLASH;
988 void DropToFloor_Handler()
990 builtin_droptofloor();
991 self.dropped_origin = self.origin;
996 InitializeEntity(self, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
1001 float trace_hits_box_a0, trace_hits_box_a1;
1003 float trace_hits_box_1d(float end, float thmi, float thma)
1007 // just check if x is in range
1015 // do the trace with respect to x
1016 // 0 -> end has to stay in thmi -> thma
1017 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1018 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1019 if (trace_hits_box_a0 > trace_hits_box_a1)
1025 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1030 // now it is a trace from 0 to end
1032 trace_hits_box_a0 = 0;
1033 trace_hits_box_a1 = 1;
1035 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1037 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1039 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1045 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1047 return trace_hits_box(start, end, thmi - ma, thma - mi);
1050 float SUB_NoImpactCheck()
1052 // zero hitcontents = this is not the real impact, but either the
1053 // mirror-impact of something hitting the projectile instead of the
1054 // projectile hitting the something, or a touchareagrid one. Neither of
1055 // these stop the projectile from moving, so...
1056 if(trace_dphitcontents == 0)
1058 //dprint("A hit happened with zero hit contents... DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct.\n");
1059 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(self), self.classname, vtos(self.origin));
1062 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1064 if (other == world && self.size != '0 0 0')
1067 tic = self.velocity * sys_frametime;
1068 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
1069 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
1070 if (trace_fraction >= 1)
1072 LOG_TRACE("Odd... did not hit...?\n");
1074 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1076 LOG_TRACE("Detected and prevented the sky-grapple bug.\n");
1084 #define SUB_OwnerCheck() (other && (other == self.owner))
1086 void W_Crylink_Dequeue(entity e);
1087 float WarpZone_Projectile_Touch_ImpactFilter_Callback()
1089 if(SUB_OwnerCheck())
1091 if(SUB_NoImpactCheck())
1093 if(self.classname == "nade")
1094 return false; // no checks here
1095 else if(self.classname == "grapplinghook")
1096 RemoveGrapplingHook(self.realowner);
1097 else if(self.classname == "spike")
1099 W_Crylink_Dequeue(self);
1106 if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1107 UpdateCSQCProjectile(self);
1111 /** engine callback */
1112 void URI_Get_Callback(float id, float status, string data)
1114 if(url_URI_Get_Callback(id, status, data))
1118 else if (id == URI_GET_DISCARD)
1122 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1125 Curl_URI_Get_Callback(id, status, data);
1127 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1130 OnlineBanList_URI_Get_Callback(id, status, data);
1132 else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
1134 // handled by a mutator
1138 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1142 string uid2name(string myuid) {
1144 s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1146 // FIXME remove this later after 0.6 release
1147 // convert old style broken records to correct style
1150 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1153 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1154 db_remove(ServerProgsDB, strcat("uid2name", myuid));
1159 s = "^1Unregistered Player";
1163 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1166 vector start, org, delta, end, enddown, mstart;
1169 m = e.dphitcontentsmask;
1170 e.dphitcontentsmask = goodcontents | badcontents;
1173 delta = boundmax - boundmin;
1177 for (i = 0; i < attempts; ++i)
1179 start.x = org.x + random() * delta.x;
1180 start.y = org.y + random() * delta.y;
1181 start.z = org.z + random() * delta.z;
1183 // rule 1: start inside world bounds, and outside
1184 // solid, and don't start from somewhere where you can
1185 // fall down to evil
1186 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1187 if (trace_fraction >= 1)
1189 if (trace_startsolid)
1191 if (trace_dphitcontents & badcontents)
1193 if (trace_dphitq3surfaceflags & badsurfaceflags)
1196 // rule 2: if we are too high, lower the point
1197 if (trace_fraction * delta.z > maxaboveground)
1198 start = trace_endpos + '0 0 1' * maxaboveground;
1199 enddown = trace_endpos;
1201 // rule 3: make sure we aren't outside the map. This only works
1202 // for somewhat well formed maps. A good rule of thumb is that
1203 // the map should have a convex outside hull.
1204 // these can be traceLINES as we already verified the starting box
1205 mstart = start + 0.5 * (e.mins + e.maxs);
1206 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1207 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1209 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1210 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1212 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1213 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1215 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1216 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1218 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1219 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1222 // rule 4: we must "see" some spawnpoint or item
1223 for(sp = world; (sp = find(sp, classname, "info_player_deathmatch")); )
1224 if(checkpvs(mstart, sp))
1225 if((traceline(mstart, sp.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1229 for(sp = world; (sp = findflags(sp, flags, FL_ITEM)); )
1230 if(checkpvs(mstart, sp))
1231 if((traceline(mstart, sp.origin + (sp.mins + sp.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\n");
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, world, "");
1341 setorigin(e, e.origin);
1344 void follow_sameorigin(entity e, entity to)
1346 e.movetype = 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 e.movetype = 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 ent.movetype = 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 ent.movetype = MOVETYPE_FLY;
1389 PROJECTILE_MAKETRIGGER(ent);
1392 float LostMovetypeFollow(entity ent)
1395 if(ent.movetype != MOVETYPE_FOLLOW)
1401 if(ent.aiment.classname != ent.aiment_classname)
1403 if(ent.aiment.deadflag != ent.aiment_deadflag)
1409 float isPushable(entity e)
1420 case "droppedweapon":
1421 case "keepawayball":
1422 case "nexball_basketball":
1423 case "nexball_football":
1425 case "bullet": // antilagged bullets can't hit this either
1428 if (e.projectiledeathtype)