]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/miscfunctions.qc
Extract a few more functions
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / miscfunctions.qc
1 #include "miscfunctions.qh"
2 #include "_all.qh"
3 #include "antilag.qh"
4 #include "command/common.qh"
5 #include "constants.qh"
6 #include "g_hook.qh"
7 #include "ipban.qh"
8 #include "mutators/mutators_include.qh"
9 #include "t_items.qh"
10 #include "weapons/accuracy.qh"
11 #include "weapons/csqcprojectile.qh"
12 #include "weapons/selection.qh"
13 #include "../common/command/generic.qh"
14 #include "../common/constants.qh"
15 #include "../common/deathtypes.qh"
16 #include "../common/mapinfo.qh"
17 #include "../common/notifications.qh"
18 #include "../common/playerstats.qh"
19 #include "../common/teams.qh"
20 #include "../common/triggers/subs.qh"
21 #include "../common/util.qh"
22 #include "../common/turrets/sv_turrets.qh"
23 #include "../common/weapons/all.qh"
24 #include "../csqcmodellib/sv_model.qh"
25 #include "../warpzonelib/anglestransform.qh"
26 #include "../warpzonelib/server.qh"
27
28 void crosshair_trace(entity pl)
29 {
30         traceline_antilag(pl, pl.cursor_trace_start, pl.cursor_trace_start + normalize(pl.cursor_trace_endpos - pl.cursor_trace_start) * MAX_SHOT_DISTANCE, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
31 }
32 void crosshair_trace_plusvisibletriggers(entity pl)
33 {
34         entity first;
35         entity e;
36         first = findchainfloat(solid, SOLID_TRIGGER);
37
38         for (e = first; e; e = e.chain)
39                 if (e.model != "")
40                         e.solid = SOLID_BSP;
41
42         crosshair_trace(pl);
43
44         for (e = first; e; e = e.chain)
45                 e.solid = SOLID_TRIGGER;
46 }
47 void WarpZone_crosshair_trace(entity pl)
48 {
49         WarpZone_traceline_antilag(pl, pl.cursor_trace_start, pl.cursor_trace_start + normalize(pl.cursor_trace_endpos - pl.cursor_trace_start) * MAX_SHOT_DISTANCE, MOVE_NORMAL, pl, ANTILAG_LATENCY(pl));
50 }
51
52
53 string admin_name(void)
54 {
55         if(autocvar_sv_adminnick != "")
56                 return autocvar_sv_adminnick;
57         else
58                 return "SERVER ADMIN";
59 }
60
61 void DistributeEvenly_Init(float amount, float totalweight)
62 {
63     if (DistributeEvenly_amount)
64     {
65         dprint("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ");
66         dprint(ftos(DistributeEvenly_totalweight), " left!)\n");
67     }
68     if (totalweight == 0)
69         DistributeEvenly_amount = 0;
70     else
71         DistributeEvenly_amount = amount;
72     DistributeEvenly_totalweight = totalweight;
73 }
74 float DistributeEvenly_Get(float weight)
75 {
76     float f;
77     if (weight <= 0)
78         return 0;
79     f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
80     DistributeEvenly_totalweight -= weight;
81     DistributeEvenly_amount -= f;
82     return f;
83 }
84 float DistributeEvenly_GetRandomized(float weight)
85 {
86     float f;
87     if (weight <= 0)
88         return 0;
89     f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight);
90     DistributeEvenly_totalweight -= weight;
91     DistributeEvenly_amount -= f;
92     return f;
93 }
94
95
96 void GameLogEcho(string s)
97 {
98     string fn;
99     int matches;
100
101     if (autocvar_sv_eventlog_files)
102     {
103         if (!logfile_open)
104         {
105             logfile_open = true;
106             matches = autocvar_sv_eventlog_files_counter + 1;
107             cvar_set("sv_eventlog_files_counter", itos(matches));
108             fn = ftos(matches);
109             if (strlen(fn) < 8)
110                 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
111             fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
112             logfile = fopen(fn, FILE_APPEND);
113             fputs(logfile, ":logversion:3\n");
114         }
115         if (logfile >= 0)
116         {
117             if (autocvar_sv_eventlog_files_timestamps)
118                 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
119             else
120                 fputs(logfile, strcat(s, "\n"));
121         }
122     }
123     if (autocvar_sv_eventlog_console)
124     {
125         print(s, "\n");
126     }
127 }
128
129 void GameLogInit()
130 {
131     logfile_open = 0;
132     // will be opened later
133 }
134
135 void GameLogClose()
136 {
137     if (logfile_open && logfile >= 0)
138     {
139         fclose(logfile);
140         logfile = -1;
141     }
142 }
143
144 entity findnearest(vector point, .string field, string value, vector axismod)
145 {
146     entity localhead;
147     float i;
148     float j;
149     float len;
150     vector dist;
151
152     float num_nearest;
153     num_nearest = 0;
154
155     localhead = find(world, field, value);
156     while (localhead)
157     {
158         if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
159             dist = localhead.oldorigin;
160         else
161             dist = localhead.origin;
162         dist = dist - point;
163         dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
164         len = vlen(dist);
165
166         for (i = 0; i < num_nearest; ++i)
167         {
168             if (len < nearest_length[i])
169                 break;
170         }
171
172         // now i tells us where to insert at
173         //   INSERTION SORT! YOU'VE SEEN IT! RUN!
174         if (i < NUM_NEAREST_ENTITIES)
175         {
176             for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
177             {
178                 nearest_length[j + 1] = nearest_length[j];
179                 nearest_entity[j + 1] = nearest_entity[j];
180             }
181             nearest_length[i] = len;
182             nearest_entity[i] = localhead;
183             if (num_nearest < NUM_NEAREST_ENTITIES)
184                 num_nearest = num_nearest + 1;
185         }
186
187         localhead = find(localhead, field, value);
188     }
189
190     // now use the first one from our list that we can see
191     for (i = 0; i < num_nearest; ++i)
192     {
193         traceline(point, nearest_entity[i].origin, true, world);
194         if (trace_fraction == 1)
195         {
196             if (i != 0)
197             {
198                 dprint("Nearest point (");
199                 dprint(nearest_entity[0].netname);
200                 dprint(") is not visible, using a visible one.\n");
201             }
202             return nearest_entity[i];
203         }
204     }
205
206     if (num_nearest == 0)
207         return world;
208
209     dprint("Not seeing any location point, using nearest as fallback.\n");
210     /* DEBUGGING CODE:
211     dprint("Candidates were: ");
212     for(j = 0; j < num_nearest; ++j)
213     {
214         if(j != 0)
215                 dprint(", ");
216         dprint(nearest_entity[j].netname);
217     }
218     dprint("\n");
219     */
220
221     return nearest_entity[0];
222 }
223
224 string NearestLocation(vector p)
225 {
226     entity loc;
227     string ret;
228     ret = "somewhere";
229     loc = findnearest(p, classname, "target_location", '1 1 1');
230     if (loc)
231     {
232         ret = loc.message;
233     }
234     else
235     {
236         loc = findnearest(p, target, "###item###", '1 1 4');
237         if (loc)
238             ret = loc.netname;
239     }
240     return ret;
241 }
242
243 string formatmessage(string msg)
244 {
245         float p, p1, p2;
246         float n;
247         vector cursor;
248         entity cursor_ent;
249         string escape;
250         string replacement;
251         string ammoitems;
252         p = 0;
253         n = 7;
254
255         ammoitems = "batteries";
256         if(self.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
257         if(self.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
258         if(self.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
259         if(self.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
260
261         WarpZone_crosshair_trace(self);
262         cursor = trace_endpos;
263         cursor_ent = trace_ent;
264
265         while (1) {
266                 if (n < 1)
267                         break; // too many replacements
268
269                 n = n - 1;
270                 p1 = strstr(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
271                 p2 = strstr(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
272
273                 if (p1 < 0)
274                         p1 = p2;
275
276                 if (p2 < 0)
277                         p2 = p1;
278
279                 p = min(p1, p2);
280
281                 if (p < 0)
282                         break;
283
284                 replacement = substring(msg, p, 2);
285                 escape = substring(msg, p + 1, 1);
286
287                 switch(escape)
288                 {
289                         case "%": replacement = "%"; break;
290                         case "\\":replacement = "\\"; break;
291                         case "n": replacement = "\n"; break;
292                         case "a": replacement = ftos(floor(self.armorvalue)); break;
293                         case "h": replacement = ftos(floor(self.health)); break;
294                         case "l": replacement = NearestLocation(self.origin); break;
295                         case "y": replacement = NearestLocation(cursor); break;
296                         case "d": replacement = NearestLocation(self.death_origin); break;
297                         case "w": replacement = WEP_NAME((!self.weapon) ? (!self.switchweapon ? self.cnt : self.switchweapon) : self.weapon); break;
298                         case "W": replacement = ammoitems; break;
299                         case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
300                         case "s": replacement = ftos(vlen(self.velocity - self.velocity_z * '0 0 1')); break;
301                         case "S": replacement = ftos(vlen(self.velocity)); break;
302                         default:
303                         {
304                                 MUTATOR_CALLHOOK(FormatMessage, escape, replacement);
305                                 break;
306                         }
307                 }
308
309                 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
310                 p = p + strlen(replacement);
311         }
312         return msg;
313 }
314
315 float boolean(float value) { // if value is 0 return false (0), otherwise return true (1)
316         return (value == 0) ? false : true;
317 }
318
319 /*
320 =============
321 GetCvars
322 =============
323 Called with:
324   0:  sends the request
325   >0: receives a cvar from name=argv(f) value=argv(f+1)
326 */
327 void GetCvars_handleString(string thisname, float f, .string field, string name)
328 {
329         if (f < 0)
330         {
331                 if (self.(field))
332                         strunzone(self.(field));
333                 self.(field) = string_null;
334         }
335         else if (f > 0)
336         {
337                 if (thisname == name)
338                 {
339                         if (self.(field))
340                                 strunzone(self.(field));
341                         self.(field) = strzone(argv(f + 1));
342                 }
343         }
344         else
345                 stuffcmd(self, strcat("cl_cmd sendcvar ", name, "\n"));
346 }
347 void GetCvars_handleString_Fixup(string thisname, float f, .string field, string name, string(string) func)
348 {
349         GetCvars_handleString(thisname, f, field, name);
350         if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
351                 if (thisname == name)
352                 {
353                         string s = func(strcat1(self.(field)));
354                         if (s != self.(field))
355                         {
356                                 strunzone(self.(field));
357                                 self.(field) = strzone(s);
358                         }
359                 }
360 }
361 void GetCvars_handleFloat(string thisname, float f, .float field, string name)
362 {
363         if (f < 0)
364         {
365         }
366         else if (f > 0)
367         {
368                 if (thisname == name)
369                         self.(field) = stof(argv(f + 1));
370         }
371         else
372                 stuffcmd(self, strcat("cl_cmd sendcvar ", name, "\n"));
373 }
374 void GetCvars_handleFloatOnce(string thisname, float f, .float field, string name)
375 {
376         if (f < 0)
377         {
378         }
379         else if (f > 0)
380         {
381                 if (thisname == name)
382                 {
383                         if (!self.(field))
384                         {
385                                 self.(field) = stof(argv(f + 1));
386                                 if (!self.(field))
387                                         self.(field) = -1;
388                         }
389                 }
390         }
391         else
392         {
393                 if (!self.(field))
394                         stuffcmd(self, strcat("cl_cmd sendcvar ", name, "\n"));
395         }
396 }
397 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(string wo)
398 {
399         string o;
400         o = W_FixWeaponOrder_ForceComplete(wo);
401         if(self.weaponorder_byimpulse)
402         {
403                 strunzone(self.weaponorder_byimpulse);
404                 self.weaponorder_byimpulse = string_null;
405         }
406         self.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
407         return o;
408 }
409 void GetCvars(float f)
410 {
411         string s = string_null;
412
413         if (f > 0)
414                 s = strcat1(argv(f));
415
416         get_cvars_f = f;
417         get_cvars_s = s;
418         MUTATOR_CALLHOOK(GetCvars);
419
420         Notification_GetCvars();
421
422         GetCvars_handleFloat(s, f, autoswitch, "cl_autoswitch");
423         GetCvars_handleFloat(s, f, cvar_cl_autoscreenshot, "cl_autoscreenshot");
424         GetCvars_handleFloat(s, f, cvar_cl_jetpack_jump, "cl_jetpack_jump");
425         GetCvars_handleString(s, f, cvar_g_xonoticversion, "g_xonoticversion");
426         GetCvars_handleString(s, f, cvar_cl_physics, "cl_physics");
427         GetCvars_handleFloat(s, f, cvar_cl_handicap, "cl_handicap");
428         GetCvars_handleFloat(s, f, cvar_cl_clippedspectating, "cl_clippedspectating");
429         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
430         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
431         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
432         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
433         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
434         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
435         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
436         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
437         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
438         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
439         GetCvars_handleString_Fixup(s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
440         GetCvars_handleFloat(s, f, cvar_cl_weaponimpulsemode, "cl_weaponimpulsemode");
441         GetCvars_handleFloat(s, f, cvar_cl_autotaunt, "cl_autotaunt");
442         GetCvars_handleFloat(s, f, cvar_cl_noantilag, "cl_noantilag");
443         GetCvars_handleFloat(s, f, cvar_cl_voice_directional, "cl_voice_directional");
444         GetCvars_handleFloat(s, f, cvar_cl_voice_directional_taunt_attenuation, "cl_voice_directional_taunt_attenuation");
445         GetCvars_handleFloat(s, f, cvar_cl_accuracy_data_share, "cl_accuracy_data_share");
446         GetCvars_handleFloat(s, f, cvar_cl_accuracy_data_receive, "cl_accuracy_data_receive");
447
448         self.cvar_cl_accuracy_data_share = boolean(self.cvar_cl_accuracy_data_share);
449         self.cvar_cl_accuracy_data_receive = boolean(self.cvar_cl_accuracy_data_receive);
450
451         GetCvars_handleFloatOnce(s, f, cvar_cl_gunalign, "cl_gunalign");
452         GetCvars_handleFloat(s, f, cvar_cl_allow_uid2name, "cl_allow_uid2name");
453         GetCvars_handleFloat(s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
454         GetCvars_handleFloat(s, f, cvar_cl_movement_track_canjump, "cl_movement_track_canjump");
455         GetCvars_handleFloat(s, f, cvar_cl_newusekeysupported, "cl_newusekeysupported");
456
457         // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
458         if (f > 0)
459         {
460                 if (s == "cl_weaponpriority")
461                         self.switchweapon = w_getbestweapon(self);
462                 if (s == "cl_allow_uidtracking")
463                         PlayerStats_GameReport_AddPlayer(self);
464         }
465 }
466
467 // decolorizes and team colors the player name when needed
468 string playername(entity p)
469 {
470     string t;
471     if (teamplay && !intermission_running && IS_PLAYER(p))
472     {
473         t = Team_ColorCode(p.team);
474         return strcat(t, strdecolorize(p.netname));
475     }
476     else
477         return p.netname;
478 }
479
480 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
481 {
482         int i = weaponinfo.weapon;
483         int d = 0;
484
485         if (!i)
486                 return 0;
487
488         if (g_lms || g_ca || allguns)
489         {
490                 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
491                         d = true;
492                 else
493                         d = false;
494         }
495         else if (g_cts)
496                 d = (i == WEP_SHOTGUN.m_id);
497         else if (g_nexball)
498                 d = 0; // weapon is set a few lines later
499         else
500                 d = !(!weaponinfo.weaponstart);
501
502         if(g_grappling_hook) // if possible, redirect off-hand hook to on-hand hook
503                 d |= (i == WEP_HOOK.m_id);
504         if(!g_cts && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
505                 d = 0;
506
507         float t = weaponinfo.weaponstartoverride;
508
509         //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
510
511         // bit order in t:
512         // 1: want or not
513         // 2: is default?
514         // 4: is set by default?
515         if(t < 0)
516                 t = 4 | (3 * d);
517         else
518                 t |= (2 * d);
519
520         return t;
521 }
522
523 void readplayerstartcvars()
524 {
525         entity e;
526         float i, j, t;
527         string s;
528
529         // initialize starting values for players
530         start_weapons = '0 0 0';
531         start_weapons_default = '0 0 0';
532         start_weapons_defaultmask = '0 0 0';
533         start_items = 0;
534         start_ammo_shells = 0;
535         start_ammo_nails = 0;
536         start_ammo_rockets = 0;
537         start_ammo_cells = 0;
538         start_ammo_plasma = 0;
539         start_health = cvar("g_balance_health_start");
540         start_armorvalue = cvar("g_balance_armor_start");
541
542         g_weaponarena = 0;
543         g_weaponarena_weapons = '0 0 0';
544
545         s = cvar_string("g_weaponarena");
546         if (s == "0" || s == "")
547         {
548                 if(g_ca || g_freezetag)
549                         s = "most";
550         }
551
552         if (s == "0" || s == "")
553         {
554                 // no arena
555         }
556         else if (s == "off")
557         {
558                 // forcibly turn off weaponarena
559         }
560         else if (s == "all" || s == "1")
561         {
562                 g_weaponarena = 1;
563                 g_weaponarena_list = "All Weapons";
564                 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
565                 {
566                         e = get_weaponinfo(j);
567                         if (!(e.spawnflags & WEP_FLAG_MUTATORBLOCKED))
568                                 g_weaponarena_weapons |= WepSet_FromWeapon(j);
569                 }
570         }
571         else if (s == "most")
572         {
573                 g_weaponarena = 1;
574                 g_weaponarena_list = "Most Weapons";
575                 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
576                 {
577                         e = get_weaponinfo(j);
578                         if (!(e.spawnflags & WEP_FLAG_MUTATORBLOCKED))
579                                 if (e.spawnflags & WEP_FLAG_NORMAL)
580                                         g_weaponarena_weapons |= WepSet_FromWeapon(j);
581                 }
582         }
583         else if (s == "none")
584         {
585                 g_weaponarena = 1;
586                 g_weaponarena_list = "No Weapons";
587         }
588         else
589         {
590                 g_weaponarena = 1;
591                 t = tokenize_console(s);
592                 g_weaponarena_list = "";
593                 for (i = 0; i < t; ++i)
594                 {
595                         s = argv(i);
596                         for (j = WEP_FIRST; j <= WEP_LAST; ++j)
597                         {
598                                 e = get_weaponinfo(j);
599                                 if (e.netname == s)
600                                 {
601                                         g_weaponarena_weapons |= WepSet_FromWeapon(j);
602                                         g_weaponarena_list = strcat(g_weaponarena_list, e.message, " & ");
603                                         break;
604                                 }
605                         }
606                         if (j > WEP_LAST)
607                         {
608                                 print("The weapon mutator list contains an unknown weapon ", s, ". Skipped.\n");
609                         }
610                 }
611                 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
612         }
613
614         if(g_weaponarena)
615                 g_weaponarena_random = cvar("g_weaponarena_random");
616         else
617                 g_weaponarena_random = 0;
618         g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
619
620         if (g_weaponarena)
621         {
622                 g_weapon_stay = 0; // incompatible
623                 start_weapons = g_weaponarena_weapons;
624                 start_items |= IT_UNLIMITED_AMMO;
625         }
626         else
627         {
628                 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
629                 {
630                         e = get_weaponinfo(i);
631                         int w = want_weapon(e, false);
632                         if(w & 1)
633                                 start_weapons |= WepSet_FromWeapon(i);
634                         if(w & 2)
635                                 start_weapons_default |= WepSet_FromWeapon(i);
636                         if(w & 4)
637                                 start_weapons_defaultmask |= WepSet_FromWeapon(i);
638                 }
639         }
640
641         if(!cvar("g_use_ammunition"))
642                 start_items |= IT_UNLIMITED_AMMO;
643
644         if(start_items & IT_UNLIMITED_WEAPON_AMMO)
645         {
646                 start_ammo_shells = 999;
647                 start_ammo_nails = 999;
648                 start_ammo_rockets = 999;
649                 start_ammo_cells = 999;
650                 start_ammo_plasma = 999;
651                 start_ammo_fuel = 999;
652         }
653         else
654         {
655                 start_ammo_shells = cvar("g_start_ammo_shells");
656                 start_ammo_nails = cvar("g_start_ammo_nails");
657                 start_ammo_rockets = cvar("g_start_ammo_rockets");
658                 start_ammo_cells = cvar("g_start_ammo_cells");
659                 start_ammo_plasma = cvar("g_start_ammo_plasma");
660                 start_ammo_fuel = cvar("g_start_ammo_fuel");
661         }
662
663         if (warmup_stage)
664         {
665                 warmup_start_ammo_shells = start_ammo_shells;
666                 warmup_start_ammo_nails = start_ammo_nails;
667                 warmup_start_ammo_rockets = start_ammo_rockets;
668                 warmup_start_ammo_cells = start_ammo_cells;
669                 warmup_start_ammo_plasma = start_ammo_plasma;
670                 warmup_start_ammo_fuel = start_ammo_fuel;
671                 warmup_start_health = start_health;
672                 warmup_start_armorvalue = start_armorvalue;
673                 warmup_start_weapons = start_weapons;
674                 warmup_start_weapons_default = start_weapons_default;
675                 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
676
677                 if (!g_weaponarena && !g_ca && !g_freezetag)
678                 {
679                         warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
680                         warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
681                         warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
682                         warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
683                         warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
684                         warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
685                         warmup_start_health = cvar("g_warmup_start_health");
686                         warmup_start_armorvalue = cvar("g_warmup_start_armor");
687                         warmup_start_weapons = '0 0 0';
688                         warmup_start_weapons_default = '0 0 0';
689                         warmup_start_weapons_defaultmask = '0 0 0';
690                         for (i = WEP_FIRST; i <= WEP_LAST; ++i)
691                         {
692                                 e = get_weaponinfo(i);
693                                 int w = want_weapon(e, g_warmup_allguns);
694                                 if(w & 1)
695                                         warmup_start_weapons |= WepSet_FromWeapon(i);
696                                 if(w & 2)
697                                         warmup_start_weapons_default |= WepSet_FromWeapon(i);
698                                 if(w & 4)
699                                         warmup_start_weapons_defaultmask |= WepSet_FromWeapon(i);
700                         }
701                 }
702         }
703
704         if (g_jetpack)
705                 start_items |= ITEM_Jetpack.m_itemid;
706
707         MUTATOR_CALLHOOK(SetStartItems);
708
709         if ((start_items & ITEM_Jetpack.m_itemid) || (g_grappling_hook && (start_weapons & WEPSET_HOOK)))
710         {
711                 start_items |= ITEM_JetpackRegen.m_itemid;
712                 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
713                 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
714         }
715
716         WepSet precache_weapons = start_weapons;
717         if (g_warmup_allguns != 1)
718                 precache_weapons |= warmup_start_weapons;
719         for (i = WEP_FIRST; i <= WEP_LAST; ++i)
720         {
721                 e = get_weaponinfo(i);
722                 if(precache_weapons & WepSet_FromWeapon(i))
723                         WEP_ACTION(i, WR_INIT);
724         }
725
726         start_ammo_shells = max(0, start_ammo_shells);
727         start_ammo_nails = max(0, start_ammo_nails);
728         start_ammo_rockets = max(0, start_ammo_rockets);
729         start_ammo_cells = max(0, start_ammo_cells);
730         start_ammo_plasma = max(0, start_ammo_plasma);
731         start_ammo_fuel = max(0, start_ammo_fuel);
732
733         warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
734         warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
735         warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
736         warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
737         warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
738         warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
739 }
740
741 float sound_allowed(float destin, entity e)
742 {
743     // sounds from world may always pass
744     for (;;)
745     {
746         if (e.classname == "body")
747             e = e.enemy;
748         else if (e.realowner && e.realowner != e)
749             e = e.realowner;
750         else if (e.owner && e.owner != e)
751             e = e.owner;
752         else
753             break;
754     }
755     // sounds to self may always pass
756     if (destin == MSG_ONE)
757         if (e == msg_entity)
758             return true;
759     // sounds by players can be removed
760     if (autocvar_bot_sound_monopoly)
761         if (IS_REAL_CLIENT(e))
762             return false;
763     // anything else may pass
764     return true;
765 }
766
767 #undef sound
768 void sound(entity e, float chan, string samp, float vol, float attenu)
769 {
770     if (!sound_allowed(MSG_BROADCAST, e))
771         return;
772     sound7(e, chan, samp, vol, attenu, 0, 0);
773 }
774
775 void soundtoat(float _dest, entity e, vector o, float chan, string samp, float vol, float attenu)
776 {
777     float entno, idx;
778
779     if (!sound_allowed(_dest, e))
780         return;
781
782     entno = num_for_edict(e);
783     idx = precache_sound_index(samp);
784
785     int sflags;
786     sflags = 0;
787
788     attenu = floor(attenu * 64);
789     vol = floor(vol * 255);
790
791     if (vol != 255)
792         sflags |= SND_VOLUME;
793     if (attenu != 64)
794         sflags |= SND_ATTENUATION;
795     if (entno >= 8192 || chan < 0 || chan > 7)
796         sflags |= SND_LARGEENTITY;
797     if (idx >= 256)
798         sflags |= SND_LARGESOUND;
799
800     WriteByte(_dest, SVC_SOUND);
801     WriteByte(_dest, sflags);
802     if (sflags & SND_VOLUME)
803         WriteByte(_dest, vol);
804     if (sflags & SND_ATTENUATION)
805         WriteByte(_dest, attenu);
806     if (sflags & SND_LARGEENTITY)
807     {
808         WriteShort(_dest, entno);
809         WriteByte(_dest, chan);
810     }
811     else
812     {
813         WriteShort(_dest, entno * 8 + chan);
814     }
815     if (sflags & SND_LARGESOUND)
816         WriteShort(_dest, idx);
817     else
818         WriteByte(_dest, idx);
819
820     WriteCoord(_dest, o.x);
821     WriteCoord(_dest, o.y);
822     WriteCoord(_dest, o.z);
823 }
824 void soundto(float _dest, entity e, float chan, string samp, float vol, float _atten)
825 {
826     vector o;
827
828     if (!sound_allowed(_dest, e))
829         return;
830
831     o = e.origin + 0.5 * (e.mins + e.maxs);
832     soundtoat(_dest, e, o, chan, samp, vol, _atten);
833 }
834 void soundat(entity e, vector o, float chan, string samp, float vol, float _atten)
835 {
836     soundtoat(((chan & 8) ? MSG_ALL : MSG_BROADCAST), e, o, chan, samp, vol, _atten);
837 }
838 void stopsoundto(float _dest, entity e, float chan)
839 {
840     float entno;
841
842     if (!sound_allowed(_dest, e))
843         return;
844
845     entno = num_for_edict(e);
846
847     if (entno >= 8192 || chan < 0 || chan > 7)
848     {
849         float idx, sflags;
850         idx = precache_sound_index("misc/null.wav");
851         sflags = SND_LARGEENTITY;
852         if (idx >= 256)
853             sflags |= SND_LARGESOUND;
854         WriteByte(_dest, SVC_SOUND);
855         WriteByte(_dest, sflags);
856         WriteShort(_dest, entno);
857         WriteByte(_dest, chan);
858         if (sflags & SND_LARGESOUND)
859             WriteShort(_dest, idx);
860         else
861             WriteByte(_dest, idx);
862         WriteCoord(_dest, e.origin.x);
863         WriteCoord(_dest, e.origin.y);
864         WriteCoord(_dest, e.origin.z);
865     }
866     else
867     {
868         WriteByte(_dest, SVC_STOPSOUND);
869         WriteShort(_dest, entno * 8 + chan);
870     }
871 }
872 void stopsound(entity e, float chan)
873 {
874     if (!sound_allowed(MSG_BROADCAST, e))
875         return;
876
877     stopsoundto(MSG_BROADCAST, e, chan); // unreliable, gets there fast
878     stopsoundto(MSG_ALL, e, chan); // in case of packet loss
879 }
880
881 void play2(entity e, string filename)
882 {
883     //stuffcmd(e, strcat("play2 ", filename, "\n"));
884     msg_entity = e;
885     soundtoat(MSG_ONE, world, '0 0 0', CH_INFO, filename, VOL_BASE, ATTEN_NONE);
886 }
887
888 // use this one if you might be causing spam (e.g. from touch functions that might get called more than once per frame)
889 .float spamtime;
890 float spamsound(entity e, float chan, string samp, float vol, float _atten)
891 {
892     if (!sound_allowed(MSG_BROADCAST, e))
893         return false;
894
895     if (time > e.spamtime)
896     {
897         e.spamtime = time;
898         sound(e, chan, samp, vol, _atten);
899         return true;
900     }
901     return false;
902 }
903
904 void play2team(float t, string filename)
905 {
906     entity head;
907
908     if (autocvar_bot_sound_monopoly)
909         return;
910
911     FOR_EACH_REALPLAYER(head)
912     {
913         if (head.team == t)
914             play2(head, filename);
915     }
916 }
917
918 void play2all(string samp)
919 {
920     if (autocvar_bot_sound_monopoly)
921         return;
922
923     sound(world, CH_INFO, samp, VOL_BASE, ATTEN_NONE);
924 }
925
926 void PrecachePlayerSounds(string f);
927 void precache_playermodel(string m)
928 {
929         float globhandle, i, n;
930         string f;
931
932         if(substring(m, -9,5) == "_lod1")
933                 return;
934         if(substring(m, -9,5) == "_lod2")
935                 return;
936         precache_model(m);
937         f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
938         if(fexists(f))
939                 precache_model(f);
940         f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
941         if(fexists(f))
942                 precache_model(f);
943
944         globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
945         if (globhandle < 0)
946                 return;
947         n = search_getsize(globhandle);
948         for (i = 0; i < n; ++i)
949         {
950                 //print(search_getfilename(globhandle, i), "\n");
951                 f = search_getfilename(globhandle, i);
952                 PrecachePlayerSounds(f);
953         }
954         search_end(globhandle);
955 }
956 void precache_all_playermodels(string pattern)
957 {
958         float globhandle, i, n;
959         string f;
960
961         globhandle = search_begin(pattern, true, false);
962         if (globhandle < 0)
963                 return;
964         n = search_getsize(globhandle);
965         for (i = 0; i < n; ++i)
966         {
967                 //print(search_getfilename(globhandle, i), "\n");
968                 f = search_getfilename(globhandle, i);
969                 precache_playermodel(f);
970         }
971         search_end(globhandle);
972 }
973
974 void precache()
975 {
976     // gamemode related things
977     precache_model ("models/misc/chatbubble.spr");
978         precache_model("models/ice/ice.md3");
979
980     // Precache all player models if desired
981     if (autocvar_sv_precacheplayermodels)
982     {
983         PrecachePlayerSounds("sound/player/default.sounds");
984         precache_all_playermodels("models/player/*.zym");
985         precache_all_playermodels("models/player/*.dpm");
986         precache_all_playermodels("models/player/*.md3");
987         precache_all_playermodels("models/player/*.psk");
988         precache_all_playermodels("models/player/*.iqm");
989     }
990
991     if (autocvar_sv_defaultcharacter)
992     {
993         string s;
994         s = autocvar_sv_defaultplayermodel_red;
995         if (s != "")
996             precache_playermodel(s);
997         s = autocvar_sv_defaultplayermodel_blue;
998         if (s != "")
999             precache_playermodel(s);
1000         s = autocvar_sv_defaultplayermodel_yellow;
1001         if (s != "")
1002             precache_playermodel(s);
1003         s = autocvar_sv_defaultplayermodel_pink;
1004         if (s != "")
1005             precache_playermodel(s);
1006         s = autocvar_sv_defaultplayermodel;
1007         if (s != "")
1008             precache_playermodel(s);
1009     }
1010
1011     if (g_footsteps)
1012     {
1013         PrecacheGlobalSound((globalsound_step = "misc/footstep0 6"));
1014         PrecacheGlobalSound((globalsound_metalstep = "misc/metalfootstep0 6"));
1015     }
1016
1017     // gore and miscellaneous sounds
1018     //precache_sound ("misc/h2ohit.wav");
1019     precache_model ("models/hook.md3");
1020     precache_sound ("misc/armorimpact.wav");
1021     precache_sound ("misc/bodyimpact1.wav");
1022     precache_sound ("misc/bodyimpact2.wav");
1023     precache_sound ("misc/gib.wav");
1024     precache_sound ("misc/gib_splat01.wav");
1025     precache_sound ("misc/gib_splat02.wav");
1026     precache_sound ("misc/gib_splat03.wav");
1027     precache_sound ("misc/gib_splat04.wav");
1028     PrecacheGlobalSound((globalsound_fall = "misc/hitground 4"));
1029     PrecacheGlobalSound((globalsound_metalfall = "misc/metalhitground 4"));
1030     precache_sound ("misc/null.wav");
1031     precache_sound ("misc/spawn.wav");
1032     precache_sound ("misc/talk.wav");
1033     precache_sound ("misc/teleport.wav");
1034     precache_sound ("misc/poweroff.wav");
1035     precache_sound ("player/lava.wav");
1036     precache_sound ("player/slime.wav");
1037
1038     precache_model ("models/sprites/0.spr32");
1039     precache_model ("models/sprites/1.spr32");
1040     precache_model ("models/sprites/2.spr32");
1041     precache_model ("models/sprites/3.spr32");
1042     precache_model ("models/sprites/4.spr32");
1043     precache_model ("models/sprites/5.spr32");
1044     precache_model ("models/sprites/6.spr32");
1045     precache_model ("models/sprites/7.spr32");
1046     precache_model ("models/sprites/8.spr32");
1047     precache_model ("models/sprites/9.spr32");
1048     precache_model ("models/sprites/10.spr32");
1049
1050     // common weapon precaches
1051         precache_sound (W_Sound("reload")); // until weapons have individual reload sounds, precache the reload sound here
1052     precache_sound (W_Sound("weapon_switch"));
1053     precache_sound (W_Sound("weaponpickup"));
1054     precache_sound (W_Sound("unavailable"));
1055     precache_sound (W_Sound("dryfire"));
1056     if (g_grappling_hook)
1057     {
1058         precache_sound (W_Sound("hook_fire")); // hook
1059         precache_sound (W_Sound("hook_impact")); // hook
1060     }
1061
1062     precache_model("models/elaser.mdl");
1063     precache_model("models/laser.mdl");
1064     precache_model("models/ebomb.mdl");
1065
1066 #if 0
1067     // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
1068
1069     if (!self.noise && self.music) // quake 3 uses the music field
1070         self.noise = self.music;
1071
1072     // plays music for the level if there is any
1073     if (self.noise)
1074     {
1075         precache_sound (self.noise);
1076         ambientsound ('0 0 0', self.noise, VOL_BASE, ATTEN_NONE);
1077     }
1078 #endif
1079
1080 #include "precache-for-csqc.inc"
1081 }
1082
1083
1084 void make_safe_for_remove(entity e)
1085 {
1086     if (e.initialize_entity)
1087     {
1088         entity ent, prev = world;
1089         for (ent = initialize_entity_first; ent; )
1090         {
1091             if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
1092             {
1093                 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
1094                 // skip it in linked list
1095                 if (prev)
1096                 {
1097                     prev.initialize_entity_next = ent.initialize_entity_next;
1098                     ent = prev.initialize_entity_next;
1099                 }
1100                 else
1101                 {
1102                     initialize_entity_first = ent.initialize_entity_next;
1103                     ent = initialize_entity_first;
1104                 }
1105             }
1106             else
1107             {
1108                 prev = ent;
1109                 ent = ent.initialize_entity_next;
1110             }
1111         }
1112     }
1113 }
1114
1115 void objerror(string s)
1116 {
1117     make_safe_for_remove(self);
1118     builtin_objerror(s);
1119 }
1120
1121 .float remove_except_protected_forbidden;
1122 void remove_except_protected(entity e)
1123 {
1124         if(e.remove_except_protected_forbidden)
1125                 error("not allowed to remove this at this point");
1126         builtin_remove(e);
1127 }
1128
1129 void remove_unsafely(entity e)
1130 {
1131     if(e.classname == "spike")
1132         error("Removing spikes is forbidden (crylink bug), please report");
1133     builtin_remove(e);
1134 }
1135
1136 void remove_safely(entity e)
1137 {
1138     make_safe_for_remove(e);
1139     builtin_remove(e);
1140 }
1141
1142 void InitializeEntity(entity e, void(void) func, float order)
1143 {
1144     entity prev, cur;
1145
1146     if (!e || e.initialize_entity)
1147     {
1148         // make a proxy initializer entity
1149         entity e_old;
1150         e_old = e;
1151         e = spawn();
1152         e.classname = "initialize_entity";
1153         e.enemy = e_old;
1154     }
1155
1156     e.initialize_entity = func;
1157     e.initialize_entity_order = order;
1158
1159     cur = initialize_entity_first;
1160     prev = world;
1161     for (;;)
1162     {
1163         if (!cur || cur.initialize_entity_order > order)
1164         {
1165             // insert between prev and cur
1166             if (prev)
1167                 prev.initialize_entity_next = e;
1168             else
1169                 initialize_entity_first = e;
1170             e.initialize_entity_next = cur;
1171             return;
1172         }
1173         prev = cur;
1174         cur = cur.initialize_entity_next;
1175     }
1176 }
1177 void InitializeEntitiesRun()
1178 {
1179     entity startoflist;
1180     startoflist = initialize_entity_first;
1181     initialize_entity_first = world;
1182     remove = remove_except_protected;
1183     for (self = startoflist; self; self = self.initialize_entity_next)
1184     {
1185         self.remove_except_protected_forbidden = 1;
1186     }
1187     for (self = startoflist; self; )
1188     {
1189         entity e;
1190         var void(void) func;
1191         e = self.initialize_entity_next;
1192         func = self.initialize_entity;
1193         self.initialize_entity_order = 0;
1194         self.initialize_entity = func_null;
1195         self.initialize_entity_next = world;
1196         self.remove_except_protected_forbidden = 0;
1197         if (self.classname == "initialize_entity")
1198         {
1199             entity e_old;
1200             e_old = self.enemy;
1201             builtin_remove(self);
1202             self = e_old;
1203         }
1204         //dprint("Delayed initialization: ", self.classname, "\n");
1205         if(func)
1206             func();
1207         else
1208         {
1209             eprint(self);
1210             backtrace(strcat("Null function in: ", self.classname, "\n"));
1211         }
1212         self = e;
1213     }
1214     remove = remove_unsafely;
1215 }
1216
1217 void UncustomizeEntitiesRun()
1218 {
1219     entity oldself;
1220     oldself = self;
1221     for (self = world; (self = findfloat(self, uncustomizeentityforclient_set, 1)); )
1222         self.uncustomizeentityforclient();
1223     self = oldself;
1224 }
1225 void SetCustomizer(entity e, float(void) customizer, void(void) uncustomizer)
1226 {
1227     e.customizeentityforclient = customizer;
1228     e.uncustomizeentityforclient = uncustomizer;
1229     e.uncustomizeentityforclient_set = !!uncustomizer;
1230 }
1231
1232 void Net_LinkEntity(entity e, bool docull, float dt, bool(entity, int) sendfunc)
1233 {
1234     vector mi, ma;
1235
1236     if (e.classname == "")
1237         e.classname = "net_linked";
1238
1239     if (e.model == "" || self.modelindex == 0)
1240     {
1241         mi = e.mins;
1242         ma = e.maxs;
1243         setmodel(e, "null");
1244         setsize(e, mi, ma);
1245     }
1246
1247     e.SendEntity = sendfunc;
1248     e.SendFlags = 0xFFFFFF;
1249
1250     if (!docull)
1251         e.effects |= EF_NODEPTHTEST;
1252
1253     if (dt)
1254     {
1255         e.nextthink = time + dt;
1256         e.think = SUB_Remove;
1257     }
1258 }
1259
1260
1261 .float(entity) isEliminated;
1262 float EliminatedPlayers_SendEntity(entity to, float sendflags)
1263 {
1264         float i, f, b;
1265         entity e;
1266         WriteByte(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
1267         WriteByte(MSG_ENTITY, sendflags);
1268
1269         if(sendflags & 1)
1270         {
1271                 for(i = 1; i <= maxclients; i += 8)
1272                 {
1273                         for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
1274                         {
1275                                 if(eliminatedPlayers.isEliminated(e))
1276                                         f |= b;
1277                         }
1278                         WriteByte(MSG_ENTITY, f);
1279                 }
1280         }
1281
1282         return true;
1283 }
1284
1285 void EliminatedPlayers_Init(float(entity) isEliminated_func)
1286 {
1287         if(eliminatedPlayers)
1288         {
1289                 backtrace("Can't spawn eliminatedPlayers again!");
1290                 return;
1291         }
1292         Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
1293         eliminatedPlayers.isEliminated = isEliminated_func;
1294 }
1295
1296
1297 void adaptor_think2touch()
1298 {
1299     entity o;
1300     o = other;
1301     other = world;
1302     self.touch();
1303     other = o;
1304 }
1305
1306 void adaptor_think2use()
1307 {
1308     entity o, a;
1309     o = other;
1310     a = activator;
1311     activator = world;
1312     other = world;
1313     self.use();
1314     other = o;
1315     activator = a;
1316 }
1317
1318 void adaptor_think2use_hittype_splash() // for timed projectile detonation
1319 {
1320         if(!(self.flags & FL_ONGROUND)) // if onground, we ARE touching something, but HITTYPE_SPLASH is to be networked if the damage causing projectile is not touching ANYTHING
1321                 self.projectiledeathtype |= HITTYPE_SPLASH;
1322         adaptor_think2use();
1323 }
1324
1325 // deferred dropping
1326 void DropToFloor_Handler()
1327 {
1328     builtin_droptofloor();
1329     self.dropped_origin = self.origin;
1330 }
1331
1332 void droptofloor()
1333 {
1334     InitializeEntity(self, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
1335 }
1336
1337
1338
1339 float trace_hits_box_a0, trace_hits_box_a1;
1340
1341 float trace_hits_box_1d(float end, float thmi, float thma)
1342 {
1343     if (end == 0)
1344     {
1345         // just check if x is in range
1346         if (0 < thmi)
1347             return false;
1348         if (0 > thma)
1349             return false;
1350     }
1351     else
1352     {
1353         // do the trace with respect to x
1354         // 0 -> end has to stay in thmi -> thma
1355         trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1356         trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1357         if (trace_hits_box_a0 > trace_hits_box_a1)
1358             return false;
1359     }
1360     return true;
1361 }
1362
1363 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1364 {
1365     end -= start;
1366     thmi -= start;
1367     thma -= start;
1368     // now it is a trace from 0 to end
1369
1370     trace_hits_box_a0 = 0;
1371     trace_hits_box_a1 = 1;
1372
1373     if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1374         return false;
1375     if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1376         return false;
1377     if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1378         return false;
1379
1380     return true;
1381 }
1382
1383 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1384 {
1385     return trace_hits_box(start, end, thmi - ma, thma - mi);
1386 }
1387
1388 float SUB_NoImpactCheck()
1389 {
1390         // zero hitcontents = this is not the real impact, but either the
1391         // mirror-impact of something hitting the projectile instead of the
1392         // projectile hitting the something, or a touchareagrid one. Neither of
1393         // these stop the projectile from moving, so...
1394         if(trace_dphitcontents == 0)
1395         {
1396                 //dprint("A hit happened with zero hit contents... DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct.\n");
1397                 dprintf("A hit from a projectile happened with no hit contents! DEBUG THIS, this should never happen for projectiles! Profectile will self-destruct. (edict: %d, classname: %s, origin: %s)\n", num_for_edict(self), self.classname, vtos(self.origin));
1398                 checkclient();
1399         }
1400     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1401         return 1;
1402     if (other == world && self.size != '0 0 0')
1403     {
1404         vector tic;
1405         tic = self.velocity * sys_frametime;
1406         tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
1407         traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
1408         if (trace_fraction >= 1)
1409         {
1410             dprint("Odd... did not hit...?\n");
1411         }
1412         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1413         {
1414             dprint("Detected and prevented the sky-grapple bug.\n");
1415             return 1;
1416         }
1417     }
1418
1419     return 0;
1420 }
1421
1422 #define SUB_OwnerCheck() (other && (other == self.owner))
1423
1424 void W_Crylink_Dequeue(entity e);
1425 float WarpZone_Projectile_Touch_ImpactFilter_Callback()
1426 {
1427         if(SUB_OwnerCheck())
1428                 return true;
1429         if(SUB_NoImpactCheck())
1430         {
1431                 if(self.classname == "nade")
1432                         return false; // no checks here
1433                 else if(self.classname == "grapplinghook")
1434                         RemoveGrapplingHook(self.realowner);
1435                 else if(self.classname == "spike")
1436                 {
1437                         W_Crylink_Dequeue(self);
1438                         remove(self);
1439                 }
1440                 else
1441                         remove(self);
1442                 return true;
1443         }
1444         if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1445                 UpdateCSQCProjectile(self);
1446         return false;
1447 }
1448
1449
1450 void URI_Get_Callback(float id, float status, string data)
1451 {
1452         if(url_URI_Get_Callback(id, status, data))
1453         {
1454                 // handled
1455         }
1456         else if (id == URI_GET_DISCARD)
1457         {
1458                 // discard
1459         }
1460         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1461         {
1462                 // sv_cmd curl
1463                 Curl_URI_Get_Callback(id, status, data);
1464         }
1465         else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1466         {
1467                 // online ban list
1468                 OnlineBanList_URI_Get_Callback(id, status, data);
1469         }
1470         else
1471         {
1472                 print("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1473         }
1474 }
1475
1476 string uid2name(string myuid) {
1477         string s;
1478         s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1479
1480         // FIXME remove this later after 0.6 release
1481         // convert old style broken records to correct style
1482         if(s == "")
1483         {
1484                 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1485                 if(s != "")
1486                 {
1487                         db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1488                         db_put(ServerProgsDB, strcat("uid2name", myuid), "");
1489                 }
1490         }
1491
1492         if(s == "")
1493                 s = "^1Unregistered Player";
1494         return s;
1495 }
1496
1497 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1498 {
1499     float m, i;
1500     vector start, org, delta, end, enddown, mstart;
1501     entity sp;
1502
1503     m = e.dphitcontentsmask;
1504     e.dphitcontentsmask = goodcontents | badcontents;
1505
1506     org = world.mins;
1507     delta = world.maxs - world.mins;
1508
1509     start = end = org;
1510
1511     for (i = 0; i < attempts; ++i)
1512     {
1513         start.x = org.x + random() * delta.x;
1514         start.y = org.y + random() * delta.y;
1515         start.z = org.z + random() * delta.z;
1516
1517         // rule 1: start inside world bounds, and outside
1518         // solid, and don't start from somewhere where you can
1519         // fall down to evil
1520         tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1521         if (trace_fraction >= 1)
1522             continue;
1523         if (trace_startsolid)
1524             continue;
1525         if (trace_dphitcontents & badcontents)
1526             continue;
1527         if (trace_dphitq3surfaceflags & badsurfaceflags)
1528             continue;
1529
1530         // rule 2: if we are too high, lower the point
1531         if (trace_fraction * delta.z > maxaboveground)
1532             start = trace_endpos + '0 0 1' * maxaboveground;
1533         enddown = trace_endpos;
1534
1535         // rule 3: make sure we aren't outside the map. This only works
1536         // for somewhat well formed maps. A good rule of thumb is that
1537         // the map should have a convex outside hull.
1538         // these can be traceLINES as we already verified the starting box
1539         mstart = start + 0.5 * (e.mins + e.maxs);
1540         traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1541         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1542             continue;
1543         traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1544         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1545             continue;
1546         traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1547         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1548             continue;
1549         traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1550         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1551             continue;
1552         traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1553         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1554             continue;
1555
1556         // rule 4: we must "see" some spawnpoint or item
1557         for(sp = world; (sp = find(sp, classname, "info_player_deathmatch")); )
1558                 if(checkpvs(mstart, sp))
1559                         if((traceline(mstart, sp.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1560                                 break;
1561         if(!sp)
1562         {
1563                 for(sp = world; (sp = findflags(sp, flags, FL_ITEM)); )
1564                         if(checkpvs(mstart, sp))
1565                                 if((traceline(mstart, sp.origin + (sp.mins + sp.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1566                                         break;
1567                 if(!sp)
1568                         continue;
1569         }
1570
1571         // find a random vector to "look at"
1572         end.x = org.x + random() * delta.x;
1573         end.y = org.y + random() * delta.y;
1574         end.z = org.z + random() * delta.z;
1575         end = start + normalize(end - start) * vlen(delta);
1576
1577         // rule 4: start TO end must not be too short
1578         tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1579         if (trace_startsolid)
1580             continue;
1581         if (trace_fraction < minviewdistance / vlen(delta))
1582             continue;
1583
1584         // rule 5: don't want to look at sky
1585         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1586             continue;
1587
1588         // rule 6: we must not end up in trigger_hurt
1589         if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1590             continue;
1591
1592         break;
1593     }
1594
1595     e.dphitcontentsmask = m;
1596
1597     if (i < attempts)
1598     {
1599         setorigin(e, start);
1600         e.angles = vectoangles(end - start);
1601         dprint("Needed ", ftos(i + 1), " attempts\n");
1602         return true;
1603     }
1604     else
1605         return false;
1606 }
1607
1608 void write_recordmarker(entity pl, float tstart, float dt)
1609 {
1610     GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1611
1612     // also write a marker into demo files for demotc-race-record-extractor to find
1613     stuffcmd(pl,
1614              strcat(
1615                  strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1616                  " ", ftos(tstart), " ", ftos(dt), "\n"));
1617 }
1618
1619 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float allowcenter, float algn)
1620 {
1621         switch(algn)
1622         {
1623                 default:
1624                 case 3: // right
1625                         break;
1626
1627                 case 4: // left
1628                         vecs.y = -vecs.y;
1629                         break;
1630
1631                 case 1:
1632                         if(allowcenter) // 2: allow center handedness
1633                         {
1634                                 // center
1635                                 vecs.y = 0;
1636                                 vecs.z -= 2;
1637                         }
1638                         else
1639                         {
1640                                 // right
1641                         }
1642                         break;
1643
1644                 case 2:
1645                         if(allowcenter) // 2: allow center handedness
1646                         {
1647                                 // center
1648                                 vecs.y = 0;
1649                                 vecs.z -= 2;
1650                         }
1651                         else
1652                         {
1653                                 // left
1654                                 vecs.y = -vecs.y;
1655                         }
1656                         break;
1657         }
1658         return vecs;
1659 }
1660
1661 vector shotorg_adjust_values(vector vecs, float y_is_right, float visual, float algn)
1662 {
1663         string s;
1664         vector v;
1665
1666         if (autocvar_g_shootfromeye)
1667         {
1668                 if (visual)
1669                 {
1670                         if (autocvar_g_shootfromclient) { vecs = shotorg_adjustfromclient(vecs, y_is_right, (autocvar_g_shootfromclient >= 2), algn); }
1671                         else { vecs.y = 0; vecs.z -= 2; }
1672                 }
1673                 else
1674                 {
1675                         vecs.y = 0;
1676                         vecs.z = 0;
1677                 }
1678         }
1679         else if (autocvar_g_shootfromcenter)
1680         {
1681                 vecs.y = 0;
1682                 vecs.z -= 2;
1683         }
1684         else if ((s = autocvar_g_shootfromfixedorigin) != "")
1685         {
1686                 v = stov(s);
1687                 if (y_is_right)
1688                         v.y = -v.y;
1689                 if (v.x != 0)
1690                         vecs.x = v.x;
1691                 vecs.y = v.y;
1692                 vecs.z = v.z;
1693         }
1694         else if (autocvar_g_shootfromclient)
1695         {
1696                 vecs = shotorg_adjustfromclient(vecs, y_is_right, (autocvar_g_shootfromclient >= 2), algn);
1697         }
1698         return vecs;
1699 }
1700
1701 vector shotorg_adjust(vector vecs, float y_is_right, float visual)
1702 {
1703         return shotorg_adjust_values(vecs, y_is_right, visual, self.owner.cvar_cl_gunalign);
1704 }
1705
1706
1707 void attach_sameorigin(entity e, entity to, string tag)
1708 {
1709     vector org, t_forward, t_left, t_up, e_forward, e_up;
1710     float tagscale;
1711
1712     org = e.origin - gettaginfo(to, gettagindex(to, tag));
1713     tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1714     t_forward = v_forward * tagscale;
1715     t_left = v_right * -tagscale;
1716     t_up = v_up * tagscale;
1717
1718     e.origin_x = org * t_forward;
1719     e.origin_y = org * t_left;
1720     e.origin_z = org * t_up;
1721
1722     // current forward and up directions
1723     if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1724                 e.angles = AnglesTransform_FromVAngles(e.angles);
1725         else
1726                 e.angles = AnglesTransform_FromAngles(e.angles);
1727     fixedmakevectors(e.angles);
1728
1729     // untransform forward, up!
1730     e_forward.x = v_forward * t_forward;
1731     e_forward.y = v_forward * t_left;
1732     e_forward.z = v_forward * t_up;
1733     e_up.x = v_up * t_forward;
1734     e_up.y = v_up * t_left;
1735     e_up.z = v_up * t_up;
1736
1737     e.angles = fixedvectoangles2(e_forward, e_up);
1738     if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1739                 e.angles = AnglesTransform_ToVAngles(e.angles);
1740         else
1741                 e.angles = AnglesTransform_ToAngles(e.angles);
1742
1743     setattachment(e, to, tag);
1744     setorigin(e, e.origin);
1745 }
1746
1747 void detach_sameorigin(entity e)
1748 {
1749     vector org;
1750     org = gettaginfo(e, 0);
1751     e.angles = fixedvectoangles2(v_forward, v_up);
1752     if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1753                 e.angles = AnglesTransform_ToVAngles(e.angles);
1754         else
1755                 e.angles = AnglesTransform_ToAngles(e.angles);
1756     setorigin(e, org);
1757     setattachment(e, world, "");
1758     setorigin(e, e.origin);
1759 }
1760
1761 void follow_sameorigin(entity e, entity to)
1762 {
1763     e.movetype = MOVETYPE_FOLLOW; // make the hole follow
1764     e.aiment = to; // make the hole follow bmodel
1765     e.punchangle = to.angles; // the original angles of bmodel
1766     e.view_ofs = e.origin - to.origin; // relative origin
1767     e.v_angle = e.angles - to.angles; // relative angles
1768 }
1769
1770 void unfollow_sameorigin(entity e)
1771 {
1772     e.movetype = MOVETYPE_NONE;
1773 }
1774
1775 entity gettaginfo_relative_ent;
1776 vector gettaginfo_relative(entity e, float tag)
1777 {
1778     if (!gettaginfo_relative_ent)
1779     {
1780         gettaginfo_relative_ent = spawn();
1781         gettaginfo_relative_ent.effects = EF_NODRAW;
1782     }
1783     gettaginfo_relative_ent.model = e.model;
1784     gettaginfo_relative_ent.modelindex = e.modelindex;
1785     gettaginfo_relative_ent.frame = e.frame;
1786     return gettaginfo(gettaginfo_relative_ent, tag);
1787 }
1788
1789 .float scale2;
1790
1791 float modeleffect_SendEntity(entity to, int sf)
1792 {
1793         float f;
1794         WriteByte(MSG_ENTITY, ENT_CLIENT_MODELEFFECT);
1795
1796         f = 0;
1797         if(self.velocity != '0 0 0')
1798                 f |= 1;
1799         if(self.angles != '0 0 0')
1800                 f |= 2;
1801         if(self.avelocity != '0 0 0')
1802                 f |= 4;
1803
1804         WriteByte(MSG_ENTITY, f);
1805         WriteShort(MSG_ENTITY, self.modelindex);
1806         WriteByte(MSG_ENTITY, self.skin);
1807         WriteByte(MSG_ENTITY, self.frame);
1808         WriteCoord(MSG_ENTITY, self.origin.x);
1809         WriteCoord(MSG_ENTITY, self.origin.y);
1810         WriteCoord(MSG_ENTITY, self.origin.z);
1811         if(f & 1)
1812         {
1813                 WriteCoord(MSG_ENTITY, self.velocity.x);
1814                 WriteCoord(MSG_ENTITY, self.velocity.y);
1815                 WriteCoord(MSG_ENTITY, self.velocity.z);
1816         }
1817         if(f & 2)
1818         {
1819                 WriteCoord(MSG_ENTITY, self.angles.x);
1820                 WriteCoord(MSG_ENTITY, self.angles.y);
1821                 WriteCoord(MSG_ENTITY, self.angles.z);
1822         }
1823         if(f & 4)
1824         {
1825                 WriteCoord(MSG_ENTITY, self.avelocity.x);
1826                 WriteCoord(MSG_ENTITY, self.avelocity.y);
1827                 WriteCoord(MSG_ENTITY, self.avelocity.z);
1828         }
1829         WriteShort(MSG_ENTITY, self.scale * 256.0);
1830         WriteShort(MSG_ENTITY, self.scale2 * 256.0);
1831         WriteByte(MSG_ENTITY, self.teleport_time * 100.0);
1832         WriteByte(MSG_ENTITY, self.fade_time * 100.0);
1833         WriteByte(MSG_ENTITY, self.alpha * 255.0);
1834
1835         return true;
1836 }
1837
1838 void modeleffect_spawn(string m, float s, float f, vector o, vector v, vector ang, vector angv, float s0, float s2, float a, float t1, float t2)
1839 {
1840         entity e;
1841         float sz;
1842         e = spawn();
1843         e.classname = "modeleffect";
1844         setmodel(e, m);
1845         e.frame = f;
1846         setorigin(e, o);
1847         e.velocity = v;
1848         e.angles = ang;
1849         e.avelocity = angv;
1850         e.alpha = a;
1851         e.teleport_time = t1;
1852         e.fade_time = t2;
1853         e.skin = s;
1854         if(s0 >= 0)
1855                 e.scale = s0 / max6(-e.mins.x, -e.mins.y, -e.mins.z, e.maxs.x, e.maxs.y, e.maxs.z);
1856         else
1857                 e.scale = -s0;
1858         if(s2 >= 0)
1859                 e.scale2 = s2 / max6(-e.mins.x, -e.mins.y, -e.mins.z, e.maxs.x, e.maxs.y, e.maxs.z);
1860         else
1861                 e.scale2 = -s2;
1862         sz = max(e.scale, e.scale2);
1863         setsize(e, e.mins * sz, e.maxs * sz);
1864         Net_LinkEntity(e, false, 0.1, modeleffect_SendEntity);
1865 }
1866
1867 void shockwave_spawn(string m, vector org, float sz, float t1, float t2)
1868 {
1869         return modeleffect_spawn(m, 0, 0, org, '0 0 0', '0 0 0', '0 0 0', 0, sz, 1, t1, t2);
1870 }
1871
1872 float randombit(float bits)
1873 {
1874         if(!(bits & (bits-1))) // this ONLY holds for powers of two!
1875                 return bits;
1876
1877         float n, f, b, r;
1878
1879         r = random();
1880         b = 0;
1881         n = 0;
1882
1883         for(f = 1; f <= bits; f *= 2)
1884         {
1885                 if(bits & f)
1886                 {
1887                         ++n;
1888                         r *= n;
1889                         if(r <= 1)
1890                                 b = f;
1891                         else
1892                                 r = (r - 1) / (n - 1);
1893                 }
1894         }
1895
1896         return b;
1897 }
1898
1899 float randombits(float bits, float k, float error_return)
1900 {
1901         float r;
1902         r = 0;
1903         while(k > 0 && bits != r)
1904         {
1905                 r += randombit(bits - r);
1906                 --k;
1907         }
1908         if(error_return)
1909                 if(k > 0)
1910                         return -1; // all
1911         return r;
1912 }
1913
1914 void randombit_test(float bits, float iter)
1915 {
1916         while(iter > 0)
1917         {
1918                 print(ftos(randombit(bits)), "\n");
1919                 --iter;
1920         }
1921 }
1922
1923 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
1924 {
1925         if(halflifedist > 0)
1926                 return pow(0.5, (bound(mindist, d, maxdist) - mindist) / halflifedist);
1927         else if(halflifedist < 0)
1928                 return pow(0.5, (bound(mindist, d, maxdist) - maxdist) / halflifedist);
1929         else
1930                 return 1;
1931 }
1932
1933
1934 .string aiment_classname;
1935 .float aiment_deadflag;
1936 void SetMovetypeFollow(entity ent, entity e)
1937 {
1938         // FIXME this may not be warpzone aware
1939         ent.movetype = MOVETYPE_FOLLOW; // make the hole follow
1940         ent.solid = SOLID_NOT; // MOVETYPE_FOLLOW is always non-solid - this means this cannot be teleported by warpzones any more! Instead, we must notice when our owner gets teleported.
1941         ent.aiment = e; // make the hole follow bmodel
1942         ent.punchangle = e.angles; // the original angles of bmodel
1943         ent.view_ofs = ent.origin - e.origin; // relative origin
1944         ent.v_angle = ent.angles - e.angles; // relative angles
1945         ent.aiment_classname = strzone(e.classname);
1946         ent.aiment_deadflag = e.deadflag;
1947 }
1948 void UnsetMovetypeFollow(entity ent)
1949 {
1950         ent.movetype = MOVETYPE_FLY;
1951         PROJECTILE_MAKETRIGGER(ent);
1952         ent.aiment = world;
1953 }
1954 float LostMovetypeFollow(entity ent)
1955 {
1956 /*
1957         if(ent.movetype != MOVETYPE_FOLLOW)
1958                 if(ent.aiment)
1959                         error("???");
1960 */
1961         if(ent.aiment)
1962         {
1963                 if(ent.aiment.classname != ent.aiment_classname)
1964                         return 1;
1965                 if(ent.aiment.deadflag != ent.aiment_deadflag)
1966                         return 1;
1967         }
1968         return 0;
1969 }
1970
1971 float isPushable(entity e)
1972 {
1973         if(e.iscreature)
1974                 return true;
1975         if(e.pushable)
1976                 return true;
1977         switch(e.classname)
1978         {
1979                 case "body":
1980                 case "droppedweapon":
1981                 case "keepawayball":
1982                 case "nexball_basketball":
1983                 case "nexball_football":
1984                         return true;
1985                 case "bullet": // antilagged bullets can't hit this either
1986                         return false;
1987         }
1988         if (e.projectiledeathtype)
1989                 return true;
1990         return false;
1991 }