]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
The Big Merge:
[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         vector o;
191         o = self.origin;
192
193         setsize(self, PL_MIN, PL_MAX);
194
195         // nudge off the floor
196         setorigin(self, self.origin + '0 0 1');
197
198         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
199         if (trace_startsolid)
200         {
201                 objerror("player spawn point in solid, mapper sucks!\n");
202                 return;
203         }
204
205         // drop the spawn point onto the floor
206         // NOTE: a later version might not do this any more
207         droptofloor();
208 }
209
210 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
211 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
212 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
213 // BE CONSTANT OR strzoneD!
214 float(string haystack, string needle, float offset) strstr =
215 {
216         float len, endpos;
217         string found;
218         len = strlen(needle);
219         endpos = strlen(haystack) - len;
220         while(offset <= endpos)
221         {
222                 found = substring(haystack, offset, len);
223                 if(found == needle)
224                         return offset;
225                 offset = offset + 1;
226         }
227         return -1;
228 }
229
230 float NUM_NEAREST_ENTITIES = 4;
231 entity nearest_entity[NUM_NEAREST_ENTITIES];
232 float nearest_length[NUM_NEAREST_ENTITIES];
233 entity(vector point, .string field, string value, vector axismod) findnearest =
234 {
235         entity localhead;
236         float i;
237         float j;
238         float len;
239         vector dist;
240
241         float num_nearest;
242         num_nearest = 0;
243
244         localhead = find(world, field, value);
245         while(localhead)
246         {
247                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
248                         dist = localhead.oldorigin;
249                 else
250                         dist = localhead.origin;
251                 dist = dist - point;
252                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
253                 len = vlen(dist);
254
255                 for(i = 0; i < num_nearest; ++i)
256                 {
257                         if(len < nearest_length[i])
258                                 break;
259                 }
260
261                 // now i tells us where to insert at
262                 //   INSERTION SORT! YOU'VE SEEN IT! RUN!
263                 if(i < NUM_NEAREST_ENTITIES)
264                 {
265                         for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
266                         {
267                                 nearest_length[j + 1] = nearest_length[j];
268                                 nearest_entity[j + 1] = nearest_entity[j];
269                         }
270                         nearest_length[i] = len;
271                         nearest_entity[i] = localhead;
272                         if(num_nearest < NUM_NEAREST_ENTITIES)
273                                 num_nearest = num_nearest + 1;
274                 }
275
276                 localhead = find(localhead, field, value);
277         }
278
279         // now use the first one from our list that we can see
280         for(i = 0; i < num_nearest; ++i)
281         {
282                 traceline(point, nearest_entity[i].origin, TRUE, world);
283                 if(trace_fraction == 1)
284                 {
285                         if(i != 0)
286                         {
287                                 dprint("Nearest point (");
288                                 dprint(nearest_entity[0].netname);
289                                 dprint(") is not visible, using a visible one.\n");
290                         }
291                         return nearest_entity[i];
292                 }
293         }
294
295         if(num_nearest == 0)
296                 return world;
297
298         dprint("Not seeing any location point, using nearest as fallback.\n");
299         /* DEBUGGING CODE:
300         dprint("Candidates were: ");
301         for(j = 0; j < num_nearest; ++j)
302         {
303                 if(j != 0)
304                         dprint(", ");
305                 dprint(nearest_entity[j].netname);
306         }
307         dprint("\n");
308         */
309
310         return nearest_entity[0];
311 }
312
313 void() target_location =
314 {
315         self.classname = "target_location";
316         // location name in netname
317         // eventually support: count, teamgame selectors, line of sight?
318 };
319
320 void() info_location =
321 {
322         self.classname = "target_location";
323         self.message = self.netname;
324 };
325
326 string NearestLocation(vector p)
327 {
328         entity loc;
329         string ret;
330         ret = "somewhere";
331         loc = findnearest(p, classname, "target_location", '1 1 1');
332         if(loc)
333         {
334                 ret = loc.message;
335         }
336         else
337         {
338                 loc = findnearest(p, target, "###item###", '1 1 4');
339                 if(loc)
340                         ret = loc.netname;
341         }
342         return ret;
343 }
344
345 string(string msg) formatmessage =
346 {
347         float p;
348         float n;
349         string msg_save;
350         string escape;
351         string replacement;
352         msg_save = strzone(msg);
353         p = 0;
354         n = 7;
355         while(1)
356         {
357                 if(n < 1)
358                         break; // too many replacements
359                 n = n - 1;
360                 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
361                 if(p < 0)
362                         break;
363                 replacement = substring(msg_save, p, 2);
364                 escape = substring(msg_save, p + 1, 1);
365                 if(escape == "%")
366                         replacement = "%";
367                 else if(escape == "a")
368                         replacement = ftos(floor(self.armorvalue));
369                 else if(escape == "h")
370                         replacement = ftos(floor(self.health));
371                 else if(escape == "l")
372                         replacement = NearestLocation(self.origin);
373                 else if(escape == "y")
374                         replacement = NearestLocation(self.cursor_trace_endpos);
375                 else if(escape == "d")
376                         replacement = NearestLocation(self.death_origin);
377                 else if(escape == "w")
378                 {
379                         float wep;
380                         wep = self.weapon;
381                         if(!wep)
382                                 wep = self.switchweapon;
383                         if(!wep)
384                                 wep = self.cnt;
385                         replacement = W_Name(wep);
386                 }
387                 else if(escape == "W")
388                 {
389                         if(self.items & IT_SHELLS) replacement = "shells";
390                         else if(self.items & IT_NAILS) replacement = "bullets";
391                         else if(self.items & IT_ROCKETS) replacement = "rockets";
392                         else if(self.items & IT_CELLS) replacement = "cells";
393                         else replacement = "batteries"; // ;)
394                 }
395                 else if(escape == "x")
396                 {
397                         replacement = self.cursor_trace_ent.netname;
398                         if(!replacement || !self.cursor_trace_ent)
399                                 replacement = "nothing";
400                 }
401                 else if(escape == "p")
402                 {
403                         if(self.last_selected_player)
404                                 replacement = self.last_selected_player.netname;
405                         else
406                                 replacement = "(nobody)";
407                 }
408                 msg = strcat(substring(msg_save, 0, p), replacement);
409                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
410                 strunzone(msg_save);
411                 msg_save = strzone(msg);
412                 p = p + 2;
413         }
414         msg = strcat(msg_save, "");
415         strunzone(msg_save);
416         return msg;
417 }
418
419 /*
420 =============
421 GetCvars
422 =============
423 Called with:
424   0:  sends the request
425   >0: receives a cvar from name=argv(f) value=argv(f+1)
426 */
427 void GetCvars_handleString(float f, .string field, string name)
428 {
429         if(f < 0)
430         {
431                 if(self.field)
432                         strunzone(self.field);
433         }
434         else if(f > 0)
435         {
436                 if(argv(f) == name)
437                 {
438                         if(self.field)
439                                 strunzone(self.field);
440                         self.field = strzone(argv(f + 1));
441                 }
442         }
443         else
444                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
445 }
446 void GetCvars_handleFloat(float f, .float field, string name)
447 {
448         if(f < 0)
449         {
450         }
451         else if(f > 0)
452         {
453                 if(argv(f) == name)
454                         self.field = stof(argv(f + 1));
455         }
456         else
457                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
458 }
459 void GetCvars(float f)
460 {
461         GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
462         GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
463         GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
464         GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
465         GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
466         GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
467         GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
468         GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
469         GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
470 }
471
472 float fexists(string f)
473 {
474         float fh;
475         fh = fopen(f, FILE_READ);
476         if(fh < 0)
477                 return FALSE;
478         fclose(fh);
479         return TRUE;
480 }
481
482 void backtrace(string msg)
483 {
484         float dev;
485         dev = cvar("developer");
486         cvar_set("developer", "1");
487         dprint("\n");
488         dprint("--- CUT HERE ---\nWARNING: ");
489         dprint(msg);
490         dprint("\n");
491         remove(world); // isn't there any better way to cause a backtrace?
492         dprint("\n--- CUT UNTIL HERE ---\n");
493         cvar_set("developer", ftos(dev));
494 }
495
496 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
497 {
498         float f;
499         float d;
500         float nTeam;
501         entity head;
502
503         if(!teams_matter)
504                 return;
505
506         //if(p.frags < 0)
507         //{
508         //      p.frags = 0; // do not harm the new team!
509         //      return; // won't distribute negative scores
510         //}
511
512         if(p.frags == -666)
513                 return;
514
515         f = ceil(factor * p.frags);
516         p.frags = p.frags - f;
517
518         nTeam = 0;
519         FOR_EACH_PLAYER(head)
520                 if(head != p)
521                         if(head.team == targetteam)
522                                 nTeam = nTeam + 1;
523
524         if(nTeam == 0)
525                 return;
526
527         FOR_EACH_PLAYER(head)
528                 if(head != p)
529                         if(head.team == targetteam)
530                         {
531                                 d = floor(f / nTeam);
532                                 head.frags = head.frags + d;
533                                 f = f - d;
534                                 nTeam = nTeam - 1;
535                         }
536
537         if(nTeam != 0)
538                 error("nPlayers in team changed!");
539         if(f != 0)
540                 error(strcat("There were ", ftos(f), " frags left. BAD!"));
541 }
542
543 string Team_ColorCode(float teamid)
544 {
545         if(teamid == COLOR_TEAM1)
546                 return "^1";
547         else if(teamid == COLOR_TEAM2)
548                 return "^4";
549         else if(teamid == COLOR_TEAM3)
550                 return "^6";
551         else if(teamid == COLOR_TEAM4)
552                 return "^3";
553         else
554                 return "^7";
555 }
556
557 string decolorize(string s)
558 {
559         string out;
560         out = "";
561         while(s != "")
562         {
563                 float n;
564                 string ch1, ch2;
565                 n = 1;
566                 ch1 = substring(s, 0, 1);
567                 ch2 = substring(s, 1, 1);
568                 if(ch1 == "^")
569                 {
570                         n = 2;
571                         if(ch2 == "^")
572                                 out = strcat(out, "^^");
573                         else if(ch2 == "0")
574                                 out = strcat1(out);
575                         else if(ch2 == "1")
576                                 out = strcat1(out);
577                         else if(ch2 == "2")
578                                 out = strcat1(out);
579                         else if(ch2 == "3")
580                                 out = strcat1(out);
581                         else if(ch2 == "4")
582                                 out = strcat1(out);
583                         else if(ch2 == "5")
584                                 out = strcat1(out);
585                         else if(ch2 == "6")
586                                 out = strcat1(out);
587                         else if(ch2 == "7")
588                                 out = strcat1(out);
589                         else if(ch2 == "8")
590                                 out = strcat1(out);
591                         else if(ch2 == "9")
592                                 out = strcat1(out);
593                         else
594                         {
595                                 n = 1;
596                                 out = strcat(out, "^^");
597                         }
598                         s = substring(s, n, strlen(s) - n);
599                 }
600                 else
601                 {
602                         s = substring(s, 1, strlen(s) - 1);
603                         out = strcat(out, ch1);
604                 }
605         }
606         return out;
607 }
608 #define strdecolorize(s) decolorize(s)
609 #define strlennocol(s) strlen(decolorize(s))
610
611 #define CENTERPRIO_POINT 1
612 #define CENTERPRIO_REBALANCE 2
613 #define CENTERPRIO_VOTE 4
614 #define CENTERPRIO_NORMAL 5
615 #define CENTERPRIO_MAPVOTE 9
616 #define CENTERPRIO_ADMIN 99
617 .float centerprint_priority;
618 .float centerprint_expires;
619 void centerprint_atprio(entity e, float prio, string s)
620 {
621         if(intermission_running)
622                 if(prio < CENTERPRIO_MAPVOTE)
623                         return;
624         if(time > e.centerprint_expires)
625                 e.centerprint_priority = 0;
626         if(prio >= e.centerprint_priority)
627         {
628                 e.centerprint_priority = prio;
629                 e.centerprint_expires = time + e.cvar_scr_centertime;
630                 centerprint_builtin(e, s);
631         }
632 }
633 void centerprint_expire(entity e, float prio)
634 {
635         if(prio == e.centerprint_priority)
636         {
637                 e.centerprint_priority = 0;
638                 centerprint_builtin(e, "");
639         }
640 }
641 void centerprint(entity e, string s)
642 {
643         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
644 }
645
646 void VoteNag();
647
648 // decolorizes and team colors the player name when needed
649 string playername(entity p)
650 {
651         string t;
652         if(teams_matter && !intermission_running && p.classname == "player")
653         {
654                 t = Team_ColorCode(p.team);
655                 return strcat(t, strdecolorize(p.netname));
656         }
657         else
658                 return p.netname;
659 }
660
661 // requires that m2>m1 in all coordinates, and that m4>m3
662 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
664 // requires the same, but is a stronger condition
665 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
667 float g_pickup_shells;
668 float g_pickup_shells_max;
669 float g_pickup_nails;
670 float g_pickup_nails_max;
671 float g_pickup_rockets;
672 float g_pickup_rockets_max;
673 float g_pickup_cells;
674 float g_pickup_cells_max;
675 float g_pickup_armorshard;
676 float g_pickup_armorshard_max;
677 float g_pickup_armor;
678 float g_pickup_armor_max;
679 float g_pickup_healthshard;
680 float g_pickup_healthshard_max;
681 float g_pickup_health;
682 float g_pickup_health_max;
683 float g_pickup_healthmega;
684 float g_pickup_healthmega_max;
685
686 void readlevelcvars(void)
687 {
688         g_pickup_shells                    = cvar("g_pickup_shells");
689         g_pickup_shells_max                = cvar("g_pickup_shells_max");
690         g_pickup_nails                     = cvar("g_pickup_nails");
691         g_pickup_nails_max                 = cvar("g_pickup_nails_max");
692         g_pickup_rockets                   = cvar("g_pickup_rockets");
693         g_pickup_rockets_max               = cvar("g_pickup_rockets_max");
694         g_pickup_cells                     = cvar("g_pickup_cells");
695         g_pickup_cells_max                 = cvar("g_pickup_cells_max");
696         g_pickup_armorshard                = cvar("g_pickup_armorshard");
697         g_pickup_armorshard_max            = cvar("g_pickup_armorshard_max");
698         g_pickup_armor                     = cvar("g_pickup_armor");
699         g_pickup_armor_max                 = cvar("g_pickup_armor_max");
700         g_pickup_healthshard               = cvar("g_pickup_healthshard");
701         g_pickup_healthshard_max           = cvar("g_pickup_healthshard_max");
702         g_pickup_health                    = cvar("g_pickup_health");
703         g_pickup_health_max                = cvar("g_pickup_health_max");
704         g_pickup_healthmega                = cvar("g_pickup_healthmega");
705         g_pickup_healthmega_max            = cvar("g_pickup_healthmega_max");
706 }