1 #include "miscfunctions.qh"
3 #include "command/common.qh"
4 #include "constants.qh"
7 #include "mutators/_mod.qh"
8 #include "../common/t_items.qh"
9 #include "weapons/accuracy.qh"
10 #include "weapons/csqcprojectile.qh"
11 #include "weapons/selection.qh"
12 #include "../common/command/_mod.qh"
13 #include "../common/constants.qh"
14 #include <common/net_linked.qh>
15 #include "../common/deathtypes/all.qh"
16 #include "../common/mapinfo.qh"
17 #include "../common/notifications/all.qh"
18 #include "../common/playerstats.qh"
19 #include "../common/teams.qh"
20 #include "../common/triggers/subs.qh"
21 #include "../common/util.qh"
22 #include "../common/turrets/sv_turrets.qh"
23 #include <common/weapons/_all.qh>
24 #include "../common/vehicles/sv_vehicles.qh"
25 #include "../common/vehicles/vehicle.qh"
26 #include "../common/items/_mod.qh"
27 #include "../common/state.qh"
28 #include "../common/effects/qc/globalsound.qh"
29 #include "../common/wepent.qh"
30 #include "../lib/csqcmodel/sv_model.qh"
31 #include "../lib/warpzone/anglestransform.qh"
32 #include "../lib/warpzone/server.qh"
34 void crosshair_trace(entity pl)
36 traceline_antilag(pl, pl.cursor_trace_start, pl.cursor_trace_start + normalize(pl.cursor_trace_endpos - pl.cursor_trace_start) * MAX_SHOT_DISTANCE, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
38 .bool ctrace_solidchanged;
39 void crosshair_trace_plusvisibletriggers(entity pl)
41 FOREACH_ENTITY_FLOAT(solid, SOLID_TRIGGER,
46 it.ctrace_solidchanged = true;
52 FOREACH_ENTITY_FLOAT(ctrace_solidchanged, true,
54 it.solid = SOLID_TRIGGER;
55 it.ctrace_solidchanged = false;
58 void WarpZone_crosshair_trace(entity pl)
60 WarpZone_traceline_antilag(pl, pl.cursor_trace_start, pl.cursor_trace_start + normalize(pl.cursor_trace_endpos - pl.cursor_trace_start) * MAX_SHOT_DISTANCE, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
66 if(autocvar_sv_adminnick != "")
67 return autocvar_sv_adminnick;
69 return "SERVER ADMIN";
73 void GameLogEcho(string s)
78 if (autocvar_sv_eventlog_files)
83 matches = autocvar_sv_eventlog_files_counter + 1;
84 cvar_set("sv_eventlog_files_counter", itos(matches));
87 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
88 fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
89 logfile = fopen(fn, FILE_APPEND);
90 fputs(logfile, ":logversion:3\n");
94 if (autocvar_sv_eventlog_files_timestamps)
95 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
97 fputs(logfile, strcat(s, "\n"));
100 if (autocvar_sv_eventlog_console)
102 dedicated_print(strcat(s, "\n"));
109 // will be opened later
114 if (logfile_open && logfile >= 0)
121 entity findnearest(vector point, .string field, string value, vector axismod)
132 localhead = find(NULL, field, value);
135 if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
136 dist = localhead.oldorigin;
138 dist = localhead.origin;
140 dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
143 for (i = 0; i < num_nearest; ++i)
145 if (len < nearest_length[i])
149 // now i tells us where to insert at
150 // INSERTION SORT! YOU'VE SEEN IT! RUN!
151 if (i < NUM_NEAREST_ENTITIES)
153 for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
155 nearest_length[j + 1] = nearest_length[j];
156 nearest_entity[j + 1] = nearest_entity[j];
158 nearest_length[i] = len;
159 nearest_entity[i] = localhead;
160 if (num_nearest < NUM_NEAREST_ENTITIES)
161 num_nearest = num_nearest + 1;
164 localhead = find(localhead, field, value);
167 // now use the first one from our list that we can see
168 for (i = 0; i < num_nearest; ++i)
170 traceline(point, nearest_entity[i].origin, true, NULL);
171 if (trace_fraction == 1)
175 LOG_TRACE("Nearest point (");
176 LOG_TRACE(nearest_entity[0].netname);
177 LOG_TRACE(") is not visible, using a visible one.");
179 return nearest_entity[i];
183 if (num_nearest == 0)
186 LOG_TRACE("Not seeing any location point, using nearest as fallback.");
188 dprint("Candidates were: ");
189 for(j = 0; j < num_nearest; ++j)
193 dprint(nearest_entity[j].netname);
198 return nearest_entity[0];
201 string NearestLocation(vector p)
206 loc = findnearest(p, classname, "target_location", '1 1 1');
213 loc = findnearest(p, target, "###item###", '1 1 4');
220 string formatmessage(entity this, string msg)
232 ammoitems = "batteries";
233 if(this.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
234 if(this.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
235 if(this.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
236 if(this.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
238 WarpZone_crosshair_trace(this);
239 cursor = trace_endpos;
240 cursor_ent = trace_ent;
242 MUTATOR_CALLHOOK(PreFormatMessage, this, msg);
243 msg = M_ARGV(1, string);
247 break; // too many replacements
250 p1 = strstrofs(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
251 p2 = strstrofs(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
264 replacement = substring(msg, p, 2);
265 escape = substring(msg, p + 1, 1);
267 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
271 case "%": replacement = "%"; break;
272 case "\\":replacement = "\\"; break;
273 case "n": replacement = "\n"; break;
274 case "a": replacement = ftos(floor(this.armorvalue)); break;
275 case "h": replacement = ftos(floor(this.health)); break;
276 case "l": replacement = NearestLocation(this.origin); break;
277 case "y": replacement = NearestLocation(cursor); break;
278 case "d": replacement = NearestLocation(this.death_origin); break;
279 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;
280 case "W": replacement = ammoitems; break;
281 case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
282 case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
283 case "S": replacement = ftos(vlen(this.velocity)); break;
284 case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
285 case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
288 MUTATOR_CALLHOOK(FormatMessage, this, escape, replacement, msg);
289 replacement = M_ARGV(2, string);
294 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
295 p = p + strlen(replacement);
306 >0: receives a cvar from name=argv(f) value=argv(f+1)
308 void GetCvars_handleString(entity this, string thisname, float f, .string field, string name)
313 strunzone(this.(field));
314 this.(field) = string_null;
318 if (thisname == name)
321 strunzone(this.(field));
322 this.(field) = strzone(argv(f + 1));
326 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
328 void GetCvars_handleString_Fixup(entity this, string thisname, float f, .string field, string name, string(entity, string) func)
330 GetCvars_handleString(this, thisname, f, field, name);
331 if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
332 if (thisname == name)
334 string s = func(this, strcat1(this.(field)));
335 if (s != this.(field))
337 strunzone(this.(field));
338 this.(field) = strzone(s);
342 void GetCvars_handleFloat(entity this, string thisname, float f, .float field, string name)
349 if (thisname == name)
350 this.(field) = stof(argv(f + 1));
353 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
355 void GetCvars_handleFloatOnce(entity this, string thisname, float f, .float field, string name)
362 if (thisname == name)
366 this.(field) = stof(argv(f + 1));
375 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
378 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
381 o = W_FixWeaponOrder_ForceComplete(wo);
382 if(this.weaponorder_byimpulse)
384 strunzone(this.weaponorder_byimpulse);
385 this.weaponorder_byimpulse = string_null;
387 this.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
391 REPLICATE(autoswitch, bool, "cl_autoswitch");
393 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
395 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
397 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
399 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
401 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
403 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
405 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
407 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
409 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
411 REPLICATE(cvar_cl_physics, string, "cl_physics");
413 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
415 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
417 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
419 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
422 * @param f -1: cleanup, 0: request, 1: receive
424 void GetCvars(entity this, int f)
426 string s = string_null;
429 s = strcat1(argv(f));
433 MUTATOR_CALLHOOK(GetCvars);
435 Notification_GetCvars(this);
437 ReplicateVars(this, s, f);
439 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
440 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
441 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
442 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
443 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
444 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
445 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
446 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
447 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
448 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
449 GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
451 GetCvars_handleFloat(this, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
453 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
456 if (s == "cl_weaponpriority")
458 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
460 .entity weaponentity = weaponentities[slot];
461 if (this.(weaponentity) && (this.(weaponentity).m_weapon != WEP_Null || slot == 0))
462 this.(weaponentity).m_switchweapon = w_getbestweapon(this, weaponentity);
465 if (s == "cl_allow_uidtracking")
466 PlayerStats_GameReport_AddPlayer(this);
470 // decolorizes and team colors the player name when needed
471 string playername(entity p)
474 if (teamplay && !gameover && IS_PLAYER(p))
476 t = Team_ColorCode(p.team);
477 return strcat(t, strdecolorize(p.netname));
483 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
485 int i = weaponinfo.m_id;
487 bool allow_mutatorblocked = false;
492 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
493 d = M_ARGV(1, float);
494 allguns = M_ARGV(2, bool);
495 allow_mutatorblocked = M_ARGV(3, bool);
499 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
504 else if(!mutator_returnvalue)
505 d = !(!weaponinfo.weaponstart);
507 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
510 float t = weaponinfo.weaponstartoverride;
512 //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
517 // 4: is set by default?
526 void readplayerstartcvars()
531 // initialize starting values for players
532 start_weapons = '0 0 0';
533 start_weapons_default = '0 0 0';
534 start_weapons_defaultmask = '0 0 0';
536 start_ammo_shells = 0;
537 start_ammo_nails = 0;
538 start_ammo_rockets = 0;
539 start_ammo_cells = 0;
540 start_ammo_plasma = 0;
541 start_health = cvar("g_balance_health_start");
542 start_armorvalue = cvar("g_balance_armor_start");
545 g_weaponarena_weapons = '0 0 0';
547 s = cvar_string("g_weaponarena");
549 MUTATOR_CALLHOOK(SetWeaponArena, s);
550 s = M_ARGV(0, string);
552 if (s == "0" || s == "")
558 // forcibly turn off weaponarena
560 else if (s == "all" || s == "1")
563 g_weaponarena_list = "All Weapons";
564 FOREACH(Weapons, it != WEP_Null, LAMBDA(
565 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
566 g_weaponarena_weapons |= (it.m_wepset);
569 else if (s == "most")
572 g_weaponarena_list = "Most Weapons";
573 FOREACH(Weapons, it != WEP_Null, LAMBDA(
574 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
575 if(it.spawnflags & WEP_FLAG_NORMAL)
576 g_weaponarena_weapons |= (it.m_wepset);
579 else if (s == "none")
582 g_weaponarena_list = "No Weapons";
587 t = tokenize_console(s);
588 g_weaponarena_list = "";
589 for (i = 0; i < t; ++i)
592 FOREACH(Weapons, it != WEP_Null, LAMBDA(
595 g_weaponarena_weapons |= (it.m_wepset);
596 g_weaponarena_list = strcat(g_weaponarena_list, it.m_name, " & ");
601 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
605 g_weaponarena_random = cvar("g_weaponarena_random");
607 g_weaponarena_random = 0;
608 g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
612 g_weapon_stay = 0; // incompatible
613 start_weapons = g_weaponarena_weapons;
614 start_items |= IT_UNLIMITED_AMMO;
618 FOREACH(Weapons, it != WEP_Null, LAMBDA(
619 int w = want_weapon(it, false);
620 WepSet s = it.m_wepset;
624 start_weapons_default |= s;
626 start_weapons_defaultmask |= s;
630 if(!cvar("g_use_ammunition"))
631 start_items |= IT_UNLIMITED_AMMO;
633 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
635 start_ammo_shells = 999;
636 start_ammo_nails = 999;
637 start_ammo_rockets = 999;
638 start_ammo_cells = 999;
639 start_ammo_plasma = 999;
640 start_ammo_fuel = 999;
644 start_ammo_shells = cvar("g_start_ammo_shells");
645 start_ammo_nails = cvar("g_start_ammo_nails");
646 start_ammo_rockets = cvar("g_start_ammo_rockets");
647 start_ammo_cells = cvar("g_start_ammo_cells");
648 start_ammo_plasma = cvar("g_start_ammo_plasma");
649 start_ammo_fuel = cvar("g_start_ammo_fuel");
654 warmup_start_ammo_shells = start_ammo_shells;
655 warmup_start_ammo_nails = start_ammo_nails;
656 warmup_start_ammo_rockets = start_ammo_rockets;
657 warmup_start_ammo_cells = start_ammo_cells;
658 warmup_start_ammo_plasma = start_ammo_plasma;
659 warmup_start_ammo_fuel = start_ammo_fuel;
660 warmup_start_health = start_health;
661 warmup_start_armorvalue = start_armorvalue;
662 warmup_start_weapons = start_weapons;
663 warmup_start_weapons_default = start_weapons_default;
664 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
666 if (!g_weaponarena && !g_ca && !g_freezetag)
668 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
669 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
670 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
671 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
672 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
673 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
674 warmup_start_health = cvar("g_warmup_start_health");
675 warmup_start_armorvalue = cvar("g_warmup_start_armor");
676 warmup_start_weapons = '0 0 0';
677 warmup_start_weapons_default = '0 0 0';
678 warmup_start_weapons_defaultmask = '0 0 0';
679 FOREACH(Weapons, it != WEP_Null, LAMBDA(
680 int w = want_weapon(it, g_warmup_allguns);
681 WepSet s = (it.m_wepset);
683 warmup_start_weapons |= s;
685 warmup_start_weapons_default |= s;
687 warmup_start_weapons_defaultmask |= s;
693 start_items |= ITEM_Jetpack.m_itemid;
695 MUTATOR_CALLHOOK(SetStartItems);
697 if (start_items & ITEM_Jetpack.m_itemid)
699 start_items |= ITEM_JetpackRegen.m_itemid;
700 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
701 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
704 WepSet precache_weapons = start_weapons;
705 if (g_warmup_allguns != 1)
706 precache_weapons |= warmup_start_weapons;
707 FOREACH(Weapons, it != WEP_Null, LAMBDA(
708 if(precache_weapons & (it.m_wepset))
712 start_ammo_shells = max(0, start_ammo_shells);
713 start_ammo_nails = max(0, start_ammo_nails);
714 start_ammo_rockets = max(0, start_ammo_rockets);
715 start_ammo_cells = max(0, start_ammo_cells);
716 start_ammo_plasma = max(0, start_ammo_plasma);
717 start_ammo_fuel = max(0, start_ammo_fuel);
719 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
720 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
721 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
722 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
723 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
724 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
727 void precache_playermodel(string m)
729 float globhandle, i, n;
732 if(substring(m, -9,5) == "_lod1")
734 if(substring(m, -9,5) == "_lod2")
737 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
740 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
744 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
747 n = search_getsize(globhandle);
748 for (i = 0; i < n; ++i)
750 //print(search_getfilename(globhandle, i), "\n");
751 f = search_getfilename(globhandle, i);
752 PrecachePlayerSounds(f);
754 search_end(globhandle);
756 void precache_all_playermodels(string pattern)
758 int globhandle = search_begin(pattern, true, false);
759 if (globhandle < 0) return;
760 int n = search_getsize(globhandle);
761 for (int i = 0; i < n; ++i)
763 string s = search_getfilename(globhandle, i);
764 precache_playermodel(s);
766 search_end(globhandle);
769 void precache_playermodels(string s)
771 FOREACH_WORD(s, true, LAMBDA(precache_playermodel(it)));
776 // gamemode related things
778 // Precache all player models if desired
779 if (autocvar_sv_precacheplayermodels)
781 PrecachePlayerSounds("sound/player/default.sounds");
782 precache_all_playermodels("models/player/*.zym");
783 precache_all_playermodels("models/player/*.dpm");
784 precache_all_playermodels("models/player/*.md3");
785 precache_all_playermodels("models/player/*.psk");
786 precache_all_playermodels("models/player/*.iqm");
789 if (autocvar_sv_defaultcharacter)
791 precache_playermodels(autocvar_sv_defaultplayermodel_red);
792 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
793 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
794 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
795 precache_playermodels(autocvar_sv_defaultplayermodel);
799 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
801 if (!this.noise && this.music) // quake 3 uses the music field
802 this.noise = this.music;
804 // plays music for the level if there is any
807 precache_sound (this.noise);
808 ambientsound ('0 0 0', this.noise, VOL_BASE, ATTEN_NONE);
814 void make_safe_for_remove(entity e)
816 if (e.initialize_entity)
818 entity ent, prev = NULL;
819 for (ent = initialize_entity_first; ent; )
821 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
823 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
824 // skip it in linked list
827 prev.initialize_entity_next = ent.initialize_entity_next;
828 ent = prev.initialize_entity_next;
832 initialize_entity_first = ent.initialize_entity_next;
833 ent = initialize_entity_first;
839 ent = ent.initialize_entity_next;
845 .float remove_except_protected_forbidden;
846 void remove_except_protected(entity e)
848 if(e.remove_except_protected_forbidden)
849 error("not allowed to remove this at this point");
853 void remove_unsafely(entity e)
855 if(e.classname == "spike")
856 error("Removing spikes is forbidden (crylink bug), please report");
860 void remove_safely(entity e)
862 make_safe_for_remove(e);
866 void InitializeEntity(entity e, void(entity this) func, float order)
870 if (!e || e.initialize_entity)
872 // make a proxy initializer entity
874 e = new(initialize_entity);
878 e.initialize_entity = func;
879 e.initialize_entity_order = order;
881 cur = initialize_entity_first;
885 if (!cur || cur.initialize_entity_order > order)
887 // insert between prev and cur
889 prev.initialize_entity_next = e;
891 initialize_entity_first = e;
892 e.initialize_entity_next = cur;
896 cur = cur.initialize_entity_next;
899 void InitializeEntitiesRun()
901 entity startoflist = initialize_entity_first;
902 initialize_entity_first = NULL;
903 delete_fn = remove_except_protected;
904 for (entity e = startoflist; e; e = e.initialize_entity_next)
906 e.remove_except_protected_forbidden = 1;
908 for (entity e = startoflist; e; )
910 e.remove_except_protected_forbidden = 0;
911 e.initialize_entity_order = 0;
912 entity next = e.initialize_entity_next;
913 e.initialize_entity_next = NULL;
914 var void(entity this) func = e.initialize_entity;
915 e.initialize_entity = func_null;
916 if (e.classname == "initialize_entity")
918 entity wrappee = e.enemy;
922 //dprint("Delayed initialization: ", e.classname, "\n");
930 backtrace(strcat("Null function in: ", e.classname, "\n"));
934 delete_fn = remove_unsafely;
937 .float(entity) isEliminated;
938 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
942 WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
943 WriteByte(MSG_ENTITY, sendflags);
947 for(i = 1; i <= maxclients; i += 8)
949 for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
951 if(eliminatedPlayers.isEliminated(e))
954 WriteByte(MSG_ENTITY, f);
961 void EliminatedPlayers_Init(float(entity) isEliminated_func)
963 if(eliminatedPlayers)
965 backtrace("Can't spawn eliminatedPlayers again!");
968 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
969 eliminatedPlayers.isEliminated = isEliminated_func;
975 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
977 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
978 this.projectiledeathtype |= HITTYPE_SPLASH;
979 adaptor_think2use(this);
983 void DropToFloor_Handler(entity this)
985 WITHSELF(this, builtin_droptofloor());
986 this.dropped_origin = this.origin;
989 void droptofloor(entity this)
991 InitializeEntity(this, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
996 float trace_hits_box_a0, trace_hits_box_a1;
998 float trace_hits_box_1d(float end, float thmi, float thma)
1002 // just check if x is in range
1010 // do the trace with respect to x
1011 // 0 -> end has to stay in thmi -> thma
1012 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1013 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1014 if (trace_hits_box_a0 > trace_hits_box_a1)
1020 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1025 // now it is a trace from 0 to end
1027 trace_hits_box_a0 = 0;
1028 trace_hits_box_a1 = 1;
1030 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1032 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1034 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1040 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1042 return trace_hits_box(start, end, thmi - ma, thma - mi);
1045 bool SUB_NoImpactCheck(entity this, entity toucher)
1047 // zero hitcontents = this is not the real impact, but either the
1048 // mirror-impact of something hitting the projectile instead of the
1049 // projectile hitting the something, or a touchareagrid one. Neither of
1050 // these stop the projectile from moving, so...
1051 if(trace_dphitcontents == 0)
1053 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);
1056 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1058 if (toucher == NULL && this.size != '0 0 0')
1061 tic = this.velocity * sys_frametime;
1062 tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
1063 traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
1064 if (trace_fraction >= 1)
1066 LOG_TRACE("Odd... did not hit...?");
1068 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1070 LOG_TRACE("Detected and prevented the sky-grapple bug.");
1078 #define SUB_OwnerCheck(ent,oth) ((oth) && ((oth) == (ent).owner))
1080 void W_Crylink_Dequeue(entity e);
1081 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
1083 if(SUB_OwnerCheck(this, toucher))
1085 if(SUB_NoImpactCheck(this, toucher))
1087 if(this.classname == "nade")
1088 return false; // no checks here
1089 else if(this.classname == "grapplinghook")
1091 else if(this.classname == "spike")
1093 W_Crylink_Dequeue(this);
1100 if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1101 UpdateCSQCProjectile(this);
1105 /** engine callback */
1106 void URI_Get_Callback(float id, float status, string data)
1108 if(url_URI_Get_Callback(id, status, data))
1112 else if (id == URI_GET_DISCARD)
1116 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1119 Curl_URI_Get_Callback(id, status, data);
1121 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1124 OnlineBanList_URI_Get_Callback(id, status, data);
1126 else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
1128 // handled by a mutator
1132 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1136 string uid2name(string myuid) {
1138 s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1140 // FIXME remove this later after 0.6 release
1141 // convert old style broken records to correct style
1144 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1147 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1148 db_remove(ServerProgsDB, strcat("uid2name", myuid));
1153 s = "^1Unregistered Player";
1157 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1160 vector start, org, delta, end, enddown, mstart;
1162 m = e.dphitcontentsmask;
1163 e.dphitcontentsmask = goodcontents | badcontents;
1166 delta = boundmax - boundmin;
1170 for (i = 0; i < attempts; ++i)
1172 start.x = org.x + random() * delta.x;
1173 start.y = org.y + random() * delta.y;
1174 start.z = org.z + random() * delta.z;
1176 // rule 1: start inside world bounds, and outside
1177 // solid, and don't start from somewhere where you can
1178 // fall down to evil
1179 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1180 if (trace_fraction >= 1)
1182 if (trace_startsolid)
1184 if (trace_dphitcontents & badcontents)
1186 if (trace_dphitq3surfaceflags & badsurfaceflags)
1189 // rule 2: if we are too high, lower the point
1190 if (trace_fraction * delta.z > maxaboveground)
1191 start = trace_endpos + '0 0 1' * maxaboveground;
1192 enddown = trace_endpos;
1194 // rule 3: make sure we aren't outside the map. This only works
1195 // for somewhat well formed maps. A good rule of thumb is that
1196 // the map should have a convex outside hull.
1197 // these can be traceLINES as we already verified the starting box
1198 mstart = start + 0.5 * (e.mins + e.maxs);
1199 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1200 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1202 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1203 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1205 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1206 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1208 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1209 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1211 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1212 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1215 // rule 4: we must "see" some spawnpoint or item
1217 IL_EACH(g_spawnpoints, checkpvs(mstart, it),
1219 if((traceline(mstart, it.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1227 IL_EACH(g_items, checkpvs(mstart, it),
1229 if((traceline(mstart, it.origin + (it.mins + it.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1240 // find a random vector to "look at"
1241 end.x = org.x + random() * delta.x;
1242 end.y = org.y + random() * delta.y;
1243 end.z = org.z + random() * delta.z;
1244 end = start + normalize(end - start) * vlen(delta);
1246 // rule 4: start TO end must not be too short
1247 tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1248 if (trace_startsolid)
1250 if (trace_fraction < minviewdistance / vlen(delta))
1253 // rule 5: don't want to look at sky
1254 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1257 // rule 6: we must not end up in trigger_hurt
1258 if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1264 e.dphitcontentsmask = m;
1268 setorigin(e, start);
1269 e.angles = vectoangles(end - start);
1270 LOG_TRACE("Needed ", ftos(i + 1), " attempts");
1277 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1279 return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1282 void write_recordmarker(entity pl, float tstart, float dt)
1284 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1286 // also write a marker into demo files for demotc-race-record-extractor to find
1289 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1290 " ", ftos(tstart), " ", ftos(dt), "\n"));
1293 void attach_sameorigin(entity e, entity to, string tag)
1295 vector org, t_forward, t_left, t_up, e_forward, e_up;
1298 org = e.origin - gettaginfo(to, gettagindex(to, tag));
1299 tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1300 t_forward = v_forward * tagscale;
1301 t_left = v_right * -tagscale;
1302 t_up = v_up * tagscale;
1304 e.origin_x = org * t_forward;
1305 e.origin_y = org * t_left;
1306 e.origin_z = org * t_up;
1308 // current forward and up directions
1309 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1310 e.angles = AnglesTransform_FromVAngles(e.angles);
1312 e.angles = AnglesTransform_FromAngles(e.angles);
1313 fixedmakevectors(e.angles);
1315 // untransform forward, up!
1316 e_forward.x = v_forward * t_forward;
1317 e_forward.y = v_forward * t_left;
1318 e_forward.z = v_forward * t_up;
1319 e_up.x = v_up * t_forward;
1320 e_up.y = v_up * t_left;
1321 e_up.z = v_up * t_up;
1323 e.angles = fixedvectoangles2(e_forward, e_up);
1324 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1325 e.angles = AnglesTransform_ToVAngles(e.angles);
1327 e.angles = AnglesTransform_ToAngles(e.angles);
1329 setattachment(e, to, tag);
1330 setorigin(e, e.origin);
1333 void detach_sameorigin(entity e)
1336 org = gettaginfo(e, 0);
1337 e.angles = fixedvectoangles2(v_forward, v_up);
1338 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1339 e.angles = AnglesTransform_ToVAngles(e.angles);
1341 e.angles = AnglesTransform_ToAngles(e.angles);
1343 setattachment(e, NULL, "");
1344 setorigin(e, e.origin);
1347 void follow_sameorigin(entity e, entity to)
1349 set_movetype(e, MOVETYPE_FOLLOW); // make the hole follow
1350 e.aiment = to; // make the hole follow bmodel
1351 e.punchangle = to.angles; // the original angles of bmodel
1352 e.view_ofs = e.origin - to.origin; // relative origin
1353 e.v_angle = e.angles - to.angles; // relative angles
1356 void unfollow_sameorigin(entity e)
1358 set_movetype(e, MOVETYPE_NONE);
1361 entity gettaginfo_relative_ent;
1362 vector gettaginfo_relative(entity e, float tag)
1364 if (!gettaginfo_relative_ent)
1366 gettaginfo_relative_ent = spawn();
1367 gettaginfo_relative_ent.effects = EF_NODRAW;
1369 gettaginfo_relative_ent.model = e.model;
1370 gettaginfo_relative_ent.modelindex = e.modelindex;
1371 gettaginfo_relative_ent.frame = e.frame;
1372 return gettaginfo(gettaginfo_relative_ent, tag);
1375 .string aiment_classname;
1376 .float aiment_deadflag;
1377 void SetMovetypeFollow(entity ent, entity e)
1379 // FIXME this may not be warpzone aware
1380 set_movetype(ent, MOVETYPE_FOLLOW); // make the hole follow
1381 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.
1382 ent.aiment = e; // make the hole follow bmodel
1383 ent.punchangle = e.angles; // the original angles of bmodel
1384 ent.view_ofs = ent.origin - e.origin; // relative origin
1385 ent.v_angle = ent.angles - e.angles; // relative angles
1386 ent.aiment_classname = strzone(e.classname);
1387 ent.aiment_deadflag = e.deadflag;
1389 void UnsetMovetypeFollow(entity ent)
1391 set_movetype(ent, MOVETYPE_FLY);
1392 PROJECTILE_MAKETRIGGER(ent);
1395 float LostMovetypeFollow(entity ent)
1398 if(ent.move_movetype != MOVETYPE_FOLLOW)
1404 if(ent.aiment.classname != ent.aiment_classname)
1406 if(ent.aiment.deadflag != ent.aiment_deadflag)
1413 bool isPushable(entity e)
1424 case "droppedweapon":
1426 case "bullet": // antilagged bullets can't hit this either
1429 if (e.projectiledeathtype)