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