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