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