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