]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
weapons now have force settings
[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("sendcvar ", 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("sendcvar ", name, "\n"));
358 }
359 void GetCvars(float f)
360 {
361         GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
362         GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
363         GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
364         GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
365         GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
366         GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
367         GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
368         GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
369         GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
370 }
371
372 float fexists(string f)
373 {
374         float fh;
375         fh = fopen(f, FILE_READ);
376         if(fh < 0)
377                 return FALSE;
378         fclose(fh);
379         return TRUE;
380 }
381
382 void backtrace(string msg)
383 {
384         float dev;
385         dev = cvar("developer");
386         cvar_set("developer", "1");
387         dprint("\n");
388         dprint("--- CUT HERE ---\nWARNING: ");
389         dprint(msg);
390         dprint("\n");
391         remove(world); // isn't there any better way to cause a backtrace?
392         dprint("\n--- CUT UNTIL HERE ---\n");
393         cvar_set("developer", ftos(dev));
394 }
395
396 void DistributeFragsAmongTeam(entity p, float targetteam, float factor)
397 {
398         float f;
399         float d;
400         float nTeam;
401         entity head;
402
403         if(!teams_matter)
404                 return;
405
406         //if(p.frags < 0)
407         //{
408         //      p.frags = 0; // do not harm the new team!
409         //      return; // won't distribute negative scores
410         //}
411
412         if(p.frags == -666)
413                 return;
414
415         f = ceil(factor * p.frags);
416         p.frags = p.frags - f;
417
418         nTeam = 0;
419         FOR_EACH_PLAYER(head)
420                 if(head != p)
421                         if(head.team == targetteam)
422                                 nTeam = nTeam + 1;
423
424         if(nTeam == 0)
425                 return;
426
427         FOR_EACH_PLAYER(head)
428                 if(head != p)
429                         if(head.team == targetteam)
430                         {
431                                 d = floor(f / nTeam);
432                                 head.frags = head.frags + d;
433                                 f = f - d;
434                                 nTeam = nTeam - 1;
435                         }
436
437         if(nTeam != 0)
438                 error("nPlayers in team changed!");
439         if(f != 0)
440                 error(strcat("There were ", ftos(f), " frags left. BAD!"));
441 }
442
443 string Team_ColorCode(float teamid)
444 {
445         if(teamid == COLOR_TEAM1)
446                 return "^1";
447         else if(teamid == COLOR_TEAM2)
448                 return "^4";
449         else if(teamid == COLOR_TEAM3)
450                 return "^6";
451         else if(teamid == COLOR_TEAM4)
452                 return "^3";
453         else
454                 return "^7";
455 }
456
457 string decolorize(string s)
458 {
459         string out;
460         out = "";
461         while(s != "")
462         {
463                 float n;
464                 string ch1, ch2;
465                 n = 1;
466                 ch1 = substring(s, 0, 1);
467                 ch2 = substring(s, 1, 1);
468                 if(ch1 == "^")
469                 {
470                         n = 2;
471                         if(ch2 == "^")
472                                 out = strcat(out, "^^");
473                         else if(ch2 == "0")
474                                 out = strcat1(out);
475                         else if(ch2 == "1")
476                                 out = strcat1(out);
477                         else if(ch2 == "2")
478                                 out = strcat1(out);
479                         else if(ch2 == "3")
480                                 out = strcat1(out);
481                         else if(ch2 == "4")
482                                 out = strcat1(out);
483                         else if(ch2 == "5")
484                                 out = strcat1(out);
485                         else if(ch2 == "6")
486                                 out = strcat1(out);
487                         else if(ch2 == "7")
488                                 out = strcat1(out);
489                         else if(ch2 == "8")
490                                 out = strcat1(out);
491                         else if(ch2 == "9")
492                                 out = strcat1(out);
493                         else
494                         {
495                                 n = 1;
496                                 out = strcat(out, "^^");
497                         }
498                         s = substring(s, n, strlen(s) - n);
499                 }
500                 else
501                 {
502                         s = substring(s, 1, strlen(s) - 1);
503                         out = strcat(out, ch1);
504                 }
505         }
506         return out;
507 }
508 #define strdecolorize(s) decolorize(s)
509 #define strlennocol(s) strlen(decolorize(s))
510
511 #define CENTERPRIO_POINT 1
512 #define CENTERPRIO_REBALANCE 2
513 #define CENTERPRIO_VOTE 4
514 #define CENTERPRIO_NORMAL 5
515 #define CENTERPRIO_MAPVOTE 9
516 .float centerprint_priority;
517 .float centerprint_expires;
518 void centerprint_atprio(entity e, float prio, string s)
519 {
520         if(intermission_running)
521                 if(prio < CENTERPRIO_MAPVOTE)
522                         return;
523         if(time > e.centerprint_expires)
524                 e.centerprint_priority = 0;
525         if(prio >= e.centerprint_priority)
526         {
527                 e.centerprint_priority = prio;
528                 e.centerprint_expires = time + e.cvar_scr_centertime;
529                 centerprint_builtin(e, s);
530         }
531 }
532 void centerprint_expire(entity e, float prio)
533 {
534         if(prio == e.centerprint_priority)
535         {
536                 e.centerprint_priority = 0;
537                 centerprint_builtin(e, "");
538         }
539 }
540 void centerprint(entity e, string s)
541 {
542         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
543 }
544
545 void VoteNag();
546
547 // decolorizes and team colors the player name when needed
548 string playername(entity p)
549 {
550         string t;
551         if(teams_matter && !intermission_running && p.classname == "player")
552         {
553                 t = Team_ColorCode(p.team);
554                 return strcat(t, strdecolorize(p.netname));
555         }
556         else
557                 return p.netname;
558 }
559
560 // requires that m2>m1 in all coordinates, and that m4>m3
561 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;};
562
563 // requires the same, but is a stronger condition
564 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;};
565
566 float g_pickup_shells;
567 float g_pickup_shells_max;
568 float g_pickup_nails;
569 float g_pickup_nails_max;
570 float g_pickup_rockets;
571 float g_pickup_rockets_max;
572 float g_pickup_cells;
573 float g_pickup_cells_max;
574 float g_pickup_armorshard;
575 float g_pickup_armorshard_max;
576 float g_pickup_armor;
577 float g_pickup_armor_max;
578 float g_pickup_healthshard;
579 float g_pickup_healthshard_max;
580 float g_pickup_health;
581 float g_pickup_health_max;
582 float g_pickup_healthmega;
583 float g_pickup_healthmega_max;
584
585 void readlevelcvars(void)
586 {
587         g_pickup_shells                    = cvar("g_pickup_shells");
588         g_pickup_shells_max                = cvar("g_pickup_shells_max");
589         g_pickup_nails                     = cvar("g_pickup_nails");
590         g_pickup_nails_max                 = cvar("g_pickup_nails_max");
591         g_pickup_rockets                   = cvar("g_pickup_rockets");
592         g_pickup_rockets_max               = cvar("g_pickup_rockets_max");
593         g_pickup_cells                     = cvar("g_pickup_cells");
594         g_pickup_cells_max                 = cvar("g_pickup_cells_max");
595         g_pickup_armorshard                = cvar("g_pickup_armorshard");
596         g_pickup_armorshard_max            = cvar("g_pickup_armorshard_max");
597         g_pickup_armor                     = cvar("g_pickup_armor");
598         g_pickup_armor_max                 = cvar("g_pickup_armor_max");
599         g_pickup_healthshard               = cvar("g_pickup_healthshard");
600         g_pickup_healthshard_max           = cvar("g_pickup_healthshard_max");
601         g_pickup_health                    = cvar("g_pickup_health");
602         g_pickup_health_max                = cvar("g_pickup_health_max");
603         g_pickup_healthmega                = cvar("g_pickup_healthmega");
604         g_pickup_healthmega_max            = cvar("g_pickup_healthmega_max");
605 }