]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
- now using a fresh dpextensions.qc
[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         // nudge off the floor
100         setorigin(self, self.origin + '0 0 1');
101
102         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
103         if (trace_startsolid)
104         {
105                 objerror("player spawn point in solid, mapper sucks!\n");
106                 return;
107         }
108 }
109
110 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
111 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
112 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
113 // BE CONSTANT OR strzoneD!
114 float(string haystack, string needle, float offset) strstr =
115 {
116         float len, endpos;
117         string found;
118         len = strlen(needle);
119         endpos = strlen(haystack) - len;
120         while(offset <= endpos)
121         {
122                 found = substring(haystack, offset, len);
123                 if(found == needle)
124                         return offset;
125                 offset = offset + 1;
126         }
127         return -1;
128 }
129
130 float NUM_NEAREST_ENTITIES = 4;
131 entity nearest_entity[NUM_NEAREST_ENTITIES];
132 float nearest_length[NUM_NEAREST_ENTITIES];
133 entity(vector point, .string field, string value, vector axismod) findnearest =
134 {
135         entity localhead;
136         float i;
137         float j;
138         float len;
139         vector dist;
140
141         float num_nearest;
142         num_nearest = 0;
143
144         localhead = find(world, field, value);
145         while(localhead)
146         {
147                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
148                         dist = localhead.oldorigin;
149                 else
150                         dist = localhead.origin;
151                 dist = dist - point;
152                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
153                 len = vlen(dist);
154
155                 for(i = 0; i < num_nearest; ++i)
156                 {
157                         if(len < nearest_length[i])
158                                 break;
159                 }
160
161                 // now i tells us where to insert at
162                 //   INSERTION SORT! YOU'VE SEEN IT! RUN!
163                 if(i < NUM_NEAREST_ENTITIES)
164                 {
165                         for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
166                         {
167                                 nearest_length[j + 1] = nearest_length[j];
168                                 nearest_entity[j + 1] = nearest_entity[j];
169                         }
170                         nearest_length[i] = len;
171                         nearest_entity[i] = localhead;
172                         if(num_nearest < NUM_NEAREST_ENTITIES)
173                                 num_nearest = num_nearest + 1;
174                 }
175
176                 localhead = find(localhead, field, value);
177         }
178
179         // now use the first one from our list that we can see
180         for(i = 0; i < num_nearest; ++i)
181         {
182                 traceline(point, nearest_entity[i].origin, TRUE, world);
183                 if(trace_fraction == 1)
184                 {
185                         if(i != 0)
186                         {
187                                 dprint("Nearest point (");
188                                 dprint(nearest_entity[0].netname);
189                                 dprint(") is not visible, using a visible one.\n");
190                         }
191                         return nearest_entity[i];
192                 }
193         }
194
195         if(num_nearest == 0)
196                 return world;
197
198         dprint("Not seeing any location point, using nearest as fallback.\n");
199         /* DEBUGGING CODE:
200         dprint("Candidates were: ");
201         for(j = 0; j < num_nearest; ++j)
202         {
203                 if(j != 0)
204                         dprint(", ");
205                 dprint(nearest_entity[j].netname);
206         }
207         dprint("\n");
208         */
209
210         return nearest_entity[0];
211 }
212
213 void() target_location =
214 {
215         self.classname = "target_location";
216         // location name in netname
217         // eventually support: count, teamgame selectors, line of sight?
218 };
219
220 void() info_location =
221 {
222         self.classname = "target_location";
223         self.message = self.netname;
224 };
225
226 string NearestLocation(vector p)
227 {
228         entity loc;
229         string ret;
230         ret = "somewhere";
231         loc = findnearest(p, classname, "target_location", '1 1 1');
232         if(loc)
233         {
234                 ret = loc.message;
235         }
236         else
237         {
238                 loc = findnearest(p, target, "###item###", '1 1 4');
239                 if(loc)
240                         ret = loc.netname;
241         }
242         return ret;
243 }
244
245 string(string msg) formatmessage =
246 {
247         float p;
248         float n;
249         string msg_save;
250         string escape;
251         string replacement;
252         msg_save = strzone(msg);
253         p = 0;
254         n = 7;
255         while(1)
256         {
257                 if(n < 1)
258                         break; // too many replacements
259                 n = n - 1;
260                 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
261                 if(p < 0)
262                         break;
263                 replacement = substring(msg_save, p, 2);
264                 escape = substring(msg_save, p + 1, 1);
265                 if(escape == "%")
266                         replacement = "%";
267                 else if(escape == "a")
268                         replacement = ftos(floor(self.armorvalue));
269                 else if(escape == "h")
270                         replacement = ftos(floor(self.health));
271                 else if(escape == "l")
272                         replacement = NearestLocation(self.origin);
273                 else if(escape == "y")
274                         replacement = NearestLocation(self.cursor_trace_endpos);
275                 else if(escape == "d")
276                         replacement = NearestLocation(self.death_origin);
277                 else if(escape == "w")
278                 {
279                         float wep;
280                         wep = self.weapon;
281                         if(!wep)
282                                 wep = self.switchweapon;
283                         if(!wep)
284                                 wep = self.cnt;
285                         replacement = W_Name(wep);
286                 }
287                 else if(escape == "W")
288                 {
289                         if(self.items & IT_SHELLS) replacement = "shells";
290                         else if(self.items & IT_NAILS) replacement = "bullets";
291                         else if(self.items & IT_ROCKETS) replacement = "rockets";
292                         else if(self.items & IT_CELLS) replacement = "cells";
293                         else replacement = "batteries"; // ;)
294                 }
295                 else if(escape == "x")
296                 {
297                         replacement = self.cursor_trace_ent.netname;
298                         if(!replacement || !self.cursor_trace_ent)
299                                 replacement = "nothing";
300                 }
301                 else if(escape == "p")
302                 {
303                         if(self.last_selected_player)
304                                 replacement = self.last_selected_player.netname;
305                         else
306                                 replacement = "(nobody)";
307                 }
308                 msg = strcat(substring(msg_save, 0, p), replacement);
309                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
310                 strunzone(msg_save);
311                 msg_save = strzone(msg);
312                 p = p + 2;
313         }
314         msg = strcat(msg_save, "");
315         strunzone(msg_save);
316         return msg;
317 }
318
319 /*
320 =============
321 GetCvars
322 =============
323 Called with:
324   0:  sends the request
325   >0: receives a cvar from name=argv(f) value=argv(f+1)
326 */
327 void GetCvars_handleString(float f, .string field, string name)
328 {
329         if(f < 0)
330         {
331                 if(self.field)
332                         strunzone(self.field);
333         }
334         else if(f > 0)
335         {
336                 if(argv(f) == name)
337                 {
338                         if(self.field)
339                                 strunzone(self.field);
340                         self.field = strzone(argv(f + 1));
341                 }
342         }
343         else
344                 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
345 }
346 void GetCvars_handleFloat(float f, .float field, string name)
347 {
348         if(f < 0)
349         {
350         }
351         else if(f > 0)
352         {
353                 if(argv(f) == name)
354                         self.field = stof(argv(f + 1));
355         }
356         else
357                 stuffcmd(self, strcat("cmd reportcvar ", name, " $", name, "\n"));
358 }
359 void GetCvars(float f)
360 {
361         GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
362         GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
363         GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
364         GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
365         GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
366 }
367
368 float fexists(string f)
369 {
370         float fh;
371         fh = fopen(f, FILE_READ);
372         if(fh < 0)
373                 return FALSE;
374         fclose(fh);
375         return TRUE;
376 }
377
378 void backtrace(string msg)
379 {
380         float dev;
381         dev = cvar("developer");
382         cvar_set("developer", "1");
383         dprint("\n");
384         dprint("--- CUT HERE ---\nWARNING: ");
385         dprint(msg);
386         dprint("\n");
387         remove(world); // isn't there any better way to cause a backtrace?
388         dprint("\n--- CUT UNTIL HERE ---\n");
389         cvar_set("developer", ftos(dev));
390 }
391
392 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
393 {
394         float f;
395         float d;
396         float nTeam;
397         entity head;
398
399         if(!teams_matter)
400                 return;
401
402         //if(p.frags < 0)
403         //{
404         //      p.frags = 0; // do not harm the new team!
405         //      return; // won't distribute negative scores
406         //}
407
408         if(p.frags == -666)
409                 return;
410
411         f = ceil(factor * p.frags);
412         p.frags = p.frags - f;
413
414         nTeam = 0;
415         FOR_EACH_PLAYER(head)
416                 if(head != p)
417                         if(head.team == targetteam)
418                                 nTeam = nTeam + 1;
419
420         if(nTeam == 0)
421                 return;
422
423         FOR_EACH_PLAYER(head)
424                 if(head != p)
425                         if(head.team == targetteam)
426                         {
427                                 d = floor(f / nTeam);
428                                 head.frags = head.frags + d;
429                                 f = f - d;
430                                 nTeam = nTeam - 1;
431                         }
432
433         if(nTeam != 0)
434                 error("nPlayers in team changed!");
435         if(f != 0)
436                 error(strcat("There were ", ftos(f), " frags left. BAD!"));
437 }
438
439 string Team_ColorCode(float teamid)
440 {
441         if(teamid == COLOR_TEAM1)
442                 return "^1";
443         else if(teamid == COLOR_TEAM2)
444                 return "^4";
445         else if(teamid == COLOR_TEAM3)
446                 return "^6";
447         else if(teamid == COLOR_TEAM4)
448                 return "^3";
449         else
450                 return "^7";
451 }
452
453 #define CENTERPRIO_POINT 1
454 #define CENTERPRIO_VOTE 4
455 #define CENTERPRIO_NORMAL 5
456 #define CENTERPRIO_MAPVOTE 9
457 .float centerprint_priority;
458 .float centerprint_expires;
459 void centerprint_atprio(entity e, float prio, string s)
460 {
461         if(intermission_running)
462                 if(prio < CENTERPRIO_MAPVOTE)
463                         return;
464         if(time > e.centerprint_expires)
465                 e.centerprint_priority = 0;
466         if(prio >= e.centerprint_priority)
467         {
468                 e.centerprint_priority = prio;
469                 e.centerprint_expires = time + e.cvar_scr_centertime;
470                 centerprint_builtin(e, s);
471         }
472 }
473 void centerprint_expire(entity e, float prio)
474 {
475         if(prio == e.centerprint_priority)
476         {
477                 e.centerprint_priority = 0;
478                 centerprint_builtin(e, "");
479         }
480 }
481 void centerprint(entity e, string s)
482 {
483         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
484 }
485
486 void VoteNag();
487
488 // decolorizes and team colors the player name when needed
489 string playername(entity p)
490 {
491         string t;
492         if(teams_matter && !intermission_running && p.classname == "player")
493         {
494                 t = Team_ColorCode(p.team);
495                 return strcat(t, strdecolorize(p.netname));
496         }
497         else
498                 return p.netname;
499 }