1 #include "miscfunctions.qh"
3 #include "command/common.qh"
4 #include "constants.qh"
7 #include "mutators/all.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 = WEP_NAME(((!self.weapon) ? (!self.switchweapon ? self.cnt : self.switchweapon) : self.weapon)); 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));
377 void GetCvars(float f)
379 string s = string_null;
382 s = strcat1(argv(f));
386 MUTATOR_CALLHOOK(GetCvars);
388 Notification_GetCvars();
390 ReplicateVars(this, s, f);
392 GetCvars_handleFloat(s, f, autoswitch, "cl_autoswitch");
393 GetCvars_handleFloat(s, f, cvar_cl_autoscreenshot, "cl_autoscreenshot");
394 GetCvars_handleFloat(s, f, cvar_cl_jetpack_jump, "cl_jetpack_jump");
395 GetCvars_handleString(s, f, cvar_g_xonoticversion, "g_xonoticversion");
396 GetCvars_handleString(s, f, cvar_cl_physics, "cl_physics");
397 GetCvars_handleFloat(s, f, cvar_cl_handicap, "cl_handicap");
398 GetCvars_handleFloat(s, f, cvar_cl_clippedspectating, "cl_clippedspectating");
399 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
400 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
401 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
402 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
403 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
404 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
405 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
406 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
407 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
408 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
409 GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
410 GetCvars_handleFloat(s, f, cvar_cl_weaponimpulsemode, "cl_weaponimpulsemode");
411 GetCvars_handleFloat(s, f, cvar_cl_autotaunt, "cl_autotaunt");
412 GetCvars_handleFloat(s, f, cvar_cl_noantilag, "cl_noantilag");
413 GetCvars_handleFloat(s, f, cvar_cl_voice_directional, "cl_voice_directional");
414 GetCvars_handleFloat(s, f, cvar_cl_voice_directional_taunt_attenuation, "cl_voice_directional_taunt_attenuation");
416 GetCvars_handleFloatOnce(s, f, cvar_cl_gunalign, "cl_gunalign");
417 GetCvars_handleFloat(s, f, cvar_cl_allow_uid2name, "cl_allow_uid2name");
418 GetCvars_handleFloat(s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
419 GetCvars_handleFloat(s, f, cvar_cl_movement_track_canjump, "cl_movement_track_canjump");
420 GetCvars_handleFloat(s, f, cvar_cl_newusekeysupported, "cl_newusekeysupported");
422 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
425 if (s == "cl_weaponpriority")
426 self.switchweapon = w_getbestweapon(self);
427 if (s == "cl_allow_uidtracking")
428 PlayerStats_GameReport_AddPlayer(self);
432 // decolorizes and team colors the player name when needed
433 string playername(entity p)
436 if (teamplay && !intermission_running && IS_PLAYER(p))
438 t = Team_ColorCode(p.team);
439 return strcat(t, strdecolorize(p.netname));
445 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
447 int i = weaponinfo.weapon;
449 bool allow_mutatorblocked = false;
454 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
456 allguns = want_allguns;
457 allow_mutatorblocked = false;
461 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
466 else if(!mutator_returnvalue)
467 d = !(!weaponinfo.weaponstart);
469 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
472 float t = weaponinfo.weaponstartoverride;
474 //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
479 // 4: is set by default?
488 void readplayerstartcvars()
494 // initialize starting values for players
495 start_weapons = '0 0 0';
496 start_weapons_default = '0 0 0';
497 start_weapons_defaultmask = '0 0 0';
499 start_ammo_shells = 0;
500 start_ammo_nails = 0;
501 start_ammo_rockets = 0;
502 start_ammo_cells = 0;
503 start_ammo_plasma = 0;
504 start_health = cvar("g_balance_health_start");
505 start_armorvalue = cvar("g_balance_armor_start");
508 g_weaponarena_weapons = '0 0 0';
510 s = cvar_string("g_weaponarena");
512 MUTATOR_CALLHOOK(SetWeaponArena, s);
515 if (s == "0" || s == "")
521 // forcibly turn off weaponarena
523 else if (s == "all" || s == "1")
526 g_weaponarena_list = "All Weapons";
527 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
529 e = get_weaponinfo(j);
530 if (!(e.spawnflags & WEP_FLAG_MUTATORBLOCKED))
531 g_weaponarena_weapons |= WepSet_FromWeapon(j);
534 else if (s == "most")
537 g_weaponarena_list = "Most Weapons";
538 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
540 e = get_weaponinfo(j);
541 if (!(e.spawnflags & WEP_FLAG_MUTATORBLOCKED))
542 if (e.spawnflags & WEP_FLAG_NORMAL)
543 g_weaponarena_weapons |= WepSet_FromWeapon(j);
546 else if (s == "none")
549 g_weaponarena_list = "No Weapons";
554 t = tokenize_console(s);
555 g_weaponarena_list = "";
556 for (i = 0; i < t; ++i)
559 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
561 e = get_weaponinfo(j);
564 g_weaponarena_weapons |= WepSet_FromWeapon(j);
565 g_weaponarena_list = strcat(g_weaponarena_list, e.m_name, " & ");
571 LOG_INFO("The weapon mutator list contains an unknown weapon ", s, ". Skipped.\n");
574 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
578 g_weaponarena_random = cvar("g_weaponarena_random");
580 g_weaponarena_random = 0;
581 g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
585 g_weapon_stay = 0; // incompatible
586 start_weapons = g_weaponarena_weapons;
587 start_items |= IT_UNLIMITED_AMMO;
591 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
593 e = get_weaponinfo(i);
594 int w = want_weapon(e, false);
596 start_weapons |= WepSet_FromWeapon(i);
598 start_weapons_default |= WepSet_FromWeapon(i);
600 start_weapons_defaultmask |= WepSet_FromWeapon(i);
604 if(!cvar("g_use_ammunition"))
605 start_items |= IT_UNLIMITED_AMMO;
607 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
609 start_ammo_shells = 999;
610 start_ammo_nails = 999;
611 start_ammo_rockets = 999;
612 start_ammo_cells = 999;
613 start_ammo_plasma = 999;
614 start_ammo_fuel = 999;
618 start_ammo_shells = cvar("g_start_ammo_shells");
619 start_ammo_nails = cvar("g_start_ammo_nails");
620 start_ammo_rockets = cvar("g_start_ammo_rockets");
621 start_ammo_cells = cvar("g_start_ammo_cells");
622 start_ammo_plasma = cvar("g_start_ammo_plasma");
623 start_ammo_fuel = cvar("g_start_ammo_fuel");
628 warmup_start_ammo_shells = start_ammo_shells;
629 warmup_start_ammo_nails = start_ammo_nails;
630 warmup_start_ammo_rockets = start_ammo_rockets;
631 warmup_start_ammo_cells = start_ammo_cells;
632 warmup_start_ammo_plasma = start_ammo_plasma;
633 warmup_start_ammo_fuel = start_ammo_fuel;
634 warmup_start_health = start_health;
635 warmup_start_armorvalue = start_armorvalue;
636 warmup_start_weapons = start_weapons;
637 warmup_start_weapons_default = start_weapons_default;
638 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
640 if (!g_weaponarena && !g_ca && !g_freezetag)
642 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
643 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
644 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
645 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
646 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
647 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
648 warmup_start_health = cvar("g_warmup_start_health");
649 warmup_start_armorvalue = cvar("g_warmup_start_armor");
650 warmup_start_weapons = '0 0 0';
651 warmup_start_weapons_default = '0 0 0';
652 warmup_start_weapons_defaultmask = '0 0 0';
653 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
655 e = get_weaponinfo(i);
656 int w = want_weapon(e, g_warmup_allguns);
658 warmup_start_weapons |= WepSet_FromWeapon(i);
660 warmup_start_weapons_default |= WepSet_FromWeapon(i);
662 warmup_start_weapons_defaultmask |= WepSet_FromWeapon(i);
668 start_items |= ITEM_Jetpack.m_itemid;
670 MUTATOR_CALLHOOK(SetStartItems);
672 if (start_items & ITEM_Jetpack.m_itemid)
674 start_items |= ITEM_JetpackRegen.m_itemid;
675 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
676 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
679 WepSet precache_weapons = start_weapons;
680 if (g_warmup_allguns != 1)
681 precache_weapons |= warmup_start_weapons;
682 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
684 e = get_weaponinfo(i);
685 if(precache_weapons & WepSet_FromWeapon(i)) {
686 Weapon w = get_weaponinfo(i);
691 start_ammo_shells = max(0, start_ammo_shells);
692 start_ammo_nails = max(0, start_ammo_nails);
693 start_ammo_rockets = max(0, start_ammo_rockets);
694 start_ammo_cells = max(0, start_ammo_cells);
695 start_ammo_plasma = max(0, start_ammo_plasma);
696 start_ammo_fuel = max(0, start_ammo_fuel);
698 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
699 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
700 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
701 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
702 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
703 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
706 void precache_playermodel(string m)
708 float globhandle, i, n;
711 if(substring(m, -9,5) == "_lod1")
713 if(substring(m, -9,5) == "_lod2")
716 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
719 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
723 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
726 n = search_getsize(globhandle);
727 for (i = 0; i < n; ++i)
729 //print(search_getfilename(globhandle, i), "\n");
730 f = search_getfilename(globhandle, i);
731 PrecachePlayerSounds(f);
733 search_end(globhandle);
735 void precache_all_playermodels(string pattern)
737 float globhandle, i, n;
740 globhandle = search_begin(pattern, true, false);
743 n = search_getsize(globhandle);
744 for (i = 0; i < n; ++i)
746 //print(search_getfilename(globhandle, i), "\n");
747 f = search_getfilename(globhandle, i);
748 precache_playermodel(f);
750 search_end(globhandle);
753 void precache_playermodels(string s)
757 int n = tokenize_console(s);
758 precache_playermodel(argv(0));
760 for (int i = 1; i < n; ++i)
761 precache_model(argv(i));
767 // gamemode related things
769 // Precache all player models if desired
770 if (autocvar_sv_precacheplayermodels)
772 PrecachePlayerSounds("sound/player/default.sounds");
773 precache_all_playermodels("models/player/*.zym");
774 precache_all_playermodels("models/player/*.dpm");
775 precache_all_playermodels("models/player/*.md3");
776 precache_all_playermodels("models/player/*.psk");
777 precache_all_playermodels("models/player/*.iqm");
780 if (autocvar_sv_defaultcharacter)
782 precache_playermodels(autocvar_sv_defaultplayermodel_red);
783 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
784 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
785 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
786 precache_playermodels(autocvar_sv_defaultplayermodel);
790 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
792 if (!self.noise && self.music) // quake 3 uses the music field
793 self.noise = self.music;
795 // plays music for the level if there is any
798 precache_sound (self.noise);
799 ambientsound ('0 0 0', self.noise, VOL_BASE, ATTEN_NONE);
805 void make_safe_for_remove(entity e)
807 if (e.initialize_entity)
809 entity ent, prev = world;
810 for (ent = initialize_entity_first; ent; )
812 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
814 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
815 // skip it in linked list
818 prev.initialize_entity_next = ent.initialize_entity_next;
819 ent = prev.initialize_entity_next;
823 initialize_entity_first = ent.initialize_entity_next;
824 ent = initialize_entity_first;
830 ent = ent.initialize_entity_next;
836 void objerror(string s)
838 make_safe_for_remove(self);
842 .float remove_except_protected_forbidden;
843 void remove_except_protected(entity e)
845 if(e.remove_except_protected_forbidden)
846 error("not allowed to remove this at this point");
850 void remove_unsafely(entity e)
852 if(e.classname == "spike")
853 error("Removing spikes is forbidden (crylink bug), please report");
857 void remove_safely(entity e)
859 make_safe_for_remove(e);
863 void InitializeEntity(entity e, void() func, float order)
867 if (!e || e.initialize_entity)
869 // make a proxy initializer entity
871 e = new(initialize_entity);
875 e.initialize_entity = func;
876 e.initialize_entity_order = order;
878 cur = initialize_entity_first;
882 if (!cur || cur.initialize_entity_order > order)
884 // insert between prev and cur
886 prev.initialize_entity_next = e;
888 initialize_entity_first = e;
889 e.initialize_entity_next = cur;
893 cur = cur.initialize_entity_next;
896 void InitializeEntitiesRun()
898 entity startoflist = initialize_entity_first;
899 initialize_entity_first = NULL;
900 remove = remove_except_protected;
901 for (entity e = startoflist; e; e = e.initialize_entity_next)
903 e.remove_except_protected_forbidden = 1;
905 for (entity e = startoflist; e; )
907 e.remove_except_protected_forbidden = 0;
908 e.initialize_entity_order = 0;
909 entity next = e.initialize_entity_next;
910 e.initialize_entity_next = NULL;
911 var void() func = e.initialize_entity;
912 e.initialize_entity = func_null;
913 if (e.classname == "initialize_entity")
915 entity wrappee = e.enemy;
919 //dprint("Delayed initialization: ", e.classname, "\n");
922 WITH(entity, self, e, func());
927 backtrace(strcat("Null function in: ", e.classname, "\n"));
931 remove = remove_unsafely;
934 .float(entity) isEliminated;
935 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
939 WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
940 WriteByte(MSG_ENTITY, sendflags);
944 for(i = 1; i <= maxclients; i += 8)
946 for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
948 if(eliminatedPlayers.isEliminated(e))
951 WriteByte(MSG_ENTITY, f);
958 void EliminatedPlayers_Init(float(entity) isEliminated_func)
960 if(eliminatedPlayers)
962 backtrace("Can't spawn eliminatedPlayers again!");
965 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
966 eliminatedPlayers.isEliminated = isEliminated_func;
970 void adaptor_think2touch()
979 void adaptor_think2use()
991 void adaptor_think2use_hittype_splash() // for timed projectile detonation
993 if(!(self.flags & FL_ONGROUND)) // if onground, we ARE touching something, but HITTYPE_SPLASH is to be networked if the damage causing projectile is not touching ANYTHING
994 self.projectiledeathtype |= HITTYPE_SPLASH;
999 void DropToFloor_Handler()
1001 builtin_droptofloor();
1002 self.dropped_origin = self.origin;
1007 InitializeEntity(self, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
1012 float trace_hits_box_a0, trace_hits_box_a1;
1014 float trace_hits_box_1d(float end, float thmi, float thma)
1018 // just check if x is in range
1026 // do the trace with respect to x
1027 // 0 -> end has to stay in thmi -> thma
1028 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1029 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1030 if (trace_hits_box_a0 > trace_hits_box_a1)
1036 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1041 // now it is a trace from 0 to end
1043 trace_hits_box_a0 = 0;
1044 trace_hits_box_a1 = 1;
1046 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1048 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1050 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1056 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1058 return trace_hits_box(start, end, thmi - ma, thma - mi);
1061 float SUB_NoImpactCheck()
1063 // zero hitcontents = this is not the real impact, but either the
1064 // mirror-impact of something hitting the projectile instead of the
1065 // projectile hitting the something, or a touchareagrid one. Neither of
1066 // these stop the projectile from moving, so...
1067 if(trace_dphitcontents == 0)
1069 //dprint("A hit happened with zero hit contents... DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct.\n");
1070 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", num_for_edict(self), self.classname, vtos(self.origin));
1073 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1075 if (other == world && self.size != '0 0 0')
1078 tic = self.velocity * sys_frametime;
1079 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
1080 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
1081 if (trace_fraction >= 1)
1083 LOG_TRACE("Odd... did not hit...?\n");
1085 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1087 LOG_TRACE("Detected and prevented the sky-grapple bug.\n");
1095 #define SUB_OwnerCheck() (other && (other == self.owner))
1097 void W_Crylink_Dequeue(entity e);
1098 float WarpZone_Projectile_Touch_ImpactFilter_Callback()
1100 if(SUB_OwnerCheck())
1102 if(SUB_NoImpactCheck())
1104 if(self.classname == "nade")
1105 return false; // no checks here
1106 else if(self.classname == "grapplinghook")
1107 RemoveGrapplingHook(self.realowner);
1108 else if(self.classname == "spike")
1110 W_Crylink_Dequeue(self);
1117 if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1118 UpdateCSQCProjectile(self);
1122 /** engine callback */
1123 void URI_Get_Callback(float id, float status, string data)
1125 if(url_URI_Get_Callback(id, status, data))
1129 else if (id == URI_GET_DISCARD)
1133 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1136 Curl_URI_Get_Callback(id, status, data);
1138 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1141 OnlineBanList_URI_Get_Callback(id, status, data);
1145 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1149 string uid2name(string myuid) {
1151 s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1153 // FIXME remove this later after 0.6 release
1154 // convert old style broken records to correct style
1157 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1160 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1161 db_remove(ServerProgsDB, strcat("uid2name", myuid));
1166 s = "^1Unregistered Player";
1170 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1173 vector start, org, delta, end, enddown, mstart;
1176 m = e.dphitcontentsmask;
1177 e.dphitcontentsmask = goodcontents | badcontents;
1180 delta = boundmax - boundmin;
1184 for (i = 0; i < attempts; ++i)
1186 start.x = org.x + random() * delta.x;
1187 start.y = org.y + random() * delta.y;
1188 start.z = org.z + random() * delta.z;
1190 // rule 1: start inside world bounds, and outside
1191 // solid, and don't start from somewhere where you can
1192 // fall down to evil
1193 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1194 if (trace_fraction >= 1)
1196 if (trace_startsolid)
1198 if (trace_dphitcontents & badcontents)
1200 if (trace_dphitq3surfaceflags & badsurfaceflags)
1203 // rule 2: if we are too high, lower the point
1204 if (trace_fraction * delta.z > maxaboveground)
1205 start = trace_endpos + '0 0 1' * maxaboveground;
1206 enddown = trace_endpos;
1208 // rule 3: make sure we aren't outside the map. This only works
1209 // for somewhat well formed maps. A good rule of thumb is that
1210 // the map should have a convex outside hull.
1211 // these can be traceLINES as we already verified the starting box
1212 mstart = start + 0.5 * (e.mins + e.maxs);
1213 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1214 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1216 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1217 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1219 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1220 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1222 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1223 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1225 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1226 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1229 // rule 4: we must "see" some spawnpoint or item
1230 for(sp = world; (sp = find(sp, classname, "info_player_deathmatch")); )
1231 if(checkpvs(mstart, sp))
1232 if((traceline(mstart, sp.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1236 for(sp = world; (sp = findflags(sp, flags, FL_ITEM)); )
1237 if(checkpvs(mstart, sp))
1238 if((traceline(mstart, sp.origin + (sp.mins + sp.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1244 // find a random vector to "look at"
1245 end.x = org.x + random() * delta.x;
1246 end.y = org.y + random() * delta.y;
1247 end.z = org.z + random() * delta.z;
1248 end = start + normalize(end - start) * vlen(delta);
1250 // rule 4: start TO end must not be too short
1251 tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1252 if (trace_startsolid)
1254 if (trace_fraction < minviewdistance / vlen(delta))
1257 // rule 5: don't want to look at sky
1258 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1261 // rule 6: we must not end up in trigger_hurt
1262 if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1268 e.dphitcontentsmask = m;
1272 setorigin(e, start);
1273 e.angles = vectoangles(end - start);
1274 LOG_TRACE("Needed ", ftos(i + 1), " attempts\n");
1281 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1283 return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1286 void write_recordmarker(entity pl, float tstart, float dt)
1288 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1290 // also write a marker into demo files for demotc-race-record-extractor to find
1293 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1294 " ", ftos(tstart), " ", ftos(dt), "\n"));
1297 void attach_sameorigin(entity e, entity to, string tag)
1299 vector org, t_forward, t_left, t_up, e_forward, e_up;
1302 org = e.origin - gettaginfo(to, gettagindex(to, tag));
1303 tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1304 t_forward = v_forward * tagscale;
1305 t_left = v_right * -tagscale;
1306 t_up = v_up * tagscale;
1308 e.origin_x = org * t_forward;
1309 e.origin_y = org * t_left;
1310 e.origin_z = org * t_up;
1312 // current forward and up directions
1313 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1314 e.angles = AnglesTransform_FromVAngles(e.angles);
1316 e.angles = AnglesTransform_FromAngles(e.angles);
1317 fixedmakevectors(e.angles);
1319 // untransform forward, up!
1320 e_forward.x = v_forward * t_forward;
1321 e_forward.y = v_forward * t_left;
1322 e_forward.z = v_forward * t_up;
1323 e_up.x = v_up * t_forward;
1324 e_up.y = v_up * t_left;
1325 e_up.z = v_up * t_up;
1327 e.angles = fixedvectoangles2(e_forward, e_up);
1328 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1329 e.angles = AnglesTransform_ToVAngles(e.angles);
1331 e.angles = AnglesTransform_ToAngles(e.angles);
1333 setattachment(e, to, tag);
1334 setorigin(e, e.origin);
1337 void detach_sameorigin(entity e)
1340 org = gettaginfo(e, 0);
1341 e.angles = fixedvectoangles2(v_forward, v_up);
1342 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1343 e.angles = AnglesTransform_ToVAngles(e.angles);
1345 e.angles = AnglesTransform_ToAngles(e.angles);
1347 setattachment(e, world, "");
1348 setorigin(e, e.origin);
1351 void follow_sameorigin(entity e, entity to)
1353 e.movetype = MOVETYPE_FOLLOW; // make the hole follow
1354 e.aiment = to; // make the hole follow bmodel
1355 e.punchangle = to.angles; // the original angles of bmodel
1356 e.view_ofs = e.origin - to.origin; // relative origin
1357 e.v_angle = e.angles - to.angles; // relative angles
1360 void unfollow_sameorigin(entity e)
1362 e.movetype = MOVETYPE_NONE;
1365 entity gettaginfo_relative_ent;
1366 vector gettaginfo_relative(entity e, float tag)
1368 if (!gettaginfo_relative_ent)
1370 gettaginfo_relative_ent = spawn();
1371 gettaginfo_relative_ent.effects = EF_NODRAW;
1373 gettaginfo_relative_ent.model = e.model;
1374 gettaginfo_relative_ent.modelindex = e.modelindex;
1375 gettaginfo_relative_ent.frame = e.frame;
1376 return gettaginfo(gettaginfo_relative_ent, tag);
1379 .string aiment_classname;
1380 .float aiment_deadflag;
1381 void SetMovetypeFollow(entity ent, entity e)
1383 // FIXME this may not be warpzone aware
1384 ent.movetype = MOVETYPE_FOLLOW; // make the hole follow
1385 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.
1386 ent.aiment = e; // make the hole follow bmodel
1387 ent.punchangle = e.angles; // the original angles of bmodel
1388 ent.view_ofs = ent.origin - e.origin; // relative origin
1389 ent.v_angle = ent.angles - e.angles; // relative angles
1390 ent.aiment_classname = strzone(e.classname);
1391 ent.aiment_deadflag = e.deadflag;
1393 void UnsetMovetypeFollow(entity ent)
1395 ent.movetype = MOVETYPE_FLY;
1396 PROJECTILE_MAKETRIGGER(ent);
1399 float LostMovetypeFollow(entity ent)
1402 if(ent.movetype != MOVETYPE_FOLLOW)
1408 if(ent.aiment.classname != ent.aiment_classname)
1410 if(ent.aiment.deadflag != ent.aiment_deadflag)
1416 float isPushable(entity e)
1427 case "droppedweapon":
1428 case "keepawayball":
1429 case "nexball_basketball":
1430 case "nexball_football":
1432 case "bullet": // antilagged bullets can't hit this either
1435 if (e.projectiledeathtype)