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)
7 // change that to actually calling strcat when running on an engine without
8 // unlimited tempstrings:
9 // string strcat1(string s) = #115; // FRIK_FILE
10 #define strcat1(s) (s)
15 void(string s) bcenterprint
17 // TODO replace by MSG_ALL (would show it to spectators too, though)?
20 if(clienttype(head) == CLIENTTYPE_REAL)
24 void(string s, float check_dangerous) ServerConsoleEcho =
27 if (checkextension("DP_SV_PRINT"))
36 ch = substring(s, 0, 1);
37 if(ch != "\"" && ch != "\r" && ch != "\n")
39 s = substring(s, 1, strlen(s) - 1);
50 void(string s, float check_dangerous) GameLogEcho =
55 if(cvar("sv_eventlog_files"))
60 matches = cvar("sv_eventlog_files_counter") + 1;
61 cvar_set("sv_eventlog_files_counter", ftos(matches));
64 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
65 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
66 logfile = fopen(fn, FILE_APPEND);
69 fputs(logfile, strcat(s, "\n"));
71 if(cvar("sv_eventlog_console"))
73 ServerConsoleEcho(s, check_dangerous);
80 // will be opened later
85 if(logfile_open && logfile >= 0)
92 float math_mod(float a, float b)
94 return a - (floor(a / b) * b);
97 void relocate_spawnpoint()
99 // nudge off the floor
100 setorigin(self, self.origin + '0 0 1');
102 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
103 if (trace_startsolid)
105 objerror("player spawn point in solid, mapper sucks!\n");
110 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
111 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
112 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
113 // BE CONSTANT OR strzoneD!
114 float(string haystack, string needle, float offset) strstr =
118 len = strlen(needle);
119 endpos = strlen(haystack) - len;
120 while(offset <= endpos)
122 found = substring(haystack, offset, len);
130 float NUM_NEAREST_ENTITIES = 4;
131 entity nearest_entity[NUM_NEAREST_ENTITIES];
132 float nearest_length[NUM_NEAREST_ENTITIES];
133 entity(vector point, .string field, string value, vector axismod) findnearest =
144 localhead = find(world, field, value);
147 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
148 dist = localhead.oldorigin;
150 dist = localhead.origin;
152 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
155 for(i = 0; i < num_nearest; ++i)
157 if(len < nearest_length[i])
161 // now i tells us where to insert at
162 // INSERTION SORT! YOU'VE SEEN IT! RUN!
163 if(i < NUM_NEAREST_ENTITIES)
165 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
167 nearest_length[j + 1] = nearest_length[j];
168 nearest_entity[j + 1] = nearest_entity[j];
170 nearest_length[i] = len;
171 nearest_entity[i] = localhead;
172 if(num_nearest < NUM_NEAREST_ENTITIES)
173 num_nearest = num_nearest + 1;
176 localhead = find(localhead, field, value);
179 // now use the first one from our list that we can see
180 for(i = 0; i < num_nearest; ++i)
182 traceline(point, nearest_entity[i].origin, TRUE, world);
183 if(trace_fraction == 1)
187 dprint("Nearest point (");
188 dprint(nearest_entity[0].netname);
189 dprint(") is not visible, using a visible one.\n");
191 return nearest_entity[i];
198 dprint("Not seeing any location point, using nearest as fallback.\n");
200 dprint("Candidates were: ");
201 for(j = 0; j < num_nearest; ++j)
205 dprint(nearest_entity[j].netname);
210 return nearest_entity[0];
213 void() target_location =
215 self.classname = "target_location";
216 // location name in netname
217 // eventually support: count, teamgame selectors, line of sight?
220 void() info_location =
222 self.classname = "target_location";
223 self.message = self.netname;
226 string NearestLocation(vector p)
231 loc = findnearest(p, classname, "target_location", '1 1 1');
238 loc = findnearest(p, target, "###item###", '1 1 4');
245 string(string msg) formatmessage =
252 msg_save = strzone(msg);
258 break; // too many replacements
260 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
263 replacement = substring(msg_save, p, 2);
264 escape = substring(msg_save, p + 1, 1);
267 else if(escape == "a")
268 replacement = ftos(floor(self.armorvalue));
269 else if(escape == "h")
270 replacement = ftos(floor(self.health));
271 else if(escape == "l")
272 replacement = NearestLocation(self.origin);
273 else if(escape == "y")
274 replacement = NearestLocation(self.cursor_trace_endpos);
275 else if(escape == "d")
276 replacement = NearestLocation(self.death_origin);
277 else if(escape == "w")
282 wep = self.switchweapon;
285 replacement = W_Name(wep);
287 else if(escape == "W")
289 if(self.items & IT_SHELLS) replacement = "shells";
290 else if(self.items & IT_NAILS) replacement = "bullets";
291 else if(self.items & IT_ROCKETS) replacement = "rockets";
292 else if(self.items & IT_CELLS) replacement = "cells";
293 else replacement = "batteries"; // ;)
295 else if(escape == "x")
297 replacement = self.cursor_trace_ent.netname;
298 if(!replacement || !self.cursor_trace_ent)
299 replacement = "nothing";
301 else if(escape == "p")
303 if(self.last_selected_player)
304 replacement = self.last_selected_player.netname;
306 replacement = "(nobody)";
308 msg = strcat(substring(msg_save, 0, p), replacement);
309 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
311 msg_save = strzone(msg);
314 msg = strcat(msg_save, "");
325 >0: receives a cvar from name=argv(f) value=argv(f+1)
327 void GetCvars_handleString(float f, .string field, string name)
332 strunzone(self.field);
339 strunzone(self.field);
340 self.field = strzone(argv(f + 1));
344 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
346 void GetCvars_handleFloat(float f, .float field, string name)
354 self.field = stof(argv(f + 1));
357 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
359 void GetCvars(float f)
361 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
362 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
363 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
364 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
365 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
368 float fexists(string f)
371 fh = fopen(f, FILE_READ);
378 void backtrace(string msg)
381 dev = cvar("developer");
382 cvar_set("developer", "1");
384 dprint("--- CUT HERE ---\nWARNING: ");
387 remove(world); // isn't there any better way to cause a backtrace?
388 dprint("\n--- CUT UNTIL HERE ---\n");
389 cvar_set("developer", ftos(dev));
392 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
404 // p.frags = 0; // do not harm the new team!
405 // return; // won't distribute negative scores
411 f = ceil(factor * p.frags);
412 p.frags = p.frags - f;
415 FOR_EACH_PLAYER(head)
417 if(head.team == targetteam)
423 FOR_EACH_PLAYER(head)
425 if(head.team == targetteam)
427 d = floor(f / nTeam);
428 head.frags = head.frags + d;
434 error("nPlayers in team changed!");
436 error(strcat("There were ", ftos(f), " frags left. BAD!"));
439 string Team_ColorCode(float teamid)
441 if(teamid == COLOR_TEAM1)
443 else if(teamid == COLOR_TEAM2)
445 else if(teamid == COLOR_TEAM3)
447 else if(teamid == COLOR_TEAM4)
453 #define CENTERPRIO_POINT 1
454 #define CENTERPRIO_VOTE 4
455 #define CENTERPRIO_NORMAL 5
456 #define CENTERPRIO_MAPVOTE 9
457 .float centerprint_priority;
458 .float centerprint_expires;
459 void centerprint_atprio(entity e, float prio, string s)
461 if(intermission_running)
462 if(prio < CENTERPRIO_MAPVOTE)
464 if(time > e.centerprint_expires)
465 e.centerprint_priority = 0;
466 if(prio >= e.centerprint_priority)
468 e.centerprint_priority = prio;
469 e.centerprint_expires = time + e.cvar_scr_centertime;
470 centerprint_builtin(e, s);
473 void centerprint_expire(entity e, float prio)
475 if(prio == e.centerprint_priority)
477 e.centerprint_priority = 0;
478 centerprint_builtin(e, "");
481 void centerprint(entity e, string s)
483 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
488 // decolorizes and team colors the player name when needed
489 string playername(entity p)
492 if(teams_matter && !intermission_running && p.classname == "player")
494 t = Team_ColorCode(p.team);
495 return strcat(t, strdecolorize(p.netname));