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