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);
6 error("could not get out of solid");
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 print("origin is in solid too!");
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);
46 error("could not get out of solid");
49 // converts a number to a string with the indicated number of decimals
50 // works for up to 10 decimals!
51 string ftos_decimals(float number, float decimals)
57 // if negative, cut off the sign first
59 return strcat("-", ftos_decimals(-number, decimals));
60 // it now is always positive!
63 number = floor(number * pow(10, decimals) + 0.5);
66 result = ftos(number);
68 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
69 // if ftos had fucked it up, which should never happen: "34278.000000"
71 if(substring(result, len - 7, 1) == ".")
73 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
74 result = substring(result, 0, len - 7);
78 // is it too short? If yes, insert leading zeroes
81 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
84 // and now... INSERT THE POINT!
85 tmp = substring(result, len - decimals, decimals);
86 result = strcat(substring(result, 0, len - decimals), ".", tmp);
90 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
91 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
92 string STR_PLAYER = "player";
93 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
94 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
96 // change that to actually calling strcat when running on an engine without
97 // unlimited tempstrings:
98 // string strcat1(string s) = #115; // FRIK_FILE
99 #define strcat1(s) (s)
104 void(string s) bcenterprint
106 // TODO replace by MSG_ALL (would show it to spectators too, though)?
108 FOR_EACH_PLAYER(head)
109 if(clienttype(head) == CLIENTTYPE_REAL)
110 centerprint(head, s);
113 void(string s, float check_dangerous) ServerConsoleEcho =
116 if (checkextension("DP_SV_PRINT"))
125 ch = substring(s, 0, 1);
126 if(ch != "\"" && ch != "\r" && ch != "\n")
128 s = substring(s, 1, strlen(s) - 1);
139 void(string s, float check_dangerous) GameLogEcho =
144 if(cvar("sv_eventlog_files"))
149 matches = cvar("sv_eventlog_files_counter") + 1;
150 cvar_set("sv_eventlog_files_counter", ftos(matches));
153 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
154 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
155 logfile = fopen(fn, FILE_APPEND);
158 fputs(logfile, strcat(s, "\n"));
160 if(cvar("sv_eventlog_console"))
162 ServerConsoleEcho(s, check_dangerous);
169 // will be opened later
172 void() GameLogClose =
174 if(logfile_open && logfile >= 0)
181 float math_mod(float a, float b)
183 return a - (floor(a / b) * b);
186 void relocate_spawnpoint()
188 // nudge off the floor
189 setorigin(self, self.origin + '0 0 1');
191 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
192 if (trace_startsolid)
194 objerror("player spawn point in solid, mapper sucks!\n");
199 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
200 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
201 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
202 // BE CONSTANT OR strzoneD!
203 float(string haystack, string needle, float offset) strstr =
207 len = strlen(needle);
208 endpos = strlen(haystack) - len;
209 while(offset <= endpos)
211 found = substring(haystack, offset, len);
219 float NUM_NEAREST_ENTITIES = 4;
220 entity nearest_entity[NUM_NEAREST_ENTITIES];
221 float nearest_length[NUM_NEAREST_ENTITIES];
222 entity(vector point, .string field, string value, vector axismod) findnearest =
233 localhead = find(world, field, value);
236 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
237 dist = localhead.oldorigin;
239 dist = localhead.origin;
241 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
244 for(i = 0; i < num_nearest; ++i)
246 if(len < nearest_length[i])
250 // now i tells us where to insert at
251 // INSERTION SORT! YOU'VE SEEN IT! RUN!
252 if(i < NUM_NEAREST_ENTITIES)
254 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
256 nearest_length[j + 1] = nearest_length[j];
257 nearest_entity[j + 1] = nearest_entity[j];
259 nearest_length[i] = len;
260 nearest_entity[i] = localhead;
261 if(num_nearest < NUM_NEAREST_ENTITIES)
262 num_nearest = num_nearest + 1;
265 localhead = find(localhead, field, value);
268 // now use the first one from our list that we can see
269 for(i = 0; i < num_nearest; ++i)
271 traceline(point, nearest_entity[i].origin, TRUE, world);
272 if(trace_fraction == 1)
276 dprint("Nearest point (");
277 dprint(nearest_entity[0].netname);
278 dprint(") is not visible, using a visible one.\n");
280 return nearest_entity[i];
287 dprint("Not seeing any location point, using nearest as fallback.\n");
289 dprint("Candidates were: ");
290 for(j = 0; j < num_nearest; ++j)
294 dprint(nearest_entity[j].netname);
299 return nearest_entity[0];
302 void() target_location =
304 self.classname = "target_location";
305 // location name in netname
306 // eventually support: count, teamgame selectors, line of sight?
309 void() info_location =
311 self.classname = "target_location";
312 self.message = self.netname;
315 string NearestLocation(vector p)
320 loc = findnearest(p, classname, "target_location", '1 1 1');
327 loc = findnearest(p, target, "###item###", '1 1 4');
334 string(string msg) formatmessage =
341 msg_save = strzone(msg);
347 break; // too many replacements
349 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
352 replacement = substring(msg_save, p, 2);
353 escape = substring(msg_save, p + 1, 1);
356 else if(escape == "a")
357 replacement = ftos(floor(self.armorvalue));
358 else if(escape == "h")
359 replacement = ftos(floor(self.health));
360 else if(escape == "l")
361 replacement = NearestLocation(self.origin);
362 else if(escape == "y")
363 replacement = NearestLocation(self.cursor_trace_endpos);
364 else if(escape == "d")
365 replacement = NearestLocation(self.death_origin);
366 else if(escape == "w")
371 wep = self.switchweapon;
374 replacement = W_Name(wep);
376 else if(escape == "W")
378 if(self.items & IT_SHELLS) replacement = "shells";
379 else if(self.items & IT_NAILS) replacement = "bullets";
380 else if(self.items & IT_ROCKETS) replacement = "rockets";
381 else if(self.items & IT_CELLS) replacement = "cells";
382 else replacement = "batteries"; // ;)
384 else if(escape == "x")
386 replacement = self.cursor_trace_ent.netname;
387 if(!replacement || !self.cursor_trace_ent)
388 replacement = "nothing";
390 else if(escape == "p")
392 if(self.last_selected_player)
393 replacement = self.last_selected_player.netname;
395 replacement = "(nobody)";
397 msg = strcat(substring(msg_save, 0, p), replacement);
398 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
400 msg_save = strzone(msg);
403 msg = strcat(msg_save, "");
414 >0: receives a cvar from name=argv(f) value=argv(f+1)
416 void GetCvars_handleString(float f, .string field, string name)
421 strunzone(self.field);
428 strunzone(self.field);
429 self.field = strzone(argv(f + 1));
433 stuffcmd(self, strcat("sendcvar ", name, "\n"));
435 void GetCvars_handleFloat(float f, .float field, string name)
443 self.field = stof(argv(f + 1));
446 stuffcmd(self, strcat("sendcvar ", name, "\n"));
448 void GetCvars(float f)
450 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
451 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
452 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
453 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
454 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
455 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
456 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
457 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
458 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
461 float fexists(string f)
464 fh = fopen(f, FILE_READ);
471 void backtrace(string msg)
474 dev = cvar("developer");
475 cvar_set("developer", "1");
477 dprint("--- CUT HERE ---\nWARNING: ");
480 remove(world); // isn't there any better way to cause a backtrace?
481 dprint("\n--- CUT UNTIL HERE ---\n");
482 cvar_set("developer", ftos(dev));
485 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
497 // p.frags = 0; // do not harm the new team!
498 // return; // won't distribute negative scores
504 f = ceil(factor * p.frags);
505 p.frags = p.frags - f;
508 FOR_EACH_PLAYER(head)
510 if(head.team == targetteam)
516 FOR_EACH_PLAYER(head)
518 if(head.team == targetteam)
520 d = floor(f / nTeam);
521 head.frags = head.frags + d;
527 error("nPlayers in team changed!");
529 error(strcat("There were ", ftos(f), " frags left. BAD!"));
532 string Team_ColorCode(float teamid)
534 if(teamid == COLOR_TEAM1)
536 else if(teamid == COLOR_TEAM2)
538 else if(teamid == COLOR_TEAM3)
540 else if(teamid == COLOR_TEAM4)
546 string decolorize(string s)
555 ch1 = substring(s, 0, 1);
556 ch2 = substring(s, 1, 1);
561 out = strcat(out, "^^");
585 out = strcat(out, "^^");
587 s = substring(s, n, strlen(s) - n);
591 s = substring(s, 1, strlen(s) - 1);
592 out = strcat(out, ch1);
597 #define strdecolorize(s) decolorize(s)
598 #define strlennocol(s) strlen(decolorize(s))
600 #define CENTERPRIO_POINT 1
601 #define CENTERPRIO_REBALANCE 2
602 #define CENTERPRIO_VOTE 4
603 #define CENTERPRIO_NORMAL 5
604 #define CENTERPRIO_MAPVOTE 9
605 #define CENTERPRIO_ADMIN 99
606 .float centerprint_priority;
607 .float centerprint_expires;
608 void centerprint_atprio(entity e, float prio, string s)
610 if(intermission_running)
611 if(prio < CENTERPRIO_MAPVOTE)
613 if(time > e.centerprint_expires)
614 e.centerprint_priority = 0;
615 if(prio >= e.centerprint_priority)
617 e.centerprint_priority = prio;
618 e.centerprint_expires = time + e.cvar_scr_centertime;
619 centerprint_builtin(e, s);
622 void centerprint_expire(entity e, float prio)
624 if(prio == e.centerprint_priority)
626 e.centerprint_priority = 0;
627 centerprint_builtin(e, "");
630 void centerprint(entity e, string s)
632 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
637 // decolorizes and team colors the player name when needed
638 string playername(entity p)
641 if(teams_matter && !intermission_running && p.classname == "player")
643 t = Team_ColorCode(p.team);
644 return strcat(t, strdecolorize(p.netname));
650 // requires that m2>m1 in all coordinates, and that m4>m3
651 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;};
653 // requires the same, but is a stronger condition
654 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;};
656 float g_pickup_shells;
657 float g_pickup_shells_max;
658 float g_pickup_nails;
659 float g_pickup_nails_max;
660 float g_pickup_rockets;
661 float g_pickup_rockets_max;
662 float g_pickup_cells;
663 float g_pickup_cells_max;
664 float g_pickup_armorshard;
665 float g_pickup_armorshard_max;
666 float g_pickup_armor;
667 float g_pickup_armor_max;
668 float g_pickup_healthshard;
669 float g_pickup_healthshard_max;
670 float g_pickup_health;
671 float g_pickup_health_max;
672 float g_pickup_healthmega;
673 float g_pickup_healthmega_max;
675 void readlevelcvars(void)
677 g_pickup_shells = cvar("g_pickup_shells");
678 g_pickup_shells_max = cvar("g_pickup_shells_max");
679 g_pickup_nails = cvar("g_pickup_nails");
680 g_pickup_nails_max = cvar("g_pickup_nails_max");
681 g_pickup_rockets = cvar("g_pickup_rockets");
682 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
683 g_pickup_cells = cvar("g_pickup_cells");
684 g_pickup_cells_max = cvar("g_pickup_cells_max");
685 g_pickup_armorshard = cvar("g_pickup_armorshard");
686 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
687 g_pickup_armor = cvar("g_pickup_armor");
688 g_pickup_armor_max = cvar("g_pickup_armor_max");
689 g_pickup_healthshard = cvar("g_pickup_healthshard");
690 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
691 g_pickup_health = cvar("g_pickup_health");
692 g_pickup_health_max = cvar("g_pickup_health_max");
693 g_pickup_healthmega = cvar("g_pickup_healthmega");
694 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");