]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
need the first down-trace to not shift waypoints slightly around when loading/saving
[divverent/nexuiz.git] / data / qcsrc / server / miscfunctions.qc
1 void move_out_of_solid_expand(entity e, vector by)
2 {
3         float eps = 0.0625;
4         tracebox(e.origin, e.mins - '1 1 1' * eps, e.maxs + '1 1 1' * eps, e.origin + by, MOVE_WORLDONLY, e);
5         if(trace_startsolid)
6                 return;
7         if(trace_fraction < 1)
8         {
9                 // hit something
10                 // adjust origin in the other direction...
11                 e.origin = e.origin - by * (1 - trace_fraction);
12         }
13 }
14
15 void move_out_of_solid(entity e)
16 {
17         vector o, m0, m1;
18
19         o = e.origin;
20         traceline(o, o, MOVE_WORLDONLY, e);
21         if(trace_startsolid)
22         {
23                 dprint("origin is in solid too! (", vtos(o), ")");
24                 return;
25         }
26
27         tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
28         if(!trace_startsolid)
29                 return;
30
31         m0 = e.mins;
32         m1 = e.maxs;
33         e.mins = '0 0 0';
34         e.maxs = '0 0 0';
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);
42
43         tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
44         if(trace_startsolid)
45         {
46                 dprint("could not get out of solid (", vtos(o), ")");
47                 return;
48         }
49 }
50
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)
54 {
55         string result;
56         string tmp;
57         float len;
58
59         // if negative, cut off the sign first
60         if(number < 0)
61                 return strcat("-", ftos_decimals(-number, decimals));
62         // it now is always positive!
63
64         // 3.516 -> 352
65         number = floor(number * pow(10, decimals) + 0.5);
66
67         // 352 -> "352"
68         result = ftos(number);
69         len = strlen(result);
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"
72         if(len >= 7)
73                 if(substring(result, len - 7, 1) == ".")
74                 {
75                         dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
76                         result = substring(result, 0, len - 7);
77                         len -= 7;
78                 }
79                 // "34278"
80         // is it too short? If yes, insert leading zeroes
81         if(len <= decimals)
82         {
83                 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
84                 len = decimals + 1;
85         }
86         // and now... INSERT THE POINT!
87         tmp = substring(result, len - decimals, decimals);
88         result = strcat(substring(result, 0, len - decimals), ".", tmp);
89         return result;
90 }
91
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)
97
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)
102
103 float logfile_open;
104 float logfile;
105
106 void(string s) bcenterprint
107 {
108         // TODO replace by MSG_ALL (would show it to spectators too, though)?
109         entity head;
110         FOR_EACH_PLAYER(head)
111                 if(clienttype(head) == CLIENTTYPE_REAL)
112                         centerprint(head, s);
113 }
114
115 void(string s, float check_dangerous) ServerConsoleEcho =
116 {
117         local string ch;
118         if (checkextension("DP_SV_PRINT"))
119                 print(s, "\n");
120         else
121         {
122                 localcmd("echo \"");
123                 if(check_dangerous)
124                 {
125                         while(strlen(s))
126                         {
127                                 ch = substring(s, 0, 1);
128                                 if(ch != "\"" && ch != "\r" && ch != "\n")
129                                         localcmd(ch);
130                                 s = substring(s, 1, strlen(s) - 1);
131                         }
132                 }
133                 else
134                 {
135                         localcmd(s);
136                 }
137                 localcmd("\"\n");
138         }
139 }
140
141 void(string s, float check_dangerous) GameLogEcho =
142 {
143         string fn;
144         float matches;
145
146         if(cvar("sv_eventlog_files"))
147         {
148                 if(!logfile_open)
149                 {
150                         logfile_open = TRUE;
151                         matches = cvar("sv_eventlog_files_counter") + 1;
152                         cvar_set("sv_eventlog_files_counter", ftos(matches));
153                         fn = ftos(matches);
154                         if(strlen(fn) < 8)
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);
158                 }
159                 if(logfile >= 0)
160                         fputs(logfile, strcat(s, "\n"));
161         }
162         if(cvar("sv_eventlog_console"))
163         {
164                 ServerConsoleEcho(s, check_dangerous);
165         }
166 }
167
168 void() GameLogInit =
169 {
170         logfile_open = 0;
171         // will be opened later
172 }
173
174 void() GameLogClose =
175 {
176         if(logfile_open && logfile >= 0)
177         {
178                 fclose(logfile);
179                 logfile = -1;
180         }
181 }
182
183 float math_mod(float a, float b)
184 {
185         return a - (floor(a / b) * b);
186 }
187
188 void relocate_spawnpoint()
189 {
190         // nudge off the floor
191         setorigin(self, self.origin + '0 0 1');
192
193         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
194         if (trace_startsolid)
195         {
196                 objerror("player spawn point in solid, mapper sucks!\n");
197                 return;
198         }
199 }
200
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 =
206 {
207         float len, endpos;
208         string found;
209         len = strlen(needle);
210         endpos = strlen(haystack) - len;
211         while(offset <= endpos)
212         {
213                 found = substring(haystack, offset, len);
214                 if(found == needle)
215                         return offset;
216                 offset = offset + 1;
217         }
218         return -1;
219 }
220
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 =
225 {
226         entity localhead;
227         float i;
228         float j;
229         float len;
230         vector dist;
231
232         float num_nearest;
233         num_nearest = 0;
234
235         localhead = find(world, field, value);
236         while(localhead)
237         {
238                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
239                         dist = localhead.oldorigin;
240                 else
241                         dist = localhead.origin;
242                 dist = dist - point;
243                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
244                 len = vlen(dist);
245
246                 for(i = 0; i < num_nearest; ++i)
247                 {
248                         if(len < nearest_length[i])
249                                 break;
250                 }
251
252                 // now i tells us where to insert at
253                 //   INSERTION SORT! YOU'VE SEEN IT! RUN!
254                 if(i < NUM_NEAREST_ENTITIES)
255                 {
256                         for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
257                         {
258                                 nearest_length[j + 1] = nearest_length[j];
259                                 nearest_entity[j + 1] = nearest_entity[j];
260                         }
261                         nearest_length[i] = len;
262                         nearest_entity[i] = localhead;
263                         if(num_nearest < NUM_NEAREST_ENTITIES)
264                                 num_nearest = num_nearest + 1;
265                 }
266
267                 localhead = find(localhead, field, value);
268         }
269
270         // now use the first one from our list that we can see
271         for(i = 0; i < num_nearest; ++i)
272         {
273                 traceline(point, nearest_entity[i].origin, TRUE, world);
274                 if(trace_fraction == 1)
275                 {
276                         if(i != 0)
277                         {
278                                 dprint("Nearest point (");
279                                 dprint(nearest_entity[0].netname);
280                                 dprint(") is not visible, using a visible one.\n");
281                         }
282                         return nearest_entity[i];
283                 }
284         }
285
286         if(num_nearest == 0)
287                 return world;
288
289         dprint("Not seeing any location point, using nearest as fallback.\n");
290         /* DEBUGGING CODE:
291         dprint("Candidates were: ");
292         for(j = 0; j < num_nearest; ++j)
293         {
294                 if(j != 0)
295                         dprint(", ");
296                 dprint(nearest_entity[j].netname);
297         }
298         dprint("\n");
299         */
300
301         return nearest_entity[0];
302 }
303
304 void() target_location =
305 {
306         self.classname = "target_location";
307         // location name in netname
308         // eventually support: count, teamgame selectors, line of sight?
309 };
310
311 void() info_location =
312 {
313         self.classname = "target_location";
314         self.message = self.netname;
315 };
316
317 string NearestLocation(vector p)
318 {
319         entity loc;
320         string ret;
321         ret = "somewhere";
322         loc = findnearest(p, classname, "target_location", '1 1 1');
323         if(loc)
324         {
325                 ret = loc.message;
326         }
327         else
328         {
329                 loc = findnearest(p, target, "###item###", '1 1 4');
330                 if(loc)
331                         ret = loc.netname;
332         }
333         return ret;
334 }
335
336 string(string msg) formatmessage =
337 {
338         float p;
339         float n;
340         string msg_save;
341         string escape;
342         string replacement;
343         msg_save = strzone(msg);
344         p = 0;
345         n = 7;
346         while(1)
347         {
348                 if(n < 1)
349                         break; // too many replacements
350                 n = n - 1;
351                 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
352                 if(p < 0)
353                         break;
354                 replacement = substring(msg_save, p, 2);
355                 escape = substring(msg_save, p + 1, 1);
356                 if(escape == "%")
357                         replacement = "%";
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")
369                 {
370                         float wep;
371                         wep = self.weapon;
372                         if(!wep)
373                                 wep = self.switchweapon;
374                         if(!wep)
375                                 wep = self.cnt;
376                         replacement = W_Name(wep);
377                 }
378                 else if(escape == "W")
379                 {
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"; // ;)
385                 }
386                 else if(escape == "x")
387                 {
388                         replacement = self.cursor_trace_ent.netname;
389                         if(!replacement || !self.cursor_trace_ent)
390                                 replacement = "nothing";
391                 }
392                 else if(escape == "p")
393                 {
394                         if(self.last_selected_player)
395                                 replacement = self.last_selected_player.netname;
396                         else
397                                 replacement = "(nobody)";
398                 }
399                 msg = strcat(substring(msg_save, 0, p), replacement);
400                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
401                 strunzone(msg_save);
402                 msg_save = strzone(msg);
403                 p = p + 2;
404         }
405         msg = strcat(msg_save, "");
406         strunzone(msg_save);
407         return msg;
408 }
409
410 /*
411 =============
412 GetCvars
413 =============
414 Called with:
415   0:  sends the request
416   >0: receives a cvar from name=argv(f) value=argv(f+1)
417 */
418 void GetCvars_handleString(float f, .string field, string name)
419 {
420         if(f < 0)
421         {
422                 if(self.field)
423                         strunzone(self.field);
424         }
425         else if(f > 0)
426         {
427                 if(argv(f) == name)
428                 {
429                         if(self.field)
430                                 strunzone(self.field);
431                         self.field = strzone(argv(f + 1));
432                 }
433         }
434         else
435                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
436 }
437 void GetCvars_handleFloat(float f, .float field, string name)
438 {
439         if(f < 0)
440         {
441         }
442         else if(f > 0)
443         {
444                 if(argv(f) == name)
445                         self.field = stof(argv(f + 1));
446         }
447         else
448                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
449 }
450 void GetCvars(float f)
451 {
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");
461 }
462
463 float fexists(string f)
464 {
465         float fh;
466         fh = fopen(f, FILE_READ);
467         if(fh < 0)
468                 return FALSE;
469         fclose(fh);
470         return TRUE;
471 }
472
473 void backtrace(string msg)
474 {
475         float dev;
476         dev = cvar("developer");
477         cvar_set("developer", "1");
478         dprint("\n");
479         dprint("--- CUT HERE ---\nWARNING: ");
480         dprint(msg);
481         dprint("\n");
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));
485 }
486
487 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
488 {
489         float f;
490         float d;
491         float nTeam;
492         entity head;
493
494         if(!teams_matter)
495                 return;
496
497         //if(p.frags < 0)
498         //{
499         //      p.frags = 0; // do not harm the new team!
500         //      return; // won't distribute negative scores
501         //}
502
503         if(p.frags == -666)
504                 return;
505
506         f = ceil(factor * p.frags);
507         p.frags = p.frags - f;
508
509         nTeam = 0;
510         FOR_EACH_PLAYER(head)
511                 if(head != p)
512                         if(head.team == targetteam)
513                                 nTeam = nTeam + 1;
514
515         if(nTeam == 0)
516                 return;
517
518         FOR_EACH_PLAYER(head)
519                 if(head != p)
520                         if(head.team == targetteam)
521                         {
522                                 d = floor(f / nTeam);
523                                 head.frags = head.frags + d;
524                                 f = f - d;
525                                 nTeam = nTeam - 1;
526                         }
527
528         if(nTeam != 0)
529                 error("nPlayers in team changed!");
530         if(f != 0)
531                 error(strcat("There were ", ftos(f), " frags left. BAD!"));
532 }
533
534 string Team_ColorCode(float teamid)
535 {
536         if(teamid == COLOR_TEAM1)
537                 return "^1";
538         else if(teamid == COLOR_TEAM2)
539                 return "^4";
540         else if(teamid == COLOR_TEAM3)
541                 return "^6";
542         else if(teamid == COLOR_TEAM4)
543                 return "^3";
544         else
545                 return "^7";
546 }
547
548 string decolorize(string s)
549 {
550         string out;
551         out = "";
552         while(s != "")
553         {
554                 float n;
555                 string ch1, ch2;
556                 n = 1;
557                 ch1 = substring(s, 0, 1);
558                 ch2 = substring(s, 1, 1);
559                 if(ch1 == "^")
560                 {
561                         n = 2;
562                         if(ch2 == "^")
563                                 out = strcat(out, "^^");
564                         else if(ch2 == "0")
565                                 out = strcat1(out);
566                         else if(ch2 == "1")
567                                 out = strcat1(out);
568                         else if(ch2 == "2")
569                                 out = strcat1(out);
570                         else if(ch2 == "3")
571                                 out = strcat1(out);
572                         else if(ch2 == "4")
573                                 out = strcat1(out);
574                         else if(ch2 == "5")
575                                 out = strcat1(out);
576                         else if(ch2 == "6")
577                                 out = strcat1(out);
578                         else if(ch2 == "7")
579                                 out = strcat1(out);
580                         else if(ch2 == "8")
581                                 out = strcat1(out);
582                         else if(ch2 == "9")
583                                 out = strcat1(out);
584                         else
585                         {
586                                 n = 1;
587                                 out = strcat(out, "^^");
588                         }
589                         s = substring(s, n, strlen(s) - n);
590                 }
591                 else
592                 {
593                         s = substring(s, 1, strlen(s) - 1);
594                         out = strcat(out, ch1);
595                 }
596         }
597         return out;
598 }
599 #define strdecolorize(s) decolorize(s)
600 #define strlennocol(s) strlen(decolorize(s))
601
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)
611 {
612         if(intermission_running)
613                 if(prio < CENTERPRIO_MAPVOTE)
614                         return;
615         if(time > e.centerprint_expires)
616                 e.centerprint_priority = 0;
617         if(prio >= e.centerprint_priority)
618         {
619                 e.centerprint_priority = prio;
620                 e.centerprint_expires = time + e.cvar_scr_centertime;
621                 centerprint_builtin(e, s);
622         }
623 }
624 void centerprint_expire(entity e, float prio)
625 {
626         if(prio == e.centerprint_priority)
627         {
628                 e.centerprint_priority = 0;
629                 centerprint_builtin(e, "");
630         }
631 }
632 void centerprint(entity e, string s)
633 {
634         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
635 }
636
637 void VoteNag();
638
639 // decolorizes and team colors the player name when needed
640 string playername(entity p)
641 {
642         string t;
643         if(teams_matter && !intermission_running && p.classname == "player")
644         {
645                 t = Team_ColorCode(p.team);
646                 return strcat(t, strdecolorize(p.netname));
647         }
648         else
649                 return p.netname;
650 }
651
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;};
654
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;};
657
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;
676
677 void readlevelcvars(void)
678 {
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");
697 }