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