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