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