1 void move_out_of_solid_expand(entity e, vector by)
4 tracebox(e.origin, e.mins - '1 1 1' * eps, e.maxs + '1 1 1' * eps, e.origin + by, MOVE_WORLDONLY, e);
10 // adjust origin in the other direction...
11 e.origin = e.origin - by * (1 - trace_fraction);
16 void move_out_of_solid(entity e)
18 vector o, m0, m1, mnew;
21 traceline(o, o, MOVE_WORLDONLY, e);
24 dprint("origin is in solid too! (", vtos(o), ")");
28 tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
36 move_out_of_solid_expand(e, '1 0 0' * m0_x); e.mins_x = m0_x;
37 move_out_of_solid_expand(e, '1 0 0' * m1_x); e.maxs_x = m1_x;
38 move_out_of_solid_expand(e, '0 1 0' * m0_y); e.mins_y = m0_y;
39 move_out_of_solid_expand(e, '0 1 0' * m1_y); e.maxs_y = m1_y;
40 move_out_of_solid_expand(e, '0 0 1' * m0_z); e.mins_z = m0_z;
41 move_out_of_solid_expand(e, '0 0 1' * m1_z); e.maxs_z = m1_z;
42 setorigin(e, e.origin);
44 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
47 dprint("could not get out of solid (", vtos(o), ")");
52 // converts a number to a string with the indicated number of decimals
53 // works for up to 10 decimals!
54 string ftos_decimals(float number, float decimals)
60 // if negative, cut off the sign first
62 return strcat("-", ftos_decimals(-number, decimals));
63 // it now is always positive!
66 number = floor(number * pow(10, decimals) + 0.5);
69 result = ftos(number);
71 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
72 // if ftos had fucked it up, which should never happen: "34278.000000"
74 if(substring(result, len - 7, 1) == ".")
76 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
77 result = substring(result, 0, len - 7);
81 // is it too short? If yes, insert leading zeroes
84 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
87 // and now... INSERT THE POINT!
88 tmp = substring(result, len - decimals, decimals);
89 result = strcat(substring(result, 0, len - decimals), ".", tmp);
93 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
94 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
95 string STR_PLAYER = "player";
96 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
97 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
99 // change that to actually calling strcat when running on an engine without
100 // unlimited tempstrings:
101 // string strcat1(string s) = #115; // FRIK_FILE
102 #define strcat1(s) (s)
107 void(string s) bcenterprint
109 // TODO replace by MSG_ALL (would show it to spectators too, though)?
111 FOR_EACH_PLAYER(head)
112 if(clienttype(head) == CLIENTTYPE_REAL)
113 centerprint(head, s);
116 void(string s, float check_dangerous) ServerConsoleEcho =
119 if (checkextension("DP_SV_PRINT"))
128 ch = substring(s, 0, 1);
129 if(ch != "\"" && ch != "\r" && ch != "\n")
131 s = substring(s, 1, strlen(s) - 1);
142 void(string s, float check_dangerous) GameLogEcho =
147 if(cvar("sv_eventlog_files"))
152 matches = cvar("sv_eventlog_files_counter") + 1;
153 cvar_set("sv_eventlog_files_counter", ftos(matches));
156 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
157 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
158 logfile = fopen(fn, FILE_APPEND);
161 fputs(logfile, strcat(s, "\n"));
163 if(cvar("sv_eventlog_console"))
165 ServerConsoleEcho(s, check_dangerous);
172 // will be opened later
175 void() GameLogClose =
177 if(logfile_open && logfile >= 0)
184 float math_mod(float a, float b)
186 return a - (floor(a / b) * b);
189 void relocate_spawnpoint()
191 // nudge off the floor
192 setorigin(self, self.origin + '0 0 1');
194 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
195 if (trace_startsolid)
197 objerror("player spawn point in solid, mapper sucks!\n");
202 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
203 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
204 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
205 // BE CONSTANT OR strzoneD!
206 float(string haystack, string needle, float offset) strstr =
210 len = strlen(needle);
211 endpos = strlen(haystack) - len;
212 while(offset <= endpos)
214 found = substring(haystack, offset, len);
222 float NUM_NEAREST_ENTITIES = 4;
223 entity nearest_entity[NUM_NEAREST_ENTITIES];
224 float nearest_length[NUM_NEAREST_ENTITIES];
225 entity(vector point, .string field, string value, vector axismod) findnearest =
236 localhead = find(world, field, value);
239 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
240 dist = localhead.oldorigin;
242 dist = localhead.origin;
244 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
247 for(i = 0; i < num_nearest; ++i)
249 if(len < nearest_length[i])
253 // now i tells us where to insert at
254 // INSERTION SORT! YOU'VE SEEN IT! RUN!
255 if(i < NUM_NEAREST_ENTITIES)
257 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
259 nearest_length[j + 1] = nearest_length[j];
260 nearest_entity[j + 1] = nearest_entity[j];
262 nearest_length[i] = len;
263 nearest_entity[i] = localhead;
264 if(num_nearest < NUM_NEAREST_ENTITIES)
265 num_nearest = num_nearest + 1;
268 localhead = find(localhead, field, value);
271 // now use the first one from our list that we can see
272 for(i = 0; i < num_nearest; ++i)
274 traceline(point, nearest_entity[i].origin, TRUE, world);
275 if(trace_fraction == 1)
279 dprint("Nearest point (");
280 dprint(nearest_entity[0].netname);
281 dprint(") is not visible, using a visible one.\n");
283 return nearest_entity[i];
290 dprint("Not seeing any location point, using nearest as fallback.\n");
292 dprint("Candidates were: ");
293 for(j = 0; j < num_nearest; ++j)
297 dprint(nearest_entity[j].netname);
302 return nearest_entity[0];
305 void() target_location =
307 self.classname = "target_location";
308 // location name in netname
309 // eventually support: count, teamgame selectors, line of sight?
312 void() info_location =
314 self.classname = "target_location";
315 self.message = self.netname;
318 string NearestLocation(vector p)
323 loc = findnearest(p, classname, "target_location", '1 1 1');
330 loc = findnearest(p, target, "###item###", '1 1 4');
337 string(string msg) formatmessage =
344 msg_save = strzone(msg);
350 break; // too many replacements
352 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
355 replacement = substring(msg_save, p, 2);
356 escape = substring(msg_save, p + 1, 1);
359 else if(escape == "a")
360 replacement = ftos(floor(self.armorvalue));
361 else if(escape == "h")
362 replacement = ftos(floor(self.health));
363 else if(escape == "l")
364 replacement = NearestLocation(self.origin);
365 else if(escape == "y")
366 replacement = NearestLocation(self.cursor_trace_endpos);
367 else if(escape == "d")
368 replacement = NearestLocation(self.death_origin);
369 else if(escape == "w")
374 wep = self.switchweapon;
377 replacement = W_Name(wep);
379 else if(escape == "W")
381 if(self.items & IT_SHELLS) replacement = "shells";
382 else if(self.items & IT_NAILS) replacement = "bullets";
383 else if(self.items & IT_ROCKETS) replacement = "rockets";
384 else if(self.items & IT_CELLS) replacement = "cells";
385 else replacement = "batteries"; // ;)
387 else if(escape == "x")
389 replacement = self.cursor_trace_ent.netname;
390 if(!replacement || !self.cursor_trace_ent)
391 replacement = "nothing";
393 else if(escape == "p")
395 if(self.last_selected_player)
396 replacement = self.last_selected_player.netname;
398 replacement = "(nobody)";
400 msg = strcat(substring(msg_save, 0, p), replacement);
401 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
403 msg_save = strzone(msg);
406 msg = strcat(msg_save, "");
417 >0: receives a cvar from name=argv(f) value=argv(f+1)
419 void GetCvars_handleString(float f, .string field, string name)
424 strunzone(self.field);
431 strunzone(self.field);
432 self.field = strzone(argv(f + 1));
436 stuffcmd(self, strcat("sendcvar ", name, "\n"));
438 void GetCvars_handleFloat(float f, .float field, string name)
446 self.field = stof(argv(f + 1));
449 stuffcmd(self, strcat("sendcvar ", name, "\n"));
451 void GetCvars(float f)
453 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
454 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
455 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
456 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
457 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
458 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
459 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
460 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
461 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
464 float fexists(string f)
467 fh = fopen(f, FILE_READ);
474 void backtrace(string msg)
477 dev = cvar("developer");
478 cvar_set("developer", "1");
480 dprint("--- CUT HERE ---\nWARNING: ");
483 remove(world); // isn't there any better way to cause a backtrace?
484 dprint("\n--- CUT UNTIL HERE ---\n");
485 cvar_set("developer", ftos(dev));
488 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
500 // p.frags = 0; // do not harm the new team!
501 // return; // won't distribute negative scores
507 f = ceil(factor * p.frags);
508 p.frags = p.frags - f;
511 FOR_EACH_PLAYER(head)
513 if(head.team == targetteam)
519 FOR_EACH_PLAYER(head)
521 if(head.team == targetteam)
523 d = floor(f / nTeam);
524 head.frags = head.frags + d;
530 error("nPlayers in team changed!");
532 error(strcat("There were ", ftos(f), " frags left. BAD!"));
535 string Team_ColorCode(float teamid)
537 if(teamid == COLOR_TEAM1)
539 else if(teamid == COLOR_TEAM2)
541 else if(teamid == COLOR_TEAM3)
543 else if(teamid == COLOR_TEAM4)
549 string decolorize(string s)
558 ch1 = substring(s, 0, 1);
559 ch2 = substring(s, 1, 1);
564 out = strcat(out, "^^");
588 out = strcat(out, "^^");
590 s = substring(s, n, strlen(s) - n);
594 s = substring(s, 1, strlen(s) - 1);
595 out = strcat(out, ch1);
600 #define strdecolorize(s) decolorize(s)
601 #define strlennocol(s) strlen(decolorize(s))
603 #define CENTERPRIO_POINT 1
604 #define CENTERPRIO_REBALANCE 2
605 #define CENTERPRIO_VOTE 4
606 #define CENTERPRIO_NORMAL 5
607 #define CENTERPRIO_MAPVOTE 9
608 #define CENTERPRIO_ADMIN 99
609 .float centerprint_priority;
610 .float centerprint_expires;
611 void centerprint_atprio(entity e, float prio, string s)
613 if(intermission_running)
614 if(prio < CENTERPRIO_MAPVOTE)
616 if(time > e.centerprint_expires)
617 e.centerprint_priority = 0;
618 if(prio >= e.centerprint_priority)
620 e.centerprint_priority = prio;
621 e.centerprint_expires = time + e.cvar_scr_centertime;
622 centerprint_builtin(e, s);
625 void centerprint_expire(entity e, float prio)
627 if(prio == e.centerprint_priority)
629 e.centerprint_priority = 0;
630 centerprint_builtin(e, "");
633 void centerprint(entity e, string s)
635 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
640 // decolorizes and team colors the player name when needed
641 string playername(entity p)
644 if(teams_matter && !intermission_running && p.classname == "player")
646 t = Team_ColorCode(p.team);
647 return strcat(t, strdecolorize(p.netname));
653 // requires that m2>m1 in all coordinates, and that m4>m3
654 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;};
656 // requires the same, but is a stronger condition
657 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;};
659 float g_pickup_shells;
660 float g_pickup_shells_max;
661 float g_pickup_nails;
662 float g_pickup_nails_max;
663 float g_pickup_rockets;
664 float g_pickup_rockets_max;
665 float g_pickup_cells;
666 float g_pickup_cells_max;
667 float g_pickup_armorshard;
668 float g_pickup_armorshard_max;
669 float g_pickup_armor;
670 float g_pickup_armor_max;
671 float g_pickup_healthshard;
672 float g_pickup_healthshard_max;
673 float g_pickup_health;
674 float g_pickup_health_max;
675 float g_pickup_healthmega;
676 float g_pickup_healthmega_max;
678 void readlevelcvars(void)
680 g_pickup_shells = cvar("g_pickup_shells");
681 g_pickup_shells_max = cvar("g_pickup_shells_max");
682 g_pickup_nails = cvar("g_pickup_nails");
683 g_pickup_nails_max = cvar("g_pickup_nails_max");
684 g_pickup_rockets = cvar("g_pickup_rockets");
685 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
686 g_pickup_cells = cvar("g_pickup_cells");
687 g_pickup_cells_max = cvar("g_pickup_cells_max");
688 g_pickup_armorshard = cvar("g_pickup_armorshard");
689 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
690 g_pickup_armor = cvar("g_pickup_armor");
691 g_pickup_armor_max = cvar("g_pickup_armor_max");
692 g_pickup_healthshard = cvar("g_pickup_healthshard");
693 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
694 g_pickup_health = cvar("g_pickup_health");
695 g_pickup_health_max = cvar("g_pickup_health_max");
696 g_pickup_healthmega = cvar("g_pickup_healthmega");
697 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");