1 #include "miscfunctions.qh"
4 #include "command/common.qh"
5 #include "constants.qh"
8 #include <server/mutators/_mod.qh>
9 #include "../common/t_items.qh"
10 #include "mapvoting.qh"
11 #include "resources.qh"
14 #include "weapons/accuracy.qh"
15 #include "weapons/csqcprojectile.qh"
16 #include "weapons/selection.qh"
17 #include "../common/command/_mod.qh"
18 #include "../common/constants.qh"
19 #include <common/net_linked.qh>
20 #include <common/weapons/weapon/crylink.qh>
21 #include "../common/deathtypes/all.qh"
22 #include "../common/mapinfo.qh"
23 #include "../common/notifications/all.qh"
24 #include "../common/playerstats.qh"
25 #include "../common/teams.qh"
26 #include "../common/mapobjects/subs.qh"
27 #include "../common/util.qh"
28 #include "../common/turrets/sv_turrets.qh"
29 #include <common/weapons/_all.qh>
30 #include "../common/vehicles/sv_vehicles.qh"
31 #include "../common/vehicles/vehicle.qh"
32 #include "../common/items/_mod.qh"
33 #include "../common/state.qh"
34 #include "../common/effects/qc/globalsound.qh"
35 #include "../common/wepent.qh"
36 #include "../lib/csqcmodel/sv_model.qh"
37 #include "../lib/warpzone/anglestransform.qh"
38 #include "../lib/warpzone/server.qh"
40 void crosshair_trace(entity pl)
42 traceline_antilag(pl, CS(pl).cursor_trace_start, CS(pl).cursor_trace_start + normalize(CS(pl).cursor_trace_endpos - CS(pl).cursor_trace_start) * max_shot_distance, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
45 void crosshair_trace_plusvisibletriggers(entity pl)
47 crosshair_trace_plusvisibletriggers__is_wz(pl, false);
50 void WarpZone_crosshair_trace_plusvisibletriggers(entity pl)
52 crosshair_trace_plusvisibletriggers__is_wz(pl, true);
55 void crosshair_trace_plusvisibletriggers__is_wz(entity pl, bool is_wz)
57 FOREACH_ENTITY_FLOAT(solid, SOLID_TRIGGER,
62 IL_PUSH(g_ctrace_changed, it);
67 WarpZone_crosshair_trace(pl);
71 IL_EACH(g_ctrace_changed, true, { it.solid = SOLID_TRIGGER; });
73 IL_CLEAR(g_ctrace_changed);
76 void WarpZone_crosshair_trace(entity pl)
78 WarpZone_traceline_antilag(pl, CS(pl).cursor_trace_start, CS(pl).cursor_trace_start + normalize(CS(pl).cursor_trace_endpos - CS(pl).cursor_trace_start) * max_shot_distance, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
81 void dedicated_print(string input)
83 if (server_is_dedicated) print(input);
86 void GameLogEcho(string s)
91 if (autocvar_sv_eventlog_files)
96 matches = autocvar_sv_eventlog_files_counter + 1;
97 cvar_set("sv_eventlog_files_counter", itos(matches));
100 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
101 fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
102 logfile = fopen(fn, FILE_APPEND);
103 fputs(logfile, ":logversion:3\n");
107 if (autocvar_sv_eventlog_files_timestamps)
108 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
110 fputs(logfile, strcat(s, "\n"));
113 if (autocvar_sv_eventlog_console)
115 dedicated_print(strcat(s, "\n"));
122 // will be opened later
127 if (logfile_open && logfile >= 0)
134 entity findnearest(vector point, bool checkitems, vector axismod)
139 IL_EACH(((checkitems) ? g_items : g_locations), ((checkitems) ? (it.target == "###item###") : (it.classname == "target_location")),
141 if ((it.items == IT_KEY1 || it.items == IT_KEY2) && it.target == "###item###")
146 dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
147 float len = vlen2(dist);
150 for (l = 0; l < num_nearest; ++l)
152 if (len < nearest_length[l])
156 // now i tells us where to insert at
157 // INSERTION SORT! YOU'VE SEEN IT! RUN!
158 if (l < NUM_NEAREST_ENTITIES)
160 for (int j = NUM_NEAREST_ENTITIES - 1; j >= l; --j)
162 nearest_length[j + 1] = nearest_length[j];
163 nearest_entity[j + 1] = nearest_entity[j];
165 nearest_length[l] = len;
166 nearest_entity[l] = it;
167 if (num_nearest < NUM_NEAREST_ENTITIES)
168 num_nearest = num_nearest + 1;
172 // now use the first one from our list that we can see
173 for (int j = 0; j < num_nearest; ++j)
175 traceline(point, nearest_entity[j].origin, true, NULL);
176 if (trace_fraction == 1)
179 LOG_TRACEF("Nearest point (%s) is not visible, using a visible one.", nearest_entity[0].netname);
180 return nearest_entity[j];
184 if (num_nearest == 0)
187 LOG_TRACE("Not seeing any location point, using nearest as fallback.");
189 dprint("Candidates were: ");
190 for(j = 0; j < num_nearest; ++j)
194 dprint(nearest_entity[j].netname);
199 return nearest_entity[0];
202 string NearestLocation(vector p)
204 string ret = "somewhere";
205 entity loc = findnearest(p, false, '1 1 1');
210 loc = findnearest(p, true, '1 1 4');
217 string AmmoNameFromWeaponentity(Weapon wep)
219 string ammoitems = "batteries";
220 switch (wep.ammo_type)
222 case RES_SHELLS: ammoitems = ITEM_Shells.m_name; break;
223 case RES_BULLETS: ammoitems = ITEM_Bullets.m_name; break;
224 case RES_ROCKETS: ammoitems = ITEM_Rockets.m_name; break;
225 case RES_CELLS: ammoitems = ITEM_Cells.m_name; break;
226 case RES_PLASMA: ammoitems = ITEM_Plasma.m_name; break;
227 case RES_FUEL: ammoitems = ITEM_JetpackFuel.m_name; break;
232 string PlayerHealth(entity this)
234 float myhealth = floor(GetResource(this, RES_HEALTH));
237 else if(myhealth == -2342 || (myhealth == 2342 && mapvote_initialized))
239 else if(myhealth <= 0 || IS_DEAD(this))
241 return ftos(myhealth);
244 string WeaponNameFromWeaponentity(entity this, .entity weaponentity)
246 entity wepent = this.(weaponentity);
249 else if(wepent.m_weapon != WEP_Null)
250 return wepent.m_weapon.m_name;
251 else if(wepent.m_switchweapon != WEP_Null)
252 return wepent.m_switchweapon.m_name;
253 return "none"; //Weapons_from(wepent.cnt).m_name;
256 string formatmessage(entity this, string msg)
260 vector cursor = '0 0 0';
261 entity cursor_ent = NULL;
268 MUTATOR_CALLHOOK(PreFormatMessage, this, msg);
269 msg = M_ARGV(1, string);
273 break; // too many replacements
276 p1 = strstrofs(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
277 p2 = strstrofs(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
292 WarpZone_crosshair_trace_plusvisibletriggers(this);
293 cursor = trace_endpos;
294 cursor_ent = trace_ent;
298 replacement = substring(msg, p, 2);
299 escape = substring(msg, p + 1, 1);
301 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
305 case "%": replacement = "%"; break;
306 case "\\":replacement = "\\"; break;
307 case "n": replacement = "\n"; break;
308 case "a": replacement = ftos(floor(GetResource(this, RES_ARMOR))); break;
309 case "h": replacement = PlayerHealth(this); break;
310 case "l": replacement = NearestLocation(this.origin); break;
311 case "y": replacement = NearestLocation(cursor); break;
312 case "d": replacement = NearestLocation(this.death_origin); break;
313 case "w": replacement = WeaponNameFromWeaponentity(this, weaponentity); break;
314 case "W": replacement = AmmoNameFromWeaponentity(this.(weaponentity).m_weapon); break;
315 case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
316 case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
317 case "S": replacement = ftos(vlen(this.velocity)); break;
318 case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
319 case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
322 MUTATOR_CALLHOOK(FormatMessage, this, escape, replacement, msg);
323 replacement = M_ARGV(2, string);
328 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
329 p = p + strlen(replacement);
340 >0: receives a cvar from name=argv(f) value=argv(f+1)
342 void GetCvars_handleString(entity this, entity store, string thisname, float f, .string field, string name)
346 strfree(store.(field));
350 if (thisname == name)
352 strcpy(store.(field), argv(f + 1));
356 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
358 void GetCvars_handleString_Fixup(entity this, entity store, string thisname, float f, .string field, string name, string(entity, string) func)
360 GetCvars_handleString(this, store, thisname, f, field, name);
361 if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
362 if (thisname == name)
364 string s = func(this, strcat1(store.(field)));
365 if (s != store.(field))
367 strcpy(store.(field), s);
371 void GetCvars_handleFloat(entity this, entity store, string thisname, float f, .float field, string name)
378 if (thisname == name)
379 store.(field) = stof(argv(f + 1));
382 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
384 void GetCvars_handleFloatOnce(entity this, entity store, string thisname, float f, .float field, string name)
391 if (thisname == name)
395 store.(field) = stof(argv(f + 1));
404 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
407 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
409 string o = W_FixWeaponOrder_ForceComplete(wo);
410 strcpy(CS(this).weaponorder_byimpulse, W_FixWeaponOrder_BuildImpulseList(o));
414 REPLICATE(autoswitch, bool, "cl_autoswitch");
416 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
418 REPLICATE(cvar_cl_allow_uidranking, bool, "cl_allow_uidranking");
420 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
422 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
424 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
426 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
428 REPLICATE(cvar_cl_gunalign, int, "cl_gunalign");
430 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
432 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
434 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
436 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
438 REPLICATE(cvar_cl_physics, string, "cl_physics");
440 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
442 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
444 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
446 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
448 REPLICATE(cvar_cl_cts_noautoswitch, bool, "cl_cts_noautoswitch");
450 REPLICATE(cvar_cl_weapon_switch_reload, bool, "cl_weapon_switch_reload");
452 REPLICATE(cvar_cl_weapon_switch_fallback_to_impulse, bool, "cl_weapon_switch_fallback_to_impulse");
455 * @param f -1: cleanup, 0: request, 1: receive
457 void GetCvars(entity this, entity store, int f)
459 string s = string_null;
462 LOG_INFO("Warning: requesting cvar values is deprecated. Client should send them automatically using REPLICATE.\n");
465 s = strcat1(argv(f));
469 MUTATOR_CALLHOOK(GetCvars);
471 Notification_GetCvars(this);
473 ReplicateVars(this, store, s, f);
475 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
476 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
477 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
478 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
479 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
480 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
481 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
482 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
483 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
484 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
485 GetCvars_handleString_Fixup(this, store, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
487 GetCvars_handleFloat(this, store, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
489 // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
492 if (s == "cl_weaponpriority")
494 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
496 .entity weaponentity = weaponentities[slot];
497 if (this.(weaponentity) && (this.(weaponentity).m_weapon != WEP_Null || slot == 0))
498 this.(weaponentity).m_switchweapon = w_getbestweapon(this, weaponentity);
501 if (s == "cl_allow_uidtracking")
502 PlayerStats_GameReport_AddPlayer(this);
506 // decolorizes and team colors the player name when needed
507 string playername(entity p, bool team_colorize)
510 if (team_colorize && teamplay && !intermission_running && IS_PLAYER(p))
512 t = Team_ColorCode(p.team);
513 return strcat(t, strdecolorize(p.netname));
519 float want_weapon(entity weaponinfo, float allguns)
522 bool allow_mutatorblocked = false;
527 bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
528 d = M_ARGV(1, float);
529 allguns = M_ARGV(2, bool);
530 allow_mutatorblocked = M_ARGV(3, bool);
533 d = boolean((weaponinfo.spawnflags & WEP_FLAG_NORMAL) && !(weaponinfo.spawnflags & (WEP_FLAG_HIDDEN | WEP_FLAG_SPECIALATTACK)));
534 else if(!mutator_returnvalue)
535 d = !(!weaponinfo.weaponstart);
537 if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
540 float t = weaponinfo.weaponstartoverride;
542 //LOG_INFOF("want_weapon: %s - d: %d t: %d\n", weaponinfo.netname, d, t);
547 // 4: is set by default?
556 /// Weapons the player normally starts with outside weapon arena.
557 WepSet weapons_start()
559 WepSet ret = '0 0 0';
560 FOREACH(Weapons, it != WEP_Null, {
561 int w = want_weapon(it, false);
570 WepSet ret = '0 0 0';
571 FOREACH(Weapons, it != WEP_Null, {
572 if (!(it.spawnflags & (WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_SPECIALATTACK)))
578 WepSet weapons_devall()
580 WepSet ret = '0 0 0';
581 FOREACH(Weapons, it != WEP_Null,
588 WepSet weapons_most()
590 WepSet ret = '0 0 0';
591 FOREACH(Weapons, it != WEP_Null, {
592 if ((it.spawnflags & WEP_FLAG_NORMAL) && !(it.spawnflags & (WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_HIDDEN | WEP_FLAG_SPECIALATTACK)))
598 void weaponarena_available_all_update(entity this)
602 start_weapons = warmup_start_weapons = g_weaponarena_weapons = weapons_start() | (weaponsInMapAll & weapons_all());
606 // if no weapons are available on the map, just fall back to all weapons arena
607 start_weapons = warmup_start_weapons = g_weaponarena_weapons = weapons_all();
611 void weaponarena_available_devall_update(entity this)
615 start_weapons = warmup_start_weapons = g_weaponarena_weapons = weapons_start() | weaponsInMapAll;
619 // if no weapons are available on the map, just fall back to devall weapons arena
620 start_weapons = warmup_start_weapons = g_weaponarena_weapons = weapons_devall();
624 void weaponarena_available_most_update(entity this)
628 start_weapons = warmup_start_weapons = g_weaponarena_weapons = weapons_start() | (weaponsInMapAll & weapons_most());
632 // if no weapons are available on the map, just fall back to most weapons arena
633 start_weapons = warmup_start_weapons = g_weaponarena_weapons = weapons_most();
637 void readplayerstartcvars()
641 // initialize starting values for players
642 start_weapons = '0 0 0';
643 start_weapons_default = '0 0 0';
644 start_weapons_defaultmask = '0 0 0';
646 start_ammo_shells = 0;
647 start_ammo_nails = 0;
648 start_ammo_rockets = 0;
649 start_ammo_cells = 0;
650 start_ammo_plasma = 0;
651 if (random_start_ammo == NULL)
653 random_start_ammo = spawn();
655 start_health = cvar("g_balance_health_start");
656 start_armorvalue = cvar("g_balance_armor_start");
659 g_weaponarena_weapons = '0 0 0';
661 string s = cvar_string("g_weaponarena");
663 MUTATOR_CALLHOOK(SetWeaponArena, s);
664 s = M_ARGV(0, string);
666 if (s == "0" || s == "")
672 // forcibly turn off weaponarena
674 else if (s == "all" || s == "1")
677 g_weaponarena_list = "All Weapons";
678 g_weaponarena_weapons = weapons_all();
680 else if (s == "devall")
683 g_weaponarena_list = "Dev All Weapons";
684 g_weaponarena_weapons = weapons_devall();
686 else if (s == "most")
689 g_weaponarena_list = "Most Weapons";
690 g_weaponarena_weapons = weapons_most();
692 else if (s == "all_available")
695 g_weaponarena_list = "All Available Weapons";
697 // this needs to run after weaponsInMapAll is initialized
698 InitializeEntity(NULL, weaponarena_available_all_update, INITPRIO_FINDTARGET);
700 else if (s == "devall_available")
703 g_weaponarena_list = "Dev All Available Weapons";
705 // this needs to run after weaponsInMapAll is initialized
706 InitializeEntity(NULL, weaponarena_available_devall_update, INITPRIO_FINDTARGET);
708 else if (s == "most_available")
711 g_weaponarena_list = "Most Available Weapons";
713 // this needs to run after weaponsInMapAll is initialized
714 InitializeEntity(NULL, weaponarena_available_most_update, INITPRIO_FINDTARGET);
716 else if (s == "none")
719 g_weaponarena_list = "No Weapons";
724 t = tokenize_console(s);
725 g_weaponarena_list = "";
726 for (i = 0; i < t; ++i)
729 Weapon wep = Weapons_fromstr(s);
732 g_weaponarena_weapons |= (wep.m_wepset);
733 g_weaponarena_list = strcat(g_weaponarena_list, wep.m_name, " & ");
736 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
741 g_weapon_stay = 0; // incompatible
742 start_weapons = g_weaponarena_weapons;
743 start_items |= IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS;
747 FOREACH(Weapons, it != WEP_Null, {
748 int w = want_weapon(it, false);
749 WepSet s = it.m_wepset;
753 start_weapons_default |= s;
755 start_weapons_defaultmask |= s;
759 if(cvar("g_balance_superweapons_time") < 0)
760 start_items |= IT_UNLIMITED_SUPERWEAPONS;
762 if(!cvar("g_use_ammunition"))
763 start_items |= IT_UNLIMITED_AMMO;
765 if(start_items & IT_UNLIMITED_AMMO)
767 start_ammo_shells = 999;
768 start_ammo_nails = 999;
769 start_ammo_rockets = 999;
770 start_ammo_cells = 999;
771 start_ammo_plasma = 999;
772 start_ammo_fuel = 999;
776 start_ammo_shells = cvar("g_start_ammo_shells");
777 start_ammo_nails = cvar("g_start_ammo_nails");
778 start_ammo_rockets = cvar("g_start_ammo_rockets");
779 start_ammo_cells = cvar("g_start_ammo_cells");
780 start_ammo_plasma = cvar("g_start_ammo_plasma");
781 start_ammo_fuel = cvar("g_start_ammo_fuel");
782 random_start_weapons_count = cvar("g_random_start_weapons_count");
783 SetResource(random_start_ammo, RES_SHELLS, cvar("g_random_start_shells"));
784 SetResource(random_start_ammo, RES_BULLETS, cvar("g_random_start_bullets"));
785 SetResource(random_start_ammo, RES_ROCKETS,cvar("g_random_start_rockets"));
786 SetResource(random_start_ammo, RES_CELLS, cvar("g_random_start_cells"));
787 SetResource(random_start_ammo, RES_PLASMA, cvar("g_random_start_plasma"));
790 warmup_start_ammo_shells = start_ammo_shells;
791 warmup_start_ammo_nails = start_ammo_nails;
792 warmup_start_ammo_rockets = start_ammo_rockets;
793 warmup_start_ammo_cells = start_ammo_cells;
794 warmup_start_ammo_plasma = start_ammo_plasma;
795 warmup_start_ammo_fuel = start_ammo_fuel;
796 warmup_start_health = start_health;
797 warmup_start_armorvalue = start_armorvalue;
798 warmup_start_weapons = start_weapons;
799 warmup_start_weapons_default = start_weapons_default;
800 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
804 warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
805 warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
806 warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
807 warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
808 warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
809 warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
810 warmup_start_health = cvar("g_warmup_start_health");
811 warmup_start_armorvalue = cvar("g_warmup_start_armor");
812 warmup_start_weapons = '0 0 0';
813 warmup_start_weapons_default = '0 0 0';
814 warmup_start_weapons_defaultmask = '0 0 0';
815 FOREACH(Weapons, it != WEP_Null, {
816 int w = want_weapon(it, g_warmup_allguns);
817 WepSet s = it.m_wepset;
819 warmup_start_weapons |= s;
821 warmup_start_weapons_default |= s;
823 warmup_start_weapons_defaultmask |= s;
828 start_items |= ITEM_Jetpack.m_itemid;
830 MUTATOR_CALLHOOK(SetStartItems);
832 if (start_items & ITEM_Jetpack.m_itemid)
834 start_items |= ITEM_JetpackRegen.m_itemid;
835 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
836 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
839 start_ammo_shells = max(0, start_ammo_shells);
840 start_ammo_nails = max(0, start_ammo_nails);
841 start_ammo_rockets = max(0, start_ammo_rockets);
842 start_ammo_cells = max(0, start_ammo_cells);
843 start_ammo_plasma = max(0, start_ammo_plasma);
844 start_ammo_fuel = max(0, start_ammo_fuel);
845 SetResource(random_start_ammo, RES_SHELLS,
846 max(0, GetResource(random_start_ammo, RES_SHELLS)));
847 SetResource(random_start_ammo, RES_BULLETS,
848 max(0, GetResource(random_start_ammo, RES_BULLETS)));
849 SetResource(random_start_ammo, RES_ROCKETS,
850 max(0, GetResource(random_start_ammo, RES_ROCKETS)));
851 SetResource(random_start_ammo, RES_CELLS,
852 max(0, GetResource(random_start_ammo, RES_CELLS)));
853 SetResource(random_start_ammo, RES_PLASMA,
854 max(0, GetResource(random_start_ammo, RES_PLASMA)));
856 warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
857 warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
858 warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
859 warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
860 warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
861 warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
864 void precache_playermodel(string m)
866 float globhandle, i, n;
869 if(substring(m, -9, 5) == "_lod1")
871 if(substring(m, -9, 5) == "_lod2")
874 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
877 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
881 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
884 n = search_getsize(globhandle);
885 for (i = 0; i < n; ++i)
887 //print(search_getfilename(globhandle, i), "\n");
888 f = search_getfilename(globhandle, i);
889 PrecachePlayerSounds(f);
891 search_end(globhandle);
893 void precache_all_playermodels(string pattern)
895 int globhandle = search_begin(pattern, true, false);
896 if (globhandle < 0) return;
897 int n = search_getsize(globhandle);
898 for (int i = 0; i < n; ++i)
900 string s = search_getfilename(globhandle, i);
901 precache_playermodel(s);
903 search_end(globhandle);
906 void precache_playermodels(string s)
908 FOREACH_WORD(s, true, { precache_playermodel(it); });
913 // gamemode related things
915 // Precache all player models if desired
916 if (autocvar_sv_precacheplayermodels)
918 PrecachePlayerSounds("sound/player/default.sounds");
919 precache_all_playermodels("models/player/*.zym");
920 precache_all_playermodels("models/player/*.dpm");
921 precache_all_playermodels("models/player/*.md3");
922 precache_all_playermodels("models/player/*.psk");
923 precache_all_playermodels("models/player/*.iqm");
926 if (autocvar_sv_defaultcharacter)
928 precache_playermodels(autocvar_sv_defaultplayermodel_red);
929 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
930 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
931 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
932 precache_playermodels(autocvar_sv_defaultplayermodel);
936 // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
938 if (!this.noise && this.music) // quake 3 uses the music field
939 this.noise = this.music;
941 // plays music for the level if there is any
944 precache_sound (this.noise);
945 ambientsound ('0 0 0', this.noise, VOL_BASE, ATTEN_NONE);
951 void make_safe_for_remove(entity e)
953 if (e.initialize_entity)
955 entity ent, prev = NULL;
956 for (ent = initialize_entity_first; ent; )
958 if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
960 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
961 // skip it in linked list
964 prev.initialize_entity_next = ent.initialize_entity_next;
965 ent = prev.initialize_entity_next;
969 initialize_entity_first = ent.initialize_entity_next;
970 ent = initialize_entity_first;
976 ent = ent.initialize_entity_next;
982 .float remove_except_protected_forbidden;
983 void remove_except_protected(entity e)
985 if(e.remove_except_protected_forbidden)
986 error("not allowed to remove this at this point");
990 void remove_unsafely(entity e)
992 if(e.classname == "spike")
993 error("Removing spikes is forbidden (crylink bug), please report");
997 void remove_safely(entity e)
999 make_safe_for_remove(e);
1003 void InitializeEntity(entity e, void(entity this) func, int order)
1007 if (!e || e.initialize_entity)
1009 // make a proxy initializer entity
1011 e = new(initialize_entity);
1015 e.initialize_entity = func;
1016 e.initialize_entity_order = order;
1018 cur = initialize_entity_first;
1022 if (!cur || cur.initialize_entity_order > order)
1024 // insert between prev and cur
1026 prev.initialize_entity_next = e;
1028 initialize_entity_first = e;
1029 e.initialize_entity_next = cur;
1033 cur = cur.initialize_entity_next;
1036 void InitializeEntitiesRun()
1038 entity startoflist = initialize_entity_first;
1039 initialize_entity_first = NULL;
1040 delete_fn = remove_except_protected;
1041 for (entity e = startoflist; e; e = e.initialize_entity_next)
1043 e.remove_except_protected_forbidden = 1;
1045 for (entity e = startoflist; e; )
1047 e.remove_except_protected_forbidden = 0;
1048 e.initialize_entity_order = 0;
1049 entity next = e.initialize_entity_next;
1050 e.initialize_entity_next = NULL;
1051 var void(entity this) func = e.initialize_entity;
1052 e.initialize_entity = func_null;
1053 if (e.classname == "initialize_entity")
1055 entity wrappee = e.enemy;
1059 //dprint("Delayed initialization: ", e.classname, "\n");
1067 backtrace(strcat("Null function in: ", e.classname, "\n"));
1071 delete_fn = remove_unsafely;
1074 .float(entity) isEliminated;
1075 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
1077 Stream out = MSG_ENTITY;
1078 WriteHeader(out, ENT_CLIENT_ELIMINATEDPLAYERS);
1079 serialize(byte, out, sendflags);
1080 if (sendflags & 1) {
1081 for (int i = 1; i <= maxclients; i += 8) {
1083 entity e = edict_num(i);
1084 for (int b = 0; b < 8; ++b, e = nextent(e)) {
1085 if (eliminatedPlayers.isEliminated(e)) {
1089 serialize(byte, out, f);
1095 void EliminatedPlayers_Init(float(entity) isEliminated_func)
1097 if(eliminatedPlayers)
1099 backtrace("Can't spawn eliminatedPlayers again!");
1102 Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
1103 eliminatedPlayers.isEliminated = isEliminated_func;
1109 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
1111 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
1112 this.projectiledeathtype |= HITTYPE_SPLASH;
1113 adaptor_think2use(this);
1116 // deferred dropping
1117 void DropToFloor_Handler(entity this)
1119 WITHSELF(this, builtin_droptofloor());
1120 this.dropped_origin = this.origin;
1123 void droptofloor(entity this)
1125 InitializeEntity(this, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
1130 float trace_hits_box_a0, trace_hits_box_a1;
1132 float trace_hits_box_1d(float end, float thmi, float thma)
1136 // just check if x is in range
1144 // do the trace with respect to x
1145 // 0 -> end has to stay in thmi -> thma
1146 trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1147 trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1148 if (trace_hits_box_a0 > trace_hits_box_a1)
1154 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1159 // now it is a trace from 0 to end
1161 trace_hits_box_a0 = 0;
1162 trace_hits_box_a1 = 1;
1164 if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1166 if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1168 if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1174 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1176 return trace_hits_box(start, end, thmi - ma, thma - mi);
1179 bool SUB_NoImpactCheck(entity this, entity toucher)
1181 // zero hitcontents = this is not the real impact, but either the
1182 // mirror-impact of something hitting the projectile instead of the
1183 // projectile hitting the something, or a touchareagrid one. Neither of
1184 // these stop the projectile from moving, so...
1185 if(trace_dphitcontents == 0)
1187 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);
1188 checkclient(this); // TODO: .health is checked in the engine with this, possibly replace with a QC function?
1190 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1192 if (toucher == NULL && this.size != '0 0 0')
1195 tic = this.velocity * sys_frametime;
1196 tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
1197 traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
1198 if (trace_fraction >= 1)
1200 LOG_TRACE("Odd... did not hit...?");
1202 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1204 LOG_TRACE("Detected and prevented the sky-grapple bug.");
1212 #define SUB_OwnerCheck(ent,oth) ((oth) && ((oth) == (ent).owner))
1214 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
1216 if(SUB_OwnerCheck(this, toucher))
1218 if(SUB_NoImpactCheck(this, toucher))
1220 if(this.classname == "nade")
1221 return false; // no checks here
1222 else if(this.classname == "grapplinghook")
1228 if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1229 UpdateCSQCProjectile(this);
1233 /** engine callback */
1234 void URI_Get_Callback(float id, float status, string data)
1236 if(url_URI_Get_Callback(id, status, data))
1240 else if (id == URI_GET_DISCARD)
1244 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1247 Curl_URI_Get_Callback(id, status, data);
1249 else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1252 OnlineBanList_URI_Get_Callback(id, status, data);
1254 else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
1256 // handled by a mutator
1260 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".");
1264 string uid2name(string myuid) {
1266 s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1268 // FIXME remove this later after 0.6 release
1269 // convert old style broken records to correct style
1272 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1275 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1276 db_remove(ServerProgsDB, strcat("uid2name", myuid));
1281 s = "^1Unregistered Player";
1285 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1288 vector start, org, delta, end, enddown, mstart;
1290 m = e.dphitcontentsmask;
1291 e.dphitcontentsmask = goodcontents | badcontents;
1294 delta = boundmax - boundmin;
1298 for (i = 0; i < attempts; ++i)
1300 start.x = org.x + random() * delta.x;
1301 start.y = org.y + random() * delta.y;
1302 start.z = org.z + random() * delta.z;
1304 // rule 1: start inside world bounds, and outside
1305 // solid, and don't start from somewhere where you can
1306 // fall down to evil
1307 tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1308 if (trace_fraction >= 1)
1310 if (trace_startsolid)
1312 if (trace_dphitcontents & badcontents)
1314 if (trace_dphitq3surfaceflags & badsurfaceflags)
1317 // rule 2: if we are too high, lower the point
1318 if (trace_fraction * delta.z > maxaboveground)
1319 start = trace_endpos + '0 0 1' * maxaboveground;
1320 enddown = trace_endpos;
1322 // rule 3: make sure we aren't outside the map. This only works
1323 // for somewhat well formed maps. A good rule of thumb is that
1324 // the map should have a convex outside hull.
1325 // these can be traceLINES as we already verified the starting box
1326 mstart = start + 0.5 * (e.mins + e.maxs);
1327 traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1328 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1330 traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1331 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1333 traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1334 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1336 traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1337 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1339 traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1340 if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1343 // rule 4: we must "see" some spawnpoint or item
1345 IL_EACH(g_spawnpoints, checkpvs(mstart, it),
1347 if((traceline(mstart, it.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1355 int items_checked = 0;
1356 IL_EACH(g_items, checkpvs(mstart, it),
1358 if((traceline(mstart, it.origin + (it.mins + it.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1365 if(items_checked >= attempts)
1373 // find a random vector to "look at"
1374 end.x = org.x + random() * delta.x;
1375 end.y = org.y + random() * delta.y;
1376 end.z = org.z + random() * delta.z;
1377 end = start + normalize(end - start) * vlen(delta);
1379 // rule 4: start TO end must not be too short
1380 tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1381 if (trace_startsolid)
1383 if (trace_fraction < minviewdistance / vlen(delta))
1386 // rule 5: don't want to look at sky
1387 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1390 // rule 6: we must not end up in trigger_hurt
1391 if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1397 e.dphitcontentsmask = m;
1401 setorigin(e, start);
1402 e.angles = vectoangles(end - start);
1403 LOG_DEBUG("Needed ", ftos(i + 1), " attempts");
1410 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1412 return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1415 void write_recordmarker(entity pl, float tstart, float dt)
1417 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1419 // also write a marker into demo files for demotc-race-record-extractor to find
1422 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1423 " ", ftos(tstart), " ", ftos(dt), "\n"));
1426 void attach_sameorigin(entity e, entity to, string tag)
1428 vector org, t_forward, t_left, t_up, e_forward, e_up;
1431 org = e.origin - gettaginfo(to, gettagindex(to, tag));
1432 tagscale = (vlen(v_forward) ** -2); // undo a scale on the tag
1433 t_forward = v_forward * tagscale;
1434 t_left = v_right * -tagscale;
1435 t_up = v_up * tagscale;
1437 e.origin_x = org * t_forward;
1438 e.origin_y = org * t_left;
1439 e.origin_z = org * t_up;
1441 // current forward and up directions
1442 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1443 e.angles = AnglesTransform_FromVAngles(e.angles);
1445 e.angles = AnglesTransform_FromAngles(e.angles);
1446 fixedmakevectors(e.angles);
1448 // untransform forward, up!
1449 e_forward.x = v_forward * t_forward;
1450 e_forward.y = v_forward * t_left;
1451 e_forward.z = v_forward * t_up;
1452 e_up.x = v_up * t_forward;
1453 e_up.y = v_up * t_left;
1454 e_up.z = v_up * t_up;
1456 e.angles = fixedvectoangles2(e_forward, e_up);
1457 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1458 e.angles = AnglesTransform_ToVAngles(e.angles);
1460 e.angles = AnglesTransform_ToAngles(e.angles);
1462 setattachment(e, to, tag);
1463 setorigin(e, e.origin);
1466 void detach_sameorigin(entity e)
1469 org = gettaginfo(e, 0);
1470 e.angles = fixedvectoangles2(v_forward, v_up);
1471 if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1472 e.angles = AnglesTransform_ToVAngles(e.angles);
1474 e.angles = AnglesTransform_ToAngles(e.angles);
1476 setattachment(e, NULL, "");
1477 setorigin(e, e.origin);
1480 void follow_sameorigin(entity e, entity to)
1482 set_movetype(e, MOVETYPE_FOLLOW); // make the hole follow
1483 e.aiment = to; // make the hole follow bmodel
1484 e.punchangle = to.angles; // the original angles of bmodel
1485 e.view_ofs = e.origin - to.origin; // relative origin
1486 e.v_angle = e.angles - to.angles; // relative angles
1489 void unfollow_sameorigin(entity e)
1491 set_movetype(e, MOVETYPE_NONE);
1494 entity gettaginfo_relative_ent;
1495 vector gettaginfo_relative(entity e, float tag)
1497 if (!gettaginfo_relative_ent)
1499 gettaginfo_relative_ent = spawn();
1500 gettaginfo_relative_ent.effects = EF_NODRAW;
1502 gettaginfo_relative_ent.model = e.model;
1503 gettaginfo_relative_ent.modelindex = e.modelindex;
1504 gettaginfo_relative_ent.frame = e.frame;
1505 return gettaginfo(gettaginfo_relative_ent, tag);
1508 .string aiment_classname;
1509 .float aiment_deadflag;
1510 void SetMovetypeFollow(entity ent, entity e)
1512 // FIXME this may not be warpzone aware
1513 set_movetype(ent, MOVETYPE_FOLLOW); // make the hole follow
1514 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.
1515 ent.aiment = e; // make the hole follow bmodel
1516 ent.punchangle = e.angles; // the original angles of bmodel
1517 ent.view_ofs = ent.origin - e.origin; // relative origin
1518 ent.v_angle = ent.angles - e.angles; // relative angles
1519 ent.aiment_classname = strzone(e.classname);
1520 ent.aiment_deadflag = e.deadflag;
1522 void UnsetMovetypeFollow(entity ent)
1524 set_movetype(ent, MOVETYPE_FLY);
1525 PROJECTILE_MAKETRIGGER(ent);
1528 float LostMovetypeFollow(entity ent)
1531 if(ent.move_movetype != MOVETYPE_FOLLOW)
1537 if(ent.aiment.classname != ent.aiment_classname)
1539 if(ent.aiment.deadflag != ent.aiment_deadflag)
1546 bool isPushable(entity e)
1562 case "bullet": // antilagged bullets can't hit this either
1565 if (e.projectiledeathtype)