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");
201 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
202 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
203 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
204 // BE CONSTANT OR strzoneD!
205 float(string haystack, string needle, float offset) strstr =
209 len = strlen(needle);
210 endpos = strlen(haystack) - len;
211 while(offset <= endpos)
213 found = substring(haystack, offset, len);
221 float NUM_NEAREST_ENTITIES = 4;
222 entity nearest_entity[NUM_NEAREST_ENTITIES];
223 float nearest_length[NUM_NEAREST_ENTITIES];
224 entity(vector point, .string field, string value, vector axismod) findnearest =
235 localhead = find(world, field, value);
238 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
239 dist = localhead.oldorigin;
241 dist = localhead.origin;
243 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
246 for(i = 0; i < num_nearest; ++i)
248 if(len < nearest_length[i])
252 // now i tells us where to insert at
253 // INSERTION SORT! YOU'VE SEEN IT! RUN!
254 if(i < NUM_NEAREST_ENTITIES)
256 for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
258 nearest_length[j + 1] = nearest_length[j];
259 nearest_entity[j + 1] = nearest_entity[j];
261 nearest_length[i] = len;
262 nearest_entity[i] = localhead;
263 if(num_nearest < NUM_NEAREST_ENTITIES)
264 num_nearest = num_nearest + 1;
267 localhead = find(localhead, field, value);
270 // now use the first one from our list that we can see
271 for(i = 0; i < num_nearest; ++i)
273 traceline(point, nearest_entity[i].origin, TRUE, world);
274 if(trace_fraction == 1)
278 dprint("Nearest point (");
279 dprint(nearest_entity[0].netname);
280 dprint(") is not visible, using a visible one.\n");
282 return nearest_entity[i];
289 dprint("Not seeing any location point, using nearest as fallback.\n");
291 dprint("Candidates were: ");
292 for(j = 0; j < num_nearest; ++j)
296 dprint(nearest_entity[j].netname);
301 return nearest_entity[0];
304 void() target_location =
306 self.classname = "target_location";
307 // location name in netname
308 // eventually support: count, teamgame selectors, line of sight?
311 void() info_location =
313 self.classname = "target_location";
314 self.message = self.netname;
317 string NearestLocation(vector p)
322 loc = findnearest(p, classname, "target_location", '1 1 1');
329 loc = findnearest(p, target, "###item###", '1 1 4');
336 string(string msg) formatmessage =
343 msg_save = strzone(msg);
349 break; // too many replacements
351 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
354 replacement = substring(msg_save, p, 2);
355 escape = substring(msg_save, p + 1, 1);
358 else if(escape == "a")
359 replacement = ftos(floor(self.armorvalue));
360 else if(escape == "h")
361 replacement = ftos(floor(self.health));
362 else if(escape == "l")
363 replacement = NearestLocation(self.origin);
364 else if(escape == "y")
365 replacement = NearestLocation(self.cursor_trace_endpos);
366 else if(escape == "d")
367 replacement = NearestLocation(self.death_origin);
368 else if(escape == "w")
373 wep = self.switchweapon;
376 replacement = W_Name(wep);
378 else if(escape == "W")
380 if(self.items & IT_SHELLS) replacement = "shells";
381 else if(self.items & IT_NAILS) replacement = "bullets";
382 else if(self.items & IT_ROCKETS) replacement = "rockets";
383 else if(self.items & IT_CELLS) replacement = "cells";
384 else replacement = "batteries"; // ;)
386 else if(escape == "x")
388 replacement = self.cursor_trace_ent.netname;
389 if(!replacement || !self.cursor_trace_ent)
390 replacement = "nothing";
392 else if(escape == "p")
394 if(self.last_selected_player)
395 replacement = self.last_selected_player.netname;
397 replacement = "(nobody)";
399 msg = strcat(substring(msg_save, 0, p), replacement);
400 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
402 msg_save = strzone(msg);
405 msg = strcat(msg_save, "");
416 >0: receives a cvar from name=argv(f) value=argv(f+1)
418 void GetCvars_handleString(float f, .string field, string name)
423 strunzone(self.field);
430 strunzone(self.field);
431 self.field = strzone(argv(f + 1));
435 stuffcmd(self, strcat("sendcvar ", name, "\n"));
437 void GetCvars_handleFloat(float f, .float field, string name)
445 self.field = stof(argv(f + 1));
448 stuffcmd(self, strcat("sendcvar ", name, "\n"));
450 void GetCvars(float f)
452 GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
453 GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
454 GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
455 GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
456 GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
457 GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
458 GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
459 GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
460 GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
463 float fexists(string f)
466 fh = fopen(f, FILE_READ);
473 void backtrace(string msg)
476 dev = cvar("developer");
477 cvar_set("developer", "1");
479 dprint("--- CUT HERE ---\nWARNING: ");
482 remove(world); // isn't there any better way to cause a backtrace?
483 dprint("\n--- CUT UNTIL HERE ---\n");
484 cvar_set("developer", ftos(dev));
487 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
499 // p.frags = 0; // do not harm the new team!
500 // return; // won't distribute negative scores
506 f = ceil(factor * p.frags);
507 p.frags = p.frags - f;
510 FOR_EACH_PLAYER(head)
512 if(head.team == targetteam)
518 FOR_EACH_PLAYER(head)
520 if(head.team == targetteam)
522 d = floor(f / nTeam);
523 head.frags = head.frags + d;
529 error("nPlayers in team changed!");
531 error(strcat("There were ", ftos(f), " frags left. BAD!"));
534 string Team_ColorCode(float teamid)
536 if(teamid == COLOR_TEAM1)
538 else if(teamid == COLOR_TEAM2)
540 else if(teamid == COLOR_TEAM3)
542 else if(teamid == COLOR_TEAM4)
548 string decolorize(string s)
557 ch1 = substring(s, 0, 1);
558 ch2 = substring(s, 1, 1);
563 out = strcat(out, "^^");
587 out = strcat(out, "^^");
589 s = substring(s, n, strlen(s) - n);
593 s = substring(s, 1, strlen(s) - 1);
594 out = strcat(out, ch1);
599 #define strdecolorize(s) decolorize(s)
600 #define strlennocol(s) strlen(decolorize(s))
602 #define CENTERPRIO_POINT 1
603 #define CENTERPRIO_REBALANCE 2
604 #define CENTERPRIO_VOTE 4
605 #define CENTERPRIO_NORMAL 5
606 #define CENTERPRIO_MAPVOTE 9
607 #define CENTERPRIO_ADMIN 99
608 .float centerprint_priority;
609 .float centerprint_expires;
610 void centerprint_atprio(entity e, float prio, string s)
612 if(intermission_running)
613 if(prio < CENTERPRIO_MAPVOTE)
615 if(time > e.centerprint_expires)
616 e.centerprint_priority = 0;
617 if(prio >= e.centerprint_priority)
619 e.centerprint_priority = prio;
620 e.centerprint_expires = time + e.cvar_scr_centertime;
621 centerprint_builtin(e, s);
624 void centerprint_expire(entity e, float prio)
626 if(prio == e.centerprint_priority)
628 e.centerprint_priority = 0;
629 centerprint_builtin(e, "");
632 void centerprint(entity e, string s)
634 centerprint_atprio(e, CENTERPRIO_NORMAL, s);
639 // decolorizes and team colors the player name when needed
640 string playername(entity p)
643 if(teams_matter && !intermission_running && p.classname == "player")
645 t = Team_ColorCode(p.team);
646 return strcat(t, strdecolorize(p.netname));
652 // requires that m2>m1 in all coordinates, and that m4>m3
653 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;};
655 // requires the same, but is a stronger condition
656 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;};
658 float g_pickup_shells;
659 float g_pickup_shells_max;
660 float g_pickup_nails;
661 float g_pickup_nails_max;
662 float g_pickup_rockets;
663 float g_pickup_rockets_max;
664 float g_pickup_cells;
665 float g_pickup_cells_max;
666 float g_pickup_armorshard;
667 float g_pickup_armorshard_max;
668 float g_pickup_armor;
669 float g_pickup_armor_max;
670 float g_pickup_healthshard;
671 float g_pickup_healthshard_max;
672 float g_pickup_health;
673 float g_pickup_health_max;
674 float g_pickup_healthmega;
675 float g_pickup_healthmega_max;
677 void readlevelcvars(void)
679 g_pickup_shells = cvar("g_pickup_shells");
680 g_pickup_shells_max = cvar("g_pickup_shells_max");
681 g_pickup_nails = cvar("g_pickup_nails");
682 g_pickup_nails_max = cvar("g_pickup_nails_max");
683 g_pickup_rockets = cvar("g_pickup_rockets");
684 g_pickup_rockets_max = cvar("g_pickup_rockets_max");
685 g_pickup_cells = cvar("g_pickup_cells");
686 g_pickup_cells_max = cvar("g_pickup_cells_max");
687 g_pickup_armorshard = cvar("g_pickup_armorshard");
688 g_pickup_armorshard_max = cvar("g_pickup_armorshard_max");
689 g_pickup_armor = cvar("g_pickup_armor");
690 g_pickup_armor_max = cvar("g_pickup_armor_max");
691 g_pickup_healthshard = cvar("g_pickup_healthshard");
692 g_pickup_healthshard_max = cvar("g_pickup_healthshard_max");
693 g_pickup_health = cvar("g_pickup_health");
694 g_pickup_health_max = cvar("g_pickup_health_max");
695 g_pickup_healthmega = cvar("g_pickup_healthmega");
696 g_pickup_healthmega_max = cvar("g_pickup_healthmega_max");