]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/miscfunctions.qc
trigger_items
[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 float 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                 return 0;
84
85         tracebox(o, e.mins, e.maxs, o, MOVE_WORLDONLY, e);
86         if(!trace_startsolid)
87                 return 1;
88
89         m0 = e.mins;
90         m1 = e.maxs;
91         e.mins = '0 0 0';
92         e.maxs = '0 0 0';
93         move_out_of_solid_expand(e, '1 0 0' * m0_x); e.mins_x = m0_x;
94         move_out_of_solid_expand(e, '1 0 0' * m1_x); e.maxs_x = m1_x;
95         move_out_of_solid_expand(e, '0 1 0' * m0_y); e.mins_y = m0_y;
96         move_out_of_solid_expand(e, '0 1 0' * m1_y); e.maxs_y = m1_y;
97         move_out_of_solid_expand(e, '0 0 1' * m0_z); e.mins_z = m0_z;
98         move_out_of_solid_expand(e, '0 0 1' * m1_z); e.maxs_z = m1_z;
99         setorigin(e, e.origin);
100
101         tracebox(e.origin, e.mins, e.maxs, e.origin, MOVE_WORLDONLY, e);
102         if(trace_startsolid)
103         {
104                 setorigin(e, o);
105                 return 0;
106         }
107
108         return 1;
109 }
110
111 string STR_PLAYER = "player";
112 string STR_SPECTATOR = "spectator";
113 string STR_OBSERVER = "observer";
114
115 #if 0
116 #define FOR_EACH_CLIENT(v) for(v = world; (v = findflags(v, flags, FL_CLIENT)) != world; )
117 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
118 #define FOR_EACH_PLAYER(v) for(v = world; (v = find(v, classname, STR_PLAYER)) != world; )
119 #define FOR_EACH_REALPLAYER(v) FOR_EACH_PLAYER(v) if(clienttype(v) == CLIENTTYPE_REAL)
120 #else
121 #define FOR_EACH_CLIENTSLOT(v) for(v = world; (v = nextent(v)) && (num_for_edict(v) <= maxclients); )
122 #define FOR_EACH_CLIENT(v) FOR_EACH_CLIENTSLOT(v) if(v.flags & FL_CLIENT)
123 #define FOR_EACH_REALCLIENT(v) FOR_EACH_CLIENT(v) if(clienttype(v) == CLIENTTYPE_REAL)
124 #define FOR_EACH_PLAYER(v) FOR_EACH_CLIENT(v) if(v.classname == STR_PLAYER)
125 #define FOR_EACH_REALPLAYER(v) FOR_EACH_REALCLIENT(v) if(v.classname == STR_PLAYER)
126 #endif
127
128 // copies a string to a tempstring (so one can strunzone it)
129 string strcat1(string s) = #115; // FRIK_FILE
130
131 float logfile_open;
132 float logfile;
133
134 void bcenterprint(string s)
135 {
136         // TODO replace by MSG_ALL (would show it to spectators too, though)?
137         entity head;
138         FOR_EACH_PLAYER(head)
139                 if(clienttype(head) == CLIENTTYPE_REAL)
140                         centerprint(head, s);
141 }
142
143 void GameLogEcho(string s)
144 {
145         string fn;
146         float matches;
147
148         if(cvar("sv_eventlog_files"))
149         {
150                 if(!logfile_open)
151                 {
152                         logfile_open = TRUE;
153                         matches = cvar("sv_eventlog_files_counter") + 1;
154                         cvar_set("sv_eventlog_files_counter", ftos(matches));
155                         fn = ftos(matches);
156                         if(strlen(fn) < 8)
157                                 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
158                         fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
159                         logfile = fopen(fn, FILE_APPEND);
160                         fputs(logfile, ":logversion:2\n");
161                 }
162                 if(logfile >= 0)
163                 {
164                         if(cvar("sv_eventlog_files_timestamps"))
165                                 fputs(logfile, strcat(":time:", strftime(TRUE, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
166                         else
167                                 fputs(logfile, strcat(s, "\n"));
168                 }
169         }
170         if(cvar("sv_eventlog_console"))
171         {
172                 print(s, "\n");
173         }
174 }
175
176 void GameLogInit()
177 {
178         logfile_open = 0;
179         // will be opened later
180 }
181
182 void GameLogClose()
183 {
184         if(logfile_open && logfile >= 0)
185         {
186                 fclose(logfile);
187                 logfile = -1;
188         }
189 }
190
191 float spawnpoint_nag;
192 void relocate_spawnpoint()
193 {
194         // nudge off the floor
195         setorigin(self, self.origin + '0 0 1');
196
197         tracebox(self.origin, PL_MIN, PL_MAX, self.origin, TRUE, self);
198         if (trace_startsolid)
199         {
200                 vector o;
201                 o = self.origin;
202                 self.mins = PL_MIN;
203                 self.maxs = PL_MAX;
204                 if(!move_out_of_solid(self))
205                         objerror("could not get out of solid at all!");
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_handleString_Fixup(float f, .string field, string name, string(string) func)
493 {
494         GetCvars_handleString(f, field, name);
495         if(f >= 0) // also initialize to the fitting value for "" when sending cvars out
496         if(argv(f) == name)
497         {
498                 string s;
499                 s = func(strcat1(self.field));
500                 if(s != self.field)
501                 {
502                         strunzone(self.field);
503                         self.field = strzone(s);
504                 }
505         }
506 }
507 void GetCvars_handleFloat(float f, .float field, string name)
508 {
509         if(f < 0)
510         {
511         }
512         else if(f > 0)
513         {
514                 if(argv(f) == name)
515                         self.field = stof(argv(f + 1));
516         }
517         else
518                 stuffcmd(self, strcat("sendcvar ", name, "\n"));
519 }
520 string W_FixWeaponOrder_ForceComplete(string s);
521 string W_FixWeaponOrder_AllowIncomplete(string s);
522 void GetCvars(float f)
523 {
524         GetCvars_handleFloat(f, autoswitch, "cl_autoswitch");
525         GetCvars_handleFloat(f, cvar_cl_hidewaypoints, "cl_hidewaypoints");
526         GetCvars_handleFloat(f, cvar_cl_playerdetailreduction, "cl_playerdetailreduction");
527         GetCvars_handleFloat(f, cvar_cl_nogibs, "cl_nogibs");
528         GetCvars_handleFloat(f, cvar_scr_centertime, "scr_centertime");
529         GetCvars_handleFloat(f, cvar_cl_shownames, "cl_shownames");
530         GetCvars_handleString(f, cvar_g_nexuizversion, "g_nexuizversion");
531         GetCvars_handleFloat(f, cvar_cl_handicap, "cl_handicap");
532         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete);
533         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
534         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
535         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
536         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
537         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
538         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
539         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
540         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
541         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
542         GetCvars_handleString_Fixup(f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
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_weapons;
704 float start_items;
705 float start_switchweapon;
706 float start_ammo_shells;
707 float start_ammo_nails;
708 float start_ammo_rockets;
709 float start_ammo_cells;
710 float start_health;
711 float start_armorvalue;
712
713 entity get_weaponinfo(float w);
714
715 void readlevelcvars(void)
716 {
717         entity e;
718         float i;
719
720         sv_cheats = cvar("sv_cheats");
721         sv_gentle = cvar("sv_gentle");
722         sv_foginterval = cvar("sv_foginterval");
723         g_cloaked = cvar("g_cloaked");
724         g_jump_grunt = cvar("g_jump_grunt");
725         g_footsteps = cvar("g_footsteps");
726         g_grappling_hook = cvar("g_grappling_hook");
727         g_laserguided_missile = cvar("g_laserguided_missile");
728         g_midair = cvar("g_midair");
729         g_minstagib = cvar("g_minstagib");
730         g_nixnex = cvar("g_nixnex");
731         g_nixnex_with_laser = cvar("g_nixnex_with_laser");
732         g_norecoil = cvar("g_norecoil");
733         g_rocketarena = cvar("g_rocketarena");
734         g_vampire = cvar("g_vampire");
735         g_tourney = cvar("g_tourney");
736         sv_maxidle = cvar("sv_maxidle");
737         sv_maxidle_spectatorsareidle = cvar("sv_maxidle_spectatorsareidle");
738         sv_pogostick = cvar("sv_pogostick");
739         sv_doublejump = cvar("sv_doublejump");
740
741         g_pickup_respawntime_short = cvar("g_pickup_respawntime_short");
742         g_pickup_respawntime_medium = cvar("g_pickup_respawntime_medium");
743         g_pickup_respawntime_long = cvar("g_pickup_respawntime_long");
744         g_pickup_respawntime_powerup = cvar("g_pickup_respawntime_powerup");
745
746         if(g_minstagib) g_nixnex = g_rocketarena = 0;
747         if(g_nixnex) g_rocketarena = 0;
748
749         g_pickup_shells                    = cvar("g_pickup_shells");
750         g_pickup_shells_max                = cvar("g_pickup_shells_max");
751         g_pickup_nails                     = cvar("g_pickup_nails");
752         g_pickup_nails_max                 = cvar("g_pickup_nails_max");
753         g_pickup_rockets                   = cvar("g_pickup_rockets");
754         g_pickup_rockets_max               = cvar("g_pickup_rockets_max");
755         g_pickup_cells                     = cvar("g_pickup_cells");
756         g_pickup_cells_max                 = cvar("g_pickup_cells_max");
757         g_pickup_armorsmall                = cvar("g_pickup_armorsmall");
758         g_pickup_armorsmall_max            = cvar("g_pickup_armorsmall_max");
759         g_pickup_armormedium               = cvar("g_pickup_armormedium");
760         g_pickup_armormedium_max           = cvar("g_pickup_armormedium_max");
761         g_pickup_armorlarge                = cvar("g_pickup_armorlarge");
762         g_pickup_armorlarge_max            = cvar("g_pickup_armorlarge_max");
763         g_pickup_healthsmall               = cvar("g_pickup_healthsmall");
764         g_pickup_healthsmall_max           = cvar("g_pickup_healthsmall_max");
765         g_pickup_healthmedium              = cvar("g_pickup_healthmedium");
766         g_pickup_healthmedium_max          = cvar("g_pickup_healthmedium_max");
767         g_pickup_healthlarge               = cvar("g_pickup_healthlarge");
768         g_pickup_healthlarge_max           = cvar("g_pickup_healthlarge_max");
769         g_pickup_healthmega                = cvar("g_pickup_healthmega");
770         g_pickup_healthmega_max            = cvar("g_pickup_healthmega_max");
771
772         // initialize starting values for players
773         start_weapons = 0;
774         start_items = 0;
775         start_switchweapon = 0;
776         start_ammo_shells = 0;
777         start_ammo_nails = 0;
778         start_ammo_rockets = 0;
779         start_ammo_cells = 0;
780         start_health = cvar("g_balance_health_start");
781         start_armorvalue = cvar("g_balance_armor_start");
782
783         if(g_rocketarena)
784         {
785                 start_weapons = WEPBIT_ROCKET_LAUNCHER;
786                 start_switchweapon = WEP_ROCKET_LAUNCHER;
787                 weapon_action(start_switchweapon, WR_PRECACHE);
788                 start_ammo_rockets = 999;
789                 start_items |= IT_UNLIMITED_AMMO;
790         }
791         else if(g_nixnex)
792         {
793                 start_weapons = 0;
794                 // will be done later
795         }
796         else if(g_minstagib)
797         {
798                 start_health = 100;
799                 start_armorvalue = 0;
800                 start_weapons = WEPBIT_MINSTANEX;
801                 start_switchweapon = WEP_MINSTANEX;
802                 weapon_action(start_switchweapon, WR_PRECACHE);
803                 start_ammo_cells = cvar("g_minstagib_ammo_start");
804                 g_minstagib_invis_alpha = cvar("g_minstagib_invis_alpha");
805         }
806         else
807         {
808                 if(g_lms)
809                 {
810                         start_ammo_shells = cvar("g_lms_start_ammo_shells");
811                         start_ammo_nails = cvar("g_lms_start_ammo_nails");
812                         start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
813                         start_ammo_cells = cvar("g_lms_start_ammo_cells");
814                         start_health = cvar("g_lms_start_health");
815                         start_armorvalue = cvar("g_lms_start_armor");
816                 }
817                 else if(g_tourney) {
818                         start_ammo_shells = cvar("g_tourney_start_ammo_shells");
819                         start_ammo_nails = cvar("g_tourney_start_ammo_nails");
820                         start_ammo_rockets = cvar("g_tourney_start_ammo_rockets");
821                         start_ammo_cells = cvar("g_tourney_start_ammo_cells");
822                         start_health = cvar("g_tourney_start_health");
823                         start_armorvalue = cvar("g_tourney_start_armor");
824                 }
825                 else if (cvar("g_use_ammunition")) {
826                         start_ammo_shells = cvar("g_start_ammo_shells");
827                         start_ammo_nails = cvar("g_start_ammo_nails");
828                         start_ammo_rockets = cvar("g_start_ammo_rockets");
829                         start_ammo_cells = cvar("g_start_ammo_cells");
830                 } else {
831                         start_ammo_shells = cvar("g_pickup_shells_max");
832                         start_ammo_nails = cvar("g_pickup_nails_max");
833                         start_ammo_rockets = cvar("g_pickup_rockets_max");
834                         start_ammo_cells = cvar("g_pickup_cells_max");
835                         start_items |= IT_UNLIMITED_AMMO;
836                 }
837
838                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
839                 {
840                         e = get_weaponinfo(i);
841                         if(!(e.weapon))
842                                 continue;
843                         if(((e.spawnflags & 1) && (g_lms || g_tourney)) || cvar(strcat("g_start_weapon_", e.netname)))
844                         {
845                                 start_weapons |= e.weapons;
846                                 start_switchweapon = e.weapon;
847                                 weapon_action(e.weapon, WR_PRECACHE);
848                         }
849                 }
850         }
851 }
852
853 /*
854 // TODO sound pack system
855 string soundpack;
856
857 string precache_sound_builtin (string s) = #19;
858 void(entity e, float chan, string samp, float vol, float atten) sound_builtin = #8;
859 string precache_sound(string s)
860 {
861         return precache_sound_builtin(strcat(soundpack, s));
862 }
863 void play2(entity e, string filename)
864 {
865         stuffcmd(e, strcat("play2 ", soundpack, filename, "\n"));
866 }
867 void sound(entity e, float chan, string samp, float vol, float atten)
868 {
869         sound_builtin(e, chan, strcat(soundpack, samp), vol, atten);
870 }
871 */
872
873 // Sound functions
874 string precache_sound (string s) = #19;
875 void(entity e, float chan, string samp, float vol, float atten) sound = #8;
876 float precache_sound_index (string s) = #19;
877
878 void soundtoat(float dest, entity e, vector o, float chan, string samp, float vol, float atten)
879 {
880         WriteByte(dest, 6);
881         WriteByte(dest, 27); // all bits except SND_LOOPING
882         WriteByte(dest, vol * 255);
883         WriteByte(dest, atten * 64);
884         WriteEntity(dest, e);
885         WriteByte(dest, chan);
886         WriteShort(dest, precache_sound_index(samp));
887         WriteCoord(dest, o_x);
888         WriteCoord(dest, o_y);
889         WriteCoord(dest, o_z);
890 }
891 void soundto(float dest, entity e, float chan, string samp, float vol, float atten)
892 {
893         vector o;
894         o = e.origin + 0.5 * (e.mins + e.maxs);
895         soundtoat(dest, e, o, chan, samp, vol, atten);
896 }
897 void soundat(entity e, vector o, float chan, string samp, float vol, float atten)
898 {
899         soundtoat(MSG_BROADCAST, e, o, chan, samp, vol, atten);
900 }
901
902 void play2(entity e, string filename)
903 {
904         //stuffcmd(e, strcat("play2 ", filename, "\n"));
905         msg_entity = e;
906         soundtoat(MSG_ONE, world, '0 0 0', CHAN_AUTO, filename, VOL_BASE, ATTN_NONE);
907 }
908
909 .float announcetime;
910 void announce(entity player, string msg)
911 {
912         if(time > player.announcetime)
913         if(clienttype(player) == CLIENTTYPE_REAL)
914         {
915                 player.announcetime = time + 0.3;
916                 play2(player, msg);
917         }
918 }
919
920 void play2team(float t, string filename)
921 {
922         local entity head;
923         FOR_EACH_REALPLAYER(head)
924         {
925                 if (head.team == t)
926                         play2(head, filename);
927         }
928 }
929
930 void play2all(string samp)
931 {
932         sound(world, CHAN_AUTO, samp, VOL_BASE, ATTN_NONE);
933 }
934
935 void PrecachePlayerSounds(string f);
936 void precache_all_models(string pattern)
937 {
938         float globhandle, i, n;
939         string f;
940
941         globhandle = search_begin(pattern, TRUE, FALSE);
942         if(globhandle < 0)
943                 return;
944         n = search_getsize(globhandle);
945         for(i = 0; i < n; ++i)
946         {
947                 //print(search_getfilename(globhandle, i), "\n");
948                 f = search_getfilename(globhandle, i);
949                 precache_model(f);
950                 PrecachePlayerSounds(strcat(f, ".sounds"));
951         }
952         search_end(globhandle);
953 }
954
955 void precache()
956 {
957         // gamemode related things
958         precache_model ("models/misc/chatbubble.spr");
959         precache_model ("models/misc/teambubble.spr");
960         if (g_runematch)
961         {
962                 precache_model ("models/runematch/curse.mdl");
963                 precache_model ("models/runematch/rune.mdl");
964         }
965
966         // Precache all player models if desired
967         if (cvar("sv_precacheplayermodels"))
968         {
969                 PrecachePlayerSounds("sound/player/default.sounds");
970                 precache_all_models("models/player/*.zym");
971                 precache_all_models("models/player/*.dpm");
972                 precache_all_models("models/player/*.md3");
973                 precache_all_models("models/player/*.psk");
974                 //precache_model("models/player/carni.zym");
975                 //precache_model("models/player/crash.zym");
976                 //precache_model("models/player/grunt.zym");
977                 //precache_model("models/player/headhunter.zym");
978                 //precache_model("models/player/insurrectionist.zym");
979                 //precache_model("models/player/jeandarc.zym");
980                 //precache_model("models/player/lurk.zym");
981                 //precache_model("models/player/lycanthrope.zym");
982                 //precache_model("models/player/marine.zym");
983                 //precache_model("models/player/nexus.zym");
984                 //precache_model("models/player/pyria.zym");
985                 //precache_model("models/player/shock.zym");
986                 //precache_model("models/player/skadi.zym");
987                 //precache_model("models/player/specop.zym");
988                 //precache_model("models/player/visitant.zym");
989         }
990
991         if (g_footsteps)
992         {
993                 PrecacheGlobalSound((globalsound_step = "misc/footstep0 6"));
994                 PrecacheGlobalSound((globalsound_metalstep = "misc/metalfootstep0 6"));
995         }
996
997         // gore and miscellaneous sounds
998         //precache_sound ("misc/h2ohit.wav");
999         precache_model ("models/gibs/bloodyskull.md3");
1000         precache_model ("models/gibs/chunk.mdl");
1001         precache_model ("models/gibs/eye.md3");
1002         precache_model ("models/gibs/gib1.md3");
1003         precache_model ("models/gibs/gib2.md3");
1004         precache_model ("models/gibs/gib3.md3");
1005         precache_model ("models/gibs/gib4.md3");
1006         precache_model ("models/gibs/gib5.md3");
1007         precache_model ("models/gibs/gib6.md3");
1008         precache_model ("models/gibs/smallchest.md3");
1009         precache_model ("models/gibs/chest.md3");
1010         precache_model ("models/gibs/arm.md3");
1011         precache_model ("models/gibs/leg1.md3");
1012         precache_model ("models/gibs/leg2.md3");
1013         precache_model ("models/hook.md3");
1014         precache_sound ("misc/armorimpact.wav");
1015         precache_sound ("misc/bodyimpact1.wav");
1016         precache_sound ("misc/bodyimpact2.wav");
1017         precache_sound ("misc/gib.wav");
1018         precache_sound ("misc/gib_splat01.wav");
1019         precache_sound ("misc/gib_splat02.wav");
1020         precache_sound ("misc/gib_splat03.wav");
1021         precache_sound ("misc/gib_splat04.wav");
1022         precache_sound ("misc/hit.wav");
1023         PrecacheGlobalSound((globalsound_fall = "misc/hitground 4"));
1024         PrecacheGlobalSound((globalsound_metalfall = "misc/metalhitground 4"));
1025         precache_sound ("misc/null.wav");
1026         precache_sound ("misc/spawn.wav");
1027         precache_sound ("misc/talk.wav");
1028         precache_sound ("misc/teleport.wav");
1029         precache_sound ("player/lava.wav");
1030         precache_sound ("player/slime.wav");
1031
1032         // announcer sounds - male
1033         //precache_sound ("announcer/male/electrobitch.wav");
1034         precache_sound ("announcer/male/03kills.wav");
1035         precache_sound ("announcer/male/05kills.wav");
1036         precache_sound ("announcer/male/10kills.wav");
1037         precache_sound ("announcer/male/15kills.wav");
1038         precache_sound ("announcer/male/20kills.wav");
1039         precache_sound ("announcer/male/25kills.wav");
1040         precache_sound ("announcer/male/30kills.wav");
1041         precache_sound ("announcer/male/botlike.wav");
1042         precache_sound ("announcer/male/yoda.wav");
1043
1044         // announcer sounds - robotic
1045         precache_sound ("announcer/robotic/prepareforbattle.wav");
1046         precache_sound ("announcer/robotic/begin.wav");
1047         precache_sound ("announcer/robotic/timeoutcalled.wav");
1048         precache_sound ("announcer/robotic/1fragleft.wav");
1049         precache_sound ("announcer/robotic/1minuteremains.wav");
1050         precache_sound ("announcer/robotic/2fragsleft.wav");
1051         precache_sound ("announcer/robotic/3fragsleft.wav");
1052         if (g_minstagib)
1053         {
1054                 precache_sound ("announcer/robotic/lastsecond.wav");
1055                 precache_sound ("announcer/robotic/narrowly.wav");
1056         }
1057
1058         precache_model ("models/sprites/1.spr32");
1059         precache_model ("models/sprites/2.spr32");
1060         precache_model ("models/sprites/3.spr32");
1061         precache_model ("models/sprites/4.spr32");
1062         precache_model ("models/sprites/5.spr32");
1063         precache_model ("models/sprites/6.spr32");
1064         precache_model ("models/sprites/7.spr32");
1065         precache_model ("models/sprites/8.spr32");
1066         precache_model ("models/sprites/9.spr32");
1067         precache_model ("models/sprites/10.spr32");
1068         precache_sound ("announcer/robotic/1.ogg");
1069         precache_sound ("announcer/robotic/2.ogg");
1070         precache_sound ("announcer/robotic/3.ogg");
1071         precache_sound ("announcer/robotic/4.ogg");
1072         precache_sound ("announcer/robotic/5.ogg");
1073         precache_sound ("announcer/robotic/6.ogg");
1074         precache_sound ("announcer/robotic/7.ogg");
1075         precache_sound ("announcer/robotic/8.ogg");
1076         precache_sound ("announcer/robotic/9.ogg");
1077         precache_sound ("announcer/robotic/10.ogg");
1078
1079         // common weapon precaches
1080         precache_sound ("weapons/weapon_switch.wav");
1081         precache_sound ("weapons/weaponpickup.wav");
1082         if (cvar("g_grappling_hook"))
1083         {
1084                 precache_sound ("weapons/hook_fire.wav"); // hook
1085                 precache_sound ("weapons/hook_impact.wav"); // hook
1086         }
1087
1088         if (cvar("sv_precacheweapons") || g_nixnex)
1089         {
1090                 //precache weapon models/sounds
1091                 local float wep;
1092                 wep = WEP_FIRST;
1093                 while (wep <= WEP_LAST)
1094                 {
1095                         weapon_action(wep, WR_PRECACHE);
1096                         wep = wep + 1;
1097                 }
1098         }
1099
1100         // plays music for the level if there is any
1101         if (self.noise)
1102         {
1103                 precache_sound (self.noise);
1104                 ambientsound ('0 0 0', self.noise, VOL_BASE, ATTN_NONE);
1105         }
1106 }
1107
1108 // sorry, but using \ in macros breaks line numbers
1109 #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
1110 #define WRITESPECTATABLE_MSG_ONE(statement) WRITESPECTATABLE_MSG_ONE_VARNAME(oldmsg_entity, statement)
1111 #define WRITESPECTATABLE(msg,statement) if(msg == MSG_ONE) { WRITESPECTATABLE_MSG_ONE(statement); } else statement float WRITESPECTATABLE_workaround = 0
1112
1113 vector ExactTriggerHit_mins;
1114 vector ExactTriggerHit_maxs;
1115 float ExactTriggerHit_Recurse()
1116 {
1117         float s;
1118         entity se;
1119         float f;
1120
1121         tracebox('0 0 0', ExactTriggerHit_mins, ExactTriggerHit_maxs, '0 0 0', MOVE_NORMAL, other);
1122         if not(trace_ent)
1123                 return 0;
1124         if(trace_ent == self)
1125                 return 1;
1126
1127         se = trace_ent;
1128         s = se.solid;
1129         se.solid = SOLID_NOT;
1130         f = ExactTriggerHit_Recurse();
1131         se.solid = s;
1132
1133         return f;
1134 }
1135
1136 float ExactTriggerHit()
1137 {
1138         float f, s;
1139
1140         if not(self.modelindex)
1141                 return 1;
1142
1143         s = self.solid;
1144         self.solid = SOLID_BSP;
1145         ExactTriggerHit_mins = other.absmin;
1146         ExactTriggerHit_maxs = other.absmax;
1147         f = ExactTriggerHit_Recurse();
1148         self.solid = s;
1149
1150         return f;
1151 }
1152
1153 // WARNING: this kills the trace globals
1154 #define EXACTTRIGGER_TOUCH if not(ExactTriggerHit()) return
1155 #define EXACTTRIGGER_INIT  InitSolidBSPTrigger(); self.solid = SOLID_TRIGGER
1156
1157 #define INITPRIO_FIRST              0
1158 #define INITPRIO_GAMETYPE           0
1159 #define INITPRIO_GAMETYPE_FALLBACK  1
1160 #define INITPRIO_FINDTARGET        10
1161 #define INITPRIO_SETLOCATION       90
1162 #define INITPRIO_LINKDOORS         91
1163 #define INITPRIO_LAST              99
1164
1165 .void(void) initialize_entity;
1166 .float initialize_entity_order;
1167 .entity initialize_entity_next;
1168 entity initialize_entity_first;
1169 void InitializeEntity(entity e, void(void) func, float order)
1170 {
1171         entity prev, cur;
1172
1173         if(!e || e.initialize_entity)
1174         {
1175                 // make a proxy initializer entity
1176                 entity e_old;
1177                 e_old = e;
1178                 e = spawn();
1179                 e.classname = "initialize_entity";
1180                 e.enemy = e_old;
1181         }
1182
1183         e.initialize_entity = func;
1184         e.initialize_entity_order = order;
1185
1186         cur = initialize_entity_first;
1187         for(;;)
1188         {
1189                 if(!cur || cur.initialize_entity_order > order)
1190                 {
1191                         // insert between prev and cur
1192                         if(prev)
1193                                 prev.initialize_entity_next = e;
1194                         else
1195                                 initialize_entity_first = e;
1196                         e.initialize_entity_next = cur;
1197                         return;
1198                 }
1199                 prev = cur;
1200                 cur = cur.initialize_entity_next;
1201         }
1202 }
1203 void InitializeEntitiesRun()
1204 {
1205         for(self = initialize_entity_first; self; )
1206         {
1207                 entity e;
1208                 var void(void) func;
1209                 e = self.initialize_entity_next;
1210                 func = self.initialize_entity;
1211                 self.initialize_entity_order = 0;
1212                 self.initialize_entity = func_null;
1213                 self.initialize_entity_next = world;
1214                 if(self.classname == "initialize_entity")
1215                 {
1216                         entity e_old;
1217                         e_old = self.enemy;
1218                         remove(self);
1219                         self = e_old;
1220                 }
1221                 dprint("Delayed initialization: ", self.classname, "\n");
1222                 func();
1223                 self = e;
1224         }
1225         initialize_entity_first = world;
1226 }
1227
1228 .float nottargeted;
1229 #define IFTARGETED if(!self.nottargeted && self.targetname != "")
1230
1231 float power2of(float e)
1232 {
1233         return pow(2, e);
1234 }
1235 float log2of(float x)
1236 {
1237         // NOTE: generated code
1238         if(x > 2048)
1239                 if(x > 131072)
1240                         if(x > 1048576)
1241                                 if(x > 4194304)
1242                                         return 23;
1243                                 else
1244                                         if(x > 2097152)
1245                                                 return 22;
1246                                         else
1247                                                 return 21;
1248                         else
1249                                 if(x > 524288)
1250                                         return 20;
1251                                 else
1252                                         if(x > 262144)
1253                                                 return 19;
1254                                         else
1255                                                 return 18;
1256                 else
1257                         if(x > 16384)
1258                                 if(x > 65536)
1259                                         return 17;
1260                                 else
1261                                         if(x > 32768)
1262                                                 return 16;
1263                                         else
1264                                                 return 15;
1265                         else
1266                                 if(x > 8192)
1267                                         return 14;
1268                                 else
1269                                         if(x > 4096)
1270                                                 return 13;
1271                                         else
1272                                                 return 12;
1273         else
1274                 if(x > 32)
1275                         if(x > 256)
1276                                 if(x > 1024)
1277                                         return 11;
1278                                 else
1279                                         if(x > 512)
1280                                                 return 10;
1281                                         else
1282                                                 return 9;
1283                         else
1284                                 if(x > 128)
1285                                         return 8;
1286                                 else
1287                                         if(x > 64)
1288                                                 return 7;
1289                                         else
1290                                                 return 6;
1291                 else
1292                         if(x > 4)
1293                                 if(x > 16)
1294                                         return 5;
1295                                 else
1296                                         if(x > 8)
1297                                                 return 4;
1298                                         else
1299                                                 return 3;
1300                         else
1301                                 if(x > 2)
1302                                         return 2;
1303                                 else
1304                                         if(x > 1)
1305                                                 return 1;
1306                                         else
1307                                                 return 0;
1308 }
1309
1310 //
1311 // func_breakable 
1312 // - basically func_assault_destructible for general gameplay use
1313 //
1314 float () crandom;
1315 void () SUB_UseTargets, assault_destructible_use, SUB_Remove, SUB_Null;
1316 void LaunchDebris (string debrisname) =
1317 {
1318         local   entity dbr;
1319         
1320         if (debrisname == "" || !debrisname)
1321                 return;
1322         
1323         dbr = spawn();
1324         dbr.origin = self.origin;
1325         setmodel (dbr, debrisname );
1326         setsize (dbr, '0 0 0', '0 0 0');
1327         dbr.velocity_x = 70 * crandom();
1328         dbr.velocity_y = 70 * crandom();
1329         dbr.velocity_z = 140 + 70 * random();
1330         dbr.movetype = MOVETYPE_BOUNCE;
1331         dbr.solid = SOLID_BBOX;
1332         dbr.avelocity_x = random()*600;
1333         dbr.avelocity_y = random()*600;
1334         dbr.avelocity_z = random()*600;
1335         dbr.think = SUB_Remove;
1336         dbr.nextthink = time + 13 + random()*10;
1337 };
1338
1339 .string debris1, debris2, debris3;
1340 .string mdl_dead;
1341 void func_breakable_destroy() {
1342         if (self.mdl_dead)
1343                 setmodel(self, self.mdl_dead);
1344         else {
1345                 self.model = "";
1346                 self.solid = SOLID_NOT;
1347         }
1348         self.takedamage = DAMAGE_NO;
1349         
1350         // now throw around the debris
1351         LaunchDebris(self.debris1);
1352         LaunchDebris(self.debris2);
1353         LaunchDebris(self.debris3);
1354         
1355         SUB_UseTargets();
1356         
1357         self.event_damage = SUB_Null;
1358 }
1359
1360 void func_breakable_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force) {
1361
1362         if(self.cnt > 0) {
1363                 self.health = self.health - damage;
1364                 // add pain effects?
1365         }
1366
1367         if(self.health < 0) {
1368                 activator = attacker;
1369                 func_breakable_destroy();
1370         }
1371 }
1372
1373 // destructible walls that can be used to trigger target_objective_decrease
1374 void spawnfunc_func_breakable() {
1375         if(!self.health)
1376                 self.health = 100;
1377
1378         self.max_health = self.health;
1379
1380         //self.cnt = 0; // not yet activated
1381
1382
1383
1384         self.classname = "func_breakable";      
1385         self.mdl = self.model;
1386         setmodel(self, self.mdl);
1387         
1388         self.solid = SOLID_BSP;
1389         
1390         // precache all the models
1391         if (self.mdl_dead)
1392                 precache_model(self.mdl_dead);
1393         if (self.debris1)
1394                 precache_model(self.debris1);
1395         if (self.debris2)
1396                 precache_model(self.debris2);
1397         if (self.debris3)
1398                 precache_model(self.debris3);
1399         
1400         self.use = assault_destructible_use;    // shared use function, b/c they woudl do the same thing anyways
1401         self.event_damage = func_breakable_damage;
1402 }