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()
102 setsize(self, PL_MIN, PL_MAX);
104 // nudge off the floor
105 setorigin(self, self.origin + '0 0 1');
107 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
108 if (trace_startsolid)
110 objerror("player spawn point in solid, mapper sucks!\n");
114 // drop the spawn point onto the floor
115 // NOTE: a later version might not do this any more
119 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
120 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
121 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
122 // BE CONSTANT OR strzoneD!
123 float(string haystack, string needle, float offset) strstr =
127 len = strlen(needle);
128 endpos = strlen(haystack) - len;
129 while(offset <= endpos)
131 found = substring(haystack, offset, len);
139 float NUM_NEAREST_ENTITIES = 4;
140 entity nearest_entity[NUM_NEAREST_ENTITIES];
141 float nearest_length[NUM_NEAREST_ENTITIES];
142 entity(vector point, .string field, string value, vector axismod) findnearest =
153 localhead = find(world, field, value);
156 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
157 dist = localhead.oldorigin;
159 dist = localhead.origin;
161 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
164 for(i = 0; i < num_nearest; ++i)
166 if(len < nearest_length[i])
170 // now i tells us where to insert at
171 // INSERTION SORT! YOU'VE SEEN IT! RUN!
172 if(i < NUM_NEAREST_ENTITIES)
174 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
176 nearest_length[j + 1] = nearest_length[j];
177 nearest_entity[j + 1] = nearest_entity[j];
179 nearest_length[i] = len;
180 nearest_entity[i] = localhead;
181 if(num_nearest < NUM_NEAREST_ENTITIES)
182 num_nearest = num_nearest + 1;
185 localhead = find(localhead, field, value);
188 // now use the first one from our list that we can see
189 for(i = 0; i < num_nearest; ++i)
191 traceline(point, nearest_entity[i].origin, TRUE, world);
192 if(trace_fraction == 1)
196 dprint("Nearest point (");
197 dprint(nearest_entity[0].netname);
198 dprint(") is not visible, using a visible one.\n");
200 return nearest_entity[i];
207 dprint("Not seeing any location point, using nearest as fallback.\n");
209 dprint("Candidates were: ");
210 for(j = 0; j < num_nearest; ++j)
214 dprint(nearest_entity[j].netname);
219 return nearest_entity[0];
222 void() target_location =
224 self.classname = "target_location";
225 // location name in netname
226 // eventually support: count, teamgame selectors, line of sight?
229 void() info_location =
231 self.classname = "target_location";
232 self.message = self.netname;
235 string NearestLocation(vector p)
240 loc = findnearest(p, classname, "target_location", '1 1 1');
247 loc = findnearest(p, target, "###item###", '1 1 4');
254 string(string msg) formatmessage =
261 msg_save = strzone(msg);
267 break; // too many replacements
269 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
272 replacement = substring(msg_save, p, 2);
273 escape = substring(msg_save, p + 1, 1);
276 else if(escape == "a")
277 replacement = ftos(floor(self.armorvalue));
278 else if(escape == "h")
279 replacement = ftos(floor(self.health));
280 else if(escape == "l")
281 replacement = NearestLocation(self.origin);
282 else if(escape == "y")
283 replacement = NearestLocation(self.cursor_trace_endpos);
284 else if(escape == "d")
285 replacement = NearestLocation(self.death_origin);
286 else if(escape == "w")
291 wep = self.switchweapon;
294 replacement = W_Name(wep);
296 else if(escape == "W")
298 if(self.items & IT_SHELLS) replacement = "shells";
299 else if(self.items & IT_NAILS) replacement = "bullets";
300 else if(self.items & IT_ROCKETS) replacement = "rockets";
301 else if(self.items & IT_CELLS) replacement = "cells";
302 else replacement = "batteries"; // ;)
304 else if(escape == "x")
306 replacement = self.cursor_trace_ent.netname;
307 if(!replacement || !self.cursor_trace_ent)
308 replacement = "nothing";
310 else if(escape == "p")
312 if(self.last_selected_player)
313 replacement = self.last_selected_player.netname;
315 replacement = "(nobody)";
317 msg = strcat(substring(msg_save, 0, p), replacement);
318 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
320 msg_save = strzone(msg);
323 msg = strcat(msg_save, "");
334 >0: receives a cvar from name=argv(f) value=argv(f+1)
336 void GetCvars_handleString(float f, .string field, string name)
341 strunzone(self.field);
348 strunzone(self.field);
349 self.field = strzone(argv(f + 1));
353 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
355 void GetCvars_handleFloat(float f, .float field, string name)
363 self.field = stof(argv(f + 1));
366 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
368 void GetCvars(float f)
370 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
371 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
372 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
373 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
374 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
377 float fexists(string f)
380 fh = fopen(f, FILE_READ);
387 void backtrace(string msg)
390 dev = cvar("developer");
391 cvar_set("developer", "1");
393 dprint("--- CUT HERE ---\nWARNING: ");
396 remove(world); // isn't there any better way to cause a backtrace?
397 dprint("\n--- CUT UNTIL HERE ---\n");
398 cvar_set("developer", ftos(dev));
401 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
413 // p.frags = 0; // do not harm the new team!
414 // return; // won't distribute negative scores
420 f = ceil(factor * p.frags);
421 p.frags = p.frags - f;
424 FOR_EACH_PLAYER(head)
426 if(head.team == targetteam)
432 FOR_EACH_PLAYER(head)
434 if(head.team == targetteam)
436 d = floor(f / nTeam);
437 head.frags = head.frags + d;
443 error("nPlayers in team changed!");
445 error(strcat("There were ", ftos(f), " frags left. BAD!"));
448 string Team_ColorCode(float teamid)
450 if(teamid == COLOR_TEAM1)
452 else if(teamid == COLOR_TEAM2)
454 else if(teamid == COLOR_TEAM3)
456 else if(teamid == COLOR_TEAM4)
462 string decolorize(string s)
471 ch1 = substring(s, 0, 1);
472 ch2 = substring(s, 1, 1);
477 out = strcat(out, "^^");
501 out = strcat(out, "^^");
503 s = substring(s, n, strlen(s) - n);
507 s = substring(s, 1, strlen(s) - 1);
508 out = strcat(out, ch1);
513 #define strdecolorize(s) decolorize(s)
514 #define strlennocol(s) strlen(decolorize(s))
516 #define CENTERPRIO_POINT 1
517 #define CENTERPRIO_REBALANCE 2
518 #define CENTERPRIO_VOTE 4
519 #define CENTERPRIO_NORMAL 5
520 #define CENTERPRIO_MAPVOTE 9
521 .float centerprint_priority;
522 .float centerprint_expires;
523 void centerprint_atprio(entity e, float prio, string s)
525 if(intermission_running)
526 if(prio < CENTERPRIO_MAPVOTE)
528 if(time > e.centerprint_expires)
529 e.centerprint_priority = 0;
530 if(prio >= e.centerprint_priority)
532 e.centerprint_priority = prio;
533 e.centerprint_expires = time + e.cvar_scr_centertime;
534 centerprint_builtin(e, s);
537 void centerprint_expire(entity e, float prio)
539 if(prio == e.centerprint_priority)
541 e.centerprint_priority = 0;
542 centerprint_builtin(e, "");
545 void centerprint(entity e, string s)
547 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
552 // decolorizes and team colors the player name when needed
553 string playername(entity p)
556 if(teams_matter && !intermission_running && p.classname == "player")
558 t = Team_ColorCode(p.team);
559 return strcat(t, strdecolorize(p.netname));
565 // requires that m2>m1 in all coordinates, and that m4>m3
566 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;};
568 // requires the same, but is a stronger condition
569 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;};