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