Merge branch 'master' into Mario/turrets
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / vehicles / vehicles.qc
1 #include "../../common/turrets/util.qh"
2
3 float autocvar_g_vehicles_crush_dmg;
4 float autocvar_g_vehicles_crush_force;
5 float autocvar_g_vehicles_delayspawn;
6 float autocvar_g_vehicles_delayspawn_jitter;
7
8 float autocvar_g_vehicles_vortex_damagerate = 0.5;
9 float autocvar_g_vehicles_machinegun_damagerate = 0.5;
10 float autocvar_g_vehicles_rifle_damagerate = 0.75;
11 float autocvar_g_vehicles_vaporizer_damagerate = 0.001;
12 float autocvar_g_vehicles_tag_damagerate = 5;
13
14 float autocvar_g_vehicles;
15
16 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
17 void vehicles_return();
18 void vehicles_enter();
19 void vehicles_touch();
20 void vehicles_reset_colors();
21 void vehicles_clearreturn();
22 void vehicles_setreturn();
23
24
25 /** AuxiliaryXhair*
26     Send additional points of interest to be drawn, to vehicle owner
27 **/
28 const float MAX_AXH = 4;
29 .entity AuxiliaryXhair[MAX_AXH];
30
31 float SendAuxiliaryXhair(entity to, float sf)
32 {
33
34         WriteByte(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
35
36         WriteByte(MSG_ENTITY, self.cnt);
37
38         WriteCoord(MSG_ENTITY, self.origin.x);
39         WriteCoord(MSG_ENTITY, self.origin.y);
40         WriteCoord(MSG_ENTITY, self.origin.z);
41
42     WriteByte(MSG_ENTITY, rint(self.colormod.x * 255));
43     WriteByte(MSG_ENTITY, rint(self.colormod.y * 255));
44     WriteByte(MSG_ENTITY, rint(self.colormod.z * 255));
45
46     return true;
47 }
48
49 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
50 {
51     if (!IS_REAL_CLIENT(own))
52         return;
53
54     entity axh;
55
56     axh_id = bound(0, axh_id, MAX_AXH);
57     axh = own.(AuxiliaryXhair[axh_id]);
58
59     if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
60     {
61         axh                     = spawn();
62         axh.cnt                 = axh_id;
63         axh.drawonlytoclient    = own;
64         axh.owner               = own;
65         Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
66     }
67
68     setorigin(axh, loc);
69     axh.colormod            = clr;
70     axh.SendFlags           = 0x01;
71     own.(AuxiliaryXhair[axh_id]) = axh;
72 }
73
74 /*
75 // SVC_TEMPENTITY based, horrible with even 50 ping. hm.
76 // WriteByte(MSG_ONE, SVC_TEMPENTITY) uses reliable messagess, never use for thinsg that need continous updates.
77 void SendAuxiliaryXhair2(entity own, vector loc, vector clr, float axh_id)
78 {
79         msgexntity = own;
80
81         WriteByte(MSG_ONE, SVC_TEMPENTITY);
82         WriteByte(MSG_ONE, TE_CSQC_AUXILIARYXHAIR);
83
84         WriteByte(MSG_ONE, axh_id);
85
86         WriteCoord(MSG_ONE, loc_x);
87         WriteCoord(MSG_ONE, loc_y);
88         WriteCoord(MSG_ONE, loc_z);
89
90     WriteByte(MSG_ONE, rint(clr_x * 255));
91     WriteByte(MSG_ONE, rint(clr_y * 255));
92     WriteByte(MSG_ONE, rint(clr_z * 255));
93
94 }
95 */
96 // End AuxiliaryXhair
97
98 /**
99     Notifies the client that he enterd a vehicle, and sends
100     realavent data.
101
102     only sends vehicle_id atm (wich is a HUD_* constant, ex. HUD_SPIDERBOT)
103 **/
104 void CSQCVehicleSetup(entity own, float vehicle_id)
105 {
106     if (!IS_REAL_CLIENT(own))
107         return;
108
109         msg_entity = own;
110
111         WriteByte(MSG_ONE, SVC_TEMPENTITY);
112         WriteByte(MSG_ONE, TE_CSQC_VEHICLESETUP);
113         if(vehicle_id != 0)
114             WriteByte(MSG_ONE, vehicle_id);
115         else
116         WriteByte(MSG_ONE, 1 + own.vehicle.vehicle_weapon2mode + HUD_VEHICLE_LAST);
117 }
118
119 /** vehicles_locktarget
120
121     Generic target locking.
122
123     Figure out if what target is "locked" (if any), for missile tracking as such.
124
125     after calling, "if(self.lock_target != world && self.lock_strength == 1)" mean
126     you have a locked in target.
127
128     Exspects a crosshair_trace() or equivalent to be
129     dont before calling.
130
131 **/
132 .entity lock_target;
133 .float  lock_strength;
134 .float  lock_time;
135 .float  lock_soundtime;
136 const float     DAMAGE_TARGETDRONE = 10;
137
138 vector targetdrone_getnewspot()
139 {
140
141         vector spot;
142         float i;
143         for(i = 0; i < 100; ++i)
144         {
145                 spot = self.origin + randomvec() * 1024;
146                 tracebox(spot, self.mins, self.maxs, spot, MOVE_NORMAL, self);
147                 if(trace_fraction == 1.0 && trace_startsolid == 0 && trace_allsolid == 0)
148                         return spot;
149         }
150         return self.origin;
151 }
152
153 #if 0
154 void targetdrone_think();
155 void targetdrone_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
156 void targetdrone_renwe()
157 {
158         self.think = targetdrone_think;
159         self.nextthink = time + 0.1;
160         setorigin(self, targetdrone_getnewspot());
161         self.health = 200;
162         self.takedamage = DAMAGE_TARGETDRONE;
163         self.event_damage = targetdrone_damage;
164         self.solid = SOLID_BBOX;
165         setmodel(self, "models/runematch/rune.mdl");
166         self.effects = EF_LOWPRECISION;
167         self.scale = 10;
168         self.movetype = MOVETYPE_BOUNCEMISSILE;
169         setsize(self, '-100 -100 -100', '100 100 100');
170
171 }
172 void targetdrone_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
173 {
174         self.health -= damage;
175         if(self.health <= 0)
176         {
177                 pointparticles(particleeffectnum("explosion_medium"), self.origin, '0 0 0', 1);
178
179                 if(!self.cnt)
180                         remove(self);
181                 else
182                 {
183                         self.think = targetdrone_renwe;
184                         self.nextthink = time + 1 + random() * 2;
185                         self.solid = SOLID_NOT;
186                         setmodel(self, "");
187                 }
188         }
189 }
190 entity targetdrone_getfear()
191 {
192         entity fear;
193         float i;
194
195         for(i = 64; i <= 1024; i += 64)
196         {
197                 fear = findradius(self.origin, i);
198                 while(fear)
199                 {
200                         if(fear.bot_dodge)
201                                 return fear;
202
203                         fear = fear.chain;
204                 }
205         }
206
207         return world;
208 }
209 void targetdrone_think()
210 {
211         self.nextthink = time + 0.1;
212
213         if(self.wp00)
214         if(self.wp00.deadflag != DEAD_NO)
215                 self.wp00 = targetdrone_getfear();
216
217         if(!self.wp00)
218                 self.wp00 = targetdrone_getfear();
219
220         vector newdir;
221
222         if(self.wp00)
223                 newdir = steerlib_push(self.wp00.origin) + randomvec() * 0.75;
224         else
225                 newdir = randomvec() * 0.75;
226
227         newdir = newdir * 0.5 + normalize(self.velocity) * 0.5;
228
229         if(self.wp00)
230                 self.velocity = normalize(newdir) * (500 + (1024 / min(vlen(self.wp00.origin - self.origin), 1024)) * 700);
231         else
232                 self.velocity = normalize(newdir) * 750;
233
234         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 2, MOVE_NORMAL, self);
235         if(trace_fraction != 1.0)
236                 self.velocity = self.velocity * -1;
237
238         //normalize((normalize(self.velocity) * 0.5 + newdir * 0.5)) * 750;
239 }
240
241 void targetdrone_spawn(vector _where, float _autorenew)
242 {
243         entity drone = spawn();
244         setorigin(drone, _where);
245         drone.think = targetdrone_renwe;
246         drone.nextthink = time + 0.1;
247         drone.cnt = _autorenew;
248 }
249 #endif
250
251 void vehicles_locktarget(float incr, float decr, float _lock_time)
252 {
253     if(self.lock_target && self.lock_target.deadflag != DEAD_NO)
254     {
255         self.lock_target    = world;
256         self.lock_strength  = 0;
257         self.lock_time      = 0;
258     }
259
260     if(self.lock_time > time)
261     {
262         if(self.lock_target)
263         if(self.lock_soundtime < time)
264         {
265             self.lock_soundtime = time + 0.5;
266             play2(self.owner, "vehicles/locked.wav");
267         }
268
269         return;
270     }
271
272     if(trace_ent != world)
273     {
274         if(teamplay && trace_ent.team == self.team)
275             trace_ent = world;
276
277         if(trace_ent.deadflag != DEAD_NO)
278             trace_ent = world;
279
280                 if(!((trace_ent.vehicle_flags & VHF_ISVEHICLE) || (trace_ent.turret_flags & TUR_FLAG_ISTURRET)))
281             trace_ent = world;
282     }
283
284     if(self.lock_target == world && trace_ent != world)
285         self.lock_target = trace_ent;
286
287     if(self.lock_target && trace_ent == self.lock_target)
288     {
289         if(self.lock_strength != 1 && self.lock_strength + incr >= 1)
290         {
291             play2(self.owner, "vehicles/lock.wav");
292             self.lock_soundtime = time + 0.8;
293         }
294         else if (self.lock_strength != 1 && self.lock_soundtime < time)
295         {
296             play2(self.owner, "vehicles/locking.wav");
297             self.lock_soundtime = time + 0.3;
298         }
299
300     }
301
302     // Have a locking target
303     // Trace hit current target
304     if(trace_ent == self.lock_target && trace_ent != world)
305     {
306         self.lock_strength = min(self.lock_strength + incr, 1);
307         if(self.lock_strength == 1)
308             self.lock_time = time + _lock_time;
309     }
310     else
311     {
312         if(trace_ent)
313             self.lock_strength = max(self.lock_strength - decr * 2, 0);
314         else
315             self.lock_strength = max(self.lock_strength - decr, 0);
316
317         if(self.lock_strength == 0)
318             self.lock_target = world;
319     }
320 }
321
322 #define VEHICLE_UPDATE_PLAYER(ply,fld,vhname) \
323 ply.vehicle_##fld = (self.vehicle_##fld / autocvar_g_vehicle_##vhname##_##fld) * 100
324
325 #define vehicles_sweap_collision(orig,vel,dt,acm,mult) \
326 traceline(orig, orig + vel * dt, MOVE_NORMAL, self); \
327 if(trace_fraction != 1) \
328     acm += normalize(self.origin - trace_endpos) * (vlen(vel) * mult)
329
330 // Hover movement support
331 float  force_fromtag_power;
332 float  force_fromtag_normpower;
333 vector force_fromtag_origin;
334 vector vehicles_force_fromtag_hover(string tag_name, float spring_length, float max_power)
335 {
336     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
337     v_forward  = normalize(v_forward) * -1;
338     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
339
340     force_fromtag_power = (1 - trace_fraction) * max_power;
341     force_fromtag_normpower = force_fromtag_power / max_power;
342
343     return v_forward  * force_fromtag_power;
344 }
345
346 // Experimental hovermode wich uses attraction/repulstion from surface insted of gravity/repulsion
347 // Can possibly be use to move abt any surface (inclusing walls/celings)
348 vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float max_power)
349 {
350
351     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
352     v_forward  = normalize(v_forward) * -1;
353     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
354
355     // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
356     if(trace_fraction == 1.0)
357     {
358         force_fromtag_normpower = -0.25;
359         return '0 0 -200';
360     }
361
362     force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
363     force_fromtag_normpower = force_fromtag_power / max_power;
364
365     return v_forward  * force_fromtag_power;
366 }
367
368 // Generic vehile projectile system
369 void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
370 {
371     // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
372     if(inflictor.owner == self.owner)
373         return;
374
375     self.health -= damage;
376     self.velocity += force;
377     if(self.health < 1)
378     {
379         self.takedamage = DAMAGE_NO;
380         self.event_damage = func_null;
381         self.think = self.use;
382         self.nextthink = time;
383     }
384 }
385
386 void vehicles_projectile_explode()
387 {
388     if(self.owner && other != world)
389     {
390         if(other == self.owner.vehicle)
391             return;
392
393         if(other == self.owner.vehicle.tur_head)
394             return;
395     }
396
397         PROJECTILE_TOUCH;
398
399         self.event_damage = func_null;
400     RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, world, self.shot_force, self.totalfrags, other);
401
402     remove (self);
403 }
404
405 entity vehicles_projectile(string _mzlfx, string _mzlsound,
406                            vector _org, vector _vel,
407                            float _dmg, float _radi, float _force,  float _size,
408                            float _deahtype, float _projtype, float _health,
409                            float _cull, float _clianim, entity _owner)
410 {
411     entity proj;
412
413     proj = spawn();
414
415     PROJECTILE_MAKETRIGGER(proj);
416     setorigin(proj, _org);
417
418     proj.shot_dmg         = _dmg;
419     proj.shot_radius      = _radi;
420     proj.shot_force       = _force;
421     proj.totalfrags       = _deahtype;
422     proj.solid            = SOLID_BBOX;
423     proj.movetype         = MOVETYPE_FLYMISSILE;
424     proj.flags            = FL_PROJECTILE;
425     proj.bot_dodge        = true;
426     proj.bot_dodgerating  = _dmg;
427     proj.velocity         = _vel;
428     proj.touch            = vehicles_projectile_explode;
429     proj.use              = vehicles_projectile_explode;
430     proj.owner            = self;
431     proj.realowner        = _owner;
432     proj.think            = SUB_Remove;
433     proj.nextthink        = time + 30;
434
435     if(_health)
436     {
437         proj.takedamage       = DAMAGE_AIM;
438         proj.event_damage     = vehicles_projectile_damage;
439         proj.health           = _health;
440     }
441     else
442         proj.flags           = FL_PROJECTILE | FL_NOTARGET;
443
444     if(_mzlsound)
445         sound (self, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
446
447     if(_mzlfx)
448         pointparticles(particleeffectnum(_mzlfx), proj.origin, proj.velocity, 1);
449
450
451     setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
452
453     CSQCProjectile(proj, _clianim, _projtype, _cull);
454
455     return proj;
456 }
457 // End generic vehile projectile system
458
459 void vehicles_reset()
460 {
461         if(self.owner)
462         {
463                 entity oldself = self;
464                 self = self.owner;
465                 vehicles_exit(VHEF_RELESE);
466                 self = oldself;
467         }
468         self.alpha      = -1;
469         self.movetype   = MOVETYPE_NONE;
470         self.effects    = EF_NODRAW;
471         self.colormod  = '0 0 0';
472         self.avelocity = '0 0 0';
473         self.velocity  = '0 0 0';
474         self.event_damage = func_null;
475         self.solid = SOLID_NOT;
476         self.deadflag = DEAD_NO;
477
478         self.touch = func_null;
479         self.nextthink = 0;
480         vehicles_setreturn();
481 }
482
483 /** vehicles_spawn
484     Exetuted for all vehicles on (re)spawn.
485     Sets defaults for newly spawned units.
486 **/
487 void vehicles_spawn()
488 {
489     dprint("Spawning vehicle: ", self.netname, "\n");
490
491     // De-own & reset
492     self.vehicle_hudmodel.viewmodelforclient = self;
493
494     self.owner              = world;
495     self.touch              = vehicles_touch;
496     self.event_damage       = vehicles_damage;
497     self.reset              = vehicles_reset;
498     self.iscreature         = true;
499     self.teleportable       = false; // no teleporting for vehicles, too buggy
500     self.damagedbycontents      = true;
501     self.movetype           = MOVETYPE_WALK;
502     self.solid              = SOLID_SLIDEBOX;
503     self.takedamage         = DAMAGE_AIM;
504         self.deadflag           = DEAD_NO;
505     self.bot_attack         = true;
506     self.flags              = FL_NOTARGET;
507     self.avelocity          = '0 0 0';
508     self.velocity           = '0 0 0';
509
510     // Reset locking
511     self.lock_strength      = 0;
512     self.lock_target        = world;
513     self.misc_bulletcounter = 0;
514
515     // Return to spawn
516     self.angles             = self.pos2;
517     setorigin(self, self.pos1 + '0 0 0');
518     // Show it
519     pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
520
521     if(self.vehicle_controller)
522         self.team = self.vehicle_controller.team;
523
524     vehicles_reset_colors();
525     self.vehicle_spawn(VHSF_NORMAL);
526 }
527
528 // Better way of determening whats crushable needed! (fl_crushable?)
529 float vehicles_crushable(entity e)
530 {
531     if(IS_PLAYER(e))
532         return true;
533
534     if(e.flags & FL_MONSTER)
535         return true;
536
537     return false;
538 }
539
540 void vehicles_impact(float _minspeed, float _speedfac, float _maxpain)
541 {
542     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
543         return;
544
545     if(self.play_time < time)
546     {
547         float wc = vlen(self.velocity - self.oldvelocity);
548         //dprint("oldvel: ", vtos(self.oldvelocity), "\n");
549         //dprint("vel: ", vtos(self.velocity), "\n");
550         if(_minspeed < wc)
551         {
552             float take = min(_speedfac * wc, _maxpain);
553             Damage (self, world, world, take, DEATH_FALL, self.origin, '0 0 0');
554             self.play_time = time + 0.25;
555
556             //dprint("wc: ", ftos(wc), "\n");
557             //dprint("take: ", ftos(take), "\n");
558         }
559     }
560 }
561
562 .void() vehicle_impact;
563 void vehicles_touch()
564 {
565         if(MUTATOR_CALLHOOK(VehicleTouch))
566                 return;
567
568     // Vehicle currently in use
569     if(self.owner)
570     {
571         if(other != world)
572         if(vehicles_crushable(other))
573         {
574             if(vlen(self.velocity) != 0)
575                 Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
576
577             return; // Dont do selfdamage when hitting "soft targets".
578         }
579
580         if(self.play_time < time)
581         if(self.vehicle_impact)
582             self.vehicle_impact();
583
584         return;
585     }
586
587     if (!IS_PLAYER(other))
588         return;
589
590     if(other.deadflag != DEAD_NO)
591         return;
592
593     if(other.vehicle != world)
594         return;
595
596     vehicles_enter();
597 }
598 float autocvar_g_vehicles_allow_bots = 0;
599 void vehicles_enter()
600 {
601    // Remove this when bots know how to use vehicles
602
603     if (IS_BOT_CLIENT(other))
604         if (autocvar_g_vehicles_allow_bots)
605             dprint("Bot enters vehicle\n"); // This is where we need to disconnect (some, all?) normal bot AI and hand over to vehicle's _aiframe()
606         else
607             return;
608
609     if(self.phase > time)
610         return;
611     if(other.frozen)
612         return;
613     if(other.vehicle)
614         return;
615     if(other.deadflag != DEAD_NO)
616         return;
617
618     if(teamplay)
619     if(self.team)
620     if(self.team != other.team)
621         return;
622
623     RemoveGrapplingHook(other);
624
625     self.vehicle_ammo1   = 0;
626     self.vehicle_ammo2   = 0;
627     self.vehicle_reload1 = 0;
628     self.vehicle_reload2 = 0;
629     self.vehicle_energy  = 0;
630
631     self.owner          = other;
632     self.switchweapon   = other.switchweapon;
633
634     // .viewmodelforclient works better.
635     //self.vehicle_hudmodel.drawonlytoclient = self.owner;
636
637     self.vehicle_hudmodel.viewmodelforclient = self.owner;
638
639     self.event_damage         = vehicles_damage;
640     self.nextthink            = 0;
641     self.owner.angles         = self.angles;
642     self.owner.takedamage     = DAMAGE_NO;
643     self.owner.solid          = SOLID_NOT;
644     self.owner.movetype       = MOVETYPE_NOCLIP;
645     self.owner.alpha          = -1;
646     self.owner.vehicle        = self;
647     self.owner.event_damage   = func_null;
648     self.owner.view_ofs       = '0 0 0';
649     self.colormap             = self.owner.colormap;
650     if(self.tur_head)
651         self.tur_head.colormap    = self.owner.colormap;
652
653     self.owner.hud            = self.hud;
654     self.owner.PlayerPhysplug = self.PlayerPhysplug;
655
656     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
657     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
658     self.owner.vehicle_reload1  = self.vehicle_reload1;
659     self.owner.vehicle_reload2  = self.vehicle_reload2;
660
661     // Cant do this, hides attached objects too.
662     //self.exteriormodeltoclient = self.owner;
663     //self.tur_head.exteriormodeltoclient = self.owner;
664
665     other.flags &= ~FL_ONGROUND;
666     self.flags  &= ~FL_ONGROUND;
667
668     self.team                 = self.owner.team;
669     self.flags               -= FL_NOTARGET;
670     self.monster_attack       = true;
671
672     if (IS_REAL_CLIENT(other))
673     {
674         msg_entity = other;
675         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
676         WriteEntity(MSG_ONE, self.vehicle_viewport);
677
678         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
679         if(self.tur_head)
680         {
681             WriteAngle(MSG_ONE, self.tur_head.angles.x + self.angles.x); // tilt
682             WriteAngle(MSG_ONE, self.tur_head.angles.y + self.angles.y); // yaw
683             WriteAngle(MSG_ONE, 0);                                      // roll
684         }
685         else
686         {
687             WriteAngle(MSG_ONE,  self.angles.x * -1); // tilt
688             WriteAngle(MSG_ONE,  self.angles.y);      // yaw
689             WriteAngle(MSG_ONE,  0);                  // roll
690         }
691     }
692
693     vehicles_clearreturn();
694
695     CSQCVehicleSetup(self.owner, self.hud);
696
697     vh_player = other;
698     vh_vehicle = self;
699     MUTATOR_CALLHOOK(VehicleEnter);
700     other = vh_player;
701     self = vh_vehicle;
702
703     self.vehicle_enter();
704     antilag_clear(other);
705 }
706
707 /** vehicles_findgoodexit
708     Locates a valid location for the player to exit the vehicle.
709     Will first try prefer_spot, then up 100 random spots arround the vehicle
710     wich are in direct line of sight and empty enougth to hold a players bbox
711 **/
712 vector vehicles_findgoodexit(vector prefer_spot)
713 {
714     //vector exitspot;
715     float mysize;
716
717     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
718     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
719         return prefer_spot;
720
721     mysize = 1.5 * vlen(self.maxs - self.mins);
722     float i;
723     vector v, v2;
724     v2 = 0.5 * (self.absmin + self.absmax);
725     for(i = 0; i < 100; ++i)
726     {
727         v = randomvec();
728         v.z = 0;
729         v = v2 + normalize(v) * mysize;
730         tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
731         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
732             return v;
733     }
734
735     /*
736     exitspot = (self.origin + '0 0 48') + v_forward * mysize;
737     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
738     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
739         return exitspot;
740
741     exitspot = (self.origin + '0 0 48') - v_forward * mysize;
742     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
743     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
744         return exitspot;
745
746     exitspot = (self.origin + '0 0 48') + v_right * mysize;
747     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
748     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
749         return exitspot;
750
751     exitspot = (self.origin + '0 0 48') - v_right * mysize;
752     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
753     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
754         return exitspot;
755     */
756
757     return self.origin;
758 }
759
760 /** vehicles_exit
761     Standarrd vehicle release fucntion.
762     custom code goes in self.vehicle_exit
763 **/
764 float vehicles_exit_running;
765 void vehicles_exit(float eject)
766 {
767     entity _vehicle;
768     entity _player;
769     entity _oldself = self;
770
771     if(vehicles_exit_running)
772     {
773         dprint("^1vehicles_exit allready running! this is not good..\n");
774         return;
775     }
776
777     vehicles_exit_running = true;
778     if(IS_CLIENT(self))
779     {
780         _vehicle = self.vehicle;
781
782         if (_vehicle.vehicle_flags & VHF_PLAYERSLOT)
783         {
784             _vehicle.vehicle_exit(eject);
785             self = _oldself;
786             vehicles_exit_running = false;
787             return;
788         }
789     }
790     else
791         _vehicle = self;
792
793     _player = _vehicle.owner;
794
795     self = _vehicle;
796
797     if (_player)
798     {
799         if (IS_REAL_CLIENT(_player))
800         {
801             msg_entity = _player;
802             WriteByte (MSG_ONE, SVC_SETVIEWPORT);
803             WriteEntity( MSG_ONE, _player);
804
805             WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
806             WriteAngle(MSG_ONE, 0);
807             WriteAngle(MSG_ONE, _vehicle.angles.y);
808             WriteAngle(MSG_ONE, 0);
809         }
810
811         setsize(_player, PL_MIN,PL_MAX);
812
813         _player.takedamage     = DAMAGE_AIM;
814         _player.solid          = SOLID_SLIDEBOX;
815         _player.movetype       = MOVETYPE_WALK;
816         _player.effects        &= ~EF_NODRAW;
817         _player.alpha          = 1;
818         _player.PlayerPhysplug = func_null;
819         _player.vehicle        = world;
820         _player.view_ofs       = PL_VIEW_OFS;
821         _player.event_damage   = PlayerDamage;
822         _player.hud            = HUD_NORMAL;
823         _player.switchweapon   = _vehicle.switchweapon;
824
825         CSQCVehicleSetup(_player, HUD_NORMAL);
826     }
827     _vehicle.flags |= FL_NOTARGET;
828
829     if(_vehicle.deadflag == DEAD_NO)
830         _vehicle.avelocity          = '0 0 0';
831
832     _vehicle.tur_head.nodrawtoclient             = world;
833
834     if(!teamplay)
835         _vehicle.team = 0;
836
837     vh_player = _player;
838     vh_vehicle = _vehicle;
839     MUTATOR_CALLHOOK(VehicleExit);
840     _player = vh_player;
841     _vehicle = vh_vehicle;
842
843     _vehicle.team = _vehicle.tur_head.team;
844
845     sound (_vehicle, CH_TRIGGER_SINGLE, "misc/null.wav", 1, ATTEN_NORM);
846     _vehicle.vehicle_hudmodel.viewmodelforclient = _vehicle;
847     _vehicle.phase = time + 1;
848     _vehicle.monster_attack = false;
849
850     _vehicle.vehicle_exit(eject);
851
852     vehicles_setreturn();
853     vehicles_reset_colors();
854     _vehicle.owner = world;
855     self = _oldself;
856
857     vehicles_exit_running = false;
858 }
859
860
861 void vehicles_regen(float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
862 {
863     if(self.regen_field < field_max)
864     if(timer + rpause < time)
865     {
866         if(_healthscale)
867             regen = regen * (self.vehicle_health / self.tur_health);
868
869         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
870
871         if(self.owner)
872             self.owner.regen_field = (self.regen_field / field_max) * 100;
873     }
874 }
875
876 void shieldhit_think()
877 {
878     self.alpha -= 0.1;
879     if (self.alpha <= 0)
880     {
881         //setmodel(self, "");
882         self.alpha = -1;
883         self.effects |= EF_NODRAW;
884     }
885     else
886     {
887         self.nextthink = time + 0.1;
888     }
889 }
890
891 void vehicles_painframe()
892 {
893     if(self.owner.vehicle_health <= 50)
894     if(self.pain_frame < time)
895     {
896         float _ftmp;
897         _ftmp = self.owner.vehicle_health / 50;
898         self.pain_frame = time + 0.1 + (random() * 0.5 * _ftmp);
899         pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
900
901         if(self.vehicle_flags & VHF_DMGSHAKE)
902             self.velocity += randomvec() * 30;
903
904         if(self.vehicle_flags & VHF_DMGROLL)
905             if(self.vehicle_flags & VHF_DMGHEADROLL)
906                 self.tur_head.angles += randomvec();
907             else
908                 self.angles += randomvec();
909
910     }
911 }
912
913 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
914 {
915     self.dmg_time = time;
916
917         // WEAPONTODO
918     if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
919         damage *= autocvar_g_vehicles_vortex_damagerate;
920
921     if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
922         damage *= autocvar_g_vehicles_machinegun_damagerate;
923
924     if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
925         damage *= autocvar_g_vehicles_rifle_damagerate;
926
927     if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
928         damage *= autocvar_g_vehicles_vaporizer_damagerate;
929
930     if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
931         damage *= autocvar_g_vehicles_tag_damagerate;
932
933     self.enemy = attacker;
934
935     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
936     {
937         if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
938         {
939             self.vehicle_shieldent = spawn();
940             self.vehicle_shieldent.effects = EF_LOWPRECISION;
941
942             setmodel(self.vehicle_shieldent, "models/vhshield.md3");
943             setattachment(self.vehicle_shieldent, self, "");
944             setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
945             self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
946             self.vehicle_shieldent.think       = shieldhit_think;
947         }
948
949         self.vehicle_shieldent.colormod    = '1 1 1';
950         self.vehicle_shieldent.alpha       = 0.45;
951         self.vehicle_shieldent.angles      = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
952         self.vehicle_shieldent.nextthink   = time;
953         self.vehicle_shieldent.effects &= ~EF_NODRAW;
954
955         self.vehicle_shield -= damage;
956
957         if(self.vehicle_shield < 0)
958         {
959             self.vehicle_health            -= fabs(self.vehicle_shield);
960             self.vehicle_shieldent.colormod = '2 0 0';
961             self.vehicle_shield             = 0;
962             self.vehicle_shieldent.alpha    = 0.75;
963
964                 if(sound_allowed(MSG_BROADCAST, attacker))
965                 spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM);   // FIXME: PLACEHOLDER
966         }
967         else
968                 if(sound_allowed(MSG_BROADCAST, attacker))
969                 spamsound (self, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
970
971     }
972     else
973     {
974         self.vehicle_health -= damage;
975
976         if(sound_allowed(MSG_BROADCAST, attacker))
977             spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
978     }
979
980         if(self.damageforcescale < 1 && self.damageforcescale > 0)
981                 self.velocity += force * self.damageforcescale;
982         else
983                 self.velocity += force;
984
985     if(self.vehicle_health <= 0)
986     {
987         if(self.owner)
988             if(self.vehicle_flags & VHF_DEATHEJECT)
989                 vehicles_exit(VHEF_EJECT);
990             else
991                 vehicles_exit(VHEF_RELESE);
992
993
994         antilag_clear(self);
995
996         self.vehicle_die();
997         vehicles_setreturn();
998     }
999 }
1000
1001 void vehicles_clearreturn()
1002 {
1003     entity ret;
1004     // Remove "return helper", if any.
1005     ret = findchain(classname, "vehicle_return");
1006     while(ret)
1007     {
1008         if(ret.wp00 == self)
1009         {
1010             ret.classname   = "";
1011             ret.think       = SUB_Remove;
1012             ret.nextthink   = time + 0.1;
1013
1014             if(ret.waypointsprite_attached)
1015                 WaypointSprite_Kill(ret.waypointsprite_attached);
1016
1017             return;
1018         }
1019         ret = ret.chain;
1020     }
1021 }
1022
1023 void vehicles_return()
1024 {
1025     pointparticles(particleeffectnum("teleport"), self.wp00.origin + '0 0 64', '0 0 0', 1);
1026
1027     self.wp00.think     = vehicles_spawn;
1028     self.wp00.nextthink = time;
1029
1030     if(self.waypointsprite_attached)
1031         WaypointSprite_Kill(self.waypointsprite_attached);
1032
1033     remove(self);
1034 }
1035
1036 void vehicles_showwp_goaway()
1037 {
1038     if(self.waypointsprite_attached)
1039         WaypointSprite_Kill(self.waypointsprite_attached);
1040
1041     remove(self);
1042
1043 }
1044
1045 void vehicles_showwp()
1046 {
1047     entity oldself = world;
1048     vector rgb;
1049
1050     if(self.cnt)
1051     {
1052         self.think      = vehicles_return;
1053         self.nextthink  = self.cnt;
1054     }
1055     else
1056     {
1057         self.think      = vehicles_return;
1058         self.nextthink  = time +1;
1059
1060         oldself = self;
1061         self = spawn();
1062         setmodel(self, "null");
1063         self.team = oldself.wp00.team;
1064         self.wp00 = oldself.wp00;
1065         setorigin(self, oldself.wp00.pos1);
1066
1067         self.nextthink = time + 5;
1068         self.think = vehicles_showwp_goaway;
1069     }
1070
1071     if(teamplay && self.team)
1072             rgb = Team_ColorRGB(self.team);
1073     else
1074             rgb = '1 1 1';
1075     WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, true, RADARICON_POWERUP, rgb);
1076     if(self.waypointsprite_attached)
1077     {
1078         WaypointSprite_UpdateRule(self.waypointsprite_attached, self.wp00.team, SPRITERULE_DEFAULT);
1079         if(oldself == world)
1080             WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);
1081         WaypointSprite_Ping(self.waypointsprite_attached);
1082     }
1083
1084     if(oldself != world)
1085         self = oldself;
1086 }
1087
1088 void vehicles_setreturn()
1089 {
1090     entity ret;
1091
1092     vehicles_clearreturn();
1093
1094     ret = spawn();
1095     ret.classname   = "vehicle_return";
1096     ret.wp00       = self;
1097     ret.team        = self.team;
1098     ret.think       = vehicles_showwp;
1099
1100         if(self.deadflag != DEAD_NO)
1101         {
1102                 ret.cnt = max(game_starttime, time) + self.vehicle_respawntime;
1103                 ret.nextthink = max(game_starttime, time) + max(0, self.vehicle_respawntime - 5);
1104         }
1105         else
1106                 ret.nextthink = max(game_starttime, time) + max(0, self.vehicle_respawntime - 1);
1107
1108     setmodel(ret, "null");
1109     setorigin(ret, self.pos1 + '0 0 96');
1110 }
1111
1112 void vehicles_reset_colors()
1113 {
1114     entity e;
1115     float _effects = 0, _colormap;
1116     vector _glowmod, _colormod;
1117
1118     if(autocvar_g_nodepthtestplayers)
1119         _effects |= EF_NODEPTHTEST;
1120
1121     if(autocvar_g_fullbrightplayers)
1122         _effects |= EF_FULLBRIGHT;
1123
1124     if(self.team)
1125         _colormap = 1024 + (self.team - 1) * 17;
1126     else
1127         _colormap = 1024;
1128
1129     _glowmod  = '0 0 0';
1130     _colormod = '0 0 0';
1131
1132     // Find all ents attacked to main model and setup effects, colormod etc.
1133     e = findchainentity(tag_entity, self);
1134     while(e)
1135     {
1136         if(e != self.vehicle_shieldent)
1137         {
1138             e.effects   = _effects; //  | EF_LOWPRECISION;
1139             e.colormod  = _colormod;
1140             e.colormap  = _colormap;
1141             e.alpha     = 1;
1142         }
1143         e = e.chain;
1144     }
1145
1146     self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
1147     self.vehicle_hudmodel.colormod = self.colormod = _colormod;
1148     self.vehicle_hudmodel.colormap = self.colormap = _colormap;
1149     self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
1150
1151     self.alpha     = 1;
1152     self.avelocity = '0 0 0';
1153     self.velocity  = '0 0 0';
1154     self.effects   = _effects;
1155 }
1156
1157 void vehicle_use()
1158 {
1159     dprint("vehicle ",self.netname, " used by ", activator.classname, "\n");
1160
1161     self.tur_head.team = activator.team;
1162
1163     if(self.tur_head.team == 0)
1164         self.active = ACTIVE_NOT;
1165     else
1166         self.active = ACTIVE_ACTIVE;
1167
1168     if(self.active == ACTIVE_ACTIVE && self.deadflag == DEAD_NO)
1169     {
1170         dprint("^3Eat shit yall!\n");
1171         vehicles_setreturn();
1172         vehicles_reset_colors();
1173     }
1174     else if(self.active == ACTIVE_NOT && self.deadflag != DEAD_NO)
1175     {
1176
1177     }
1178 }
1179
1180 float vehicle_addplayerslot(    entity _owner,
1181                                 entity _slot,
1182                                 float _hud,
1183                                 string _hud_model,
1184                                 float() _framefunc,
1185                                 void(float) _exitfunc)
1186 {
1187     if (!(_owner.vehicle_flags & VHF_MULTISLOT))
1188         _owner.vehicle_flags |= VHF_MULTISLOT;
1189
1190     _slot.PlayerPhysplug = _framefunc;
1191     _slot.vehicle_exit = _exitfunc;
1192     _slot.hud = _hud;
1193     _slot.vehicle_flags = VHF_PLAYERSLOT;
1194     _slot.vehicle_viewport = spawn();
1195     _slot.vehicle_hudmodel = spawn();
1196     _slot.vehicle_hudmodel.viewmodelforclient = _slot;
1197     _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
1198
1199     setmodel(_slot.vehicle_hudmodel, _hud_model);
1200     setmodel(_slot.vehicle_viewport, "null");
1201
1202     setattachment(_slot.vehicle_hudmodel, _slot, "");
1203     setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
1204
1205     return true;
1206 }
1207
1208 float vehicle_initialize(string  net_name,
1209                          string  bodymodel,
1210                          string  topmodel,
1211                          string  hudmodel,
1212                          string  toptag,
1213                          string  hudtag,
1214                          string  viewtag,
1215                          float   vhud,
1216                          vector  min_s,
1217                          vector  max_s,
1218                          float   nodrop,
1219                          void(float _spawnflag)  spawnproc,
1220                          float   _respawntime,
1221                          float() physproc,
1222                          void()  enterproc,
1223                          void(float extflag) exitfunc,
1224                          void() dieproc,
1225                          void() thinkproc,
1226                          float  use_csqc,
1227                          float _max_health,
1228                          float _max_shield)
1229 {
1230         if(!autocvar_g_vehicles)
1231                 return false;
1232
1233     if(self.targetname)
1234     {
1235         self.vehicle_controller = find(world, target, self.targetname);
1236         if(!self.vehicle_controller)
1237         {
1238             bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
1239         }
1240         else
1241         {
1242             self.team = self.vehicle_controller.team;
1243             self.use = vehicle_use;
1244
1245             if(teamplay)
1246             {
1247                 if(self.vehicle_controller.team == 0)
1248                     self.active = ACTIVE_NOT;
1249                 else
1250                     self.active = ACTIVE_ACTIVE;
1251             }
1252         }
1253     }
1254
1255     precache_sound("onslaught/ons_hit2.wav");
1256     precache_sound("onslaught/electricity_explode.wav");
1257
1258
1259     addstat(STAT_HUD, AS_INT,  hud);
1260         addstat(STAT_VEHICLESTAT_HEALTH,  AS_INT, vehicle_health);
1261         addstat(STAT_VEHICLESTAT_SHIELD,  AS_INT, vehicle_shield);
1262         addstat(STAT_VEHICLESTAT_ENERGY,  AS_INT, vehicle_energy);
1263
1264         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT, vehicle_ammo1);
1265         addstat(STAT_VEHICLESTAT_RELOAD1, AS_INT, vehicle_reload1);
1266
1267         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT, vehicle_ammo2);
1268         addstat(STAT_VEHICLESTAT_RELOAD2, AS_INT, vehicle_reload2);
1269
1270     if(bodymodel == "")
1271         error("vehicles: missing bodymodel!");
1272
1273     if(hudmodel == "")
1274         error("vehicles: missing hudmodel!");
1275
1276     if(net_name == "")
1277         self.netname = self.classname;
1278     else
1279         self.netname = net_name;
1280
1281     if(self.team && !teamplay)
1282         self.team = 0;
1283
1284     self.vehicle_flags |= VHF_ISVEHICLE;
1285
1286     setmodel(self, bodymodel);
1287
1288     self.vehicle_viewport   = spawn();
1289     self.vehicle_hudmodel   = spawn();
1290     self.tur_head           = spawn();
1291     self.tur_head.owner     = self;
1292     self.takedamage         = DAMAGE_AIM;
1293     self.bot_attack         = true;
1294     self.iscreature         = true;
1295     self.teleportable       = false; // no teleporting for vehicles, too buggy
1296     self.damagedbycontents      = true;
1297     self.hud                = vhud;
1298     self.tur_health          = _max_health;
1299     self.tur_head.tur_health = _max_shield;
1300     self.vehicle_die         = dieproc;
1301     self.vehicle_exit        = exitfunc;
1302     self.vehicle_enter       = enterproc;
1303     self.PlayerPhysplug      = physproc;
1304     self.event_damage        = func_null;
1305     self.touch               = vehicles_touch;
1306     self.think               = vehicles_spawn;
1307     self.vehicle_spawn       = spawnproc;
1308         self.vehicle_respawntime = max(0, _respawntime);
1309     self.effects             = EF_NODRAW;
1310         self.dphitcontentsmask   = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1311         if(!autocvar_g_vehicles_delayspawn || !self.vehicle_respawntime)
1312                 self.nextthink = time;
1313         else
1314                 self.nextthink = max(time, game_starttime) + max(0, self.vehicle_respawntime + ((random() * 2 - 1) * autocvar_g_vehicles_delayspawn_jitter));
1315
1316         if(autocvar_g_playerclip_collisions)
1317                 self.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1318
1319     if(autocvar_g_nodepthtestplayers)
1320         self.effects = self.effects | EF_NODEPTHTEST;
1321
1322     if(autocvar_g_fullbrightplayers)
1323         self.effects = self.effects | EF_FULLBRIGHT;
1324
1325     setmodel(self.vehicle_hudmodel, hudmodel);
1326     setmodel(self.vehicle_viewport, "null");
1327
1328     if(topmodel != "")
1329     {
1330         setmodel(self.tur_head, topmodel);
1331         setattachment(self.tur_head, self, toptag);
1332         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
1333         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1334     }
1335     else
1336     {
1337         setattachment(self.tur_head, self, "");
1338         setattachment(self.vehicle_hudmodel, self, hudtag);
1339         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1340     }
1341
1342     setsize(self, min_s, max_s);
1343     if (!nodrop)
1344     {
1345         setorigin(self, self.origin);
1346         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1347         setorigin(self, trace_endpos);
1348     }
1349
1350     self.pos1 = self.origin;
1351     self.pos2 = self.angles;
1352     self.tur_head.team = self.team;
1353
1354         if(MUTATOR_CALLHOOK(VehicleSpawn))
1355                 return false;
1356
1357     return true;
1358 }
1359
1360 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
1361                          float _pichlimit_min, float _pichlimit_max,
1362                          float _rotlimit_min, float _rotlimit_max, float _aimspeed)
1363 {
1364     vector vtmp, vtag;
1365     float ftmp;
1366     vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
1367     vtmp = vectoangles(normalize(_target - vtag));
1368     vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
1369     vtmp = AnglesTransform_Normalize(vtmp, true);
1370     ftmp = _aimspeed * frametime;
1371     vtmp.y = bound(-ftmp, vtmp.y, ftmp);
1372     vtmp.x = bound(-ftmp, vtmp.x, ftmp);
1373     _turrret.angles_y = bound(_rotlimit_min, _turrret.angles.y + vtmp.y, _rotlimit_max);
1374     _turrret.angles_x = bound(_pichlimit_min, _turrret.angles.x + vtmp.x, _pichlimit_max);
1375     return vtag;
1376 }
1377
1378 void vehicles_gib_explode()
1379 {
1380         sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
1381         pointparticles(particleeffectnum("explosion_small"), randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
1382         remove(self);
1383 }
1384
1385 void vehicles_gib_think()
1386 {
1387         self.alpha -= 0.1;
1388         if(self.cnt >= time)
1389                 remove(self);
1390         else
1391                 self.nextthink = time + 0.1;
1392 }
1393
1394 entity vehicle_tossgib(entity _template, vector _vel, string _tag, float _burn, float _explode, float _maxtime, vector _rot)
1395 {
1396         entity _gib = spawn();
1397         setmodel(_gib, _template.model);
1398         setorigin(_gib, gettaginfo(self, gettagindex(self, _tag)));
1399         _gib.velocity = _vel;
1400         _gib.movetype = MOVETYPE_TOSS;
1401         _gib.solid = SOLID_CORPSE;
1402         _gib.colormod = '-0.5 -0.5 -0.5';
1403         _gib.effects = EF_LOWPRECISION;
1404         _gib.avelocity = _rot;
1405
1406         if(_burn)
1407                 _gib.effects |= EF_FLAME;
1408
1409         if(_explode)
1410         {
1411                 _gib.think = vehicles_gib_explode;
1412                 _gib.nextthink = time + random() * _explode;
1413                 _gib.touch = vehicles_gib_explode;
1414         }
1415         else
1416         {
1417                 _gib.cnt = time + _maxtime;
1418                 _gib.think = vehicles_gib_think;
1419                 _gib.nextthink = time + _maxtime - 1;
1420                 _gib.alpha = 1;
1421         }
1422         return _gib;
1423 }
1424
1425 /*
1426 vector predict_target(entity _targ, vector _from, float _shot_speed)
1427 {
1428     float i;                // loop
1429     float _distance;        // How far to target
1430     float _impact_time;     // How long untill projectile impacts
1431     vector _predict_pos;    // Predicted enemy location
1432     vector _original_origin;// Where target is before predicted
1433
1434      _original_origin = real_origin(_targ); // Typicaly center of target BBOX
1435
1436     _predict_pos = _original_origin;
1437     for(i = 0; i < 4; ++i)  // Loop a few times to increase prediction accuracy (increase loop count if accuracy is to low)
1438     {
1439         _distance = vlen(_predict_pos - _from); // Get distance to previos predicted location
1440         _impact_time = _distance / _shot_speed; // Calculate impact time
1441         _predict_pos = _original_origin + _targ.velocity * _impact_time; // Calculate new predicted location
1442     }
1443
1444     return _predict_pos;
1445 }
1446 */