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