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");
13 DistributeEvenly_amount = amount;
14 DistributeEvenly_totalweight = totalweight;
16 float DistributeEvenly_Get(float weight)
21 f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
22 DistributeEvenly_totalweight -= weight;
23 DistributeEvenly_amount -= f;
27 void move_out_of_solid_expand(entity e, vector by)
30 tracebox(e.origin, e.mins - '1 1 1' * eps, e.maxs + '1 1 1' * eps, e.origin + by, MOVE_WORLDONLY, e);
33 if(trace_fraction < 1)
36 // adjust origin in the other direction...
37 e.origin = e.origin - by * (1 - trace_fraction);
41 void move_out_of_solid(entity e)
46 traceline(o, o, MOVE_WORLDONLY, e);
49 dprint("origin is in solid too! (", vtos(o), ")");
53 tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
61 move_out_of_solid_expand(e, '1 0 0' * m0_x); e.mins_x = m0_x;
62 move_out_of_solid_expand(e, '1 0 0' * m1_x); e.maxs_x = m1_x;
63 move_out_of_solid_expand(e, '0 1 0' * m0_y); e.mins_y = m0_y;
64 move_out_of_solid_expand(e, '0 1 0' * m1_y); e.maxs_y = m1_y;
65 move_out_of_solid_expand(e, '0 0 1' * m0_z); e.mins_z = m0_z;
66 move_out_of_solid_expand(e, '0 0 1' * m1_z); e.maxs_z = m1_z;
67 setorigin(e, e.origin);
69 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
72 dprint("could not get out of solid (", vtos(o), ")");
77 // converts a number to a string with the indicated number of decimals
78 // works for up to 10 decimals!
79 string ftos_decimals(float number, float decimals)
85 // if negative, cut off the sign first
87 return strcat("-", ftos_decimals(-number, decimals));
88 // it now is always positive!
91 number = floor(number * pow(10, decimals) + 0.5);
94 result = ftos(number);
96 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
97 // if ftos had fucked it up, which should never happen: "34278.000000"
99 if(substring(result, len - 7, 1) == ".")
101 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
102 result = substring(result, 0, len - 7);
106 // is it too short? If yes, insert leading zeroes
109 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
112 // and now... INSERT THE POINT!
113 tmp = substring(result, len - decimals, decimals);
114 result = strcat(substring(result, 0, len - decimals), ".", tmp);
118 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
119 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
120 string STR_PLAYER = "player";
121 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
122 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
124 // change that to actually calling strcat when running on an engine without
125 // unlimited tempstrings:
126 // string strcat1(string s) = #115; // FRIK_FILE
127 #define strcat1(s) (s)
132 void(string s) bcenterprint
134 // TODO replace by MSG_ALL (would show it to spectators too, though)?
136 FOR_EACH_PLAYER(head)
137 if(clienttype(head) == CLIENTTYPE_REAL)
138 centerprint(head, s);
141 void(string s, float check_dangerous) ServerConsoleEcho =
144 if (checkextension("DP_SV_PRINT"))
153 ch = substring(s, 0, 1);
154 if(ch != "\"" && ch != "\r" && ch != "\n")
156 s = substring(s, 1, strlen(s) - 1);
167 void(string s, float check_dangerous) GameLogEcho =
172 if(cvar("sv_eventlog_files"))
177 matches = cvar("sv_eventlog_files_counter") + 1;
178 cvar_set("sv_eventlog_files_counter", ftos(matches));
181 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
182 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
183 logfile = fopen(fn, FILE_APPEND);
186 fputs(logfile, strcat(s, "\n"));
188 if(cvar("sv_eventlog_console"))
190 ServerConsoleEcho(s, check_dangerous);
197 // will be opened later
200 void() GameLogClose =
202 if(logfile_open && logfile >= 0)
209 float math_mod(float a, float b)
211 return a - (floor(a / b) * b);
214 void relocate_spawnpoint()
216 // nudge off the floor
217 setorigin(self, self.origin + '0 0 1');
219 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
220 if (trace_startsolid)
222 objerror("player spawn point in solid, mapper sucks!\n");
226 if(cvar("g_spawnpoints_autodrop"))
228 setsize(self, PL_MIN, PL_MAX);
233 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
234 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
235 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
236 // BE CONSTANT OR strzoneD!
237 float(string haystack, string needle, float offset) strstr =
241 len = strlen(needle);
242 endpos = strlen(haystack) - len;
243 while(offset <= endpos)
245 found = substring(haystack, offset, len);
253 float NUM_NEAREST_ENTITIES = 4;
254 entity nearest_entity[NUM_NEAREST_ENTITIES];
255 float nearest_length[NUM_NEAREST_ENTITIES];
256 entity(vector point, .string field, string value, vector axismod) findnearest =
267 localhead = find(world, field, value);
270 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
271 dist = localhead.oldorigin;
273 dist = localhead.origin;
275 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
278 for(i = 0; i < num_nearest; ++i)
280 if(len < nearest_length[i])
284 // now i tells us where to insert at
285 // INSERTION SORT! YOU'VE SEEN IT! RUN!
286 if(i < NUM_NEAREST_ENTITIES)
288 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
290 nearest_length[j + 1] = nearest_length[j];
291 nearest_entity[j + 1] = nearest_entity[j];
293 nearest_length[i] = len;
294 nearest_entity[i] = localhead;
295 if(num_nearest < NUM_NEAREST_ENTITIES)
296 num_nearest = num_nearest + 1;
299 localhead = find(localhead, field, value);
302 // now use the first one from our list that we can see
303 for(i = 0; i < num_nearest; ++i)
305 traceline(point, nearest_entity[i].origin, TRUE, world);
306 if(trace_fraction == 1)
310 dprint("Nearest point (");
311 dprint(nearest_entity[0].netname);
312 dprint(") is not visible, using a visible one.\n");
314 return nearest_entity[i];
321 dprint("Not seeing any location point, using nearest as fallback.\n");
323 dprint("Candidates were: ");
324 for(j = 0; j < num_nearest; ++j)
328 dprint(nearest_entity[j].netname);
333 return nearest_entity[0];
336 void() target_location =
338 self.classname = "target_location";
339 // location name in netname
340 // eventually support: count, teamgame selectors, line of sight?
343 void() info_location =
345 self.classname = "target_location";
346 self.message = self.netname;
349 string NearestLocation(vector p)
354 loc = findnearest(p, classname, "target_location", '1 1 1');
361 loc = findnearest(p, target, "###item###", '1 1 4');
368 string(string msg) formatmessage =
375 msg_save = strzone(msg);
381 break; // too many replacements
383 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
386 replacement = substring(msg_save, p, 2);
387 escape = substring(msg_save, p + 1, 1);
390 else if(escape == "a")
391 replacement = ftos(floor(self.armorvalue));
392 else if(escape == "h")
393 replacement = ftos(floor(self.health));
394 else if(escape == "l")
395 replacement = NearestLocation(self.origin);
396 else if(escape == "y")
397 replacement = NearestLocation(self.cursor_trace_endpos);
398 else if(escape == "d")
399 replacement = NearestLocation(self.death_origin);
400 else if(escape == "w")
405 wep = self.switchweapon;
408 replacement = W_Name(wep);
410 else if(escape == "W")
412 if(self.items & IT_SHELLS) replacement = "shells";
413 else if(self.items & IT_NAILS) replacement = "bullets";
414 else if(self.items & IT_ROCKETS) replacement = "rockets";
415 else if(self.items & IT_CELLS) replacement = "cells";
416 else replacement = "batteries"; // ;)
418 else if(escape == "x")
420 replacement = self.cursor_trace_ent.netname;
421 if(!replacement || !self.cursor_trace_ent)
422 replacement = "nothing";
424 else if(escape == "p")
426 if(self.last_selected_player)
427 replacement = self.last_selected_player.netname;
429 replacement = "(nobody)";
431 msg = strcat(substring(msg_save, 0, p), replacement);
432 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
434 msg_save = strzone(msg);
437 msg = strcat(msg_save, "");
448 >0: receives a cvar from name=argv(f) value=argv(f+1)
450 void GetCvars_handleString(float f, .string field, string name)
455 strunzone(self.field);
462 strunzone(self.field);
463 self.field = strzone(argv(f + 1));
467 stuffcmd(self, strcat("sendcvar ", name, "\n"));
469 void GetCvars_handleFloat(float f, .float field, string name)
477 self.field = stof(argv(f + 1));
480 stuffcmd(self, strcat("sendcvar ", name, "\n"));
482 void GetCvars(float f)
484 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
485 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
486 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
487 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
488 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
489 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
490 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
491 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
492 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
495 float fexists(string f)
498 fh = fopen(f, FILE_READ);
505 void backtrace(string msg)
508 dev = cvar("developer");
509 cvar_set("developer", "1");
511 dprint("--- CUT HERE ---\nWARNING: ");
514 remove(world); // isn't there any better way to cause a backtrace?
515 dprint("\n--- CUT UNTIL HERE ---\n");
516 cvar_set("developer", ftos(dev));
519 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
530 // p.frags = 0; // do not harm the new team!
531 // return; // won't distribute negative scores
537 f = ceil(factor * p.frags);
538 p.frags = p.frags - f;
541 FOR_EACH_PLAYER(head)
543 if(head.team == targetteam)
549 DistributeEvenly_Init(f, nTeam);
551 FOR_EACH_PLAYER(head)
553 if(head.team == targetteam)
554 head.frags = head.frags + DistributeEvenly_Get(1);
557 string Team_ColorCode(float teamid)
559 if(teamid == COLOR_TEAM1)
561 else if(teamid == COLOR_TEAM2)
563 else if(teamid == COLOR_TEAM3)
565 else if(teamid == COLOR_TEAM4)
572 string decolorize(string s)
581 ch1 = substring(s, 0, 1);
582 ch2 = substring(s, 1, 1);
587 out = strcat(out, "^^");
611 out = strcat(out, "^^");
613 s = substring(s, n, strlen(s) - n);
617 s = substring(s, 1, strlen(s) - 1);
618 out = strcat(out, ch1);
623 #define strdecolorize(s) decolorize(s)
624 #define strlennocol(s) strlen(decolorize(s))
627 #define CENTERPRIO_POINT 1
628 #define CENTERPRIO_SPAM 2
629 #define CENTERPRIO_REBALANCE 2
630 #define CENTERPRIO_VOTE 4
631 #define CENTERPRIO_NORMAL 5
632 #define CENTERPRIO_MAPVOTE 9
633 #define CENTERPRIO_ADMIN 99
634 .float centerprint_priority;
635 .float centerprint_expires;
636 void centerprint_atprio(entity e, float prio, string s)
638 if(intermission_running)
639 if(prio < CENTERPRIO_MAPVOTE)
641 if(time > e.centerprint_expires)
642 e.centerprint_priority = 0;
643 if(prio >= e.centerprint_priority)
645 e.centerprint_priority = prio;
646 e.centerprint_expires = time + e.cvar_scr_centertime;
647 centerprint_builtin(e, s);
650 void centerprint_expire(entity e, float prio)
652 if(prio == e.centerprint_priority)
654 e.centerprint_priority = 0;
655 centerprint_builtin(e, "");
658 void centerprint(entity e, string s)
660 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
665 // decolorizes and team colors the player name when needed
666 string playername(entity p)
669 if(teams_matter && !intermission_running && p.classname == "player")
671 t = Team_ColorCode(p.team);
672 return strcat(t, strdecolorize(p.netname));
678 // requires that m2>m1 in all coordinates, and that m4>m3
679 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;};
681 // requires the same, but is a stronger condition
682 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;};
684 float g_pickup_shells;
685 float g_pickup_shells_max;
686 float g_pickup_nails;
687 float g_pickup_nails_max;
688 float g_pickup_rockets;
689 float g_pickup_rockets_max;
690 float g_pickup_cells;
691 float g_pickup_cells_max;
692 float g_pickup_armorshard;
693 float g_pickup_armorshard_max;
694 float g_pickup_armor;
695 float g_pickup_armor_max;
696 float g_pickup_healthshard;
697 float g_pickup_healthshard_max;
698 float g_pickup_health;
699 float g_pickup_health_max;
700 float g_pickup_healthmega;
701 float g_pickup_healthmega_max;
703 void readlevelcvars(void)
705 g_pickup_shells = cvar("g_pickup_shells");
706 g_pickup_shells_max = cvar("g_pickup_shells_max");
707 g_pickup_nails = cvar("g_pickup_nails");
708 g_pickup_nails_max = cvar("g_pickup_nails_max");
709 g_pickup_rockets = cvar("g_pickup_rockets");
710 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
711 g_pickup_cells = cvar("g_pickup_cells");
712 g_pickup_cells_max = cvar("g_pickup_cells_max");
713 g_pickup_armorshard = cvar("g_pickup_armorshard");
714 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
715 g_pickup_armor = cvar("g_pickup_armor");
716 g_pickup_armor_max = cvar("g_pickup_armor_max");
717 g_pickup_healthshard = cvar("g_pickup_healthshard");
718 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
719 g_pickup_health = cvar("g_pickup_health");
720 g_pickup_health_max = cvar("g_pickup_health_max");
721 g_pickup_healthmega = cvar("g_pickup_healthmega");
722 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");
726 // TODO sound pack system
729 string precache_sound_builtin (string s) = #19;
730 void(entity e, float chan, string samp, float vol, float atten) sound_builtin = #8;
731 string precache_sound(string s)
733 return precache_sound_builtin(strcat(soundpack, s));
735 void play2(entity e, string filename)
737 stuffcmd(e, strcat("play2 ", soundpack, filename, "\n"));
739 void sound(entity e, float chan, string samp, float vol, float atten)
741 sound_builtin(e, chan, strcat(soundpack, samp), vol, atten);
745 string precache_sound (string s) = #19;
746 void(entity e, float chan, string samp, float vol, float atten) sound = #8;
747 void play2(entity e, string filename)
749 stuffcmd(e, strcat("play2 ", filename, "\n"));