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