1 // converts a number to a string with the indicated number of decimals
2 // works for up to 10 decimals!
3 string ftos_decimals(float number, float decimals)
9 // if negative, cut off the sign first
11 return strcat("-", ftos_decimals(-number, decimals));
12 // it now is always positive!
15 number = floor(number * pow(10, decimals) + 0.5);
18 result = ftos(number);
20 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
21 // if ftos had fucked it up, which should never happen: "34278.000000"
23 if(substring(result, len - 7, 1) == ".")
25 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
26 result = substring(result, 0, len - 7);
30 // is it too short? If yes, insert leading zeroes
33 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
36 // and now... INSERT THE POINT!
37 tmp = substring(result, len - decimals, decimals);
38 result = strcat(substring(result, 0, len - decimals), ".", tmp);
42 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
43 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
44 string STR_PLAYER = "player";
45 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
46 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
48 // change that to actually calling strcat when running on an engine without
49 // unlimited tempstrings:
50 // string strcat1(string s) = #115; // FRIK_FILE
51 #define strcat1(s) (s)
56 void(string s) bcenterprint
58 // TODO replace by MSG_ALL (would show it to spectators too, though)?
61 if(clienttype(head) == CLIENTTYPE_REAL)
65 void(string s, float check_dangerous) ServerConsoleEcho =
68 if (checkextension("DP_SV_PRINT"))
77 ch = substring(s, 0, 1);
78 if(ch != "\"" && ch != "\r" && ch != "\n")
80 s = substring(s, 1, strlen(s) - 1);
91 void(string s, float check_dangerous) GameLogEcho =
96 if(cvar("sv_eventlog_files"))
101 matches = cvar("sv_eventlog_files_counter") + 1;
102 cvar_set("sv_eventlog_files_counter", ftos(matches));
105 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
106 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
107 logfile = fopen(fn, FILE_APPEND);
110 fputs(logfile, strcat(s, "\n"));
112 if(cvar("sv_eventlog_console"))
114 ServerConsoleEcho(s, check_dangerous);
121 // will be opened later
124 void() GameLogClose =
126 if(logfile_open && logfile >= 0)
133 float math_mod(float a, float b)
135 return a - (floor(a / b) * b);
138 void relocate_spawnpoint()
140 // nudge off the floor
141 setorigin(self, self.origin + '0 0 1');
143 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
144 if (trace_startsolid)
146 objerror("player spawn point in solid, mapper sucks!\n");
151 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
152 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
153 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
154 // BE CONSTANT OR strzoneD!
155 float(string haystack, string needle, float offset) strstr =
159 len = strlen(needle);
160 endpos = strlen(haystack) - len;
161 while(offset <= endpos)
163 found = substring(haystack, offset, len);
171 float NUM_NEAREST_ENTITIES = 4;
172 entity nearest_entity[NUM_NEAREST_ENTITIES];
173 float nearest_length[NUM_NEAREST_ENTITIES];
174 entity(vector point, .string field, string value, vector axismod) findnearest =
185 localhead = find(world, field, value);
188 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
189 dist = localhead.oldorigin;
191 dist = localhead.origin;
193 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
196 for(i = 0; i < num_nearest; ++i)
198 if(len < nearest_length[i])
202 // now i tells us where to insert at
203 // INSERTION SORT! YOU'VE SEEN IT! RUN!
204 if(i < NUM_NEAREST_ENTITIES)
206 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
208 nearest_length[j + 1] = nearest_length[j];
209 nearest_entity[j + 1] = nearest_entity[j];
211 nearest_length[i] = len;
212 nearest_entity[i] = localhead;
213 if(num_nearest < NUM_NEAREST_ENTITIES)
214 num_nearest = num_nearest + 1;
217 localhead = find(localhead, field, value);
220 // now use the first one from our list that we can see
221 for(i = 0; i < num_nearest; ++i)
223 traceline(point, nearest_entity[i].origin, TRUE, world);
224 if(trace_fraction == 1)
228 dprint("Nearest point (");
229 dprint(nearest_entity[0].netname);
230 dprint(") is not visible, using a visible one.\n");
232 return nearest_entity[i];
239 dprint("Not seeing any location point, using nearest as fallback.\n");
241 dprint("Candidates were: ");
242 for(j = 0; j < num_nearest; ++j)
246 dprint(nearest_entity[j].netname);
251 return nearest_entity[0];
254 void() target_location =
256 self.classname = "target_location";
257 // location name in netname
258 // eventually support: count, teamgame selectors, line of sight?
261 void() info_location =
263 self.classname = "target_location";
264 self.message = self.netname;
267 string NearestLocation(vector p)
272 loc = findnearest(p, classname, "target_location", '1 1 1');
279 loc = findnearest(p, target, "###item###", '1 1 4');
286 string(string msg) formatmessage =
293 msg_save = strzone(msg);
299 break; // too many replacements
301 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
304 replacement = substring(msg_save, p, 2);
305 escape = substring(msg_save, p + 1, 1);
308 else if(escape == "a")
309 replacement = ftos(floor(self.armorvalue));
310 else if(escape == "h")
311 replacement = ftos(floor(self.health));
312 else if(escape == "l")
313 replacement = NearestLocation(self.origin);
314 else if(escape == "y")
315 replacement = NearestLocation(self.cursor_trace_endpos);
316 else if(escape == "d")
317 replacement = NearestLocation(self.death_origin);
318 else if(escape == "w")
323 wep = self.switchweapon;
326 replacement = W_Name(wep);
328 else if(escape == "W")
330 if(self.items & IT_SHELLS) replacement = "shells";
331 else if(self.items & IT_NAILS) replacement = "bullets";
332 else if(self.items & IT_ROCKETS) replacement = "rockets";
333 else if(self.items & IT_CELLS) replacement = "cells";
334 else replacement = "batteries"; // ;)
336 else if(escape == "x")
338 replacement = self.cursor_trace_ent.netname;
339 if(!replacement || !self.cursor_trace_ent)
340 replacement = "nothing";
342 else if(escape == "p")
344 if(self.last_selected_player)
345 replacement = self.last_selected_player.netname;
347 replacement = "(nobody)";
349 msg = strcat(substring(msg_save, 0, p), replacement);
350 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
352 msg_save = strzone(msg);
355 msg = strcat(msg_save, "");
366 >0: receives a cvar from name=argv(f) value=argv(f+1)
368 void GetCvars_handleString(float f, .string field, string name)
373 strunzone(self.field);
380 strunzone(self.field);
381 self.field = strzone(argv(f + 1));
385 stuffcmd(self, strcat("sendcvar ", name, "\n"));
387 void GetCvars_handleFloat(float f, .float field, string name)
395 self.field = stof(argv(f + 1));
398 stuffcmd(self, strcat("sendcvar ", name, "\n"));
400 void GetCvars(float f)
402 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
403 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
404 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
405 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
406 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
407 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
408 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
409 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
410 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
413 float fexists(string f)
416 fh = fopen(f, FILE_READ);
423 void backtrace(string msg)
426 dev = cvar("developer");
427 cvar_set("developer", "1");
429 dprint("--- CUT HERE ---\nWARNING: ");
432 remove(world); // isn't there any better way to cause a backtrace?
433 dprint("\n--- CUT UNTIL HERE ---\n");
434 cvar_set("developer", ftos(dev));
437 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
449 // p.frags = 0; // do not harm the new team!
450 // return; // won't distribute negative scores
456 f = ceil(factor * p.frags);
457 p.frags = p.frags - f;
460 FOR_EACH_PLAYER(head)
462 if(head.team == targetteam)
468 FOR_EACH_PLAYER(head)
470 if(head.team == targetteam)
472 d = floor(f / nTeam);
473 head.frags = head.frags + d;
479 error("nPlayers in team changed!");
481 error(strcat("There were ", ftos(f), " frags left. BAD!"));
484 string Team_ColorCode(float teamid)
486 if(teamid == COLOR_TEAM1)
488 else if(teamid == COLOR_TEAM2)
490 else if(teamid == COLOR_TEAM3)
492 else if(teamid == COLOR_TEAM4)
498 string decolorize(string s)
507 ch1 = substring(s, 0, 1);
508 ch2 = substring(s, 1, 1);
513 out = strcat(out, "^^");
537 out = strcat(out, "^^");
539 s = substring(s, n, strlen(s) - n);
543 s = substring(s, 1, strlen(s) - 1);
544 out = strcat(out, ch1);
549 #define strdecolorize(s) decolorize(s)
550 #define strlennocol(s) strlen(decolorize(s))
552 #define CENTERPRIO_POINT 1
553 #define CENTERPRIO_REBALANCE 2
554 #define CENTERPRIO_VOTE 4
555 #define CENTERPRIO_NORMAL 5
556 #define CENTERPRIO_MAPVOTE 9
557 #define CENTERPRIO_ADMIN 99
558 .float centerprint_priority;
559 .float centerprint_expires;
560 void centerprint_atprio(entity e, float prio, string s)
562 if(intermission_running)
563 if(prio < CENTERPRIO_MAPVOTE)
565 if(time > e.centerprint_expires)
566 e.centerprint_priority = 0;
567 if(prio >= e.centerprint_priority)
569 e.centerprint_priority = prio;
570 e.centerprint_expires = time + e.cvar_scr_centertime;
571 centerprint_builtin(e, s);
574 void centerprint_expire(entity e, float prio)
576 if(prio == e.centerprint_priority)
578 e.centerprint_priority = 0;
579 centerprint_builtin(e, "");
582 void centerprint(entity e, string s)
584 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
589 // decolorizes and team colors the player name when needed
590 string playername(entity p)
593 if(teams_matter && !intermission_running && p.classname == "player")
595 t = Team_ColorCode(p.team);
596 return strcat(t, strdecolorize(p.netname));
602 // requires that m2>m1 in all coordinates, and that m4>m3
603 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;};
605 // requires the same, but is a stronger condition
606 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;};
608 float g_pickup_shells;
609 float g_pickup_shells_max;
610 float g_pickup_nails;
611 float g_pickup_nails_max;
612 float g_pickup_rockets;
613 float g_pickup_rockets_max;
614 float g_pickup_cells;
615 float g_pickup_cells_max;
616 float g_pickup_armorshard;
617 float g_pickup_armorshard_max;
618 float g_pickup_armor;
619 float g_pickup_armor_max;
620 float g_pickup_healthshard;
621 float g_pickup_healthshard_max;
622 float g_pickup_health;
623 float g_pickup_health_max;
624 float g_pickup_healthmega;
625 float g_pickup_healthmega_max;
627 void readlevelcvars(void)
629 g_pickup_shells = cvar("g_pickup_shells");
630 g_pickup_shells_max = cvar("g_pickup_shells_max");
631 g_pickup_nails = cvar("g_pickup_nails");
632 g_pickup_nails_max = cvar("g_pickup_nails_max");
633 g_pickup_rockets = cvar("g_pickup_rockets");
634 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
635 g_pickup_cells = cvar("g_pickup_cells");
636 g_pickup_cells_max = cvar("g_pickup_cells_max");
637 g_pickup_armorshard = cvar("g_pickup_armorshard");
638 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
639 g_pickup_armor = cvar("g_pickup_armor");
640 g_pickup_armor_max = cvar("g_pickup_armor_max");
641 g_pickup_healthshard = cvar("g_pickup_healthshard");
642 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
643 g_pickup_health = cvar("g_pickup_health");
644 g_pickup_health_max = cvar("g_pickup_health_max");
645 g_pickup_healthmega = cvar("g_pickup_healthmega");
646 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");