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