]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
NO MORE TDEATH :)
[divverent/nexuiz.git] / data / qcsrc / server / miscfunctions.qc
1 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
2 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
3 string STR_PLAYER = "player";
4 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
5 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
6
7 // change that to actually calling strcat when running on an engine without
8 // unlimited tempstrings:
9 // string strcat1(string s) = #115; // FRIK_FILE
10 #define strcat1(s) (s)
11
12 float logfile_open;
13 float logfile;
14
15 void(string s) bcenterprint
16 {
17         // TODO replace by MSG_ALL (would show it to spectators too, though)?
18         entity head;
19         FOR_EACH_PLAYER(head)
20                 if(clienttype(head) == CLIENTTYPE_REAL)
21                         centerprint(head, s);
22 }
23
24 void(string s, float check_dangerous) ServerConsoleEcho =
25 {
26         local string ch;
27         if (checkextension("DP_SV_PRINT"))
28                 print(s, "\n");
29         else
30         {
31                 localcmd("echo \"");
32                 if(check_dangerous)
33                 {
34                         while(strlen(s))
35                         {
36                                 ch = substring(s, 0, 1);
37                                 if(ch != "\"" && ch != "\r" && ch != "\n")
38                                         localcmd(ch);
39                                 s = substring(s, 1, strlen(s) - 1);
40                         }
41                 }
42                 else
43                 {
44                         localcmd(s);
45                 }
46                 localcmd("\"\n");
47         }
48 }
49
50 void(string s, float check_dangerous) GameLogEcho =
51 {
52         string fn;
53         float matches;
54
55         if(cvar("sv_eventlog_files"))
56         {
57                 if(!logfile_open)
58                 {
59                         logfile_open = TRUE;
60                         matches = cvar("sv_eventlog_files_counter") + 1;
61                         cvar_set("sv_eventlog_files_counter", ftos(matches));
62                         fn = ftos(matches);
63                         if(strlen(fn) < 8)
64                                 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
65                         fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
66                         logfile = fopen(fn, FILE_APPEND);
67                 }
68                 if(logfile >= 0)
69                         fputs(logfile, strcat(s, "\n"));
70         }
71         if(cvar("sv_eventlog_console"))
72         {
73                 ServerConsoleEcho(s, check_dangerous);
74         }
75 }
76
77 void() GameLogInit =
78 {
79         logfile_open = 0;
80         // will be opened later
81 }
82
83 void() GameLogClose =
84 {
85         if(logfile_open && logfile >= 0)
86         {
87                 fclose(logfile);
88                 logfile = -1;
89         }
90 }
91
92 float math_mod(float a, float b)
93 {
94         return a - (floor(a / b) * b);
95 }
96
97 void relocate_spawnpoint()
98 {
99         vector o;
100         o = self.origin;
101
102         setsize(self, PL_MIN, PL_MAX);
103
104         // nudge off the floor
105         setorigin(self, self.origin + '0 0 1');
106
107         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
108         if (trace_startsolid)
109         {
110                 objerror("player spawn point in solid, mapper sucks!\n");
111                 return;
112         }
113
114         // drop the spawn point onto the floor
115         // NOTE: a later version might not do this any more
116         droptofloor();
117 }
118
119 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
120 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
121 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
122 // BE CONSTANT OR strzoneD!
123 float(string haystack, string needle, float offset) strstr =
124 {
125         float len, endpos;
126         string found;
127         len = strlen(needle);
128         endpos = strlen(haystack) - len;
129         while(offset <= endpos)
130         {
131                 found = substring(haystack, offset, len);
132                 if(found == needle)
133                         return offset;
134                 offset = offset + 1;
135         }
136         return -1;
137 }
138
139 float NUM_NEAREST_ENTITIES = 4;
140 entity nearest_entity[NUM_NEAREST_ENTITIES];
141 float nearest_length[NUM_NEAREST_ENTITIES];
142 entity(vector point, .string field, string value, vector axismod) findnearest =
143 {
144         entity localhead;
145         float i;
146         float j;
147         float len;
148         vector dist;
149
150         float num_nearest;
151         num_nearest = 0;
152
153         localhead = find(world, field, value);
154         while(localhead)
155         {
156                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
157                         dist = localhead.oldorigin;
158                 else
159                         dist = localhead.origin;
160                 dist = dist - point;
161                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
162                 len = vlen(dist);
163
164                 for(i = 0; i < num_nearest; ++i)
165                 {
166                         if(len < nearest_length[i])
167                                 break;
168                 }
169
170                 // now i tells us where to insert at
171                 //   INSERTION SORT! YOU'VE SEEN IT! RUN!
172                 if(i < NUM_NEAREST_ENTITIES)
173                 {
174                         for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
175                         {
176                                 nearest_length[j + 1] = nearest_length[j];
177                                 nearest_entity[j + 1] = nearest_entity[j];
178                         }
179                         nearest_length[i] = len;
180                         nearest_entity[i] = localhead;
181                         if(num_nearest < NUM_NEAREST_ENTITIES)
182                                 num_nearest = num_nearest + 1;
183                 }
184
185                 localhead = find(localhead, field, value);
186         }
187
188         // now use the first one from our list that we can see
189         for(i = 0; i < num_nearest; ++i)
190         {
191                 traceline(point, nearest_entity[i].origin, TRUE, world);
192                 if(trace_fraction == 1)
193                 {
194                         if(i != 0)
195                         {
196                                 dprint("Nearest point (");
197                                 dprint(nearest_entity[0].netname);
198                                 dprint(") is not visible, using a visible one.\n");
199                         }
200                         return nearest_entity[i];
201                 }
202         }
203
204         if(num_nearest == 0)
205                 return world;
206
207         dprint("Not seeing any location point, using nearest as fallback.\n");
208         /* DEBUGGING CODE:
209         dprint("Candidates were: ");
210         for(j = 0; j < num_nearest; ++j)
211         {
212                 if(j != 0)
213                         dprint(", ");
214                 dprint(nearest_entity[j].netname);
215         }
216         dprint("\n");
217         */
218
219         return nearest_entity[0];
220 }
221
222 void() target_location =
223 {
224         self.classname = "target_location";
225         // location name in netname
226         // eventually support: count, teamgame selectors, line of sight?
227 };
228
229 void() info_location =
230 {
231         self.classname = "target_location";
232         self.message = self.netname;
233 };
234
235 string NearestLocation(vector p)
236 {
237         entity loc;
238         string ret;
239         ret = "somewhere";
240         loc = findnearest(p, classname, "target_location", '1 1 1');
241         if(loc)
242         {
243                 ret = loc.message;
244         }
245         else
246         {
247                 loc = findnearest(p, target, "###item###", '1 1 4');
248                 if(loc)
249                         ret = loc.netname;
250         }
251         return ret;
252 }
253
254 string(string msg) formatmessage =
255 {
256         float p;
257         float n;
258         string msg_save;
259         string escape;
260         string replacement;
261         msg_save = strzone(msg);
262         p = 0;
263         n = 7;
264         while(1)
265         {
266                 if(n < 1)
267                         break; // too many replacements
268                 n = n - 1;
269                 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
270                 if(p < 0)
271                         break;
272                 replacement = substring(msg_save, p, 2);
273                 escape = substring(msg_save, p + 1, 1);
274                 if(escape == "%")
275                         replacement = "%";
276                 else if(escape == "a")
277                         replacement = ftos(floor(self.armorvalue));
278                 else if(escape == "h")
279                         replacement = ftos(floor(self.health));
280                 else if(escape == "l")
281                         replacement = NearestLocation(self.origin);
282                 else if(escape == "y")
283                         replacement = NearestLocation(self.cursor_trace_endpos);
284                 else if(escape == "d")
285                         replacement = NearestLocation(self.death_origin);
286                 else if(escape == "w")
287                 {
288                         float wep;
289                         wep = self.weapon;
290                         if(!wep)
291                                 wep = self.switchweapon;
292                         if(!wep)
293                                 wep = self.cnt;
294                         replacement = W_Name(wep);
295                 }
296                 else if(escape == "W")
297                 {
298                         if(self.items & IT_SHELLS) replacement = "shells";
299                         else if(self.items & IT_NAILS) replacement = "bullets";
300                         else if(self.items & IT_ROCKETS) replacement = "rockets";
301                         else if(self.items & IT_CELLS) replacement = "cells";
302                         else replacement = "batteries"; // ;)
303                 }
304                 else if(escape == "x")
305                 {
306                         replacement = self.cursor_trace_ent.netname;
307                         if(!replacement || !self.cursor_trace_ent)
308                                 replacement = "nothing";
309                 }
310                 else if(escape == "p")
311                 {
312                         if(self.last_selected_player)
313                                 replacement = self.last_selected_player.netname;
314                         else
315                                 replacement = "(nobody)";
316                 }
317                 msg = strcat(substring(msg_save, 0, p), replacement);
318                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
319                 strunzone(msg_save);
320                 msg_save = strzone(msg);
321                 p = p + 2;
322         }
323         msg = strcat(msg_save, "");
324         strunzone(msg_save);
325         return msg;
326 }
327
328 /*
329 =============
330 GetCvars
331 =============
332 Called with:
333   0:  sends the request
334   >0: receives a cvar from name=argv(f) value=argv(f+1)
335 */
336 void GetCvars_handleString(float f, .string field, string name)
337 {
338         if(f < 0)
339         {
340                 if(self.field)
341                         strunzone(self.field);
342         }
343         else if(f > 0)
344         {
345                 if(argv(f) == name)
346                 {
347                         if(self.field)
348                                 strunzone(self.field);
349                         self.field = strzone(argv(f + 1));
350                 }
351         }
352         else
353                 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
354 }
355 void GetCvars_handleFloat(float f, .float field, string name)
356 {
357         if(f < 0)
358         {
359         }
360         else if(f > 0)
361         {
362                 if(argv(f) == name)
363                         self.field = stof(argv(f + 1));
364         }
365         else
366                 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
367 }
368 void GetCvars(float f)
369 {
370         GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
371         GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
372         GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
373         GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
374         GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
375 }
376
377 float fexists(string f)
378 {
379         float fh;
380         fh = fopen(f, FILE_READ);
381         if(fh < 0)
382                 return FALSE;
383         fclose(fh);
384         return TRUE;
385 }
386
387 void backtrace(string msg)
388 {
389         float dev;
390         dev = cvar("developer");
391         cvar_set("developer", "1");
392         dprint("\n");
393         dprint("--- CUT HERE ---\nWARNING: ");
394         dprint(msg);
395         dprint("\n");
396         remove(world); // isn't there any better way to cause a backtrace?
397         dprint("\n--- CUT UNTIL HERE ---\n");
398         cvar_set("developer", ftos(dev));
399 }
400
401 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
402 {
403         float f;
404         float d;
405         float nTeam;
406         entity head;
407
408         if(!teams_matter)
409                 return;
410
411         //if(p.frags < 0)
412         //{
413         //      p.frags = 0; // do not harm the new team!
414         //      return; // won't distribute negative scores
415         //}
416
417         if(p.frags == -666)
418                 return;
419
420         f = ceil(factor * p.frags);
421         p.frags = p.frags - f;
422
423         nTeam = 0;
424         FOR_EACH_PLAYER(head)
425                 if(head != p)
426                         if(head.team == targetteam)
427                                 nTeam = nTeam + 1;
428
429         if(nTeam == 0)
430                 return;
431
432         FOR_EACH_PLAYER(head)
433                 if(head != p)
434                         if(head.team == targetteam)
435                         {
436                                 d = floor(f / nTeam);
437                                 head.frags = head.frags + d;
438                                 f = f - d;
439                                 nTeam = nTeam - 1;
440                         }
441
442         if(nTeam != 0)
443                 error("nPlayers in team changed!");
444         if(f != 0)
445                 error(strcat("There were ", ftos(f), " frags left. BAD!"));
446 }
447
448 string Team_ColorCode(float teamid)
449 {
450         if(teamid == COLOR_TEAM1)
451                 return "^1";
452         else if(teamid == COLOR_TEAM2)
453                 return "^4";
454         else if(teamid == COLOR_TEAM3)
455                 return "^6";
456         else if(teamid == COLOR_TEAM4)
457                 return "^3";
458         else
459                 return "^7";
460 }
461
462 string decolorize(string s)
463 {
464         string out;
465         out = "";
466         while(s != "")
467         {
468                 float n;
469                 string ch1, ch2;
470                 n = 1;
471                 ch1 = substring(s, 0, 1);
472                 ch2 = substring(s, 1, 1);
473                 if(ch1 == "^")
474                 {
475                         n = 2;
476                         if(ch2 == "^")
477                                 out = strcat(out, "^^");
478                         else if(ch2 == "0")
479                                 out = strcat1(out);
480                         else if(ch2 == "1")
481                                 out = strcat1(out);
482                         else if(ch2 == "2")
483                                 out = strcat1(out);
484                         else if(ch2 == "3")
485                                 out = strcat1(out);
486                         else if(ch2 == "4")
487                                 out = strcat1(out);
488                         else if(ch2 == "5")
489                                 out = strcat1(out);
490                         else if(ch2 == "6")
491                                 out = strcat1(out);
492                         else if(ch2 == "7")
493                                 out = strcat1(out);
494                         else if(ch2 == "8")
495                                 out = strcat1(out);
496                         else if(ch2 == "9")
497                                 out = strcat1(out);
498                         else
499                         {
500                                 n = 1;
501                                 out = strcat(out, "^^");
502                         }
503                         s = substring(s, n, strlen(s) - n);
504                 }
505                 else
506                 {
507                         s = substring(s, 1, strlen(s) - 1);
508                         out = strcat(out, ch1);
509                 }
510         }
511         return out;
512 }
513 #define strdecolorize(s) decolorize(s)
514 #define strlennocol(s) strlen(decolorize(s))
515
516 #define CENTERPRIO_POINT 1
517 #define CENTERPRIO_REBALANCE 2
518 #define CENTERPRIO_VOTE 4
519 #define CENTERPRIO_NORMAL 5
520 #define CENTERPRIO_MAPVOTE 9
521 .float centerprint_priority;
522 .float centerprint_expires;
523 void centerprint_atprio(entity e, float prio, string s)
524 {
525         if(intermission_running)
526                 if(prio < CENTERPRIO_MAPVOTE)
527                         return;
528         if(time > e.centerprint_expires)
529                 e.centerprint_priority = 0;
530         if(prio >= e.centerprint_priority)
531         {
532                 e.centerprint_priority = prio;
533                 e.centerprint_expires = time + e.cvar_scr_centertime;
534                 centerprint_builtin(e, s);
535         }
536 }
537 void centerprint_expire(entity e, float prio)
538 {
539         if(prio == e.centerprint_priority)
540         {
541                 e.centerprint_priority = 0;
542                 centerprint_builtin(e, "");
543         }
544 }
545 void centerprint(entity e, string s)
546 {
547         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
548 }
549
550 void VoteNag();
551
552 // decolorizes and team colors the player name when needed
553 string playername(entity p)
554 {
555         string t;
556         if(teams_matter && !intermission_running && p.classname == "player")
557         {
558                 t = Team_ColorCode(p.team);
559                 return strcat(t, strdecolorize(p.netname));
560         }
561         else
562                 return p.netname;
563 }
564
565 // requires that m2>m1 in all coordinates, and that m4>m3
566 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;};
567
568 // requires the same, but is a stronger condition
569 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;};