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