1 void() info_player_deathmatch; // needed for the other spawnpoints
2 string ColoredTeamName(float t);
4 float DistributeEvenly_amount;
5 float DistributeEvenly_totalweight;
6 void DistributeEvenly_Init(float amount, float totalweight)
8 if(DistributeEvenly_amount)
10 dprint("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ");
11 dprint(ftos(DistributeEvenly_totalweight), " left!)\n");
14 DistributeEvenly_amount = 0;
16 DistributeEvenly_amount = amount;
17 DistributeEvenly_totalweight = totalweight;
19 float DistributeEvenly_Get(float weight)
24 f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
25 DistributeEvenly_totalweight -= weight;
26 DistributeEvenly_amount -= f;
30 void move_out_of_solid_expand(entity e, vector by)
33 tracebox(e.origin, e.mins - '1 1 1' * eps, e.maxs + '1 1 1' * eps, e.origin + by, MOVE_WORLDONLY, e);
36 if(trace_fraction < 1)
39 // adjust origin in the other direction...
40 e.origin = e.origin - by * (1 - trace_fraction);
44 void move_out_of_solid(entity e)
49 traceline(o, o, MOVE_WORLDONLY, e);
52 dprint("origin is in solid too! (", vtos(o), ")");
56 tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
64 move_out_of_solid_expand(e, '1 0 0' * m0_x); e.mins_x = m0_x;
65 move_out_of_solid_expand(e, '1 0 0' * m1_x); e.maxs_x = m1_x;
66 move_out_of_solid_expand(e, '0 1 0' * m0_y); e.mins_y = m0_y;
67 move_out_of_solid_expand(e, '0 1 0' * m1_y); e.maxs_y = m1_y;
68 move_out_of_solid_expand(e, '0 0 1' * m0_z); e.mins_z = m0_z;
69 move_out_of_solid_expand(e, '0 0 1' * m1_z); e.maxs_z = m1_z;
70 setorigin(e, e.origin);
72 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
75 dprint("could not get out of solid (", vtos(o), ")\n");
80 // converts a number to a string with the indicated number of decimals
81 // works for up to 10 decimals!
82 string ftos_decimals(float number, float decimals)
88 // if negative, cut off the sign first
90 return strcat("-", ftos_decimals(-number, decimals));
91 // it now is always positive!
94 number = floor(number * pow(10, decimals) + 0.5);
97 result = ftos(number);
99 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
100 // if ftos had fucked it up, which should never happen: "34278.000000"
102 if(substring(result, len - 7, 1) == ".")
104 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
105 result = substring(result, 0, len - 7);
109 // is it too short? If yes, insert leading zeroes
112 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
115 // and now... INSERT THE POINT!
116 tmp = substring(result, len - decimals, decimals);
117 result = strcat(substring(result, 0, len - decimals), ".", tmp);
121 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
122 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
123 string STR_PLAYER = "player";
124 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
125 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
127 // change that to actually calling strcat when running on an engine without
128 // unlimited tempstrings:
129 // string strcat1(string s) = #115; // FRIK_FILE
130 #define strcat1(s) (s)
135 void(string s) bcenterprint
137 // TODO replace by MSG_ALL (would show it to spectators too, though)?
139 FOR_EACH_PLAYER(head)
140 if(clienttype(head) == CLIENTTYPE_REAL)
141 centerprint(head, s);
144 void(string s, float check_dangerous) ServerConsoleEcho =
147 if (checkextension("DP_SV_PRINT"))
156 ch = substring(s, 0, 1);
157 if(ch != "\"" && ch != "\r" && ch != "\n")
159 s = substring(s, 1, strlen(s) - 1);
170 void(string s, float check_dangerous) GameLogEcho =
175 if(cvar("sv_eventlog_files"))
180 matches = cvar("sv_eventlog_files_counter") + 1;
181 cvar_set("sv_eventlog_files_counter", ftos(matches));
184 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
185 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
186 logfile = fopen(fn, FILE_APPEND);
189 fputs(logfile, strcat(s, "\n"));
191 if(cvar("sv_eventlog_console"))
193 ServerConsoleEcho(s, check_dangerous);
200 // will be opened later
203 void() GameLogClose =
205 if(logfile_open && logfile >= 0)
212 float math_mod(float a, float b)
214 return a - (floor(a / b) * b);
217 void relocate_spawnpoint()
219 // nudge off the floor
220 setorigin(self, self.origin + '0 0 1');
222 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
223 if (trace_startsolid)
225 objerror("player spawn point in solid, mapper sucks!\n");
229 if(cvar("g_spawnpoints_autodrop"))
231 setsize(self, PL_MIN, PL_MAX);
236 #define strstr strstrofs
238 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
239 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
240 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
241 // BE CONSTANT OR strzoneD!
242 float(string haystack, string needle, float offset) strstr =
246 len = strlen(needle);
247 endpos = strlen(haystack) - len;
248 while(offset <= endpos)
250 found = substring(haystack, offset, len);
259 float NUM_NEAREST_ENTITIES = 4;
260 entity nearest_entity[NUM_NEAREST_ENTITIES];
261 float nearest_length[NUM_NEAREST_ENTITIES];
262 entity(vector point, .string field, string value, vector axismod) findnearest =
273 localhead = find(world, field, value);
276 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
277 dist = localhead.oldorigin;
279 dist = localhead.origin;
281 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
284 for(i = 0; i < num_nearest; ++i)
286 if(len < nearest_length[i])
290 // now i tells us where to insert at
291 // INSERTION SORT! YOU'VE SEEN IT! RUN!
292 if(i < NUM_NEAREST_ENTITIES)
294 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
296 nearest_length[j + 1] = nearest_length[j];
297 nearest_entity[j + 1] = nearest_entity[j];
299 nearest_length[i] = len;
300 nearest_entity[i] = localhead;
301 if(num_nearest < NUM_NEAREST_ENTITIES)
302 num_nearest = num_nearest + 1;
305 localhead = find(localhead, field, value);
308 // now use the first one from our list that we can see
309 for(i = 0; i < num_nearest; ++i)
311 traceline(point, nearest_entity[i].origin, TRUE, world);
312 if(trace_fraction == 1)
316 dprint("Nearest point (");
317 dprint(nearest_entity[0].netname);
318 dprint(") is not visible, using a visible one.\n");
320 return nearest_entity[i];
327 dprint("Not seeing any location point, using nearest as fallback.\n");
329 dprint("Candidates were: ");
330 for(j = 0; j < num_nearest; ++j)
334 dprint(nearest_entity[j].netname);
339 return nearest_entity[0];
342 void() target_location =
344 self.classname = "target_location";
345 // location name in netname
346 // eventually support: count, teamgame selectors, line of sight?
349 void() info_location =
351 self.classname = "target_location";
352 self.message = self.netname;
355 string NearestLocation(vector p)
360 loc = findnearest(p, classname, "target_location", '1 1 1');
367 loc = findnearest(p, target, "###item###", '1 1 4');
374 string(string msg) formatmessage =
381 msg_save = strzone(msg);
387 break; // too many replacements
389 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
392 replacement = substring(msg_save, p, 2);
393 escape = substring(msg_save, p + 1, 1);
396 else if(escape == "a")
397 replacement = ftos(floor(self.armorvalue));
398 else if(escape == "h")
399 replacement = ftos(floor(self.health));
400 else if(escape == "l")
401 replacement = NearestLocation(self.origin);
402 else if(escape == "y")
403 replacement = NearestLocation(self.cursor_trace_endpos);
404 else if(escape == "d")
405 replacement = NearestLocation(self.death_origin);
406 else if(escape == "w")
411 wep = self.switchweapon;
414 replacement = W_Name(wep);
416 else if(escape == "W")
418 if(self.items & IT_SHELLS) replacement = "shells";
419 else if(self.items & IT_NAILS) replacement = "bullets";
420 else if(self.items & IT_ROCKETS) replacement = "rockets";
421 else if(self.items & IT_CELLS) replacement = "cells";
422 else replacement = "batteries"; // ;)
424 else if(escape == "x")
426 replacement = self.cursor_trace_ent.netname;
427 if(!replacement || !self.cursor_trace_ent)
428 replacement = "nothing";
430 else if(escape == "p")
432 if(self.last_selected_player)
433 replacement = self.last_selected_player.netname;
435 replacement = "(nobody)";
437 msg = strcat(substring(msg_save, 0, p), replacement);
438 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
440 msg_save = strzone(msg);
443 msg = strcat(msg_save, "");
454 >0: receives a cvar from name=argv(f) value=argv(f+1)
456 void GetCvars_handleString(float f, .string field, string name)
461 strunzone(self.field);
468 strunzone(self.field);
469 self.field = strzone(argv(f + 1));
473 stuffcmd(self, strcat("sendcvar ", name, "\n"));
475 void GetCvars_handleFloat(float f, .float field, string name)
483 self.field = stof(argv(f + 1));
486 stuffcmd(self, strcat("sendcvar ", name, "\n"));
488 void GetCvars(float f)
490 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
491 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
492 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
493 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
494 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
495 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
496 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
497 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
498 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
499 GetCvars_handleFloat(f, cvar_cl_handicap, "cl_handicap");
502 float fexists(string f)
505 fh = fopen(f, FILE_READ);
512 void backtrace(string msg)
515 dev = cvar("developer");
516 cvar_set("developer", "1");
518 dprint("--- CUT HERE ---\nWARNING: ");
521 remove(world); // isn't there any better way to cause a backtrace?
522 dprint("\n--- CUT UNTIL HERE ---\n");
523 cvar_set("developer", ftos(dev));
526 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
537 // p.frags = 0; // do not harm the new team!
538 // return; // won't distribute negative scores
544 f = ceil(factor * p.frags);
545 p.frags = p.frags - f;
548 FOR_EACH_PLAYER(head)
550 if(head.team == targetteam)
556 DistributeEvenly_Init(f, nTeam);
558 FOR_EACH_PLAYER(head)
560 if(head.team == targetteam)
561 head.frags = head.frags + DistributeEvenly_Get(1);
564 string Team_ColorCode(float teamid)
566 if(teamid == COLOR_TEAM1)
568 else if(teamid == COLOR_TEAM2)
570 else if(teamid == COLOR_TEAM3)
572 else if(teamid == COLOR_TEAM4)
579 string decolorize(string s)
588 ch1 = substring(s, 0, 1);
589 ch2 = substring(s, 1, 1);
594 out = strcat(out, "^^");
618 out = strcat(out, "^^");
620 s = substring(s, n, strlen(s) - n);
624 s = substring(s, 1, strlen(s) - 1);
625 out = strcat(out, ch1);
630 #define strdecolorize(s) decolorize(s)
631 #define strlennocol(s) strlen(decolorize(s))
634 #define CENTERPRIO_POINT 1
635 #define CENTERPRIO_SPAM 2
636 #define CENTERPRIO_REBALANCE 2
637 #define CENTERPRIO_VOTE 4
638 #define CENTERPRIO_NORMAL 5
639 #define CENTERPRIO_MAPVOTE 9
640 #define CENTERPRIO_ADMIN 99
641 .float centerprint_priority;
642 .float centerprint_expires;
643 void centerprint_atprio(entity e, float prio, string s)
645 if(intermission_running)
646 if(prio < CENTERPRIO_MAPVOTE)
648 if(time > e.centerprint_expires)
649 e.centerprint_priority = 0;
650 if(prio >= e.centerprint_priority)
652 e.centerprint_priority = prio;
653 e.centerprint_expires = time + e.cvar_scr_centertime;
654 centerprint_builtin(e, s);
657 void centerprint_expire(entity e, float prio)
659 if(prio == e.centerprint_priority)
661 e.centerprint_priority = 0;
662 centerprint_builtin(e, "");
665 void centerprint(entity e, string s)
667 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
672 // decolorizes and team colors the player name when needed
673 string playername(entity p)
676 if(teams_matter && !intermission_running && p.classname == "player")
678 t = Team_ColorCode(p.team);
679 return strcat(t, strdecolorize(p.netname));
685 vector(vector m1, vector m2) randompos =
689 v_x = m2_x * random() + m1_x;
690 v_y = m2_y * random() + m1_y;
691 v_z = m2_z * random() + m1_z;
695 // requires that m2>m1 in all coordinates, and that m4>m3
696 float(vector m1, vector m2, vector m3, vector m4) boxesoverlap = {return m2_x >= m3_x && m1_x <= m4_x && m2_y >= m3_y && m1_y <= m4_y && m2_z >= m3_z && m1_z <= m4_z;};
698 // requires the same, but is a stronger condition
699 float(vector smins, vector smaxs, vector bmins, vector bmaxs) boxinsidebox = {return smins_x >= bmins_x && smaxs_x <= bmaxs_x && smins_y >= bmins_y && smaxs_y <= bmaxs_y && smins_z >= bmins_z && smaxs_z <= bmaxs_z;};
701 float g_pickup_shells;
702 float g_pickup_shells_max;
703 float g_pickup_nails;
704 float g_pickup_nails_max;
705 float g_pickup_rockets;
706 float g_pickup_rockets_max;
707 float g_pickup_cells;
708 float g_pickup_cells_max;
709 float g_pickup_armorshard;
710 float g_pickup_armorshard_max;
711 float g_pickup_armor;
712 float g_pickup_armor_max;
713 float g_pickup_healthshard;
714 float g_pickup_healthshard_max;
715 float g_pickup_health;
716 float g_pickup_health_max;
717 float g_pickup_healthmega;
718 float g_pickup_healthmega_max;
721 float start_switchweapon;
722 float start_ammo_shells;
723 float start_ammo_nails;
724 float start_ammo_rockets;
725 float start_ammo_cells;
727 float start_armorvalue;
729 void readlevelcvars(void)
731 g_pickup_shells = cvar("g_pickup_shells");
732 g_pickup_shells_max = cvar("g_pickup_shells_max");
733 g_pickup_nails = cvar("g_pickup_nails");
734 g_pickup_nails_max = cvar("g_pickup_nails_max");
735 g_pickup_rockets = cvar("g_pickup_rockets");
736 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
737 g_pickup_cells = cvar("g_pickup_cells");
738 g_pickup_cells_max = cvar("g_pickup_cells_max");
739 g_pickup_armorshard = cvar("g_pickup_armorshard");
740 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
741 g_pickup_armor = cvar("g_pickup_armor");
742 g_pickup_armor_max = cvar("g_pickup_armor_max");
743 g_pickup_healthshard = cvar("g_pickup_healthshard");
744 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
745 g_pickup_health = cvar("g_pickup_health");
746 g_pickup_health_max = cvar("g_pickup_health_max");
747 g_pickup_healthmega = cvar("g_pickup_healthmega");
748 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");
750 // initialize starting values for players
752 start_switchweapon = 0;
753 start_ammo_shells = 0;
754 start_ammo_nails = 0;
755 start_ammo_rockets = 0;
756 start_ammo_cells = 0;
757 start_health = cvar("g_balance_health_start");
758 start_armorvalue = cvar("g_balance_armor_start");
760 if(cvar("g_instagib"))
762 start_items = IT_NEX;
763 start_switchweapon = WEP_NEX;
764 weapon_action(start_switchweapon, WR_PRECACHE);
765 start_ammo_cells = 999;
767 else if(cvar("g_rocketarena"))
769 start_items = IT_ROCKET_LAUNCHER;
770 start_switchweapon = WEP_ROCKET_LAUNCHER;
771 weapon_action(start_switchweapon, WR_PRECACHE);
772 start_ammo_rockets = 999;
774 else if(cvar("g_nixnex"))
777 // will be done later
779 else if(cvar("g_minstagib"))
782 start_armorvalue = 0;
783 start_items = IT_NEX;
784 start_switchweapon = WEP_NEX;
785 weapon_action(start_switchweapon, WR_PRECACHE);
786 start_ammo_cells = cvar("g_minstagib_ammo_start");
792 start_ammo_shells = cvar("g_lms_start_ammo_shells");
793 start_ammo_nails = cvar("g_lms_start_ammo_nails");
794 start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
795 start_ammo_cells = cvar("g_lms_start_ammo_cells");
796 start_health = cvar("g_lms_start_health");
797 start_armorvalue = cvar("g_lms_start_armor");
799 else if (cvar("g_use_ammunition")) {
800 start_ammo_shells = cvar("g_start_ammo_shells");
801 start_ammo_nails = cvar("g_start_ammo_nails");
802 start_ammo_rockets = cvar("g_start_ammo_rockets");
803 start_ammo_cells = cvar("g_start_ammo_cells");
805 start_ammo_shells = cvar("g_pickup_shells_max");
806 start_ammo_nails = cvar("g_pickup_nails_max");
807 start_ammo_rockets = cvar("g_pickup_rockets_max");
808 start_ammo_cells = cvar("g_pickup_cells_max");
811 if (cvar("g_start_weapon_laser") || cvar("g_lms"))
813 start_items = start_items | IT_LASER;
814 start_switchweapon = WEP_LASER;
815 weapon_action(start_switchweapon, WR_PRECACHE);
817 if (cvar("g_start_weapon_shotgun") || cvar("g_lms"))
819 start_items = start_items | IT_SHOTGUN;
820 start_switchweapon = WEP_SHOTGUN;
821 weapon_action(start_switchweapon, WR_PRECACHE);
823 if (cvar("g_start_weapon_uzi") || cvar("g_lms"))
825 start_items = start_items | IT_UZI;
826 start_switchweapon = WEP_UZI;
827 weapon_action(start_switchweapon, WR_PRECACHE);
829 if (cvar("g_start_weapon_grenadelauncher") || cvar("g_lms"))
831 start_items = start_items | IT_GRENADE_LAUNCHER;
832 start_switchweapon = WEP_GRENADE_LAUNCHER;
833 weapon_action(start_switchweapon, WR_PRECACHE);
835 if (cvar("g_start_weapon_electro") || cvar("g_lms"))
837 start_items = start_items | IT_ELECTRO;
838 start_switchweapon = WEP_ELECTRO;
839 weapon_action(start_switchweapon, WR_PRECACHE);
841 if (cvar("g_start_weapon_crylink") || cvar("g_lms"))
843 start_items = start_items | IT_CRYLINK;
844 start_switchweapon = WEP_CRYLINK;
845 weapon_action(start_switchweapon, WR_PRECACHE);
847 if (cvar("g_start_weapon_nex") || cvar("g_lms"))
849 start_items = start_items | IT_NEX;
850 start_switchweapon = WEP_NEX;
851 weapon_action(start_switchweapon, WR_PRECACHE);
853 if (cvar("g_start_weapon_hagar") || cvar("g_lms"))
855 start_items = start_items | IT_HAGAR;
856 start_switchweapon = WEP_HAGAR;
857 weapon_action(start_switchweapon, WR_PRECACHE);
859 if (cvar("g_start_weapon_rocketlauncher") || cvar("g_lms"))
861 start_items = start_items | IT_ROCKET_LAUNCHER;
862 start_switchweapon = WEP_ROCKET_LAUNCHER;
863 weapon_action(start_switchweapon, WR_PRECACHE);
869 // TODO sound pack system
872 string precache_sound_builtin (string s) = #19;
873 void(entity e, float chan, string samp, float vol, float atten) sound_builtin = #8;
874 string precache_sound(string s)
876 return precache_sound_builtin(strcat(soundpack, s));
878 void play2(entity e, string filename)
880 stuffcmd(e, strcat("play2 ", soundpack, filename, "\n"));
882 void sound(entity e, float chan, string samp, float vol, float atten)
884 sound_builtin(e, chan, strcat(soundpack, samp), vol, atten);
888 string precache_sound (string s) = #19;
889 void(entity e, float chan, string samp, float vol, float atten) sound = #8;
890 void play2(entity e, string filename)
892 stuffcmd(e, strcat("play2 ", filename, "\n"));
895 void play2team(float t, string filename)
898 FOR_EACH_REALPLAYER(head)
901 play2(head, filename);