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