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