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), ")");
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 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
237 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
238 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
239 // BE CONSTANT OR strzoneD!
240 float(string haystack, string needle, float offset) strstr =
244 len = strlen(needle);
245 endpos = strlen(haystack) - len;
246 while(offset <= endpos)
248 found = substring(haystack, offset, len);
256 float NUM_NEAREST_ENTITIES = 4;
257 entity nearest_entity[NUM_NEAREST_ENTITIES];
258 float nearest_length[NUM_NEAREST_ENTITIES];
259 entity(vector point, .string field, string value, vector axismod) findnearest =
270 localhead = find(world, field, value);
273 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
274 dist = localhead.oldorigin;
276 dist = localhead.origin;
278 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
281 for(i = 0; i < num_nearest; ++i)
283 if(len < nearest_length[i])
287 // now i tells us where to insert at
288 // INSERTION SORT! YOU'VE SEEN IT! RUN!
289 if(i < NUM_NEAREST_ENTITIES)
291 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
293 nearest_length[j + 1] = nearest_length[j];
294 nearest_entity[j + 1] = nearest_entity[j];
296 nearest_length[i] = len;
297 nearest_entity[i] = localhead;
298 if(num_nearest < NUM_NEAREST_ENTITIES)
299 num_nearest = num_nearest + 1;
302 localhead = find(localhead, field, value);
305 // now use the first one from our list that we can see
306 for(i = 0; i < num_nearest; ++i)
308 traceline(point, nearest_entity[i].origin, TRUE, world);
309 if(trace_fraction == 1)
313 dprint("Nearest point (");
314 dprint(nearest_entity[0].netname);
315 dprint(") is not visible, using a visible one.\n");
317 return nearest_entity[i];
324 dprint("Not seeing any location point, using nearest as fallback.\n");
326 dprint("Candidates were: ");
327 for(j = 0; j < num_nearest; ++j)
331 dprint(nearest_entity[j].netname);
336 return nearest_entity[0];
339 void() target_location =
341 self.classname = "target_location";
342 // location name in netname
343 // eventually support: count, teamgame selectors, line of sight?
346 void() info_location =
348 self.classname = "target_location";
349 self.message = self.netname;
352 string NearestLocation(vector p)
357 loc = findnearest(p, classname, "target_location", '1 1 1');
364 loc = findnearest(p, target, "###item###", '1 1 4');
371 string(string msg) formatmessage =
378 msg_save = strzone(msg);
384 break; // too many replacements
386 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
389 replacement = substring(msg_save, p, 2);
390 escape = substring(msg_save, p + 1, 1);
393 else if(escape == "a")
394 replacement = ftos(floor(self.armorvalue));
395 else if(escape == "h")
396 replacement = ftos(floor(self.health));
397 else if(escape == "l")
398 replacement = NearestLocation(self.origin);
399 else if(escape == "y")
400 replacement = NearestLocation(self.cursor_trace_endpos);
401 else if(escape == "d")
402 replacement = NearestLocation(self.death_origin);
403 else if(escape == "w")
408 wep = self.switchweapon;
411 replacement = W_Name(wep);
413 else if(escape == "W")
415 if(self.items & IT_SHELLS) replacement = "shells";
416 else if(self.items & IT_NAILS) replacement = "bullets";
417 else if(self.items & IT_ROCKETS) replacement = "rockets";
418 else if(self.items & IT_CELLS) replacement = "cells";
419 else replacement = "batteries"; // ;)
421 else if(escape == "x")
423 replacement = self.cursor_trace_ent.netname;
424 if(!replacement || !self.cursor_trace_ent)
425 replacement = "nothing";
427 else if(escape == "p")
429 if(self.last_selected_player)
430 replacement = self.last_selected_player.netname;
432 replacement = "(nobody)";
434 msg = strcat(substring(msg_save, 0, p), replacement);
435 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
437 msg_save = strzone(msg);
440 msg = strcat(msg_save, "");
451 >0: receives a cvar from name=argv(f) value=argv(f+1)
453 void GetCvars_handleString(float f, .string field, string name)
458 strunzone(self.field);
465 strunzone(self.field);
466 self.field = strzone(argv(f + 1));
470 stuffcmd(self, strcat("sendcvar ", name, "\n"));
472 void GetCvars_handleFloat(float f, .float field, string name)
480 self.field = stof(argv(f + 1));
483 stuffcmd(self, strcat("sendcvar ", name, "\n"));
485 void GetCvars(float f)
487 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
488 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
489 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
490 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
491 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
492 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
493 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
494 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
495 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
496 GetCvars_handleFloat(f, cvar_cl_handicap, "cl_handicap");
499 float fexists(string f)
502 fh = fopen(f, FILE_READ);
509 void backtrace(string msg)
512 dev = cvar("developer");
513 cvar_set("developer", "1");
515 dprint("--- CUT HERE ---\nWARNING: ");
518 remove(world); // isn't there any better way to cause a backtrace?
519 dprint("\n--- CUT UNTIL HERE ---\n");
520 cvar_set("developer", ftos(dev));
523 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
534 // p.frags = 0; // do not harm the new team!
535 // return; // won't distribute negative scores
541 f = ceil(factor * p.frags);
542 p.frags = p.frags - f;
545 FOR_EACH_PLAYER(head)
547 if(head.team == targetteam)
553 DistributeEvenly_Init(f, nTeam);
555 FOR_EACH_PLAYER(head)
557 if(head.team == targetteam)
558 head.frags = head.frags + DistributeEvenly_Get(1);
561 string Team_ColorCode(float teamid)
563 if(teamid == COLOR_TEAM1)
565 else if(teamid == COLOR_TEAM2)
567 else if(teamid == COLOR_TEAM3)
569 else if(teamid == COLOR_TEAM4)
576 string decolorize(string s)
585 ch1 = substring(s, 0, 1);
586 ch2 = substring(s, 1, 1);
591 out = strcat(out, "^^");
615 out = strcat(out, "^^");
617 s = substring(s, n, strlen(s) - n);
621 s = substring(s, 1, strlen(s) - 1);
622 out = strcat(out, ch1);
627 #define strdecolorize(s) decolorize(s)
628 #define strlennocol(s) strlen(decolorize(s))
631 #define CENTERPRIO_POINT 1
632 #define CENTERPRIO_SPAM 2
633 #define CENTERPRIO_REBALANCE 2
634 #define CENTERPRIO_VOTE 4
635 #define CENTERPRIO_NORMAL 5
636 #define CENTERPRIO_MAPVOTE 9
637 #define CENTERPRIO_ADMIN 99
638 .float centerprint_priority;
639 .float centerprint_expires;
640 void centerprint_atprio(entity e, float prio, string s)
642 if(intermission_running)
643 if(prio < CENTERPRIO_MAPVOTE)
645 if(time > e.centerprint_expires)
646 e.centerprint_priority = 0;
647 if(prio >= e.centerprint_priority)
649 e.centerprint_priority = prio;
650 e.centerprint_expires = time + e.cvar_scr_centertime;
651 centerprint_builtin(e, s);
654 void centerprint_expire(entity e, float prio)
656 if(prio == e.centerprint_priority)
658 e.centerprint_priority = 0;
659 centerprint_builtin(e, "");
662 void centerprint(entity e, string s)
664 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
669 // decolorizes and team colors the player name when needed
670 string playername(entity p)
673 if(teams_matter && !intermission_running && p.classname == "player")
675 t = Team_ColorCode(p.team);
676 return strcat(t, strdecolorize(p.netname));
682 vector(vector m1, vector m2) randompos =
686 v_x = m2_x * random() + m1_x;
687 v_y = m2_y * random() + m1_y;
688 v_z = m2_z * random() + m1_z;
692 // requires that m2>m1 in all coordinates, and that m4>m3
693 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;};
695 // requires the same, but is a stronger condition
696 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;};
698 float g_pickup_shells;
699 float g_pickup_shells_max;
700 float g_pickup_nails;
701 float g_pickup_nails_max;
702 float g_pickup_rockets;
703 float g_pickup_rockets_max;
704 float g_pickup_cells;
705 float g_pickup_cells_max;
706 float g_pickup_armorshard;
707 float g_pickup_armorshard_max;
708 float g_pickup_armor;
709 float g_pickup_armor_max;
710 float g_pickup_healthshard;
711 float g_pickup_healthshard_max;
712 float g_pickup_health;
713 float g_pickup_health_max;
714 float g_pickup_healthmega;
715 float g_pickup_healthmega_max;
718 float start_switchweapon;
719 float start_ammo_shells;
720 float start_ammo_nails;
721 float start_ammo_rockets;
722 float start_ammo_cells;
724 float start_armorvalue;
726 void readlevelcvars(void)
728 g_pickup_shells = cvar("g_pickup_shells");
729 g_pickup_shells_max = cvar("g_pickup_shells_max");
730 g_pickup_nails = cvar("g_pickup_nails");
731 g_pickup_nails_max = cvar("g_pickup_nails_max");
732 g_pickup_rockets = cvar("g_pickup_rockets");
733 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
734 g_pickup_cells = cvar("g_pickup_cells");
735 g_pickup_cells_max = cvar("g_pickup_cells_max");
736 g_pickup_armorshard = cvar("g_pickup_armorshard");
737 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
738 g_pickup_armor = cvar("g_pickup_armor");
739 g_pickup_armor_max = cvar("g_pickup_armor_max");
740 g_pickup_healthshard = cvar("g_pickup_healthshard");
741 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
742 g_pickup_health = cvar("g_pickup_health");
743 g_pickup_health_max = cvar("g_pickup_health_max");
744 g_pickup_healthmega = cvar("g_pickup_healthmega");
745 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");
747 // initialize starting values for players
749 start_switchweapon = 0;
750 start_ammo_shells = 0;
751 start_ammo_nails = 0;
752 start_ammo_rockets = 0;
753 start_ammo_cells = 0;
754 start_health = cvar("g_balance_health_start");
755 start_armorvalue = cvar("g_balance_armor_start");
757 if(cvar("g_instagib"))
759 start_items = IT_NEX;
760 start_switchweapon = WEP_NEX;
761 weapon_action(start_switchweapon, WR_PRECACHE);
762 start_ammo_cells = 999;
764 else if(cvar("g_rocketarena"))
766 start_items = IT_ROCKET_LAUNCHER;
767 start_switchweapon = WEP_ROCKET_LAUNCHER;
768 weapon_action(start_switchweapon, WR_PRECACHE);
769 start_ammo_rockets = 999;
771 else if(cvar("g_nixnex"))
774 // will be done later
776 else if(cvar("g_minstagib"))
779 start_armorvalue = 0;
780 start_items = IT_NEX;
781 start_switchweapon = WEP_NEX;
782 weapon_action(start_switchweapon, WR_PRECACHE);
783 start_ammo_cells = cvar("g_minstagib_ammo_start");
789 start_ammo_shells = cvar("g_lms_start_ammo_shells");
790 start_ammo_nails = cvar("g_lms_start_ammo_nails");
791 start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
792 start_ammo_cells = cvar("g_lms_start_ammo_cells");
793 start_health = cvar("g_lms_start_health");
794 start_armorvalue = cvar("g_lms_start_armor");
796 else if (cvar("g_use_ammunition")) {
797 start_ammo_shells = cvar("g_start_ammo_shells");
798 start_ammo_nails = cvar("g_start_ammo_nails");
799 start_ammo_rockets = cvar("g_start_ammo_rockets");
800 start_ammo_cells = cvar("g_start_ammo_cells");
802 start_ammo_shells = cvar("g_pickup_shells_max");
803 start_ammo_nails = cvar("g_pickup_nails_max");
804 start_ammo_rockets = cvar("g_pickup_rockets_max");
805 start_ammo_cells = cvar("g_pickup_cells_max");
808 if (cvar("g_start_weapon_laser") || cvar("g_lms"))
810 start_items = start_items | IT_LASER;
811 start_switchweapon = WEP_LASER;
812 weapon_action(start_switchweapon, WR_PRECACHE);
814 if (cvar("g_start_weapon_shotgun") || cvar("g_lms"))
816 start_items = start_items | IT_SHOTGUN;
817 start_switchweapon = WEP_SHOTGUN;
818 weapon_action(start_switchweapon, WR_PRECACHE);
820 if (cvar("g_start_weapon_uzi") || cvar("g_lms"))
822 start_items = start_items | IT_UZI;
823 start_switchweapon = WEP_UZI;
824 weapon_action(start_switchweapon, WR_PRECACHE);
826 if (cvar("g_start_weapon_grenadelauncher") || cvar("g_lms"))
828 start_items = start_items | IT_GRENADE_LAUNCHER;
829 start_switchweapon = WEP_GRENADE_LAUNCHER;
830 weapon_action(start_switchweapon, WR_PRECACHE);
832 if (cvar("g_start_weapon_electro") || cvar("g_lms"))
834 start_items = start_items | IT_ELECTRO;
835 start_switchweapon = WEP_ELECTRO;
836 weapon_action(start_switchweapon, WR_PRECACHE);
838 if (cvar("g_start_weapon_crylink") || cvar("g_lms"))
840 start_items = start_items | IT_CRYLINK;
841 start_switchweapon = WEP_CRYLINK;
842 weapon_action(start_switchweapon, WR_PRECACHE);
844 if (cvar("g_start_weapon_nex") || cvar("g_lms"))
846 start_items = start_items | IT_NEX;
847 start_switchweapon = WEP_NEX;
848 weapon_action(start_switchweapon, WR_PRECACHE);
850 if (cvar("g_start_weapon_hagar") || cvar("g_lms"))
852 start_items = start_items | IT_HAGAR;
853 start_switchweapon = WEP_HAGAR;
854 weapon_action(start_switchweapon, WR_PRECACHE);
856 if (cvar("g_start_weapon_rocketlauncher") || cvar("g_lms"))
858 start_items = start_items | IT_ROCKET_LAUNCHER;
859 start_switchweapon = WEP_ROCKET_LAUNCHER;
860 weapon_action(start_switchweapon, WR_PRECACHE);
866 // TODO sound pack system
869 string precache_sound_builtin (string s) = #19;
870 void(entity e, float chan, string samp, float vol, float atten) sound_builtin = #8;
871 string precache_sound(string s)
873 return precache_sound_builtin(strcat(soundpack, s));
875 void play2(entity e, string filename)
877 stuffcmd(e, strcat("play2 ", soundpack, filename, "\n"));
879 void sound(entity e, float chan, string samp, float vol, float atten)
881 sound_builtin(e, chan, strcat(soundpack, samp), vol, atten);
885 string precache_sound (string s) = #19;
886 void(entity e, float chan, string samp, float vol, float atten) sound = #8;
887 void play2(entity e, string filename)
889 stuffcmd(e, strcat("play2 ", filename, "\n"));
892 void play2team(float t, string filename)
895 FOR_EACH_REALPLAYER(head)
898 play2(head, filename);