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