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