]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
no longer need workaround against broken strlennocol
[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         if(cvar("g_spawnpoints_autodrop"))
201         {
202                 setsize(self, PL_MIN, PL_MAX);
203                 droptofloor();
204         }
205 }
206
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 =
212 {
213         float len, endpos;
214         string found;
215         len = strlen(needle);
216         endpos = strlen(haystack) - len;
217         while(offset <= endpos)
218         {
219                 found = substring(haystack, offset, len);
220                 if(found == needle)
221                         return offset;
222                 offset = offset + 1;
223         }
224         return -1;
225 }
226
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 =
231 {
232         entity localhead;
233         float i;
234         float j;
235         float len;
236         vector dist;
237
238         float num_nearest;
239         num_nearest = 0;
240
241         localhead = find(world, field, value);
242         while(localhead)
243         {
244                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
245                         dist = localhead.oldorigin;
246                 else
247                         dist = localhead.origin;
248                 dist = dist - point;
249                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
250                 len = vlen(dist);
251
252                 for(i = 0; i < num_nearest; ++i)
253                 {
254                         if(len < nearest_length[i])
255                                 break;
256                 }
257
258                 // now i tells us where to insert at
259                 //   INSERTION SORT! YOU'VE SEEN IT! RUN!
260                 if(i < NUM_NEAREST_ENTITIES)
261                 {
262                         for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
263                         {
264                                 nearest_length[j + 1] = nearest_length[j];
265                                 nearest_entity[j + 1] = nearest_entity[j];
266                         }
267                         nearest_length[i] = len;
268                         nearest_entity[i] = localhead;
269                         if(num_nearest < NUM_NEAREST_ENTITIES)
270                                 num_nearest = num_nearest + 1;
271                 }
272
273                 localhead = find(localhead, field, value);
274         }
275
276         // now use the first one from our list that we can see
277         for(i = 0; i < num_nearest; ++i)
278         {
279                 traceline(point, nearest_entity[i].origin, TRUE, world);
280                 if(trace_fraction == 1)
281                 {
282                         if(i != 0)
283                         {
284                                 dprint("Nearest point (");
285                                 dprint(nearest_entity[0].netname);
286                                 dprint(") is not visible, using a visible one.\n");
287                         }
288                         return nearest_entity[i];
289                 }
290         }
291
292         if(num_nearest == 0)
293                 return world;
294
295         dprint("Not seeing any location point, using nearest as fallback.\n");
296         /* DEBUGGING CODE:
297         dprint("Candidates were: ");
298         for(j = 0; j < num_nearest; ++j)
299         {
300                 if(j != 0)
301                         dprint(", ");
302                 dprint(nearest_entity[j].netname);
303         }
304         dprint("\n");
305         */
306
307         return nearest_entity[0];
308 }
309
310 void() target_location =
311 {
312         self.classname = "target_location";
313         // location name in netname
314         // eventually support: count, teamgame selectors, line of sight?
315 };
316
317 void() info_location =
318 {
319         self.classname = "target_location";
320         self.message = self.netname;
321 };
322
323 string NearestLocation(vector p)
324 {
325         entity loc;
326         string ret;
327         ret = "somewhere";
328         loc = findnearest(p, classname, "target_location", '1 1 1');
329         if(loc)
330         {
331                 ret = loc.message;
332         }
333         else
334         {
335                 loc = findnearest(p, target, "###item###", '1 1 4');
336                 if(loc)
337                         ret = loc.netname;
338         }
339         return ret;
340 }
341
342 string(string msg) formatmessage =
343 {
344         float p;
345         float n;
346         string msg_save;
347         string escape;
348         string replacement;
349         msg_save = strzone(msg);
350         p = 0;
351         n = 7;
352         while(1)
353         {
354                 if(n < 1)
355                         break; // too many replacements
356                 n = n - 1;
357                 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
358                 if(p < 0)
359                         break;
360                 replacement = substring(msg_save, p, 2);
361                 escape = substring(msg_save, p + 1, 1);
362                 if(escape == "%")
363                         replacement = "%";
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")
375                 {
376                         float wep;
377                         wep = self.weapon;
378                         if(!wep)
379                                 wep = self.switchweapon;
380                         if(!wep)
381                                 wep = self.cnt;
382                         replacement = W_Name(wep);
383                 }
384                 else if(escape == "W")
385                 {
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"; // ;)
391                 }
392                 else if(escape == "x")
393                 {
394                         replacement = self.cursor_trace_ent.netname;
395                         if(!replacement || !self.cursor_trace_ent)
396                                 replacement = "nothing";
397                 }
398                 else if(escape == "p")
399                 {
400                         if(self.last_selected_player)
401                                 replacement = self.last_selected_player.netname;
402                         else
403                                 replacement = "(nobody)";
404                 }
405                 msg = strcat(substring(msg_save, 0, p), replacement);
406                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
407                 strunzone(msg_save);
408                 msg_save = strzone(msg);
409                 p = p + 2;
410         }
411         msg = strcat(msg_save, "");
412         strunzone(msg_save);
413         return msg;
414 }
415
416 /*
417 =============
418 GetCvars
419 =============
420 Called with:
421   0:  sends the request
422   >0: receives a cvar from name=argv(f) value=argv(f+1)
423 */
424 void GetCvars_handleString(float f, .string field, string name)
425 {
426         if(f < 0)
427         {
428                 if(self.field)
429                         strunzone(self.field);
430         }
431         else if(f > 0)
432         {
433                 if(argv(f) == name)
434                 {
435                         if(self.field)
436                                 strunzone(self.field);
437                         self.field = strzone(argv(f + 1));
438                 }
439         }
440         else
441                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
442 }
443 void GetCvars_handleFloat(float f, .float field, string name)
444 {
445         if(f < 0)
446         {
447         }
448         else if(f > 0)
449         {
450                 if(argv(f) == name)
451                         self.field = stof(argv(f + 1));
452         }
453         else
454                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
455 }
456 void GetCvars(float f)
457 {
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");
467 }
468
469 float fexists(string f)
470 {
471         float fh;
472         fh = fopen(f, FILE_READ);
473         if(fh < 0)
474                 return FALSE;
475         fclose(fh);
476         return TRUE;
477 }
478
479 void backtrace(string msg)
480 {
481         float dev;
482         dev = cvar("developer");
483         cvar_set("developer", "1");
484         dprint("\n");
485         dprint("--- CUT HERE ---\nWARNING: ");
486         dprint(msg);
487         dprint("\n");
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));
491 }
492
493 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
494 {
495         float f;
496         float d;
497         float nTeam;
498         entity head;
499
500         if(!teams_matter)
501                 return;
502
503         //if(p.frags < 0)
504         //{
505         //      p.frags = 0; // do not harm the new team!
506         //      return; // won't distribute negative scores
507         //}
508
509         if(p.frags == -666)
510                 return;
511
512         f = ceil(factor * p.frags);
513         p.frags = p.frags - f;
514
515         nTeam = 0;
516         FOR_EACH_PLAYER(head)
517                 if(head != p)
518                         if(head.team == targetteam)
519                                 nTeam = nTeam + 1;
520
521         if(nTeam == 0)
522                 return;
523
524         FOR_EACH_PLAYER(head)
525                 if(head != p)
526                         if(head.team == targetteam)
527                         {
528                                 d = floor(f / nTeam);
529                                 head.frags = head.frags + d;
530                                 f = f - d;
531                                 nTeam = nTeam - 1;
532                         }
533
534         if(nTeam != 0)
535                 error("nPlayers in team changed!");
536         if(f != 0)
537                 error(strcat("There were ", ftos(f), " frags left. BAD!"));
538 }
539
540 string Team_ColorCode(float teamid)
541 {
542         if(teamid == COLOR_TEAM1)
543                 return "^1";
544         else if(teamid == COLOR_TEAM2)
545                 return "^4";
546         else if(teamid == COLOR_TEAM3)
547                 return "^6";
548         else if(teamid == COLOR_TEAM4)
549                 return "^3";
550         else
551                 return "^7";
552 }
553
554 /*
555 string decolorize(string s)
556 {
557         string out;
558         out = "";
559         while(s != "")
560         {
561                 float n;
562                 string ch1, ch2;
563                 n = 1;
564                 ch1 = substring(s, 0, 1);
565                 ch2 = substring(s, 1, 1);
566                 if(ch1 == "^")
567                 {
568                         n = 2;
569                         if(ch2 == "^")
570                                 out = strcat(out, "^^");
571                         else if(ch2 == "0")
572                                 out = strcat1(out);
573                         else if(ch2 == "1")
574                                 out = strcat1(out);
575                         else if(ch2 == "2")
576                                 out = strcat1(out);
577                         else if(ch2 == "3")
578                                 out = strcat1(out);
579                         else if(ch2 == "4")
580                                 out = strcat1(out);
581                         else if(ch2 == "5")
582                                 out = strcat1(out);
583                         else if(ch2 == "6")
584                                 out = strcat1(out);
585                         else if(ch2 == "7")
586                                 out = strcat1(out);
587                         else if(ch2 == "8")
588                                 out = strcat1(out);
589                         else if(ch2 == "9")
590                                 out = strcat1(out);
591                         else
592                         {
593                                 n = 1;
594                                 out = strcat(out, "^^");
595                         }
596                         s = substring(s, n, strlen(s) - n);
597                 }
598                 else
599                 {
600                         s = substring(s, 1, strlen(s) - 1);
601                         out = strcat(out, ch1);
602                 }
603         }
604         return out;
605 }
606 #define strdecolorize(s) decolorize(s)
607 #define strlennocol(s) strlen(decolorize(s))
608 */
609
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)
619 {
620         if(intermission_running)
621                 if(prio < CENTERPRIO_MAPVOTE)
622                         return;
623         if(time > e.centerprint_expires)
624                 e.centerprint_priority = 0;
625         if(prio >= e.centerprint_priority)
626         {
627                 e.centerprint_priority = prio;
628                 e.centerprint_expires = time + e.cvar_scr_centertime;
629                 centerprint_builtin(e, s);
630         }
631 }
632 void centerprint_expire(entity e, float prio)
633 {
634         if(prio == e.centerprint_priority)
635         {
636                 e.centerprint_priority = 0;
637                 centerprint_builtin(e, "");
638         }
639 }
640 void centerprint(entity e, string s)
641 {
642         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
643 }
644
645 void VoteNag();
646
647 // decolorizes and team colors the player name when needed
648 string playername(entity p)
649 {
650         string t;
651         if(teams_matter && !intermission_running && p.classname == "player")
652         {
653                 t = Team_ColorCode(p.team);
654                 return strcat(t, strdecolorize(p.netname));
655         }
656         else
657                 return p.netname;
658 }
659
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;};
662
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;};
665
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;
684
685 void readlevelcvars(void)
686 {
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");
705 }