1 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
2 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
3 string STR_PLAYER = "player";
4 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
5 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
10 void(string s) bcenterprint
12 // TODO replace by MSG_ALL (would show it to spectators too, though)?
15 if(clienttype(head) == CLIENTTYPE_REAL)
19 void(string s, float check_dangerous) ServerConsoleEcho =
22 if (checkextension("DP_SV_PRINT"))
31 ch = substring(s, 0, 1);
32 if(ch != "\"" && ch != "\r" && ch != "\n")
34 s = substring(s, 1, strlen(s) - 1);
45 void(string s, float check_dangerous) GameLogEcho =
50 if(cvar("sv_eventlog_files"))
55 matches = cvar("sv_eventlog_files_counter") + 1;
56 cvar_set("sv_eventlog_files_counter", ftos(matches));
59 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
60 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
61 logfile = fopen(fn, FILE_APPEND);
64 fputs(logfile, strcat(s, "\n"));
66 if(cvar("sv_eventlog_console"))
68 ServerConsoleEcho(s, check_dangerous);
75 // will be opened later
80 if(logfile_open && logfile >= 0)
87 float math_mod(float a, float b)
89 return a - (floor(a / b) * b);
92 void relocate_spawnpoint()
94 // nudge off the floor
95 setorigin(self, self.origin + '0 0 1');
97 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
100 objerror("player spawn point in solid, mapper sucks!\n");
105 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
106 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
107 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
108 // BE CONSTANT OR strzoneD!
109 float(string haystack, string needle, float offset) strstr =
113 len = strlen(needle);
114 endpos = strlen(haystack) - len;
115 while(offset <= endpos)
117 found = substring(haystack, offset, len);
125 float NUM_NEAREST_ENTITIES = 4;
126 entity nearest_entity[NUM_NEAREST_ENTITIES];
127 float nearest_length[NUM_NEAREST_ENTITIES];
128 entity(vector point, .string field, string value, vector axismod) findnearest =
139 localhead = find(world, field, value);
142 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
143 dist = localhead.oldorigin;
145 dist = localhead.origin;
147 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
150 for(i = 0; i < num_nearest; ++i)
152 if(len < nearest_length[i])
156 // now i tells us where to insert at
157 // INSERTION SORT! YOU'VE SEEN IT! RUN!
158 if(i < NUM_NEAREST_ENTITIES)
160 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
162 nearest_length[j + 1] = nearest_length[j];
163 nearest_entity[j + 1] = nearest_entity[j];
165 nearest_length[i] = len;
166 nearest_entity[i] = localhead;
167 if(num_nearest < NUM_NEAREST_ENTITIES)
168 num_nearest = num_nearest + 1;
171 localhead = find(localhead, field, value);
174 // now use the first one from our list that we can see
175 for(i = 0; i < num_nearest; ++i)
177 traceline(point, nearest_entity[i].origin, TRUE, world);
178 if(trace_fraction == 1)
182 dprint("Nearest point (");
183 dprint(nearest_entity[0].netname);
184 dprint(") is not visible, using a visible one.\n");
186 return nearest_entity[i];
193 dprint("Not seeing any location point, using nearest as fallback.\n");
195 dprint("Candidates were: ");
196 for(j = 0; j < num_nearest; ++j)
200 dprint(nearest_entity[j].netname);
205 return nearest_entity[0];
208 void() target_location =
210 self.classname = "target_location";
211 // location name in netname
212 // eventually support: count, teamgame selectors, line of sight?
215 void() info_location =
217 self.classname = "target_location";
218 self.message = self.netname;
221 string NearestLocation(vector p)
226 loc = findnearest(p, classname, "target_location", '1 1 1');
233 loc = findnearest(p, target, "###item###", '1 1 4');
240 string(string msg) formatmessage =
247 msg_save = strzone(msg);
253 break; // too many replacements
255 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
258 replacement = substring(msg_save, p, 2);
259 escape = substring(msg_save, p + 1, 1);
262 else if(escape == "a")
263 replacement = ftos(floor(self.armorvalue));
264 else if(escape == "h")
265 replacement = ftos(floor(self.health));
266 else if(escape == "l")
267 replacement = NearestLocation(self.origin);
268 else if(escape == "y")
269 replacement = NearestLocation(self.cursor_trace_endpos);
270 else if(escape == "d")
271 replacement = NearestLocation(self.death_origin);
272 else if(escape == "w")
277 wep = self.switchweapon;
280 replacement = W_Name(wep);
282 else if(escape == "W")
284 if(self.items & IT_SHELLS) replacement = "shells";
285 else if(self.items & IT_NAILS) replacement = "bullets";
286 else if(self.items & IT_ROCKETS) replacement = "rockets";
287 else if(self.items & IT_CELLS) replacement = "cells";
288 else replacement = "batteries"; // ;)
290 else if(escape == "x")
292 replacement = self.cursor_trace_ent.netname;
293 if(!replacement || !self.cursor_trace_ent)
294 replacement = "nothing";
296 else if(escape == "p")
298 if(self.last_selected_player)
299 replacement = self.last_selected_player.netname;
301 replacement = "(nobody)";
303 msg = strcat(substring(msg_save, 0, p), replacement);
304 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
306 msg_save = strzone(msg);
309 msg = strcat(msg_save);
320 >0: receives a cvar from name=argv(f) value=argv(f+1)
322 void GetCvars_handleString(float f, .string field, string name)
327 strunzone(self.field);
334 strunzone(self.field);
335 self.field = strzone(argv(f + 1));
339 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
341 void GetCvars_handleFloat(float f, .float field, string name)
349 self.field = stof(argv(f + 1));
352 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
354 void GetCvars(float f)
356 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
357 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
358 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
359 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
360 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
363 float fexists(string f)
366 fh = fopen(f, FILE_READ);
373 void backtrace(string msg)
376 dev = cvar("developer");
377 cvar_set("developer", "1");
379 dprint("--- CUT HERE ---\nWARNING: ");
382 remove(world); // isn't there any better way to cause a backtrace?
383 dprint("\n--- CUT UNTIL HERE ---\n");
384 cvar_set("developer", ftos(dev));
387 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
399 // p.frags = 0; // do not harm the new team!
400 // return; // won't distribute negative scores
406 f = ceil(factor * p.frags);
407 p.frags = p.frags - f;
410 FOR_EACH_PLAYER(head)
412 if(head.team == targetteam)
418 FOR_EACH_PLAYER(head)
420 if(head.team == targetteam)
422 d = floor(f / nTeam);
423 head.frags = head.frags + d;
429 error("nPlayers in team changed!");
431 error(strcat("There were ", ftos(f), " frags left. BAD!"));
434 string Team_ColorCode(float teamid)
436 if(teamid == COLOR_TEAM1)
438 else if(teamid == COLOR_TEAM2)
440 else if(teamid == COLOR_TEAM3)
442 else if(teamid == COLOR_TEAM4)
448 string decolorize(string s)
457 ch1 = substring(s, 0, 1);
458 ch2 = substring(s, 1, 1);
463 out = strcat(out, "^^");
487 out = strcat(out, "^^");
489 s = substring(s, n, strlen(s) - n);
493 s = substring(s, 1, strlen(s) - 1);
494 out = strcat(out, ch1);
500 #define CENTERPRIO_POINT 1
501 #define CENTERPRIO_VOTE 4
502 #define CENTERPRIO_NORMAL 5
503 #define CENTERPRIO_MAPVOTE 9
504 .float centerprint_priority;
505 .float centerprint_expires;
506 void centerprint_atprio(entity e, float prio, string s)
508 if(intermission_running)
509 if(prio < CENTERPRIO_MAPVOTE)
511 if(time > e.centerprint_expires)
512 e.centerprint_priority = 0;
513 if(prio >= e.centerprint_priority)
515 e.centerprint_priority = prio;
516 e.centerprint_expires = time + e.cvar_scr_centertime;
517 centerprint_builtin(e, s);
520 void centerprint_expire(entity e, float prio)
522 if(prio == e.centerprint_priority)
524 e.centerprint_priority = 0;
525 centerprint_builtin(e, "");
528 void centerprint(entity e, string s)
530 centerprint_atprio(e, CENTERPRIO_NORMAL, s);