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);
15 void move_out_of_solid(entity e)
20 traceline(o, o, MOVE_WORLDONLY, e);
23 dprint("origin is in solid too! (", vtos(o), ")");
27 tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
35 move_out_of_solid_expand(e, '1 0 0' * m0_x); e.mins_x = m0_x;
36 move_out_of_solid_expand(e, '1 0 0' * m1_x); e.maxs_x = m1_x;
37 move_out_of_solid_expand(e, '0 1 0' * m0_y); e.mins_y = m0_y;
38 move_out_of_solid_expand(e, '0 1 0' * m1_y); e.maxs_y = m1_y;
39 move_out_of_solid_expand(e, '0 0 1' * m0_z); e.mins_z = m0_z;
40 move_out_of_solid_expand(e, '0 0 1' * m1_z); e.maxs_z = m1_z;
41 setorigin(e, e.origin);
43 tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
46 dprint("could not get out of solid (", vtos(o), ")");
51 // converts a number to a string with the indicated number of decimals
52 // works for up to 10 decimals!
53 string ftos_decimals(float number, float decimals)
59 // if negative, cut off the sign first
61 return strcat("-", ftos_decimals(-number, decimals));
62 // it now is always positive!
65 number = floor(number * pow(10, decimals) + 0.5);
68 result = ftos(number);
70 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
71 // if ftos had fucked it up, which should never happen: "34278.000000"
73 if(substring(result, len - 7, 1) == ".")
75 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
76 result = substring(result, 0, len - 7);
80 // is it too short? If yes, insert leading zeroes
83 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
86 // and now... INSERT THE POINT!
87 tmp = substring(result, len - decimals, decimals);
88 result = strcat(substring(result, 0, len - decimals), ".", tmp);
92 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
93 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
94 string STR_PLAYER = "player";
95 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
96 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
98 // change that to actually calling strcat when running on an engine without
99 // unlimited tempstrings:
100 // string strcat1(string s) = #115; // FRIK_FILE
101 #define strcat1(s) (s)
106 void(string s) bcenterprint
108 // TODO replace by MSG_ALL (would show it to spectators too, though)?
110 FOR_EACH_PLAYER(head)
111 if(clienttype(head) == CLIENTTYPE_REAL)
112 centerprint(head, s);
115 void(string s, float check_dangerous) ServerConsoleEcho =
118 if (checkextension("DP_SV_PRINT"))
127 ch = substring(s, 0, 1);
128 if(ch != "\"" && ch != "\r" && ch != "\n")
130 s = substring(s, 1, strlen(s) - 1);
141 void(string s, float check_dangerous) GameLogEcho =
146 if(cvar("sv_eventlog_files"))
151 matches = cvar("sv_eventlog_files_counter") + 1;
152 cvar_set("sv_eventlog_files_counter", ftos(matches));
155 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
156 fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
157 logfile = fopen(fn, FILE_APPEND);
160 fputs(logfile, strcat(s, "\n"));
162 if(cvar("sv_eventlog_console"))
164 ServerConsoleEcho(s, check_dangerous);
171 // will be opened later
174 void() GameLogClose =
176 if(logfile_open && logfile >= 0)
183 float math_mod(float a, float b)
185 return a - (floor(a / b) * b);
188 void relocate_spawnpoint()
190 // nudge off the floor
191 setorigin(self, self.origin + '0 0 1');
193 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
194 if (trace_startsolid)
196 objerror("player spawn point in solid, mapper sucks!\n");
200 if(cvar("g_spawnpoints_autodrop"))
202 setsize(self, PL_MIN, PL_MAX);
207 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
208 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
209 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
210 // BE CONSTANT OR strzoneD!
211 float(string haystack, string needle, float offset) strstr =
215 len = strlen(needle);
216 endpos = strlen(haystack) - len;
217 while(offset <= endpos)
219 found = substring(haystack, offset, len);
227 float NUM_NEAREST_ENTITIES = 4;
228 entity nearest_entity[NUM_NEAREST_ENTITIES];
229 float nearest_length[NUM_NEAREST_ENTITIES];
230 entity(vector point, .string field, string value, vector axismod) findnearest =
241 localhead = find(world, field, value);
244 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
245 dist = localhead.oldorigin;
247 dist = localhead.origin;
249 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
252 for(i = 0; i < num_nearest; ++i)
254 if(len < nearest_length[i])
258 // now i tells us where to insert at
259 // INSERTION SORT! YOU'VE SEEN IT! RUN!
260 if(i < NUM_NEAREST_ENTITIES)
262 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
264 nearest_length[j + 1] = nearest_length[j];
265 nearest_entity[j + 1] = nearest_entity[j];
267 nearest_length[i] = len;
268 nearest_entity[i] = localhead;
269 if(num_nearest < NUM_NEAREST_ENTITIES)
270 num_nearest = num_nearest + 1;
273 localhead = find(localhead, field, value);
276 // now use the first one from our list that we can see
277 for(i = 0; i < num_nearest; ++i)
279 traceline(point, nearest_entity[i].origin, TRUE, world);
280 if(trace_fraction == 1)
284 dprint("Nearest point (");
285 dprint(nearest_entity[0].netname);
286 dprint(") is not visible, using a visible one.\n");
288 return nearest_entity[i];
295 dprint("Not seeing any location point, using nearest as fallback.\n");
297 dprint("Candidates were: ");
298 for(j = 0; j < num_nearest; ++j)
302 dprint(nearest_entity[j].netname);
307 return nearest_entity[0];
310 void() target_location =
312 self.classname = "target_location";
313 // location name in netname
314 // eventually support: count, teamgame selectors, line of sight?
317 void() info_location =
319 self.classname = "target_location";
320 self.message = self.netname;
323 string NearestLocation(vector p)
328 loc = findnearest(p, classname, "target_location", '1 1 1');
335 loc = findnearest(p, target, "###item###", '1 1 4');
342 string(string msg) formatmessage =
349 msg_save = strzone(msg);
355 break; // too many replacements
357 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
360 replacement = substring(msg_save, p, 2);
361 escape = substring(msg_save, p + 1, 1);
364 else if(escape == "a")
365 replacement = ftos(floor(self.armorvalue));
366 else if(escape == "h")
367 replacement = ftos(floor(self.health));
368 else if(escape == "l")
369 replacement = NearestLocation(self.origin);
370 else if(escape == "y")
371 replacement = NearestLocation(self.cursor_trace_endpos);
372 else if(escape == "d")
373 replacement = NearestLocation(self.death_origin);
374 else if(escape == "w")
379 wep = self.switchweapon;
382 replacement = W_Name(wep);
384 else if(escape == "W")
386 if(self.items & IT_SHELLS) replacement = "shells";
387 else if(self.items & IT_NAILS) replacement = "bullets";
388 else if(self.items & IT_ROCKETS) replacement = "rockets";
389 else if(self.items & IT_CELLS) replacement = "cells";
390 else replacement = "batteries"; // ;)
392 else if(escape == "x")
394 replacement = self.cursor_trace_ent.netname;
395 if(!replacement || !self.cursor_trace_ent)
396 replacement = "nothing";
398 else if(escape == "p")
400 if(self.last_selected_player)
401 replacement = self.last_selected_player.netname;
403 replacement = "(nobody)";
405 msg = strcat(substring(msg_save, 0, p), replacement);
406 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
408 msg_save = strzone(msg);
411 msg = strcat(msg_save, "");
422 >0: receives a cvar from name=argv(f) value=argv(f+1)
424 void GetCvars_handleString(float f, .string field, string name)
429 strunzone(self.field);
436 strunzone(self.field);
437 self.field = strzone(argv(f + 1));
441 stuffcmd(self, strcat("sendcvar ", name, "\n"));
443 void GetCvars_handleFloat(float f, .float field, string name)
451 self.field = stof(argv(f + 1));
454 stuffcmd(self, strcat("sendcvar ", name, "\n"));
456 void GetCvars(float f)
458 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
459 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
460 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
461 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
462 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
463 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
464 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
465 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
466 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
469 float fexists(string f)
472 fh = fopen(f, FILE_READ);
479 void backtrace(string msg)
482 dev = cvar("developer");
483 cvar_set("developer", "1");
485 dprint("--- CUT HERE ---\nWARNING: ");
488 remove(world); // isn't there any better way to cause a backtrace?
489 dprint("\n--- CUT UNTIL HERE ---\n");
490 cvar_set("developer", ftos(dev));
493 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
505 // p.frags = 0; // do not harm the new team!
506 // return; // won't distribute negative scores
512 f = ceil(factor * p.frags);
513 p.frags = p.frags - f;
516 FOR_EACH_PLAYER(head)
518 if(head.team == targetteam)
524 FOR_EACH_PLAYER(head)
526 if(head.team == targetteam)
528 d = floor(f / nTeam);
529 head.frags = head.frags + d;
535 error("nPlayers in team changed!");
537 error(strcat("There were ", ftos(f), " frags left. BAD!"));
540 string Team_ColorCode(float teamid)
542 if(teamid == COLOR_TEAM1)
544 else if(teamid == COLOR_TEAM2)
546 else if(teamid == COLOR_TEAM3)
548 else if(teamid == COLOR_TEAM4)
555 string decolorize(string s)
564 ch1 = substring(s, 0, 1);
565 ch2 = substring(s, 1, 1);
570 out = strcat(out, "^^");
594 out = strcat(out, "^^");
596 s = substring(s, n, strlen(s) - n);
600 s = substring(s, 1, strlen(s) - 1);
601 out = strcat(out, ch1);
606 #define strdecolorize(s) decolorize(s)
607 #define strlennocol(s) strlen(decolorize(s))
610 #define CENTERPRIO_POINT 1
611 #define CENTERPRIO_REBALANCE 2
612 #define CENTERPRIO_VOTE 4
613 #define CENTERPRIO_NORMAL 5
614 #define CENTERPRIO_MAPVOTE 9
615 #define CENTERPRIO_ADMIN 99
616 .float centerprint_priority;
617 .float centerprint_expires;
618 void centerprint_atprio(entity e, float prio, string s)
620 if(intermission_running)
621 if(prio < CENTERPRIO_MAPVOTE)
623 if(time > e.centerprint_expires)
624 e.centerprint_priority = 0;
625 if(prio >= e.centerprint_priority)
627 e.centerprint_priority = prio;
628 e.centerprint_expires = time + e.cvar_scr_centertime;
629 centerprint_builtin(e, s);
632 void centerprint_expire(entity e, float prio)
634 if(prio == e.centerprint_priority)
636 e.centerprint_priority = 0;
637 centerprint_builtin(e, "");
640 void centerprint(entity e, string s)
642 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
647 // decolorizes and team colors the player name when needed
648 string playername(entity p)
651 if(teams_matter && !intermission_running && p.classname == "player")
653 t = Team_ColorCode(p.team);
654 return strcat(t, strdecolorize(p.netname));
660 // requires that m2>m1 in all coordinates, and that m4>m3
661 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;};
663 // requires the same, but is a stronger condition
664 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;};
666 float g_pickup_shells;
667 float g_pickup_shells_max;
668 float g_pickup_nails;
669 float g_pickup_nails_max;
670 float g_pickup_rockets;
671 float g_pickup_rockets_max;
672 float g_pickup_cells;
673 float g_pickup_cells_max;
674 float g_pickup_armorshard;
675 float g_pickup_armorshard_max;
676 float g_pickup_armor;
677 float g_pickup_armor_max;
678 float g_pickup_healthshard;
679 float g_pickup_healthshard_max;
680 float g_pickup_health;
681 float g_pickup_health_max;
682 float g_pickup_healthmega;
683 float g_pickup_healthmega_max;
685 void readlevelcvars(void)
687 g_pickup_shells = cvar("g_pickup_shells");
688 g_pickup_shells_max = cvar("g_pickup_shells_max");
689 g_pickup_nails = cvar("g_pickup_nails");
690 g_pickup_nails_max = cvar("g_pickup_nails_max");
691 g_pickup_rockets = cvar("g_pickup_rockets");
692 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
693 g_pickup_cells = cvar("g_pickup_cells");
694 g_pickup_cells_max = cvar("g_pickup_cells_max");
695 g_pickup_armorshard = cvar("g_pickup_armorshard");
696 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
697 g_pickup_armor = cvar("g_pickup_armor");
698 g_pickup_armor_max = cvar("g_pickup_armor_max");
699 g_pickup_healthshard = cvar("g_pickup_healthshard");
700 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
701 g_pickup_health = cvar("g_pickup_health");
702 g_pickup_health_max = cvar("g_pickup_health_max");
703 g_pickup_healthmega = cvar("g_pickup_healthmega");
704 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");