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