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