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