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/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/_mod.qh"
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 .bool ctrace_solidchanged;
37 void crosshair_trace_plusvisibletriggers(entity pl)
39 FOREACH_ENTITY_FLOAT(solid, SOLID_TRIGGER,
44 it.ctrace_solidchanged = true;
50 FOREACH_ENTITY_FLOAT(ctrace_solidchanged, true,
52 it.solid = SOLID_TRIGGER;
53 it.ctrace_solidchanged = false;
56 void WarpZone_crosshair_trace(entity pl)
58 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));
64 if(autocvar_sv_adminnick != "")
65 return autocvar_sv_adminnick;
67 return "SERVER ADMIN";
71 void GameLogEcho(string s)
76 if (autocvar_sv_eventlog_files)
81 matches = autocvar_sv_eventlog_files_counter + 1;
82 cvar_set("sv_eventlog_files_counter", itos(matches));
85 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
86 fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
87 logfile = fopen(fn, FILE_APPEND);
88 fputs(logfile, ":logversion:3\n");
92 if (autocvar_sv_eventlog_files_timestamps)
93 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
95 fputs(logfile, strcat(s, "\n"));
98 if (autocvar_sv_eventlog_console)
100 dedicated_print(strcat(s, "\n"));
107 // will be opened later
112 if (logfile_open && logfile >= 0)
119 entity findnearest(vector point, .string field, string value, vector axismod)
130 localhead = find(NULL, field, value);
133 if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
134 dist = localhead.oldorigin;
136 dist = localhead.origin;
138 dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
141 for (i = 0; i < num_nearest; ++i)
143 if (len < nearest_length[i])
147 // now i tells us where to insert at
148 // INSERTION SORT! YOU'VE SEEN IT! RUN!
149 if (i < NUM_NEAREST_ENTITIES)
151 for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
153 nearest_length[j + 1] = nearest_length[j];
154 nearest_entity[j + 1] = nearest_entity[j];
156 nearest_length[i] = len;
157 nearest_entity[i] = localhead;
158 if (num_nearest < NUM_NEAREST_ENTITIES)
159 num_nearest = num_nearest + 1;
162 localhead = find(localhead, field, value);
165 // now use the first one from our list that we can see
166 for (i = 0; i < num_nearest; ++i)
168 traceline(point, nearest_entity[i].origin, true, NULL);
169 if (trace_fraction == 1)
173 LOG_TRACE("Nearest point (");
174 LOG_TRACE(nearest_entity[0].netname);
175 LOG_TRACE(") is not visible, using a visible one.");
177 return nearest_entity[i];
181 if (num_nearest == 0)
184 LOG_TRACE("Not seeing any location point, using nearest as fallback.");
186 dprint("Candidates were: ");
187 for(j = 0; j < num_nearest; ++j)
191 dprint(nearest_entity[j].netname);
196 return nearest_entity[0];
199 string NearestLocation(vector p)
204 loc = findnearest(p, classname, "target_location", '1 1 1');
211 loc = findnearest(p, target, "###item###", '1 1 4');
218 string formatmessage(entity this, string msg)
230 ammoitems = "batteries";
231 if(this.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
232 if(this.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
233 if(this.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
234 if(this.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
236 WarpZone_crosshair_trace(this);
237 cursor = trace_endpos;
238 cursor_ent = trace_ent;
242 break; // too many replacements
245 p1 = strstrofs(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
246 p2 = strstrofs(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
259 replacement = substring(msg, p, 2);
260 escape = substring(msg, p + 1, 1);
262 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
266 case "%": replacement = "%"; break;
267 case "\\":replacement = "\\"; break;
268 case "n": replacement = "\n"; break;
269 case "a": replacement = ftos(floor(this.armorvalue)); break;
270 case "h": replacement = ftos(floor(this.health)); break;
271 case "l": replacement = NearestLocation(this.origin); break;
272 case "y": replacement = NearestLocation(cursor); break;
273 case "d": replacement = NearestLocation(this.death_origin); break;
274 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;
275 case "W": replacement = ammoitems; break;
276 case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
277 case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
278 case "S": replacement = ftos(vlen(this.velocity)); break;
279 case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
280 case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
283 MUTATOR_CALLHOOK(FormatMessage, this, escape, replacement, msg);
284 replacement = M_ARGV(2, string);
289 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
290 p = p + strlen(replacement);
301 >0: receives a cvar from name=argv(f) value=argv(f+1)
303 void GetCvars_handleString(entity this, string thisname, float f, .string field, string name)
308 strunzone(this.(field));
309 this.(field) = string_null;
313 if (thisname == name)
316 strunzone(this.(field));
317 this.(field) = strzone(argv(f + 1));
321 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
323 void GetCvars_handleString_Fixup(entity this, string thisname, float f, .string field, string name, string(entity, string) func)
325 GetCvars_handleString(this, thisname, f, field, name);
326 if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
327 if (thisname == name)
329 string s = func(this, strcat1(this.(field)));
330 if (s != this.(field))
332 strunzone(this.(field));
333 this.(field) = strzone(s);
337 void GetCvars_handleFloat(entity this, string thisname, float f, .float field, string name)
344 if (thisname == name)
345 this.(field) = stof(argv(f + 1));
348 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
350 void GetCvars_handleFloatOnce(entity this, string thisname, float f, .float field, string name)
357 if (thisname == name)
361 this.(field) = stof(argv(f + 1));
370 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
373 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
376 o = W_FixWeaponOrder_ForceComplete(wo);
377 if(this.weaponorder_byimpulse)
379 strunzone(this.weaponorder_byimpulse);
380 this.weaponorder_byimpulse = string_null;
382 this.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
386 REPLICATE(autoswitch, bool, "cl_autoswitch");
388 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
390 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
392 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
394 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
396 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
398 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
400 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
402 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
404 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
406 REPLICATE(cvar_cl_physics, string, "cl_physics");
408 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
410 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
412 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
414 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
417 * @param f -1: cleanup, 0: request, 1: receive
419 void GetCvars(entity this, int f)
421 string s = string_null;
424 s = strcat1(argv(f));
428 MUTATOR_CALLHOOK(GetCvars);
430 Notification_GetCvars(this);
432 ReplicateVars(this, s, f);
434 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
435 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
436 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
437 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
438 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
439 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
440 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
441 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
442 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
443 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
444 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
446 GetCvars_handleFloat(this, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
448 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
451 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
452 if (s == "cl_weaponpriority")
453 if (this.(weaponentity)) this.(weaponentity).m_switchweapon = w_getbestweapon(this);
454 if (s == "cl_allow_uidtracking")
455 PlayerStats_GameReport_AddPlayer(this);
459 // decolorizes and team colors the player name when needed
460 string playername(entity p)
463 if (teamplay && !intermission_running && IS_PLAYER(p))
465 t = Team_ColorCode(p.team);
466 return strcat(t, strdecolorize(p.netname));
472 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
474 int i = weaponinfo.m_id;
476 bool allow_mutatorblocked = false;
481 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
482 d = M_ARGV(1, float);
483 allguns = M_ARGV(2, bool);
484 allow_mutatorblocked = M_ARGV(3, bool);
488 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
493 else if(!mutator_returnvalue)
494 d = !(!weaponinfo.weaponstart);
496 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
499 float t = weaponinfo.weaponstartoverride;
501 //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
506 // 4: is set by default?
515 void readplayerstartcvars()
520 // initialize starting values for players
521 start_weapons = '0 0 0';
522 start_weapons_default = '0 0 0';
523 start_weapons_defaultmask = '0 0 0';
525 start_ammo_shells = 0;
526 start_ammo_nails = 0;
527 start_ammo_rockets = 0;
528 start_ammo_cells = 0;
529 start_ammo_plasma = 0;
530 start_health = cvar("g_balance_health_start");
531 start_armorvalue = cvar("g_balance_armor_start");
534 g_weaponarena_weapons = '0 0 0';
536 s = cvar_string("g_weaponarena");
538 MUTATOR_CALLHOOK(SetWeaponArena, s);
539 s = M_ARGV(0, string);
541 if (s == "0" || s == "")
547 // forcibly turn off weaponarena
549 else if (s == "all" || s == "1")
552 g_weaponarena_list = "All Weapons";
553 FOREACH(Weapons, it != WEP_Null, LAMBDA(
554 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
555 g_weaponarena_weapons |= (it.m_wepset);
558 else if (s == "most")
561 g_weaponarena_list = "Most Weapons";
562 FOREACH(Weapons, it != WEP_Null, LAMBDA(
563 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
564 if(it.spawnflags & WEP_FLAG_NORMAL)
565 g_weaponarena_weapons |= (it.m_wepset);
568 else if (s == "none")
571 g_weaponarena_list = "No Weapons";
576 t = tokenize_console(s);
577 g_weaponarena_list = "";
578 for (i = 0; i < t; ++i)
581 FOREACH(Weapons, it != WEP_Null, LAMBDA(
584 g_weaponarena_weapons |= (it.m_wepset);
585 g_weaponarena_list = strcat(g_weaponarena_list, it.m_name, " & ");
590 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
594 g_weaponarena_random = cvar("g_weaponarena_random");
596 g_weaponarena_random = 0;
597 g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
601 g_weapon_stay = 0; // incompatible
602 start_weapons = g_weaponarena_weapons;
603 start_items |= IT_UNLIMITED_AMMO;
607 FOREACH(Weapons, it != WEP_Null, LAMBDA(
608 int w = want_weapon(it, false);
609 WepSet s = it.m_wepset;
613 start_weapons_default |= s;
615 start_weapons_defaultmask |= s;
619 if(!cvar("g_use_ammunition"))
620 start_items |= IT_UNLIMITED_AMMO;
622 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
624 start_ammo_shells = 999;
625 start_ammo_nails = 999;
626 start_ammo_rockets = 999;
627 start_ammo_cells = 999;
628 start_ammo_plasma = 999;
629 start_ammo_fuel = 999;
633 start_ammo_shells = cvar("g_start_ammo_shells");
634 start_ammo_nails = cvar("g_start_ammo_nails");
635 start_ammo_rockets = cvar("g_start_ammo_rockets");
636 start_ammo_cells = cvar("g_start_ammo_cells");
637 start_ammo_plasma = cvar("g_start_ammo_plasma");
638 start_ammo_fuel = cvar("g_start_ammo_fuel");
643 warmup_start_ammo_shells = start_ammo_shells;
644 warmup_start_ammo_nails = start_ammo_nails;
645 warmup_start_ammo_rockets = start_ammo_rockets;
646 warmup_start_ammo_cells = start_ammo_cells;
647 warmup_start_ammo_plasma = start_ammo_plasma;
648 warmup_start_ammo_fuel = start_ammo_fuel;
649 warmup_start_health = start_health;
650 warmup_start_armorvalue = start_armorvalue;
651 warmup_start_weapons = start_weapons;
652 warmup_start_weapons_default = start_weapons_default;
653 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
655 if (!g_weaponarena && !g_ca && !g_freezetag)
657 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
658 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
659 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
660 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
661 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
662 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
663 warmup_start_health = cvar("g_warmup_start_health");
664 warmup_start_armorvalue = cvar("g_warmup_start_armor");
665 warmup_start_weapons = '0 0 0';
666 warmup_start_weapons_default = '0 0 0';
667 warmup_start_weapons_defaultmask = '0 0 0';
668 FOREACH(Weapons, it != WEP_Null, LAMBDA(
669 int w = want_weapon(it, g_warmup_allguns);
670 WepSet s = (it.m_wepset);
672 warmup_start_weapons |= s;
674 warmup_start_weapons_default |= s;
676 warmup_start_weapons_defaultmask |= s;
682 start_items |= ITEM_Jetpack.m_itemid;
684 MUTATOR_CALLHOOK(SetStartItems);
686 if (start_items & ITEM_Jetpack.m_itemid)
688 start_items |= ITEM_JetpackRegen.m_itemid;
689 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
690 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
693 WepSet precache_weapons = start_weapons;
694 if (g_warmup_allguns != 1)
695 precache_weapons |= warmup_start_weapons;
696 FOREACH(Weapons, it != WEP_Null, LAMBDA(
697 if(precache_weapons & (it.m_wepset))
701 start_ammo_shells = max(0, start_ammo_shells);
702 start_ammo_nails = max(0, start_ammo_nails);
703 start_ammo_rockets = max(0, start_ammo_rockets);
704 start_ammo_cells = max(0, start_ammo_cells);
705 start_ammo_plasma = max(0, start_ammo_plasma);
706 start_ammo_fuel = max(0, start_ammo_fuel);
708 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
709 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
710 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
711 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
712 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
713 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
716 void precache_playermodel(string m)
718 float globhandle, i, n;
721 if(substring(m, -9,5) == "_lod1")
723 if(substring(m, -9,5) == "_lod2")
726 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
729 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
733 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
736 n = search_getsize(globhandle);
737 for (i = 0; i < n; ++i)
739 //print(search_getfilename(globhandle, i), "\n");
740 f = search_getfilename(globhandle, i);
741 PrecachePlayerSounds(f);
743 search_end(globhandle);
745 void precache_all_playermodels(string pattern)
747 int globhandle = search_begin(pattern, true, false);
748 if (globhandle < 0) return;
749 int n = search_getsize(globhandle);
750 for (int i = 0; i < n; ++i)
752 string s = search_getfilename(globhandle, i);
753 precache_playermodel(s);
755 search_end(globhandle);
758 void precache_playermodels(string s)
760 FOREACH_WORD(s, true, LAMBDA(precache_playermodel(it)));
765 // gamemode related things
767 // Precache all player models if desired
768 if (autocvar_sv_precacheplayermodels)
770 PrecachePlayerSounds("sound/player/default.sounds");
771 precache_all_playermodels("models/player/*.zym");
772 precache_all_playermodels("models/player/*.dpm");
773 precache_all_playermodels("models/player/*.md3");
774 precache_all_playermodels("models/player/*.psk");
775 precache_all_playermodels("models/player/*.iqm");
778 if (autocvar_sv_defaultcharacter)
780 precache_playermodels(autocvar_sv_defaultplayermodel_red);
781 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
782 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
783 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
784 precache_playermodels(autocvar_sv_defaultplayermodel);
788 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
790 if (!this.noise && this.music) // quake 3 uses the music field
791 this.noise = this.music;
793 // plays music for the level if there is any
796 precache_sound (this.noise);
797 ambientsound ('0 0 0', this.noise, VOL_BASE, ATTEN_NONE);
803 void make_safe_for_remove(entity e)
805 if (e.initialize_entity)
807 entity ent, prev = NULL;
808 for (ent = initialize_entity_first; ent; )
810 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
812 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
813 // skip it in linked list
816 prev.initialize_entity_next = ent.initialize_entity_next;
817 ent = prev.initialize_entity_next;
821 initialize_entity_first = ent.initialize_entity_next;
822 ent = initialize_entity_first;
828 ent = ent.initialize_entity_next;
834 .float remove_except_protected_forbidden;
835 void remove_except_protected(entity e)
837 if(e.remove_except_protected_forbidden)
838 error("not allowed to remove this at this point");
842 void remove_unsafely(entity e)
844 if(e.classname == "spike")
845 error("Removing spikes is forbidden (crylink bug), please report");
849 void remove_safely(entity e)
851 make_safe_for_remove(e);
855 void InitializeEntity(entity e, void(entity this) func, float order)
859 if (!e || e.initialize_entity)
861 // make a proxy initializer entity
863 e = new(initialize_entity);
867 e.initialize_entity = func;
868 e.initialize_entity_order = order;
870 cur = initialize_entity_first;
874 if (!cur || cur.initialize_entity_order > order)
876 // insert between prev and cur
878 prev.initialize_entity_next = e;
880 initialize_entity_first = e;
881 e.initialize_entity_next = cur;
885 cur = cur.initialize_entity_next;
888 void InitializeEntitiesRun()
890 entity startoflist = initialize_entity_first;
891 initialize_entity_first = NULL;
892 delete_fn = remove_except_protected;
893 for (entity e = startoflist; e; e = e.initialize_entity_next)
895 e.remove_except_protected_forbidden = 1;
897 for (entity e = startoflist; e; )
899 e.remove_except_protected_forbidden = 0;
900 e.initialize_entity_order = 0;
901 entity next = e.initialize_entity_next;
902 e.initialize_entity_next = NULL;
903 var void(entity this) func = e.initialize_entity;
904 e.initialize_entity = func_null;
905 if (e.classname == "initialize_entity")
907 entity wrappee = e.enemy;
911 //dprint("Delayed initialization: ", e.classname, "\n");
919 backtrace(strcat("Null function in: ", e.classname, "\n"));
923 delete_fn = remove_unsafely;
926 .float(entity) isEliminated;
927 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
931 WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
932 WriteByte(MSG_ENTITY, sendflags);
936 for(i = 1; i <= maxclients; i += 8)
938 for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
940 if(eliminatedPlayers.isEliminated(e))
943 WriteByte(MSG_ENTITY, f);
950 void EliminatedPlayers_Init(float(entity) isEliminated_func)
952 if(eliminatedPlayers)
954 backtrace("Can't spawn eliminatedPlayers again!");
957 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
958 eliminatedPlayers.isEliminated = isEliminated_func;
964 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
966 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
967 this.projectiledeathtype |= HITTYPE_SPLASH;
968 adaptor_think2use(this);
972 void DropToFloor_Handler(entity this)
974 WITHSELF(this, builtin_droptofloor());
975 this.dropped_origin = this.origin;
978 void droptofloor(entity this)
980 InitializeEntity(this, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
985 float trace_hits_box_a0, trace_hits_box_a1;
987 float trace_hits_box_1d(float end, float thmi, float thma)
991 // just check if x is in range
999 // do the trace with respect to x
1000 // 0 -> end has to stay in thmi -> thma
1001 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1002 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1003 if (trace_hits_box_a0 > trace_hits_box_a1)
1009 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1014 // now it is a trace from 0 to end
1016 trace_hits_box_a0 = 0;
1017 trace_hits_box_a1 = 1;
1019 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1021 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1023 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1029 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1031 return trace_hits_box(start, end, thmi - ma, thma - mi);
1034 bool SUB_NoImpactCheck(entity this, entity toucher)
1036 // zero hitcontents = this is not the real impact, but either the
1037 // mirror-impact of something hitting the projectile instead of the
1038 // projectile hitting the something, or a touchareagrid one. Neither of
1039 // these stop the projectile from moving, so...
1040 if(trace_dphitcontents == 0)
1042 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);
1045 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1047 if (toucher == NULL && 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...?");
1057 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1059 LOG_TRACE("Detected and prevented the sky-grapple bug.");
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;
1151 m = e.dphitcontentsmask;
1152 e.dphitcontentsmask = goodcontents | badcontents;
1155 delta = boundmax - boundmin;
1159 for (i = 0; i < attempts; ++i)
1161 start.x = org.x + random() * delta.x;
1162 start.y = org.y + random() * delta.y;
1163 start.z = org.z + random() * delta.z;
1165 // rule 1: start inside world bounds, and outside
1166 // solid, and don't start from somewhere where you can
1167 // fall down to evil
1168 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1169 if (trace_fraction >= 1)
1171 if (trace_startsolid)
1173 if (trace_dphitcontents & badcontents)
1175 if (trace_dphitq3surfaceflags & badsurfaceflags)
1178 // rule 2: if we are too high, lower the point
1179 if (trace_fraction * delta.z > maxaboveground)
1180 start = trace_endpos + '0 0 1' * maxaboveground;
1181 enddown = trace_endpos;
1183 // rule 3: make sure we aren't outside the map. This only works
1184 // for somewhat well formed maps. A good rule of thumb is that
1185 // the map should have a convex outside hull.
1186 // these can be traceLINES as we already verified the starting box
1187 mstart = start + 0.5 * (e.mins + e.maxs);
1188 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1189 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1191 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1192 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1194 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1195 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1197 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1198 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1200 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1201 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1204 // rule 4: we must "see" some spawnpoint or item
1206 IL_EACH(g_spawnpoints, checkpvs(mstart, it),
1208 if((traceline(mstart, it.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1216 IL_EACH(g_items, checkpvs(mstart, it),
1218 if((traceline(mstart, it.origin + (it.mins + it.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1229 // find a random vector to "look at"
1230 end.x = org.x + random() * delta.x;
1231 end.y = org.y + random() * delta.y;
1232 end.z = org.z + random() * delta.z;
1233 end = start + normalize(end - start) * vlen(delta);
1235 // rule 4: start TO end must not be too short
1236 tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1237 if (trace_startsolid)
1239 if (trace_fraction < minviewdistance / vlen(delta))
1242 // rule 5: don't want to look at sky
1243 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1246 // rule 6: we must not end up in trigger_hurt
1247 if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1253 e.dphitcontentsmask = m;
1257 setorigin(e, start);
1258 e.angles = vectoangles(end - start);
1259 LOG_TRACE("Needed ", ftos(i + 1), " attempts");
1266 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1268 return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1271 void write_recordmarker(entity pl, float tstart, float dt)
1273 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1275 // also write a marker into demo files for demotc-race-record-extractor to find
1278 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1279 " ", ftos(tstart), " ", ftos(dt), "\n"));
1282 void attach_sameorigin(entity e, entity to, string tag)
1284 vector org, t_forward, t_left, t_up, e_forward, e_up;
1287 org = e.origin - gettaginfo(to, gettagindex(to, tag));
1288 tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1289 t_forward = v_forward * tagscale;
1290 t_left = v_right * -tagscale;
1291 t_up = v_up * tagscale;
1293 e.origin_x = org * t_forward;
1294 e.origin_y = org * t_left;
1295 e.origin_z = org * t_up;
1297 // current forward and up directions
1298 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1299 e.angles = AnglesTransform_FromVAngles(e.angles);
1301 e.angles = AnglesTransform_FromAngles(e.angles);
1302 fixedmakevectors(e.angles);
1304 // untransform forward, up!
1305 e_forward.x = v_forward * t_forward;
1306 e_forward.y = v_forward * t_left;
1307 e_forward.z = v_forward * t_up;
1308 e_up.x = v_up * t_forward;
1309 e_up.y = v_up * t_left;
1310 e_up.z = v_up * t_up;
1312 e.angles = fixedvectoangles2(e_forward, e_up);
1313 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1314 e.angles = AnglesTransform_ToVAngles(e.angles);
1316 e.angles = AnglesTransform_ToAngles(e.angles);
1318 setattachment(e, to, tag);
1319 setorigin(e, e.origin);
1322 void detach_sameorigin(entity e)
1325 org = gettaginfo(e, 0);
1326 e.angles = fixedvectoangles2(v_forward, v_up);
1327 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1328 e.angles = AnglesTransform_ToVAngles(e.angles);
1330 e.angles = AnglesTransform_ToAngles(e.angles);
1332 setattachment(e, NULL, "");
1333 setorigin(e, e.origin);
1336 void follow_sameorigin(entity e, entity to)
1338 set_movetype(e, MOVETYPE_FOLLOW); // make the hole follow
1339 e.aiment = to; // make the hole follow bmodel
1340 e.punchangle = to.angles; // the original angles of bmodel
1341 e.view_ofs = e.origin - to.origin; // relative origin
1342 e.v_angle = e.angles - to.angles; // relative angles
1345 void unfollow_sameorigin(entity e)
1347 set_movetype(e, MOVETYPE_NONE);
1350 entity gettaginfo_relative_ent;
1351 vector gettaginfo_relative(entity e, float tag)
1353 if (!gettaginfo_relative_ent)
1355 gettaginfo_relative_ent = spawn();
1356 gettaginfo_relative_ent.effects = EF_NODRAW;
1358 gettaginfo_relative_ent.model = e.model;
1359 gettaginfo_relative_ent.modelindex = e.modelindex;
1360 gettaginfo_relative_ent.frame = e.frame;
1361 return gettaginfo(gettaginfo_relative_ent, tag);
1364 .string aiment_classname;
1365 .float aiment_deadflag;
1366 void SetMovetypeFollow(entity ent, entity e)
1368 // FIXME this may not be warpzone aware
1369 set_movetype(ent, MOVETYPE_FOLLOW); // make the hole follow
1370 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.
1371 ent.aiment = e; // make the hole follow bmodel
1372 ent.punchangle = e.angles; // the original angles of bmodel
1373 ent.view_ofs = ent.origin - e.origin; // relative origin
1374 ent.v_angle = ent.angles - e.angles; // relative angles
1375 ent.aiment_classname = strzone(e.classname);
1376 ent.aiment_deadflag = e.deadflag;
1378 void UnsetMovetypeFollow(entity ent)
1380 set_movetype(ent, MOVETYPE_FLY);
1381 PROJECTILE_MAKETRIGGER(ent);
1384 float LostMovetypeFollow(entity ent)
1387 if(ent.move_movetype != MOVETYPE_FOLLOW)
1393 if(ent.aiment.classname != ent.aiment_classname)
1395 if(ent.aiment.deadflag != ent.aiment_deadflag)
1402 bool isPushable(entity e)
1413 case "droppedweapon":
1415 case "bullet": // antilagged bullets can't hit this either
1418 if (e.projectiledeathtype)