]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/miscfunctions.qc
Fix weaplast
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / miscfunctions.qc
1 #include "miscfunctions.qh"
2 #include "antilag.qh"
3 #include "command/common.qh"
4 #include "constants.qh"
5 #include "g_hook.qh"
6 #include "ipban.qh"
7 #include "mutators/_mod.qh"
8 #include "../common/t_items.qh"
9 #include "weapons/accuracy.qh"
10 #include "weapons/csqcprojectile.qh"
11 #include "weapons/selection.qh"
12 #include "../common/command/_mod.qh"
13 #include "../common/constants.qh"
14 #include "../common/deathtypes/all.qh"
15 #include "../common/mapinfo.qh"
16 #include "../common/notifications/all.qh"
17 #include "../common/playerstats.qh"
18 #include "../common/teams.qh"
19 #include "../common/triggers/subs.qh"
20 #include "../common/util.qh"
21 #include "../common/turrets/sv_turrets.qh"
22 #include <common/weapons/_all.qh>
23 #include "../common/vehicles/sv_vehicles.qh"
24 #include "../common/vehicles/vehicle.qh"
25 #include "../common/items/_mod.qh"
26 #include "../common/state.qh"
27 #include "../common/effects/qc/globalsound.qh"
28 #include "../lib/csqcmodel/sv_model.qh"
29 #include "../lib/warpzone/anglestransform.qh"
30 #include "../lib/warpzone/server.qh"
31
32 void crosshair_trace(entity pl)
33 {
34         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));
35 }
36 .bool ctrace_solidchanged;
37 void crosshair_trace_plusvisibletriggers(entity pl)
38 {
39         FOREACH_ENTITY_FLOAT(solid, SOLID_TRIGGER,
40         {
41                 if(it.model != "")
42                 {
43                         it.solid = SOLID_BSP;
44                         it.ctrace_solidchanged = true;
45                 }
46         });
47
48         crosshair_trace(pl);
49
50         FOREACH_ENTITY_FLOAT(ctrace_solidchanged, true,
51         {
52                 it.solid = SOLID_TRIGGER;
53                 it.ctrace_solidchanged = false;
54         });
55 }
56 void WarpZone_crosshair_trace(entity pl)
57 {
58         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));
59 }
60
61
62 string admin_name()
63 {
64         if(autocvar_sv_adminnick != "")
65                 return autocvar_sv_adminnick;
66         else
67                 return "SERVER ADMIN";
68 }
69
70
71 void GameLogEcho(string s)
72 {
73     string fn;
74     int matches;
75
76     if (autocvar_sv_eventlog_files)
77     {
78         if (!logfile_open)
79         {
80             logfile_open = true;
81             matches = autocvar_sv_eventlog_files_counter + 1;
82             cvar_set("sv_eventlog_files_counter", itos(matches));
83             fn = ftos(matches);
84             if (strlen(fn) < 8)
85                 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
86             fn = strcat(autocvar_sv_eventlog_files_nameprefix, fn, autocvar_sv_eventlog_files_namesuffix);
87             logfile = fopen(fn, FILE_APPEND);
88             fputs(logfile, ":logversion:3\n");
89         }
90         if (logfile >= 0)
91         {
92             if (autocvar_sv_eventlog_files_timestamps)
93                 fputs(logfile, strcat(":time:", strftime(true, "%Y-%m-%d %H:%M:%S", "\n", s, "\n")));
94             else
95                 fputs(logfile, strcat(s, "\n"));
96         }
97     }
98     if (autocvar_sv_eventlog_console)
99     {
100         dedicated_print(strcat(s, "\n"));
101     }
102 }
103
104 void GameLogInit()
105 {
106     logfile_open = 0;
107     // will be opened later
108 }
109
110 void GameLogClose()
111 {
112     if (logfile_open && logfile >= 0)
113     {
114         fclose(logfile);
115         logfile = -1;
116     }
117 }
118
119 entity findnearest(vector point, .string field, string value, vector axismod)
120 {
121     entity localhead;
122     float i;
123     float j;
124     float len;
125     vector dist;
126
127     float num_nearest;
128     num_nearest = 0;
129
130     localhead = find(NULL, field, value);
131     while (localhead)
132     {
133         if ((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target == "###item###")
134             dist = localhead.oldorigin;
135         else
136             dist = localhead.origin;
137         dist = dist - point;
138         dist = dist.x * axismod.x * '1 0 0' + dist.y * axismod.y * '0 1 0' + dist.z * axismod.z * '0 0 1';
139         len = vlen(dist);
140
141         for (i = 0; i < num_nearest; ++i)
142         {
143             if (len < nearest_length[i])
144                 break;
145         }
146
147         // now i tells us where to insert at
148         //   INSERTION SORT! YOU'VE SEEN IT! RUN!
149         if (i < NUM_NEAREST_ENTITIES)
150         {
151             for (j = NUM_NEAREST_ENTITIES - 1; j >= i; --j)
152             {
153                 nearest_length[j + 1] = nearest_length[j];
154                 nearest_entity[j + 1] = nearest_entity[j];
155             }
156             nearest_length[i] = len;
157             nearest_entity[i] = localhead;
158             if (num_nearest < NUM_NEAREST_ENTITIES)
159                 num_nearest = num_nearest + 1;
160         }
161
162         localhead = find(localhead, field, value);
163     }
164
165     // now use the first one from our list that we can see
166     for (i = 0; i < num_nearest; ++i)
167     {
168         traceline(point, nearest_entity[i].origin, true, NULL);
169         if (trace_fraction == 1)
170         {
171             if (i != 0)
172             {
173                 LOG_TRACE("Nearest point (");
174                 LOG_TRACE(nearest_entity[0].netname);
175                 LOG_TRACE(") is not visible, using a visible one.");
176             }
177             return nearest_entity[i];
178         }
179     }
180
181     if (num_nearest == 0)
182         return NULL;
183
184     LOG_TRACE("Not seeing any location point, using nearest as fallback.");
185     /* DEBUGGING CODE:
186     dprint("Candidates were: ");
187     for(j = 0; j < num_nearest; ++j)
188     {
189         if(j != 0)
190                 dprint(", ");
191         dprint(nearest_entity[j].netname);
192     }
193     dprint("\n");
194     */
195
196     return nearest_entity[0];
197 }
198
199 string NearestLocation(vector p)
200 {
201     entity loc;
202     string ret;
203     ret = "somewhere";
204     loc = findnearest(p, classname, "target_location", '1 1 1');
205     if (loc)
206     {
207         ret = loc.message;
208     }
209     else
210     {
211         loc = findnearest(p, target, "###item###", '1 1 4');
212         if (loc)
213             ret = loc.netname;
214     }
215     return ret;
216 }
217
218 string formatmessage(entity this, string msg)
219 {
220         float p, p1, p2;
221         float n;
222         vector cursor;
223         entity cursor_ent;
224         string escape;
225         string replacement;
226         string ammoitems;
227         p = 0;
228         n = 7;
229
230         ammoitems = "batteries";
231         if(this.items & ITEM_Plasma.m_itemid) ammoitems = ITEM_Plasma.m_name;
232         if(this.items & ITEM_Cells.m_itemid) ammoitems = ITEM_Cells.m_name;
233         if(this.items & ITEM_Rockets.m_itemid) ammoitems = ITEM_Rockets.m_name;
234         if(this.items & ITEM_Shells.m_itemid) ammoitems = ITEM_Shells.m_name;
235
236         WarpZone_crosshair_trace(this);
237         cursor = trace_endpos;
238         cursor_ent = trace_ent;
239
240         while (1) {
241                 if (n < 1)
242                         break; // too many replacements
243
244                 n = n - 1;
245                 p1 = strstrofs(msg, "%", p); // NOTE: this destroys msg as it's a tempstring!
246                 p2 = strstrofs(msg, "\\", p); // NOTE: this destroys msg as it's a tempstring!
247
248                 if (p1 < 0)
249                         p1 = p2;
250
251                 if (p2 < 0)
252                         p2 = p1;
253
254                 p = min(p1, p2);
255
256                 if (p < 0)
257                         break;
258
259                 replacement = substring(msg, p, 2);
260                 escape = substring(msg, p + 1, 1);
261
262                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
263
264                 switch(escape)
265                 {
266                         case "%": replacement = "%"; break;
267                         case "\\":replacement = "\\"; break;
268                         case "n": replacement = "\n"; break;
269                         case "a": replacement = ftos(floor(this.armorvalue)); break;
270                         case "h": replacement = ftos(floor(this.health)); break;
271                         case "l": replacement = NearestLocation(this.origin); break;
272                         case "y": replacement = NearestLocation(cursor); break;
273                         case "d": replacement = NearestLocation(this.death_origin); break;
274                         case "w": replacement = ((this.(weaponentity).m_weapon == WEP_Null) ? ((this.(weaponentity).m_switchweapon == WEP_Null) ? Weapons_from(this.(weaponentity).cnt) : this.(weaponentity).m_switchweapon) : this.(weaponentity).m_weapon).m_name; break;
275                         case "W": replacement = ammoitems; break;
276                         case "x": replacement = ((cursor_ent.netname == "" || !cursor_ent) ? "nothing" : cursor_ent.netname); break;
277                         case "s": replacement = ftos(vlen(this.velocity - this.velocity_z * '0 0 1')); break;
278                         case "S": replacement = ftos(vlen(this.velocity)); break;
279                         case "t": replacement = seconds_tostring(ceil(max(0, autocvar_timelimit * 60 + game_starttime - time))); break;
280                         case "T": replacement = seconds_tostring(floor(time - game_starttime)); break;
281                         default:
282                         {
283                                 MUTATOR_CALLHOOK(FormatMessage, this, escape, replacement, msg);
284                                 replacement = M_ARGV(2, string);
285                                 break;
286                         }
287                 }
288
289                 msg = strcat(substring(msg, 0, p), replacement, substring(msg, p+2, strlen(msg) - (p+2)));
290                 p = p + strlen(replacement);
291         }
292         return msg;
293 }
294
295 /*
296 =============
297 GetCvars
298 =============
299 Called with:
300   0:  sends the request
301   >0: receives a cvar from name=argv(f) value=argv(f+1)
302 */
303 void GetCvars_handleString(entity this, string thisname, float f, .string field, string name)
304 {
305         if (f < 0)
306         {
307                 if (this.(field))
308                         strunzone(this.(field));
309                 this.(field) = string_null;
310         }
311         else if (f > 0)
312         {
313                 if (thisname == name)
314                 {
315                         if (this.(field))
316                                 strunzone(this.(field));
317                         this.(field) = strzone(argv(f + 1));
318                 }
319         }
320         else
321                 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
322 }
323 void GetCvars_handleString_Fixup(entity this, string thisname, float f, .string field, string name, string(entity, string) func)
324 {
325         GetCvars_handleString(this, thisname, f, field, name);
326         if (f >= 0) // also initialize to the fitting value for "" when sending cvars out
327                 if (thisname == name)
328                 {
329                         string s = func(this, strcat1(this.(field)));
330                         if (s != this.(field))
331                         {
332                                 strunzone(this.(field));
333                                 this.(field) = strzone(s);
334                         }
335                 }
336 }
337 void GetCvars_handleFloat(entity this, string thisname, float f, .float field, string name)
338 {
339         if (f < 0)
340         {
341         }
342         else if (f > 0)
343         {
344                 if (thisname == name)
345                         this.(field) = stof(argv(f + 1));
346         }
347         else
348                 stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
349 }
350 void GetCvars_handleFloatOnce(entity this, string thisname, float f, .float field, string name)
351 {
352         if (f < 0)
353         {
354         }
355         else if (f > 0)
356         {
357                 if (thisname == name)
358                 {
359                         if (!this.(field))
360                         {
361                                 this.(field) = stof(argv(f + 1));
362                                 if (!this.(field))
363                                         this.(field) = -1;
364                         }
365                 }
366         }
367         else
368         {
369                 if (!this.(field))
370                         stuffcmd(this, strcat("cl_cmd sendcvar ", name, "\n"));
371         }
372 }
373 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo)
374 {
375         string o;
376         o = W_FixWeaponOrder_ForceComplete(wo);
377         if(this.weaponorder_byimpulse)
378         {
379                 strunzone(this.weaponorder_byimpulse);
380                 this.weaponorder_byimpulse = string_null;
381         }
382         this.weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(o));
383         return o;
384 }
385
386 REPLICATE(autoswitch, bool, "cl_autoswitch");
387
388 REPLICATE(cvar_cl_allow_uid2name, bool, "cl_allow_uid2name");
389
390 REPLICATE(cvar_cl_autoscreenshot, int, "cl_autoscreenshot");
391
392 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
393
394 REPLICATE(cvar_cl_clippedspectating, bool, "cl_clippedspectating");
395
396 REPLICATE(cvar_cl_handicap, float, "cl_handicap");
397
398 REPLICATE(cvar_cl_jetpack_jump, bool, "cl_jetpack_jump");
399
400 REPLICATE(cvar_cl_movement_track_canjump, bool, "cl_movement_track_canjump");
401
402 REPLICATE(cvar_cl_newusekeysupported, bool, "cl_newusekeysupported");
403
404 REPLICATE(cvar_cl_noantilag, bool, "cl_noantilag");
405
406 REPLICATE(cvar_cl_physics, string, "cl_physics");
407
408 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
409
410 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
411
412 REPLICATE(cvar_cl_weaponimpulsemode, int, "cl_weaponimpulsemode");
413
414 REPLICATE(cvar_g_xonoticversion, string, "g_xonoticversion");
415
416 /**
417  * @param f -1: cleanup, 0: request, 1: receive
418  */
419 void GetCvars(entity this, int f)
420 {
421         string s = string_null;
422
423         if (f > 0)
424                 s = strcat1(argv(f));
425
426         get_cvars_f = f;
427         get_cvars_s = s;
428         MUTATOR_CALLHOOK(GetCvars);
429
430         Notification_GetCvars(this);
431
432         ReplicateVars(this, s, f);
433
434         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriority, "cl_weaponpriority", W_FixWeaponOrder_ForceComplete_AndBuildImpulseList);
435         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[0], "cl_weaponpriority0", W_FixWeaponOrder_AllowIncomplete);
436         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[1], "cl_weaponpriority1", W_FixWeaponOrder_AllowIncomplete);
437         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[2], "cl_weaponpriority2", W_FixWeaponOrder_AllowIncomplete);
438         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[3], "cl_weaponpriority3", W_FixWeaponOrder_AllowIncomplete);
439         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[4], "cl_weaponpriority4", W_FixWeaponOrder_AllowIncomplete);
440         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[5], "cl_weaponpriority5", W_FixWeaponOrder_AllowIncomplete);
441         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[6], "cl_weaponpriority6", W_FixWeaponOrder_AllowIncomplete);
442         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[7], "cl_weaponpriority7", W_FixWeaponOrder_AllowIncomplete);
443         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[8], "cl_weaponpriority8", W_FixWeaponOrder_AllowIncomplete);
444         GetCvars_handleString_Fixup(this, s, f, cvar_cl_weaponpriorities[9], "cl_weaponpriority9", W_FixWeaponOrder_AllowIncomplete);
445
446         GetCvars_handleFloat(this, s, f, cvar_cl_allow_uidtracking, "cl_allow_uidtracking");
447
448         // fixup of switchweapon (needed for LMS or when spectating is disabled, as PutClientInServer comes too early)
449         if (f > 0)
450         {
451                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
452                 if (s == "cl_weaponpriority")
453                         if (this.(weaponentity)) this.(weaponentity).m_switchweapon = w_getbestweapon(this);
454                 if (s == "cl_allow_uidtracking")
455                         PlayerStats_GameReport_AddPlayer(this);
456         }
457 }
458
459 // decolorizes and team colors the player name when needed
460 string playername(entity p)
461 {
462     string t;
463     if (teamplay && !intermission_running && IS_PLAYER(p))
464     {
465         t = Team_ColorCode(p.team);
466         return strcat(t, strdecolorize(p.netname));
467     }
468     else
469         return p.netname;
470 }
471
472 float want_weapon(entity weaponinfo, float allguns) // WEAPONTODO: what still needs done?
473 {
474         int i = weaponinfo.m_id;
475         int d = 0;
476         bool allow_mutatorblocked = false;
477
478         if(!i)
479                 return 0;
480
481         bool mutator_returnvalue = MUTATOR_CALLHOOK(WantWeapon, weaponinfo, d, allguns, allow_mutatorblocked);
482         d = M_ARGV(1, float);
483         allguns = M_ARGV(2, bool);
484         allow_mutatorblocked = M_ARGV(3, bool);
485
486         if(allguns)
487         {
488                 if(weaponinfo.spawnflags & WEP_FLAG_NORMAL)
489                         d = true;
490                 else
491                         d = false;
492         }
493         else if(!mutator_returnvalue)
494                 d = !(!weaponinfo.weaponstart);
495
496         if(!allow_mutatorblocked && (weaponinfo.spawnflags & WEP_FLAG_MUTATORBLOCKED)) // never default mutator blocked guns
497                 d = 0;
498
499         float t = weaponinfo.weaponstartoverride;
500
501         //print(strcat("want_weapon: ", weaponinfo.netname, " - d: ", ftos(d), ", t: ", ftos(t), ". \n"));
502
503         // bit order in t:
504         // 1: want or not
505         // 2: is default?
506         // 4: is set by default?
507         if(t < 0)
508                 t = 4 | (3 * d);
509         else
510                 t |= (2 * d);
511
512         return t;
513 }
514
515 void readplayerstartcvars()
516 {
517         float i, t;
518         string s;
519
520         // initialize starting values for players
521         start_weapons = '0 0 0';
522         start_weapons_default = '0 0 0';
523         start_weapons_defaultmask = '0 0 0';
524         start_items = 0;
525         start_ammo_shells = 0;
526         start_ammo_nails = 0;
527         start_ammo_rockets = 0;
528         start_ammo_cells = 0;
529         start_ammo_plasma = 0;
530         start_health = cvar("g_balance_health_start");
531         start_armorvalue = cvar("g_balance_armor_start");
532
533         g_weaponarena = 0;
534         g_weaponarena_weapons = '0 0 0';
535
536         s = cvar_string("g_weaponarena");
537
538         MUTATOR_CALLHOOK(SetWeaponArena, s);
539         s = M_ARGV(0, string);
540
541         if (s == "0" || s == "")
542         {
543                 // no arena
544         }
545         else if (s == "off")
546         {
547                 // forcibly turn off weaponarena
548         }
549         else if (s == "all" || s == "1")
550         {
551                 g_weaponarena = 1;
552                 g_weaponarena_list = "All Weapons";
553                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
554                         if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
555                                 g_weaponarena_weapons |= (it.m_wepset);
556                 ));
557         }
558         else if (s == "most")
559         {
560                 g_weaponarena = 1;
561                 g_weaponarena_list = "Most Weapons";
562                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
563                         if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
564                                 if(it.spawnflags & WEP_FLAG_NORMAL)
565                                         g_weaponarena_weapons |= (it.m_wepset);
566                 ));
567         }
568         else if (s == "none")
569         {
570                 g_weaponarena = 1;
571                 g_weaponarena_list = "No Weapons";
572         }
573         else
574         {
575                 g_weaponarena = 1;
576                 t = tokenize_console(s);
577                 g_weaponarena_list = "";
578                 for (i = 0; i < t; ++i)
579                 {
580                         s = argv(i);
581                         FOREACH(Weapons, it != WEP_Null, LAMBDA(
582                                 if(it.netname == s)
583                                 {
584                                         g_weaponarena_weapons |= (it.m_wepset);
585                                         g_weaponarena_list = strcat(g_weaponarena_list, it.m_name, " & ");
586                                         break;
587                                 }
588                         ));
589                 }
590                 g_weaponarena_list = strzone(substring(g_weaponarena_list, 0, strlen(g_weaponarena_list) - 3));
591         }
592
593         if(g_weaponarena)
594                 g_weaponarena_random = cvar("g_weaponarena_random");
595         else
596                 g_weaponarena_random = 0;
597         g_weaponarena_random_with_blaster = cvar("g_weaponarena_random_with_blaster");
598
599         if (g_weaponarena)
600         {
601                 g_weapon_stay = 0; // incompatible
602                 start_weapons = g_weaponarena_weapons;
603                 start_items |= IT_UNLIMITED_AMMO;
604         }
605         else
606         {
607                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
608                         int w = want_weapon(it, false);
609                         WepSet s = it.m_wepset;
610                         if(w & 1)
611                                 start_weapons |= s;
612                         if(w & 2)
613                                 start_weapons_default |= s;
614                         if(w & 4)
615                                 start_weapons_defaultmask |= s;
616                 ));
617         }
618
619         if(!cvar("g_use_ammunition"))
620                 start_items |= IT_UNLIMITED_AMMO;
621
622         if(start_items & IT_UNLIMITED_WEAPON_AMMO)
623         {
624                 start_ammo_shells = 999;
625                 start_ammo_nails = 999;
626                 start_ammo_rockets = 999;
627                 start_ammo_cells = 999;
628                 start_ammo_plasma = 999;
629                 start_ammo_fuel = 999;
630         }
631         else
632         {
633                 start_ammo_shells = cvar("g_start_ammo_shells");
634                 start_ammo_nails = cvar("g_start_ammo_nails");
635                 start_ammo_rockets = cvar("g_start_ammo_rockets");
636                 start_ammo_cells = cvar("g_start_ammo_cells");
637                 start_ammo_plasma = cvar("g_start_ammo_plasma");
638                 start_ammo_fuel = cvar("g_start_ammo_fuel");
639         }
640
641         if (warmup_stage)
642         {
643                 warmup_start_ammo_shells = start_ammo_shells;
644                 warmup_start_ammo_nails = start_ammo_nails;
645                 warmup_start_ammo_rockets = start_ammo_rockets;
646                 warmup_start_ammo_cells = start_ammo_cells;
647                 warmup_start_ammo_plasma = start_ammo_plasma;
648                 warmup_start_ammo_fuel = start_ammo_fuel;
649                 warmup_start_health = start_health;
650                 warmup_start_armorvalue = start_armorvalue;
651                 warmup_start_weapons = start_weapons;
652                 warmup_start_weapons_default = start_weapons_default;
653                 warmup_start_weapons_defaultmask = start_weapons_defaultmask;
654
655                 if (!g_weaponarena && !g_ca && !g_freezetag)
656                 {
657                         warmup_start_ammo_shells = cvar("g_warmup_start_ammo_shells");
658                         warmup_start_ammo_nails = cvar("g_warmup_start_ammo_nails");
659                         warmup_start_ammo_rockets = cvar("g_warmup_start_ammo_rockets");
660                         warmup_start_ammo_cells = cvar("g_warmup_start_ammo_cells");
661                         warmup_start_ammo_plasma = cvar("g_warmup_start_ammo_plasma");
662                         warmup_start_ammo_fuel = cvar("g_warmup_start_ammo_fuel");
663                         warmup_start_health = cvar("g_warmup_start_health");
664                         warmup_start_armorvalue = cvar("g_warmup_start_armor");
665                         warmup_start_weapons = '0 0 0';
666                         warmup_start_weapons_default = '0 0 0';
667                         warmup_start_weapons_defaultmask = '0 0 0';
668                         FOREACH(Weapons, it != WEP_Null, LAMBDA(
669                                 int w = want_weapon(it, g_warmup_allguns);
670                                 WepSet s = (it.m_wepset);
671                                 if(w & 1)
672                                         warmup_start_weapons |= s;
673                                 if(w & 2)
674                                         warmup_start_weapons_default |= s;
675                                 if(w & 4)
676                                         warmup_start_weapons_defaultmask |= s;
677                         ));
678                 }
679         }
680
681         if (g_jetpack)
682                 start_items |= ITEM_Jetpack.m_itemid;
683
684         MUTATOR_CALLHOOK(SetStartItems);
685
686         if (start_items & ITEM_Jetpack.m_itemid)
687         {
688                 start_items |= ITEM_JetpackRegen.m_itemid;
689                 start_ammo_fuel = max(start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
690                 warmup_start_ammo_fuel = max(warmup_start_ammo_fuel, cvar("g_balance_fuel_rotstable"));
691         }
692
693         WepSet precache_weapons = start_weapons;
694         if (g_warmup_allguns != 1)
695                 precache_weapons |= warmup_start_weapons;
696         FOREACH(Weapons, it != WEP_Null, LAMBDA(
697                 if(precache_weapons & (it.m_wepset))
698                         it.wr_init(it);
699         ));
700
701         start_ammo_shells = max(0, start_ammo_shells);
702         start_ammo_nails = max(0, start_ammo_nails);
703         start_ammo_rockets = max(0, start_ammo_rockets);
704         start_ammo_cells = max(0, start_ammo_cells);
705         start_ammo_plasma = max(0, start_ammo_plasma);
706         start_ammo_fuel = max(0, start_ammo_fuel);
707
708         warmup_start_ammo_shells = max(0, warmup_start_ammo_shells);
709         warmup_start_ammo_nails = max(0, warmup_start_ammo_nails);
710         warmup_start_ammo_rockets = max(0, warmup_start_ammo_rockets);
711         warmup_start_ammo_cells = max(0, warmup_start_ammo_cells);
712         warmup_start_ammo_plasma = max(0, warmup_start_ammo_plasma);
713         warmup_start_ammo_fuel = max(0, warmup_start_ammo_fuel);
714 }
715
716 void precache_playermodel(string m)
717 {
718         float globhandle, i, n;
719         string f;
720
721         if(substring(m, -9,5) == "_lod1")
722                 return;
723         if(substring(m, -9,5) == "_lod2")
724                 return;
725         precache_model(m);
726         f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
727         if(fexists(f))
728                 precache_model(f);
729         f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
730         if(fexists(f))
731                 precache_model(f);
732
733         globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
734         if (globhandle < 0)
735                 return;
736         n = search_getsize(globhandle);
737         for (i = 0; i < n; ++i)
738         {
739                 //print(search_getfilename(globhandle, i), "\n");
740                 f = search_getfilename(globhandle, i);
741                 PrecachePlayerSounds(f);
742         }
743         search_end(globhandle);
744 }
745 void precache_all_playermodels(string pattern)
746 {
747         int globhandle = search_begin(pattern, true, false);
748         if (globhandle < 0) return;
749         int n = search_getsize(globhandle);
750         for (int i = 0; i < n; ++i)
751         {
752                 string s = search_getfilename(globhandle, i);
753                 precache_playermodel(s);
754         }
755         search_end(globhandle);
756 }
757
758 void precache_playermodels(string s)
759 {
760         FOREACH_WORD(s, true, LAMBDA(precache_playermodel(it)));
761 }
762
763 void precache()
764 {
765     // gamemode related things
766
767     // Precache all player models if desired
768     if (autocvar_sv_precacheplayermodels)
769     {
770         PrecachePlayerSounds("sound/player/default.sounds");
771         precache_all_playermodels("models/player/*.zym");
772         precache_all_playermodels("models/player/*.dpm");
773         precache_all_playermodels("models/player/*.md3");
774         precache_all_playermodels("models/player/*.psk");
775         precache_all_playermodels("models/player/*.iqm");
776     }
777
778     if (autocvar_sv_defaultcharacter)
779     {
780                 precache_playermodels(autocvar_sv_defaultplayermodel_red);
781                 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
782                 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
783                 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
784                 precache_playermodels(autocvar_sv_defaultplayermodel);
785     }
786
787 #if 0
788     // Disabled this code because it simply does not work (e.g. ignores bgmvolume, overlaps with "cd loop" controlled tracks).
789
790     if (!this.noise && this.music) // quake 3 uses the music field
791         this.noise = this.music;
792
793     // plays music for the level if there is any
794     if (this.noise)
795     {
796         precache_sound (this.noise);
797         ambientsound ('0 0 0', this.noise, VOL_BASE, ATTEN_NONE);
798     }
799 #endif
800 }
801
802
803 void make_safe_for_remove(entity e)
804 {
805     if (e.initialize_entity)
806     {
807         entity ent, prev = NULL;
808         for (ent = initialize_entity_first; ent; )
809         {
810             if ((ent == e) || ((ent.classname == "initialize_entity") && (ent.enemy == e)))
811             {
812                 //print("make_safe_for_remove: getting rid of initializer ", etos(ent), "\n");
813                 // skip it in linked list
814                 if (prev)
815                 {
816                     prev.initialize_entity_next = ent.initialize_entity_next;
817                     ent = prev.initialize_entity_next;
818                 }
819                 else
820                 {
821                     initialize_entity_first = ent.initialize_entity_next;
822                     ent = initialize_entity_first;
823                 }
824             }
825             else
826             {
827                 prev = ent;
828                 ent = ent.initialize_entity_next;
829             }
830         }
831     }
832 }
833
834 .float remove_except_protected_forbidden;
835 void remove_except_protected(entity e)
836 {
837         if(e.remove_except_protected_forbidden)
838                 error("not allowed to remove this at this point");
839         builtin_remove(e);
840 }
841
842 void remove_unsafely(entity e)
843 {
844     if(e.classname == "spike")
845         error("Removing spikes is forbidden (crylink bug), please report");
846     builtin_remove(e);
847 }
848
849 void remove_safely(entity e)
850 {
851     make_safe_for_remove(e);
852     builtin_remove(e);
853 }
854
855 void InitializeEntity(entity e, void(entity this) func, float order)
856 {
857     entity prev, cur;
858
859     if (!e || e.initialize_entity)
860     {
861         // make a proxy initializer entity
862         entity e_old = e;
863         e = new(initialize_entity);
864         e.enemy = e_old;
865     }
866
867     e.initialize_entity = func;
868     e.initialize_entity_order = order;
869
870     cur = initialize_entity_first;
871     prev = NULL;
872     for (;;)
873     {
874         if (!cur || cur.initialize_entity_order > order)
875         {
876             // insert between prev and cur
877             if (prev)
878                 prev.initialize_entity_next = e;
879             else
880                 initialize_entity_first = e;
881             e.initialize_entity_next = cur;
882             return;
883         }
884         prev = cur;
885         cur = cur.initialize_entity_next;
886     }
887 }
888 void InitializeEntitiesRun()
889 {
890     entity startoflist = initialize_entity_first;
891     initialize_entity_first = NULL;
892     delete_fn = remove_except_protected;
893     for (entity e = startoflist; e; e = e.initialize_entity_next)
894     {
895                 e.remove_except_protected_forbidden = 1;
896     }
897     for (entity e = startoflist; e; )
898     {
899                 e.remove_except_protected_forbidden = 0;
900         e.initialize_entity_order = 0;
901         entity next = e.initialize_entity_next;
902         e.initialize_entity_next = NULL;
903         var void(entity this) func = e.initialize_entity;
904         e.initialize_entity = func_null;
905         if (e.classname == "initialize_entity")
906         {
907             entity wrappee = e.enemy;
908             builtin_remove(e);
909             e = wrappee;
910         }
911         //dprint("Delayed initialization: ", e.classname, "\n");
912         if (func)
913         {
914                 func(e);
915         }
916         else
917         {
918             eprint(e);
919             backtrace(strcat("Null function in: ", e.classname, "\n"));
920         }
921         e = next;
922     }
923     delete_fn = remove_unsafely;
924 }
925
926 .float(entity) isEliminated;
927 bool EliminatedPlayers_SendEntity(entity this, entity to, float sendflags)
928 {
929         float i, f, b;
930         entity e;
931         WriteHeader(MSG_ENTITY, ENT_CLIENT_ELIMINATEDPLAYERS);
932         WriteByte(MSG_ENTITY, sendflags);
933
934         if(sendflags & 1)
935         {
936                 for(i = 1; i <= maxclients; i += 8)
937                 {
938                         for(f = 0, e = edict_num(i), b = 1; b < 256; b *= 2, e = nextent(e))
939                         {
940                                 if(eliminatedPlayers.isEliminated(e))
941                                         f |= b;
942                         }
943                         WriteByte(MSG_ENTITY, f);
944                 }
945         }
946
947         return true;
948 }
949
950 void EliminatedPlayers_Init(float(entity) isEliminated_func)
951 {
952         if(eliminatedPlayers)
953         {
954                 backtrace("Can't spawn eliminatedPlayers again!");
955                 return;
956         }
957         Net_LinkEntity(eliminatedPlayers = spawn(), false, 0, EliminatedPlayers_SendEntity);
958         eliminatedPlayers.isEliminated = isEliminated_func;
959 }
960
961
962
963
964 void adaptor_think2use_hittype_splash(entity this) // for timed projectile detonation
965 {
966         if(!(IS_ONGROUND(this))) // if onground, we ARE touching something, but HITTYPE_SPLASH is to be networked if the damage causing projectile is not touching ANYTHING
967                 this.projectiledeathtype |= HITTYPE_SPLASH;
968         adaptor_think2use(this);
969 }
970
971 // deferred dropping
972 void DropToFloor_Handler(entity this)
973 {
974     WITHSELF(this, builtin_droptofloor());
975     this.dropped_origin = this.origin;
976 }
977
978 void droptofloor(entity this)
979 {
980     InitializeEntity(this, DropToFloor_Handler, INITPRIO_DROPTOFLOOR);
981 }
982
983
984
985 float trace_hits_box_a0, trace_hits_box_a1;
986
987 float trace_hits_box_1d(float end, float thmi, float thma)
988 {
989     if (end == 0)
990     {
991         // just check if x is in range
992         if (0 < thmi)
993             return false;
994         if (0 > thma)
995             return false;
996     }
997     else
998     {
999         // do the trace with respect to x
1000         // 0 -> end has to stay in thmi -> thma
1001         trace_hits_box_a0 = max(trace_hits_box_a0, min(thmi / end, thma / end));
1002         trace_hits_box_a1 = min(trace_hits_box_a1, max(thmi / end, thma / end));
1003         if (trace_hits_box_a0 > trace_hits_box_a1)
1004             return false;
1005     }
1006     return true;
1007 }
1008
1009 float trace_hits_box(vector start, vector end, vector thmi, vector thma)
1010 {
1011     end -= start;
1012     thmi -= start;
1013     thma -= start;
1014     // now it is a trace from 0 to end
1015
1016     trace_hits_box_a0 = 0;
1017     trace_hits_box_a1 = 1;
1018
1019     if (!trace_hits_box_1d(end.x, thmi.x, thma.x))
1020         return false;
1021     if (!trace_hits_box_1d(end.y, thmi.y, thma.y))
1022         return false;
1023     if (!trace_hits_box_1d(end.z, thmi.z, thma.z))
1024         return false;
1025
1026     return true;
1027 }
1028
1029 float tracebox_hits_box(vector start, vector mi, vector ma, vector end, vector thmi, vector thma)
1030 {
1031     return trace_hits_box(start, end, thmi - ma, thma - mi);
1032 }
1033
1034 bool SUB_NoImpactCheck(entity this, entity toucher)
1035 {
1036         // zero hitcontents = this is not the real impact, but either the
1037         // mirror-impact of something hitting the projectile instead of the
1038         // projectile hitting the something, or a touchareagrid one. Neither of
1039         // these stop the projectile from moving, so...
1040         if(trace_dphitcontents == 0)
1041         {
1042                 LOG_TRACEF("A hit from a projectile happened with no hit contents! DEBUG THIS, this should never happen for projectiles! Projectile will self-destruct. (edict: %i, classname: %s, origin: %v)", this, this.classname, this.origin);
1043                 checkclient(this);
1044         }
1045     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1046         return true;
1047     if (toucher == NULL && this.size != '0 0 0')
1048     {
1049         vector tic;
1050         tic = this.velocity * sys_frametime;
1051         tic = tic + normalize(tic) * vlen(this.maxs - this.mins);
1052         traceline(this.origin - tic, this.origin + tic, MOVE_NORMAL, this);
1053         if (trace_fraction >= 1)
1054         {
1055             LOG_TRACE("Odd... did not hit...?");
1056         }
1057         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
1058         {
1059             LOG_TRACE("Detected and prevented the sky-grapple bug.");
1060             return true;
1061         }
1062     }
1063
1064     return false;
1065 }
1066
1067 #define SUB_OwnerCheck(ent,oth) ((oth) && ((oth) == (ent).owner))
1068
1069 void W_Crylink_Dequeue(entity e);
1070 bool WarpZone_Projectile_Touch_ImpactFilter_Callback(entity this, entity toucher)
1071 {
1072         if(SUB_OwnerCheck(this, toucher))
1073                 return true;
1074         if(SUB_NoImpactCheck(this, toucher))
1075         {
1076                 if(this.classname == "nade")
1077                         return false; // no checks here
1078                 else if(this.classname == "grapplinghook")
1079                         RemoveGrapplingHook(this.realowner);
1080                 else if(this.classname == "spike")
1081                 {
1082                         W_Crylink_Dequeue(this);
1083                         delete(this);
1084                 }
1085                 else
1086                         delete(this);
1087                 return true;
1088         }
1089         if(trace_ent && trace_ent.solid > SOLID_TRIGGER)
1090                 UpdateCSQCProjectile(this);
1091         return false;
1092 }
1093
1094 /** engine callback */
1095 void URI_Get_Callback(float id, float status, string data)
1096 {
1097         if(url_URI_Get_Callback(id, status, data))
1098         {
1099                 // handled
1100         }
1101         else if (id == URI_GET_DISCARD)
1102         {
1103                 // discard
1104         }
1105         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
1106         {
1107                 // sv_cmd curl
1108                 Curl_URI_Get_Callback(id, status, data);
1109         }
1110         else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
1111         {
1112                 // online ban list
1113                 OnlineBanList_URI_Get_Callback(id, status, data);
1114         }
1115         else if (MUTATOR_CALLHOOK(URI_GetCallback, id, status, data))
1116         {
1117                 // handled by a mutator
1118         }
1119         else
1120         {
1121                 LOG_INFO("Received HTTP request data for an invalid id ", ftos(id), ".\n");
1122         }
1123 }
1124
1125 string uid2name(string myuid) {
1126         string s;
1127         s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
1128
1129         // FIXME remove this later after 0.6 release
1130         // convert old style broken records to correct style
1131         if(s == "")
1132         {
1133                 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
1134                 if(s != "")
1135                 {
1136                         db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
1137                         db_remove(ServerProgsDB, strcat("uid2name", myuid));
1138                 }
1139         }
1140
1141         if(s == "")
1142                 s = "^1Unregistered Player";
1143         return s;
1144 }
1145
1146 float MoveToRandomLocationWithinBounds(entity e, vector boundmin, vector boundmax, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1147 {
1148     float m, i;
1149     vector start, org, delta, end, enddown, mstart;
1150
1151     m = e.dphitcontentsmask;
1152     e.dphitcontentsmask = goodcontents | badcontents;
1153
1154     org = boundmin;
1155     delta = boundmax - boundmin;
1156
1157     start = end = org;
1158
1159     for (i = 0; i < attempts; ++i)
1160     {
1161         start.x = org.x + random() * delta.x;
1162         start.y = org.y + random() * delta.y;
1163         start.z = org.z + random() * delta.z;
1164
1165         // rule 1: start inside world bounds, and outside
1166         // solid, and don't start from somewhere where you can
1167         // fall down to evil
1168         tracebox(start, e.mins, e.maxs, start - '0 0 1' * delta.z, MOVE_NORMAL, e);
1169         if (trace_fraction >= 1)
1170             continue;
1171         if (trace_startsolid)
1172             continue;
1173         if (trace_dphitcontents & badcontents)
1174             continue;
1175         if (trace_dphitq3surfaceflags & badsurfaceflags)
1176             continue;
1177
1178         // rule 2: if we are too high, lower the point
1179         if (trace_fraction * delta.z > maxaboveground)
1180             start = trace_endpos + '0 0 1' * maxaboveground;
1181         enddown = trace_endpos;
1182
1183         // rule 3: make sure we aren't outside the map. This only works
1184         // for somewhat well formed maps. A good rule of thumb is that
1185         // the map should have a convex outside hull.
1186         // these can be traceLINES as we already verified the starting box
1187         mstart = start + 0.5 * (e.mins + e.maxs);
1188         traceline(mstart, mstart + '1 0 0' * delta.x, MOVE_NORMAL, e);
1189         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1190             continue;
1191         traceline(mstart, mstart - '1 0 0' * delta.x, MOVE_NORMAL, e);
1192         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1193             continue;
1194         traceline(mstart, mstart + '0 1 0' * delta.y, MOVE_NORMAL, e);
1195         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1196             continue;
1197         traceline(mstart, mstart - '0 1 0' * delta.y, MOVE_NORMAL, e);
1198         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1199             continue;
1200         traceline(mstart, mstart + '0 0 1' * delta.z, MOVE_NORMAL, e);
1201         if (trace_fraction >= 1 || trace_dphittexturename == "common/caulk")
1202             continue;
1203
1204         // rule 4: we must "see" some spawnpoint or item
1205     entity sp = NULL;
1206     IL_EACH(g_spawnpoints, checkpvs(mstart, it),
1207     {
1208         if((traceline(mstart, it.origin, MOVE_NORMAL, e), trace_fraction) >= 1)
1209         {
1210                 sp = it;
1211                 break;
1212         }
1213     });
1214         if(!sp)
1215         {
1216                 IL_EACH(g_items, checkpvs(mstart, it),
1217                 {
1218                         if((traceline(mstart, it.origin + (it.mins + it.maxs) * 0.5, MOVE_NORMAL, e), trace_fraction) >= 1)
1219                         {
1220                                 sp = it;
1221                                 break;
1222                         }
1223                 });
1224
1225                 if(!sp)
1226                         continue;
1227         }
1228
1229         // find a random vector to "look at"
1230         end.x = org.x + random() * delta.x;
1231         end.y = org.y + random() * delta.y;
1232         end.z = org.z + random() * delta.z;
1233         end = start + normalize(end - start) * vlen(delta);
1234
1235         // rule 4: start TO end must not be too short
1236         tracebox(start, e.mins, e.maxs, end, MOVE_NORMAL, e);
1237         if (trace_startsolid)
1238             continue;
1239         if (trace_fraction < minviewdistance / vlen(delta))
1240             continue;
1241
1242         // rule 5: don't want to look at sky
1243         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1244             continue;
1245
1246         // rule 6: we must not end up in trigger_hurt
1247         if (tracebox_hits_trigger_hurt(start, e.mins, e.maxs, enddown))
1248             continue;
1249
1250         break;
1251     }
1252
1253     e.dphitcontentsmask = m;
1254
1255     if (i < attempts)
1256     {
1257         setorigin(e, start);
1258         e.angles = vectoangles(end - start);
1259         LOG_TRACE("Needed ", ftos(i + 1), " attempts");
1260         return true;
1261     }
1262     else
1263         return false;
1264 }
1265
1266 float MoveToRandomMapLocation(entity e, float goodcontents, float badcontents, float badsurfaceflags, float attempts, float maxaboveground, float minviewdistance)
1267 {
1268         return MoveToRandomLocationWithinBounds(e, world.mins, world.maxs, goodcontents, badcontents, badsurfaceflags, attempts, maxaboveground, minviewdistance);
1269 }
1270
1271 void write_recordmarker(entity pl, float tstart, float dt)
1272 {
1273     GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
1274
1275     // also write a marker into demo files for demotc-race-record-extractor to find
1276     stuffcmd(pl,
1277              strcat(
1278                  strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
1279                  " ", ftos(tstart), " ", ftos(dt), "\n"));
1280 }
1281
1282 void attach_sameorigin(entity e, entity to, string tag)
1283 {
1284     vector org, t_forward, t_left, t_up, e_forward, e_up;
1285     float tagscale;
1286
1287     org = e.origin - gettaginfo(to, gettagindex(to, tag));
1288     tagscale = pow(vlen(v_forward), -2); // undo a scale on the tag
1289     t_forward = v_forward * tagscale;
1290     t_left = v_right * -tagscale;
1291     t_up = v_up * tagscale;
1292
1293     e.origin_x = org * t_forward;
1294     e.origin_y = org * t_left;
1295     e.origin_z = org * t_up;
1296
1297     // current forward and up directions
1298     if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1299                 e.angles = AnglesTransform_FromVAngles(e.angles);
1300         else
1301                 e.angles = AnglesTransform_FromAngles(e.angles);
1302     fixedmakevectors(e.angles);
1303
1304     // untransform forward, up!
1305     e_forward.x = v_forward * t_forward;
1306     e_forward.y = v_forward * t_left;
1307     e_forward.z = v_forward * t_up;
1308     e_up.x = v_up * t_forward;
1309     e_up.y = v_up * t_left;
1310     e_up.z = v_up * t_up;
1311
1312     e.angles = fixedvectoangles2(e_forward, e_up);
1313     if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1314                 e.angles = AnglesTransform_ToVAngles(e.angles);
1315         else
1316                 e.angles = AnglesTransform_ToAngles(e.angles);
1317
1318     setattachment(e, to, tag);
1319     setorigin(e, e.origin);
1320 }
1321
1322 void detach_sameorigin(entity e)
1323 {
1324     vector org;
1325     org = gettaginfo(e, 0);
1326     e.angles = fixedvectoangles2(v_forward, v_up);
1327     if (substring(e.model, 0, 1) == "*") // bmodels have their own rules
1328                 e.angles = AnglesTransform_ToVAngles(e.angles);
1329         else
1330                 e.angles = AnglesTransform_ToAngles(e.angles);
1331     setorigin(e, org);
1332     setattachment(e, NULL, "");
1333     setorigin(e, e.origin);
1334 }
1335
1336 void follow_sameorigin(entity e, entity to)
1337 {
1338     set_movetype(e, MOVETYPE_FOLLOW); // make the hole follow
1339     e.aiment = to; // make the hole follow bmodel
1340     e.punchangle = to.angles; // the original angles of bmodel
1341     e.view_ofs = e.origin - to.origin; // relative origin
1342     e.v_angle = e.angles - to.angles; // relative angles
1343 }
1344
1345 void unfollow_sameorigin(entity e)
1346 {
1347     set_movetype(e, MOVETYPE_NONE);
1348 }
1349
1350 entity gettaginfo_relative_ent;
1351 vector gettaginfo_relative(entity e, float tag)
1352 {
1353     if (!gettaginfo_relative_ent)
1354     {
1355         gettaginfo_relative_ent = spawn();
1356         gettaginfo_relative_ent.effects = EF_NODRAW;
1357     }
1358     gettaginfo_relative_ent.model = e.model;
1359     gettaginfo_relative_ent.modelindex = e.modelindex;
1360     gettaginfo_relative_ent.frame = e.frame;
1361     return gettaginfo(gettaginfo_relative_ent, tag);
1362 }
1363
1364 .string aiment_classname;
1365 .float aiment_deadflag;
1366 void SetMovetypeFollow(entity ent, entity e)
1367 {
1368         // FIXME this may not be warpzone aware
1369         set_movetype(ent, MOVETYPE_FOLLOW); // make the hole follow
1370         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.
1371         ent.aiment = e; // make the hole follow bmodel
1372         ent.punchangle = e.angles; // the original angles of bmodel
1373         ent.view_ofs = ent.origin - e.origin; // relative origin
1374         ent.v_angle = ent.angles - e.angles; // relative angles
1375         ent.aiment_classname = strzone(e.classname);
1376         ent.aiment_deadflag = e.deadflag;
1377 }
1378 void UnsetMovetypeFollow(entity ent)
1379 {
1380         set_movetype(ent, MOVETYPE_FLY);
1381         PROJECTILE_MAKETRIGGER(ent);
1382         ent.aiment = NULL;
1383 }
1384 float LostMovetypeFollow(entity ent)
1385 {
1386 /*
1387         if(ent.move_movetype != MOVETYPE_FOLLOW)
1388                 if(ent.aiment)
1389                         error("???");
1390 */
1391         if(ent.aiment)
1392         {
1393                 if(ent.aiment.classname != ent.aiment_classname)
1394                         return 1;
1395                 if(ent.aiment.deadflag != ent.aiment_deadflag)
1396                         return 1;
1397         }
1398         return 0;
1399 }
1400
1401 .bool pushable;
1402 bool isPushable(entity e)
1403 {
1404         if(e.pushable)
1405                 return true;
1406         if(IS_VEHICLE(e))
1407                 return false;
1408         if(e.iscreature)
1409                 return true;
1410         switch(e.classname)
1411         {
1412                 case "body":
1413                 case "droppedweapon":
1414                         return true;
1415                 case "bullet": // antilagged bullets can't hit this either
1416                         return false;
1417         }
1418         if (e.projectiledeathtype)
1419                 return true;
1420         return false;
1421 }