]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
make the chat message actually work
[divverent/nexuiz.git] / data / qcsrc / server / miscfunctions.qc
1 void() spawnfunc_info_player_deathmatch; // needed for the other spawnpoints
2 void() spawnpoint_use;
3 string ColoredTeamName(float t);
4
5 float RandomSelection_totalweight;
6 float RandomSelection_best_priority;
7 entity RandomSelection_chosen_ent;
8 float RandomSelection_chosen_float;
9 void RandomSelection_Init()
10 {
11         RandomSelection_totalweight = 0;
12         RandomSelection_chosen_ent = world;
13         RandomSelection_chosen_float = 0;
14         RandomSelection_best_priority = -1;
15 }
16 void RandomSelection_Add(entity e, float f, float weight, float priority)
17 {
18         if(priority > RandomSelection_best_priority)
19         {
20                 RandomSelection_best_priority = priority;
21                 RandomSelection_chosen_ent = e;
22                 RandomSelection_chosen_float = f;
23                 RandomSelection_totalweight = weight;
24         }
25         else if(priority == RandomSelection_best_priority)
26         {
27                 RandomSelection_totalweight += weight;
28                 if(random() * RandomSelection_totalweight <= weight)
29                 {
30                         RandomSelection_chosen_ent = e;
31                         RandomSelection_chosen_float = f;
32                 }
33         }
34 }
35
36 float DistributeEvenly_amount;
37 float DistributeEvenly_totalweight;
38 void DistributeEvenly_Init(float amount, float totalweight)
39 {
40         if(DistributeEvenly_amount)
41         {
42                 dprint("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ");
43                 dprint(ftos(DistributeEvenly_totalweight), " left!)\n");
44         }
45         if(totalweight == 0)
46                 DistributeEvenly_amount = 0;
47         else
48                 DistributeEvenly_amount = amount;
49         DistributeEvenly_totalweight = totalweight;
50 }
51 float DistributeEvenly_Get(float weight)
52 {
53         float f;
54         if(weight <= 0)
55                 return 0;
56         f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
57         DistributeEvenly_totalweight -= weight;
58         DistributeEvenly_amount -= f;
59         return f;
60 }
61
62 void move_out_of_solid_expand(entity e, vector by)
63 {
64         float eps = 0.0625;
65         tracebox(e.origin, e.mins - '1 1 1' * eps, e.maxs + '1 1 1' * eps, e.origin + by, MOVE_WORLDONLY, e);
66         if(trace_startsolid)
67                 return;
68         if(trace_fraction < 1)
69         {
70                 // hit something
71                 // adjust origin in the other direction...
72                 e.origin = e.origin - by * (1 - trace_fraction);
73         }
74 }
75
76 void move_out_of_solid(entity e)
77 {
78         vector o, m0, m1;
79
80         o = e.origin;
81         traceline(o, o, MOVE_WORLDONLY, e);
82         if(trace_startsolid)
83         {
84                 dprint("origin is in solid too! (", vtos(o), ")");
85                 return;
86         }
87
88         tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
89         if(!trace_startsolid)
90                 return;
91
92         m0 = e.mins;
93         m1 = e.maxs;
94         e.mins = '0 0 0';
95         e.maxs = '0 0 0';
96         move_out_of_solid_expand(e, '1 0 0' * m0_x); e.mins_x = m0_x;
97         move_out_of_solid_expand(e, '1 0 0' * m1_x); e.maxs_x = m1_x;
98         move_out_of_solid_expand(e, '0 1 0' * m0_y); e.mins_y = m0_y;
99         move_out_of_solid_expand(e, '0 1 0' * m1_y); e.maxs_y = m1_y;
100         move_out_of_solid_expand(e, '0 0 1' * m0_z); e.mins_z = m0_z;
101         move_out_of_solid_expand(e, '0 0 1' * m1_z); e.maxs_z = m1_z;
102         setorigin(e, e.origin);
103
104         tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
105         if(trace_startsolid)
106         {
107                 dprint("could not get out of solid (", vtos(o), ")\n");
108                 return;
109         }
110 }
111
112 string STR_PLAYER = "player";
113
114 #if 0
115 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
116 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
117 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
118 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
119 #else
120 #define FOR_EACH_CLIENTSLOT(v) for(v = world; (v = nextent(v)) && (num_for_edict(v) <= maxclients); )
121 #define FOR_EACH_CLIENT(v) FOR_EACH_CLIENTSLOT(v) if(v.flags & FL_CLIENT)
122 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
123 #define FOR_EACH_PLAYER(v) FOR_EACH_CLIENT(v) if(v.classname == STR_PLAYER)
124 #define FOR_EACH_REALPLAYER(v) FOR_EACH_REALCLIENT(v) if(v.classname == STR_PLAYER)
125 #endif
126
127 // change that to actually calling strcat when running on an engine without
128 // unlimited tempstrings:
129 // string strcat1(string s) = #115; // FRIK_FILE
130 #define strcat1(s) (s)
131
132 float logfile_open;
133 float logfile;
134
135 void bcenterprint(string s)
136 {
137         // TODO replace by MSG_ALL (would show it to spectators too, though)?
138         entity head;
139         FOR_EACH_PLAYER(head)
140                 if(clienttype(head) == CLIENTTYPE_REAL)
141                         centerprint(head, s);
142 }
143
144 void ServerConsoleEcho(string s, float check_dangerous)
145 {
146         local string ch;
147         if (checkextension("DP_SV_PRINT"))
148                 print(s, "\n");
149         else
150         {
151                 localcmd("echo \"");
152                 if(check_dangerous)
153                 {
154                         while(strlen(s))
155                         {
156                                 ch = substring(s, 0, 1);
157                                 if(ch != "\"" && ch != "\r" && ch != "\n")
158                                         localcmd(ch);
159                                 s = substring(s, 1, strlen(s) - 1);
160                         }
161                 }
162                 else
163                 {
164                         localcmd(s);
165                 }
166                 localcmd("\"\n");
167         }
168 }
169
170 void GameLogEcho(string s, float check_dangerous)
171 {
172         string fn;
173         float matches;
174
175         if(cvar("sv_eventlog_files"))
176         {
177                 if(!logfile_open)
178                 {
179                         logfile_open = TRUE;
180                         matches = cvar("sv_eventlog_files_counter") + 1;
181                         cvar_set("sv_eventlog_files_counter", ftos(matches));
182                         fn = ftos(matches);
183                         if(strlen(fn) < 8)
184                                 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
185                         fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
186                         logfile = fopen(fn, FILE_APPEND);
187                         fputs(logfile, ":logversion:2\n");
188                 }
189                 if(logfile >= 0)
190                 {
191                         if(cvar("sv_eventlog_files_timestamps"))
192                                 fputs(logfile, strcat(":time:", strftime(TRUE, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
193                         else
194                                 fputs(logfile, strcat(s, "\n"));
195                 }
196         }
197         if(cvar("sv_eventlog_console"))
198         {
199                 ServerConsoleEcho(s, check_dangerous);
200         }
201 }
202
203 void GameLogInit()
204 {
205         logfile_open = 0;
206         // will be opened later
207 }
208
209 void GameLogClose()
210 {
211         if(logfile_open && logfile >= 0)
212         {
213                 fclose(logfile);
214                 logfile = -1;
215         }
216 }
217
218 float spawnpoint_nag;
219 void relocate_spawnpoint()
220 {
221         // nudge off the floor
222         setorigin(self, self.origin + '0 0 1');
223
224         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
225         if (trace_startsolid)
226         {
227                 vector o;
228                 o = self.origin;
229                 self.mins = PL_MIN;
230                 self.maxs = PL_MAX;
231                 move_out_of_solid(self);
232                 print("^1NOTE: this map needs FIXING. Spawnpoint at ", vtos(o - '0 0 1'));
233                 print(" needs to be moved out of solid, e.g. by '", ftos(self.origin_x - o_x));
234                 print(" ", ftos(self.origin_y - o_y));
235                 print(" ", ftos(self.origin_z - o_z), "'\n");
236                 if(cvar("g_spawnpoints_auto_move_out_of_solid"))
237                 {
238                         if(!spawnpoint_nag)
239                                 print("\{1}^1NOTE: this map needs FIXING (it contains spawnpoints in solid, see server log)\n");
240                         spawnpoint_nag = 1;
241                 }
242                 else
243                 {
244                         self.origin = o;
245                         self.mins = self.maxs = '0 0 0';
246                         objerror("player spawn point in solid, mapper sucks!\n");
247                         return;
248                 }
249         }
250
251         if(cvar("g_spawnpoints_autodrop"))
252         {
253                 setsize(self, PL_MIN, PL_MAX);
254                 droptofloor();
255         }
256
257         self.use = spawnpoint_use;
258         self.team_saved = self.team;
259         if(!self.cnt)
260                 self.cnt = 1;
261
262         if(g_ctf || g_assault || g_onslaught || g_domination)
263         if(self.team)
264                 have_team_spawns = 1;
265 }
266
267 #define strstr strstrofs
268 /*
269 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
270 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
271 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
272 // BE CONSTANT OR strzoneD!
273 float strstr(string haystack, string needle, float offset)
274 {
275         float len, endpos;
276         string found;
277         len = strlen(needle);
278         endpos = strlen(haystack) - len;
279         while(offset <= endpos)
280         {
281                 found = substring(haystack, offset, len);
282                 if(found == needle)
283                         return offset;
284                 offset = offset + 1;
285         }
286         return -1;
287 }
288 */
289
290 float NUM_NEAREST_ENTITIES = 4;
291 entity nearest_entity[NUM_NEAREST_ENTITIES];
292 float nearest_length[NUM_NEAREST_ENTITIES];
293 entity findnearest(vector point, .string field, string value, vector axismod)
294 {
295         entity localhead;
296         float i;
297         float j;
298         float len;
299         vector dist;
300
301         float num_nearest;
302         num_nearest = 0;
303
304         localhead = find(world, field, value);
305         while(localhead)
306         {
307                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
308                         dist = localhead.oldorigin;
309                 else
310                         dist = localhead.origin;
311                 dist = dist - point;
312                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
313                 len = vlen(dist);
314
315                 for(i = 0; i < num_nearest; ++i)
316                 {
317                         if(len < nearest_length[i])
318                                 break;
319                 }
320
321                 // now i tells us where to insert at
322                 //   INSERTION SORT! YOU'VE SEEN IT! RUN!
323                 if(i < NUM_NEAREST_ENTITIES)
324                 {
325                         for(j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
326                         {
327                                 nearest_length[j + 1] = nearest_length[j];
328                                 nearest_entity[j + 1] = nearest_entity[j];
329                         }
330                         nearest_length[i] = len;
331                         nearest_entity[i] = localhead;
332                         if(num_nearest < NUM_NEAREST_ENTITIES)
333                                 num_nearest = num_nearest + 1;
334                 }
335
336                 localhead = find(localhead, field, value);
337         }
338
339         // now use the first one from our list that we can see
340         for(i = 0; i < num_nearest; ++i)
341         {
342                 traceline(point, nearest_entity[i].origin, TRUE, world);
343                 if(trace_fraction == 1)
344                 {
345                         if(i != 0)
346                         {
347                                 dprint("Nearest point (");
348                                 dprint(nearest_entity[0].netname);
349                                 dprint(") is not visible, using a visible one.\n");
350                         }
351                         return nearest_entity[i];
352                 }
353         }
354
355         if(num_nearest == 0)
356                 return world;
357
358         dprint("Not seeing any location point, using nearest as fallback.\n");
359         /* DEBUGGING CODE:
360         dprint("Candidates were: ");
361         for(j = 0; j < num_nearest; ++j)
362         {
363                 if(j != 0)
364                         dprint(", ");
365                 dprint(nearest_entity[j].netname);
366         }
367         dprint("\n");
368         */
369
370         return nearest_entity[0];
371 }
372
373 void spawnfunc_target_location()
374 {
375         self.classname = "target_location";
376         // location name in netname
377         // eventually support: count, teamgame selectors, line of sight?
378 };
379
380 void spawnfunc_info_location()
381 {
382         self.classname = "target_location";
383         self.message = self.netname;
384 };
385
386 string NearestLocation(vector p)
387 {
388         entity loc;
389         string ret;
390         ret = "somewhere";
391         loc = findnearest(p, classname, "target_location", '1 1 1');
392         if(loc)
393         {
394                 ret = loc.message;
395         }
396         else
397         {
398                 loc = findnearest(p, target, "###item###", '1 1 4');
399                 if(loc)
400                         ret = loc.netname;
401         }
402         return ret;
403 }
404
405 string formatmessage(string msg)
406 {
407         float p;
408         float n;
409         string msg_save;
410         string escape;
411         string replacement;
412         msg_save = strzone(msg);
413         p = 0;
414         n = 7;
415         while(1)
416         {
417                 if(n < 1)
418                         break; // too many replacements
419                 n = n - 1;
420                 p = strstr(msg_save, "%", p); // NOTE: this destroys msg as it's a tempstring!
421                 if(p < 0)
422                         break;
423                 replacement = substring(msg_save, p, 2);
424                 escape = substring(msg_save, p + 1, 1);
425                 if(escape == "%")
426                         replacement = "%";
427                 else if(escape == "a")
428                         replacement = ftos(floor(self.armorvalue));
429                 else if(escape == "h")
430                         replacement = ftos(floor(self.health));
431                 else if(escape == "l")
432                         replacement = NearestLocation(self.origin);
433                 else if(escape == "y")
434                         replacement = NearestLocation(self.cursor_trace_endpos);
435                 else if(escape == "d")
436                         replacement = NearestLocation(self.death_origin);
437                 else if(escape == "w")
438                 {
439                         float wep;
440                         wep = self.weapon;
441                         if(!wep)
442                                 wep = self.switchweapon;
443                         if(!wep)
444                                 wep = self.cnt;
445                         replacement = W_Name(wep);
446                 }
447                 else if(escape == "W")
448                 {
449                         if(self.items & IT_SHELLS) replacement = "shells";
450                         else if(self.items & IT_NAILS) replacement = "bullets";
451                         else if(self.items & IT_ROCKETS) replacement = "rockets";
452                         else if(self.items & IT_CELLS) replacement = "cells";
453                         else replacement = "batteries"; // ;)
454                 }
455                 else if(escape == "x")
456                 {
457                         replacement = self.cursor_trace_ent.netname;
458                         if(!replacement || !self.cursor_trace_ent)
459                                 replacement = "nothing";
460                 }
461                 else if(escape == "p")
462                 {
463                         if(self.last_selected_player)
464                                 replacement = self.last_selected_player.netname;
465                         else
466                                 replacement = "(nobody)";
467                 }
468                 msg = strcat(substring(msg_save, 0, p), replacement);
469                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
470                 strunzone(msg_save);
471                 msg_save = strzone(msg);
472                 p = p + 2;
473         }
474         msg = strcat(msg_save, "");
475         strunzone(msg_save);
476         return msg;
477 }
478
479 /*
480 =============
481 GetCvars
482 =============
483 Called with:
484   0:  sends the request
485   >0: receives a cvar from name=argv(f) value=argv(f+1)
486 */
487 void GetCvars_handleString(float f, .string field, string name)
488 {
489         if(f < 0)
490         {
491                 if(self.field)
492                         strunzone(self.field);
493         }
494         else if(f > 0)
495         {
496                 if(argv(f) == name)
497                 {
498                         if(self.field)
499                                 strunzone(self.field);
500                         self.field = strzone(argv(f + 1));
501                 }
502         }
503         else
504                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
505 }
506 void GetCvars_handleFloat(float f, .float field, string name)
507 {
508         if(f < 0)
509         {
510         }
511         else if(f > 0)
512         {
513                 if(argv(f) == name)
514                         self.field = stof(argv(f + 1));
515         }
516         else
517                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
518 }
519 void GetCvars(float f)
520 {
521         GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
522         GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
523         GetCvars_handleFloat(f, cvar_cl_zoomfactor, "cl_zoomfactor");
524         GetCvars_handleFloat(f, cvar_cl_zoomspeed, "cl_zoomspeed");
525         GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
526         GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
527         GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
528         GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
529         GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
530         GetCvars_handleFloat(f, cvar_cl_handicap, "cl_handicap");
531 }
532
533 float fexists(string f)
534 {
535         float fh;
536         fh = fopen(f, FILE_READ);
537         if(fh < 0)
538                 return FALSE;
539         fclose(fh);
540         return TRUE;
541 }
542
543 void backtrace(string msg)
544 {
545         float dev;
546         dev = cvar("developer");
547         cvar_set("developer", "1");
548         dprint("\n");
549         dprint("--- CUT HERE ---\nWARNING: ");
550         dprint(msg);
551         dprint("\n");
552         remove(world); // isn't there any better way to cause a backtrace?
553         dprint("\n--- CUT UNTIL HERE ---\n");
554         cvar_set("developer", ftos(dev));
555 }
556
557 string Team_ColorCode(float teamid)
558 {
559         if(teamid == COLOR_TEAM1)
560                 return "^1";
561         else if(teamid == COLOR_TEAM2)
562                 return "^4";
563         else if(teamid == COLOR_TEAM3)
564                 return "^3";
565         else if(teamid == COLOR_TEAM4)
566                 return "^6";
567         else
568                 return "^7";
569 }
570 string Team_ColorName(float t)
571 {
572         // fixme: Search for team entities and get their .netname's!
573         if(t == COLOR_TEAM1)
574                 return "Red";
575         if(t == COLOR_TEAM2)
576                 return "Blue";
577         if(t == COLOR_TEAM3)
578                 return "Yellow";
579         if(t == COLOR_TEAM4)
580                 return "Pink";
581         return "Neutral";
582 }
583 string Team_ColorNameLowerCase(float t)
584 {
585         // fixme: Search for team entities and get their .netname's!
586         if(t == COLOR_TEAM1)
587                 return "red";
588         if(t == COLOR_TEAM2)
589                 return "blue";
590         if(t == COLOR_TEAM3)
591                 return "yellow";
592         if(t == COLOR_TEAM4)
593                 return "pink";
594         return "neutral";
595 }
596
597 /*
598 string decolorize(string s)
599 {
600         string out;
601         out = "";
602         while(s != "")
603         {
604                 float n;
605                 string ch1, ch2;
606                 n = 1;
607                 ch1 = substring(s, 0, 1);
608                 ch2 = substring(s, 1, 1);
609                 if(ch1 == "^")
610                 {
611                         n = 2;
612                         if(ch2 == "^")
613                                 out = strcat(out, "^^");
614                         else if(ch2 == "0")
615                                 out = strcat1(out);
616                         else if(ch2 == "1")
617                                 out = strcat1(out);
618                         else if(ch2 == "2")
619                                 out = strcat1(out);
620                         else if(ch2 == "3")
621                                 out = strcat1(out);
622                         else if(ch2 == "4")
623                                 out = strcat1(out);
624                         else if(ch2 == "5")
625                                 out = strcat1(out);
626                         else if(ch2 == "6")
627                                 out = strcat1(out);
628                         else if(ch2 == "7")
629                                 out = strcat1(out);
630                         else if(ch2 == "8")
631                                 out = strcat1(out);
632                         else if(ch2 == "9")
633                                 out = strcat1(out);
634                         else
635                         {
636                                 n = 1;
637                                 out = strcat(out, "^^");
638                         }
639                         s = substring(s, n, strlen(s) - n);
640                 }
641                 else
642                 {
643                         s = substring(s, 1, strlen(s) - 1);
644                         out = strcat(out, ch1);
645                 }
646         }
647         return out;
648 }
649 #define strdecolorize(s) decolorize(s)
650 #define strlennocol(s) strlen(decolorize(s))
651 */
652
653 #define CENTERPRIO_POINT 1
654 #define CENTERPRIO_SPAM 2
655 #define CENTERPRIO_REBALANCE 2
656 #define CENTERPRIO_VOTE 4
657 #define CENTERPRIO_NORMAL 5
658 #define CENTERPRIO_MAPVOTE 9
659 #define CENTERPRIO_IDLEKICK 50
660 #define CENTERPRIO_ADMIN 99
661 .float centerprint_priority;
662 .float centerprint_expires;
663 void centerprint_atprio(entity e, float prio, string s)
664 {
665         if(intermission_running)
666                 if(prio < CENTERPRIO_MAPVOTE)
667                         return;
668         if(time > e.centerprint_expires)
669                 e.centerprint_priority = 0;
670         if(prio >= e.centerprint_priority)
671         {
672                 e.centerprint_priority = prio;
673                 if(timeoutStatus == 2)
674                         e.centerprint_expires = time + (e.cvar_scr_centertime * TIMEOUT_SLOWMO_VALUE);
675                 else
676                         e.centerprint_expires = time + e.cvar_scr_centertime;
677                 centerprint_builtin(e, s);
678         }
679 }
680 void centerprint_expire(entity e, float prio)
681 {
682         if(prio == e.centerprint_priority)
683         {
684                 e.centerprint_priority = 0;
685                 centerprint_builtin(e, "");
686         }
687 }
688 void centerprint(entity e, string s)
689 {
690         centerprint_atprio(e, CENTERPRIO_NORMAL, s);
691 }
692
693 void VoteNag();
694
695 // decolorizes and team colors the player name when needed
696 string playername(entity p)
697 {
698         string t;
699         if(teams_matter && !intermission_running && p.classname == "player")
700         {
701                 t = Team_ColorCode(p.team);
702                 return strcat(t, strdecolorize(p.netname));
703         }
704         else
705                 return p.netname;
706 }
707
708 vector randompos(vector m1, vector m2)
709 {
710         local vector v;
711         m2 = m2 - m1;
712         v_x = m2_x * random() + m1_x;
713         v_y = m2_y * random() + m1_y;
714         v_z = m2_z * random() + m1_z;
715         return  v;
716 };
717
718 // requires that m2>m1 in all coordinates, and that m4>m3
719 float boxesoverlap(vector m1, vector m2, vector m3, vector m4) {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;};
720
721 // requires the same, but is a stronger condition
722 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs) {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;};
723
724 float g_pickup_shells;
725 float g_pickup_shells_max;
726 float g_pickup_nails;
727 float g_pickup_nails_max;
728 float g_pickup_rockets;
729 float g_pickup_rockets_max;
730 float g_pickup_cells;
731 float g_pickup_cells_max;
732 float g_pickup_armorsmall;
733 float g_pickup_armorsmall_max;
734 float g_pickup_armormedium;
735 float g_pickup_armormedium_max;
736 float g_pickup_armorlarge;
737 float g_pickup_armorlarge_max;
738 float g_pickup_healthsmall;
739 float g_pickup_healthsmall_max;
740 float g_pickup_healthmedium;
741 float g_pickup_healthmedium_max;
742 float g_pickup_healthlarge;
743 float g_pickup_healthlarge_max;
744 float g_pickup_healthmega;
745 float g_pickup_healthmega_max;
746
747 float start_items;
748 float start_switchweapon;
749 float start_ammo_shells;
750 float start_ammo_nails;
751 float start_ammo_rockets;
752 float start_ammo_cells;
753 float start_health;
754 float start_armorvalue;
755
756 void readlevelcvars(void)
757 {
758         sv_cheats = cvar("sv_cheats");
759         sv_gentle = cvar("sv_gentle");
760         sv_foginterval = cvar("sv_foginterval");
761         g_cloaked = cvar("g_cloaked");
762         g_jump_grunt = cvar("g_jump_grunt");
763         g_footsteps = cvar("g_footsteps");
764         g_grappling_hook = cvar("g_grappling_hook");
765         g_instagib = cvar("g_instagib");
766         g_laserguided_missile = cvar("g_laserguided_missile");
767         g_midair = cvar("g_midair");
768         g_minstagib = cvar("g_minstagib");
769         g_nixnex = cvar("g_nixnex");
770         g_nixnex_with_laser = cvar("g_nixnex_with_laser");
771         g_norecoil = cvar("g_norecoil");
772         g_rocketarena = cvar("g_rocketarena");
773         g_vampire = cvar("g_vampire");
774         g_tourney = cvar("g_tourney");
775         sv_maxidle = cvar("sv_maxidle");
776         sv_maxidle_spectatorsareidle = cvar("sv_maxidle_spectatorsareidle");
777         sv_pogostick = cvar("sv_pogostick");
778         sv_doublejump = cvar("sv_doublejump");
779         g_ctf_win_mode = cvar("g_ctf_win_mode");
780
781         g_pickup_shells                    = cvar("g_pickup_shells");
782         g_pickup_shells_max                = cvar("g_pickup_shells_max");
783         g_pickup_nails                     = cvar("g_pickup_nails");
784         g_pickup_nails_max                 = cvar("g_pickup_nails_max");
785         g_pickup_rockets                   = cvar("g_pickup_rockets");
786         g_pickup_rockets_max               = cvar("g_pickup_rockets_max");
787         g_pickup_cells                     = cvar("g_pickup_cells");
788         g_pickup_cells_max                 = cvar("g_pickup_cells_max");
789         g_pickup_armorsmall                = cvar("g_pickup_armorsmall");
790         g_pickup_armorsmall_max            = cvar("g_pickup_armorsmall_max");
791         g_pickup_armormedium               = cvar("g_pickup_armormedium");
792         g_pickup_armormedium_max           = cvar("g_pickup_armormedium_max");
793         g_pickup_armorlarge                = cvar("g_pickup_armorlarge");
794         g_pickup_armorlarge_max            = cvar("g_pickup_armorlarge_max");
795         g_pickup_healthsmall               = cvar("g_pickup_healthsmall");
796         g_pickup_healthsmall_max           = cvar("g_pickup_healthsmall_max");
797         g_pickup_healthmedium              = cvar("g_pickup_healthmedium");
798         g_pickup_healthmedium_max          = cvar("g_pickup_healthmedium_max");
799         g_pickup_healthlarge               = cvar("g_pickup_healthlarge");
800         g_pickup_healthlarge_max           = cvar("g_pickup_healthlarge_max");
801         g_pickup_healthmega                = cvar("g_pickup_healthmega");
802         g_pickup_healthmega_max            = cvar("g_pickup_healthmega_max");
803
804         // initialize starting values for players
805         start_items = 0;
806         start_switchweapon = 0;
807         start_ammo_shells = 0;
808         start_ammo_nails = 0;
809         start_ammo_rockets = 0;
810         start_ammo_cells = 0;
811         start_health = cvar("g_balance_health_start");
812         start_armorvalue = cvar("g_balance_armor_start");
813
814         if(g_instagib)
815         {
816                 start_items = IT_NEX;
817                 start_switchweapon = WEP_NEX;
818                 weapon_action(start_switchweapon, WR_PRECACHE);
819                 start_ammo_cells = 999;
820         }
821         else if(g_rocketarena)
822         {
823                 start_items = IT_ROCKET_LAUNCHER;
824                 start_switchweapon = WEP_ROCKET_LAUNCHER;
825                 weapon_action(start_switchweapon, WR_PRECACHE);
826                 start_ammo_rockets = 999;
827         }
828         else if(g_nixnex)
829         {
830                 start_items = 0;
831                 // will be done later
832         }
833         else if(g_minstagib)
834         {
835                 start_health = 100;
836                 start_armorvalue = 0;
837                 start_items = IT_NEX;
838                 start_switchweapon = WEP_NEX;
839                 weapon_action(start_switchweapon, WR_PRECACHE);
840                 start_ammo_cells = cvar("g_minstagib_ammo_start");
841                 g_minstagib_invis_alpha = cvar("g_minstagib_invis_alpha");
842         }
843         else
844         {
845                 if(g_lms)
846                 {
847                         start_ammo_shells = cvar("g_lms_start_ammo_shells");
848                         start_ammo_nails = cvar("g_lms_start_ammo_nails");
849                         start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
850                         start_ammo_cells = cvar("g_lms_start_ammo_cells");
851                         start_health = cvar("g_lms_start_health");
852                         start_armorvalue = cvar("g_lms_start_armor");
853                 }
854                 else if(g_tourney) {
855                         start_ammo_shells = cvar("g_tourney_start_ammo_shells");
856                         start_ammo_nails = cvar("g_tourney_start_ammo_nails");
857                         start_ammo_rockets = cvar("g_tourney_start_ammo_rockets");
858                         start_ammo_cells = cvar("g_tourney_start_ammo_cells");
859                         start_health = cvar("g_tourney_start_health");
860                         start_armorvalue = cvar("g_tourney_start_armor");
861                 }
862                 else if (cvar("g_use_ammunition")) {
863                         start_ammo_shells = cvar("g_start_ammo_shells");
864                         start_ammo_nails = cvar("g_start_ammo_nails");
865                         start_ammo_rockets = cvar("g_start_ammo_rockets");
866                         start_ammo_cells = cvar("g_start_ammo_cells");
867                 } else {
868                         start_ammo_shells = cvar("g_pickup_shells_max");
869                         start_ammo_nails = cvar("g_pickup_nails_max");
870                         start_ammo_rockets = cvar("g_pickup_rockets_max");
871                         start_ammo_cells = cvar("g_pickup_cells_max");
872                 }
873
874                 if (cvar("g_start_weapon_laser") || g_lms)
875                 {
876                         start_items = start_items | IT_LASER;
877                         start_switchweapon = WEP_LASER;
878                         weapon_action(start_switchweapon, WR_PRECACHE);
879                 }
880                 if (cvar("g_start_weapon_shotgun") || g_lms)
881                 {
882                         start_items = start_items | IT_SHOTGUN;
883                         start_switchweapon = WEP_SHOTGUN;
884                         weapon_action(start_switchweapon, WR_PRECACHE);
885                 }
886                 if (cvar("g_start_weapon_uzi") || g_lms || g_tourney)
887                 {
888                         start_items = start_items | IT_UZI;
889                         start_switchweapon = WEP_UZI;
890                         weapon_action(start_switchweapon, WR_PRECACHE);
891                 }
892                 if (cvar("g_start_weapon_grenadelauncher") || g_lms || g_tourney)
893                 {
894                         start_items = start_items | IT_GRENADE_LAUNCHER;
895                         start_switchweapon = WEP_GRENADE_LAUNCHER;
896                         weapon_action(start_switchweapon, WR_PRECACHE);
897                 }
898                 if (cvar("g_start_weapon_electro") || g_lms || g_tourney)
899                 {
900                         start_items = start_items | IT_ELECTRO;
901                         start_switchweapon = WEP_ELECTRO;
902                         weapon_action(start_switchweapon, WR_PRECACHE);
903                 }
904                 if (cvar("g_start_weapon_crylink") || g_lms || g_tourney)
905                 {
906                         start_items = start_items | IT_CRYLINK;
907                         start_switchweapon = WEP_CRYLINK;
908                         weapon_action(start_switchweapon, WR_PRECACHE);
909                 }
910                 if (cvar("g_start_weapon_nex") || g_lms || g_tourney)
911                 {
912                         start_items = start_items | IT_NEX;
913                         start_switchweapon = WEP_NEX;
914                         weapon_action(start_switchweapon, WR_PRECACHE);
915                 }
916                 if (cvar("g_start_weapon_hagar") || g_lms || g_tourney)
917                 {
918                         start_items = start_items | IT_HAGAR;
919                         start_switchweapon = WEP_HAGAR;
920                         weapon_action(start_switchweapon, WR_PRECACHE);
921                 }
922                 if (cvar("g_start_weapon_rocketlauncher") || g_lms || g_tourney)
923                 {
924                         start_items = start_items | IT_ROCKET_LAUNCHER;
925                         start_switchweapon = WEP_ROCKET_LAUNCHER;
926                         weapon_action(start_switchweapon, WR_PRECACHE);
927                 }
928         }
929 }
930
931 /*
932 // TODO sound pack system
933 string soundpack;
934
935 string precache_sound_builtin (string s) = #19;
936 void(entity e, float chan, string samp, float vol, float atten) sound_builtin = #8;
937 string precache_sound(string s)
938 {
939         return precache_sound_builtin(strcat(soundpack, s));
940 }
941 void play2(entity e, string filename)
942 {
943         stuffcmd(e, strcat("play2 ", soundpack, filename, "\n"));
944 }
945 void sound(entity e, float chan, string samp, float vol, float atten)
946 {
947         sound_builtin(e, chan, strcat(soundpack, samp), vol, atten);
948 }
949 */
950
951 // Sound functions
952 string precache_sound (string s) = #19;
953 void(entity e, float chan, string samp, float vol, float atten) sound = #8;
954 float precache_sound_index (string s) = #19;
955
956 void soundtoat(float dest, entity e, vector o, float chan, string samp, float vol, float atten)
957 {
958         WriteByte(dest, 6);
959         WriteByte(dest, 27); // all bits except SND_LOOPING
960         WriteByte(dest, vol * 255);
961         WriteByte(dest, atten * 64);
962         WriteEntity(dest, e);
963         WriteByte(dest, chan);
964         WriteShort(dest, precache_sound_index(samp));
965         WriteCoord(dest, o_x);
966         WriteCoord(dest, o_y);
967         WriteCoord(dest, o_z);
968 }
969 void soundto(float dest, entity e, float chan, string samp, float vol, float atten)
970 {
971         vector o;
972         o = e.origin + 0.5 * (e.mins + e.maxs);
973         soundtoat(dest, e, o, chan, samp, vol, atten);
974 }
975 void soundat(entity e, vector o, float chan, string samp, float vol, float atten)
976 {
977         soundtoat(MSG_BROADCAST, e, o, chan, samp, vol, atten);
978 }
979
980 void play2(entity e, string filename)
981 {
982         //stuffcmd(e, strcat("play2 ", filename, "\n"));
983         msg_entity = e;
984         soundtoat(MSG_ONE, world, '0 0 0', CHAN_AUTO, filename, VOL_BASE, ATTN_NONE);
985 }
986
987 .float announcetime;
988 void announce(entity player, string msg)
989 {
990         if(time > player.announcetime)
991         if(clienttype(player) == CLIENTTYPE_REAL)
992         {
993                 player.announcetime = time + 0.3;
994                 play2(player, msg);
995         }
996 }
997
998 void play2team(float t, string filename)
999 {
1000         local entity head;
1001         FOR_EACH_REALPLAYER(head)
1002         {
1003                 if (head.team == t)
1004                         play2(head, filename);
1005         }
1006 }
1007
1008 void play2all(string samp)
1009 {
1010         sound(world, CHAN_AUTO, samp, VOL_BASE, ATTN_NONE);
1011 }
1012
1013 void PrecachePlayerSounds(string f);
1014 void precache_all_models(string pattern)
1015 {
1016         float globhandle, i, n;
1017         string f;
1018
1019         globhandle = search_begin(pattern, TRUE, FALSE);
1020         if(globhandle < 0)
1021                 return;
1022         n = search_getsize(globhandle);
1023         for(i = 0; i < n; ++i)
1024         {
1025                 //print(search_getfilename(globhandle, i), "\n");
1026                 f = search_getfilename(globhandle, i);
1027                 precache_model(f);
1028                 PrecachePlayerSounds(strcat(f, ".sounds"));
1029         }
1030         search_end(globhandle);
1031 }
1032
1033 void precache()
1034 {
1035         // gamemode related things
1036         precache_model ("models/misc/chatbubble.spr");
1037         precache_model ("models/misc/teambubble.spr");
1038         if (g_runematch)
1039         {
1040                 precache_model ("models/runematch/curse.mdl");
1041                 precache_model ("models/runematch/rune.mdl");
1042         }
1043
1044         // Precache all player models if desired
1045         if (cvar("sv_precacheplayermodels"))
1046         {
1047                 PrecachePlayerSounds("sound/player/default.sounds");
1048                 precache_all_models("models/player/*.zym");
1049                 precache_all_models("models/player/*.dpm");
1050                 precache_all_models("models/player/*.md3");
1051                 precache_all_models("models/player/*.psk");
1052                 //precache_model("models/player/carni.zym");
1053                 //precache_model("models/player/crash.zym");
1054                 //precache_model("models/player/grunt.zym");
1055                 //precache_model("models/player/headhunter.zym");
1056                 //precache_model("models/player/insurrectionist.zym");
1057                 //precache_model("models/player/jeandarc.zym");
1058                 //precache_model("models/player/lurk.zym");
1059                 //precache_model("models/player/lycanthrope.zym");
1060                 //precache_model("models/player/marine.zym");
1061                 //precache_model("models/player/nexus.zym");
1062                 //precache_model("models/player/pyria.zym");
1063                 //precache_model("models/player/shock.zym");
1064                 //precache_model("models/player/skadi.zym");
1065                 //precache_model("models/player/specop.zym");
1066                 //precache_model("models/player/visitant.zym");
1067         }
1068
1069         if (g_footsteps)
1070         {
1071                 PrecacheGlobalSound((globalsound_step = "misc/footstep0 6"));
1072                 PrecacheGlobalSound((globalsound_metalstep = "misc/metalfootstep0 6"));
1073         }
1074
1075         // gore and miscellaneous sounds
1076         //precache_sound ("misc/h2ohit.wav");
1077         precache_model ("models/gibs/bloodyskull.md3");
1078         precache_model ("models/gibs/chunk.mdl");
1079         precache_model ("models/gibs/eye.md3");
1080         precache_model ("models/gibs/gib1.md3");
1081         precache_model ("models/gibs/gib2.md3");
1082         precache_model ("models/gibs/gib3.md3");
1083         precache_model ("models/gibs/gib4.md3");
1084         precache_model ("models/gibs/gib5.md3");
1085         precache_model ("models/gibs/gib6.md3");
1086         precache_model ("models/gibs/smallchest.md3");
1087         precache_model ("models/gibs/chest.md3");
1088         precache_model ("models/gibs/arm.md3");
1089         precache_model ("models/gibs/leg1.md3");
1090         precache_model ("models/gibs/leg2.md3");
1091         precache_model ("models/hook.md3");
1092         precache_sound ("misc/armorimpact.wav");
1093         precache_sound ("misc/bodyimpact1.wav");
1094         precache_sound ("misc/bodyimpact2.wav");
1095         precache_sound ("misc/gib.wav");
1096         precache_sound ("misc/gib_splat01.wav");
1097         precache_sound ("misc/gib_splat02.wav");
1098         precache_sound ("misc/gib_splat03.wav");
1099         precache_sound ("misc/gib_splat04.wav");
1100         precache_sound ("misc/hit.wav");
1101         PrecacheGlobalSound((globalsound_fall = "misc/hitground 4"));
1102         PrecacheGlobalSound((globalsound_metalfall = "misc/metalhitground 4"));
1103         precache_sound ("misc/null.wav");
1104         precache_sound ("misc/spawn.wav");
1105         precache_sound ("misc/talk.wav");
1106         precache_sound ("misc/teleport.wav");
1107         precache_sound ("player/lava.wav");
1108         precache_sound ("player/slime.wav");
1109
1110         // announcer sounds - male
1111         //precache_sound ("announcer/male/electrobitch.wav");
1112         precache_sound ("announcer/male/03kills.wav");
1113         precache_sound ("announcer/male/05kills.wav");
1114         precache_sound ("announcer/male/10kills.wav");
1115         precache_sound ("announcer/male/15kills.wav");
1116         precache_sound ("announcer/male/20kills.wav");
1117         precache_sound ("announcer/male/25kills.wav");
1118         precache_sound ("announcer/male/30kills.wav");
1119         precache_sound ("announcer/male/botlike.wav");
1120         precache_sound ("announcer/male/yoda.wav");
1121
1122         // announcer sounds - robotic
1123         precache_sound ("announcer/robotic/prepareforbattle.wav");
1124         precache_sound ("announcer/robotic/begin.wav");
1125         precache_sound ("announcer/robotic/timeoutcalled.wav");
1126         precache_sound ("announcer/robotic/1fragleft.wav");
1127         precache_sound ("announcer/robotic/1minuteremains.wav");
1128         precache_sound ("announcer/robotic/2fragsleft.wav");
1129         precache_sound ("announcer/robotic/3fragsleft.wav");
1130         if (g_minstagib)
1131         {
1132                 precache_sound ("announcer/robotic/lastsecond.wav");
1133                 precache_sound ("announcer/robotic/narrowly.wav");
1134         }
1135
1136         precache_model ("models/sprites/1.spr32");
1137         precache_model ("models/sprites/2.spr32");
1138         precache_model ("models/sprites/3.spr32");
1139         precache_model ("models/sprites/4.spr32");
1140         precache_model ("models/sprites/5.spr32");
1141         precache_model ("models/sprites/6.spr32");
1142         precache_model ("models/sprites/7.spr32");
1143         precache_model ("models/sprites/8.spr32");
1144         precache_model ("models/sprites/9.spr32");
1145         precache_model ("models/sprites/10.spr32");
1146         precache_sound ("announcer/robotic/1.ogg");
1147         precache_sound ("announcer/robotic/2.ogg");
1148         precache_sound ("announcer/robotic/3.ogg");
1149         precache_sound ("announcer/robotic/4.ogg");
1150         precache_sound ("announcer/robotic/5.ogg");
1151         precache_sound ("announcer/robotic/6.ogg");
1152         precache_sound ("announcer/robotic/7.ogg");
1153         precache_sound ("announcer/robotic/8.ogg");
1154         precache_sound ("announcer/robotic/9.ogg");
1155         precache_sound ("announcer/robotic/10.ogg");
1156
1157         // common weapon precaches
1158         precache_sound ("weapons/weapon_switch.wav");
1159         precache_sound ("weapons/weaponpickup.wav");
1160         if (cvar("g_grappling_hook"))
1161         {
1162                 precache_sound ("weapons/hook_fire.wav"); // hook
1163                 precache_sound ("weapons/hook_impact.wav"); // hook
1164         }
1165
1166         if (cvar("sv_precacheweapons") || g_nixnex)
1167         {
1168                 //precache weapon models/sounds
1169                 local float wep;
1170                 wep = WEP_FIRST;
1171                 while (wep <= WEP_LAST)
1172                 {
1173                         weapon_action(wep, WR_PRECACHE);
1174                         wep = wep + 1;
1175                 }
1176         }
1177
1178         // plays music for the level if there is any
1179         if (self.noise)
1180         {
1181                 precache_sound (self.noise);
1182                 ambientsound ('0 0 0', self.noise, VOL_BASE, ATTN_NONE);
1183         }
1184 }