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