Fix extra life powerup giving infinite lives in lms & minstagib
[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 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 (clienttype(own) != CLIENTTYPE_REAL)
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 (clienttype(own) != CLIENTTYPE_REAL)
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 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 not (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, 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, ATTN_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(e.classname == "player")
507         return TRUE;
508
509     if(e.classname == "monster_zombie")
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     // Vehicle currently in use
541     if(self.owner)
542     {
543         if(other != world)
544         if(vehicles_crushable(other))
545         {
546             if(vlen(self.velocity) != 0)
547                 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);
548
549             return; // Dont do selfdamage when hitting "soft targets".
550         }
551
552         if(self.play_time < time)
553         if(self.vehicle_impact)
554             self.vehicle_impact();
555
556         return;
557     }
558
559     if(other.classname != "player")
560         return;
561
562     if(other.deadflag != DEAD_NO)
563         return;
564
565     if(other.vehicle != world)
566         return;
567
568     vehicles_enter();
569 }
570 var float autocvar_g_vehicles_allow_bots = 0;
571 void vehicles_enter()
572 {
573    // Remove this when bots know how to use vehicles
574    
575     if (clienttype(other) == CLIENTTYPE_BOT)    
576         if (autocvar_g_vehicles_allow_bots)
577             dprint("Bot enters vehicle\n"); // This is where we need to disconnect (some, all?) normal bot AI and hand over to vehicle's _aiframe()
578         else
579             return;
580
581     if(self.phase > time)
582         return;
583
584     if(teamplay)
585     if(self.team)
586     if(self.team != other.team)
587         return;
588
589     RemoveGrapplingHook(other);
590
591     self.vehicle_ammo1   = 0;
592     self.vehicle_ammo2   = 0;
593     self.vehicle_reload1 = 0;
594     self.vehicle_reload2 = 0;
595     self.vehicle_energy  = 0;
596
597     self.owner          = other;
598     self.switchweapon   = other.switchweapon;
599
600     // .viewmodelforclient works better.
601     //self.vehicle_hudmodel.drawonlytoclient = self.owner;
602
603     self.vehicle_hudmodel.viewmodelforclient = self.owner;
604
605     self.event_damage         = vehicles_damage;
606     self.nextthink            = 0;
607     self.owner.angles         = self.angles;
608     self.owner.takedamage     = DAMAGE_NO;
609     self.owner.solid          = SOLID_NOT;
610     self.owner.movetype       = MOVETYPE_NOCLIP;
611     self.owner.alpha          = -1;
612     self.owner.vehicle        = self;
613     self.owner.event_damage   = func_null;
614     self.owner.view_ofs       = '0 0 0';
615     self.colormap             = self.owner.colormap;
616     if(self.tur_head)
617         self.tur_head.colormap    = self.owner.colormap;
618
619     self.owner.hud            = self.hud;
620     self.owner.PlayerPhysplug = self.PlayerPhysplug;
621
622     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
623     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
624     self.owner.vehicle_reload1  = self.vehicle_reload1;
625     self.owner.vehicle_reload2  = self.vehicle_reload2;
626
627     // Cant do this, hides attached objects too.
628     //self.exteriormodeltoclient = self.owner;
629     //self.tur_head.exteriormodeltoclient = self.owner;
630
631     other.flags &~= FL_ONGROUND;
632     self.flags  &~= FL_ONGROUND;
633
634     self.team                 = self.owner.team;
635     self.flags               -= FL_NOTARGET;
636     
637     if (clienttype(other) == CLIENTTYPE_REAL)
638     {
639         msg_entity = other;
640         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
641         WriteEntity(MSG_ONE, self.vehicle_viewport);
642                 
643         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
644         if(self.tur_head)
645         {
646             WriteAngle(MSG_ONE, self.tur_head.angles_x + self.angles_x); // tilt
647             WriteAngle(MSG_ONE, self.tur_head.angles_y + self.angles_y); // yaw
648             WriteAngle(MSG_ONE, 0);                                      // roll
649         }
650         else
651         {
652             WriteAngle(MSG_ONE,  self.angles_x * -1); // tilt
653             WriteAngle(MSG_ONE,  self.angles_y);      // yaw
654             WriteAngle(MSG_ONE,  0);                  // roll
655         }
656     }
657
658     vehicles_clearrturn();
659
660     CSQCVehicleSetup(self.owner, self.hud);
661     
662     vh_player = other;
663     vh_vehicle = self;
664     MUTATOR_CALLHOOK(VehicleEnter);
665     other = vh_player;
666     self = vh_vehicle;
667
668     self.vehicle_enter();
669     antilag_clear(other);
670 }
671
672 /** vehicles_findgoodexit
673     Locates a valid location for the player to exit the vehicle.
674     Will first try prefer_spot, then up 100 random spots arround the vehicle
675     wich are in direct line of sight and empty enougth to hold a players bbox
676 **/
677 vector vehicles_findgoodexit(vector prefer_spot)
678 {
679     //vector exitspot;
680     float mysize;
681
682     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
683     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
684         return prefer_spot;
685
686     mysize = 1.5 * vlen(self.maxs - self.mins);
687     float i;
688     vector v, v2;
689     v2 = 0.5 * (self.absmin + self.absmax);
690     for(i = 0; i < 100; ++i)
691     {
692         v = randomvec();
693         v_z = 0;
694         v = v2 + normalize(v) * mysize;
695         tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
696         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
697             return v;
698     }
699
700     /*
701     exitspot = (self.origin + '0 0 48') + v_forward * mysize;
702     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
703     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
704         return exitspot;
705
706     exitspot = (self.origin + '0 0 48') - v_forward * mysize;
707     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
708     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
709         return exitspot;
710
711     exitspot = (self.origin + '0 0 48') + v_right * mysize;
712     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
713     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
714         return exitspot;
715
716     exitspot = (self.origin + '0 0 48') - v_right * mysize;
717     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
718     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
719         return exitspot;
720     */
721
722     return self.origin;
723 }
724
725 /** vehicles_exit
726     Standarrd vehicle release fucntion.
727     custom code goes in self.vehicle_exit
728 **/
729 float vehicles_exit_running;
730 void vehicles_exit(float eject)
731 {
732     entity _vehicle;
733     entity _player;
734     entity _oldself = self;
735     
736     if(vehicles_exit_running)
737     {
738         dprint("^1vehicles_exit allready running! this is not good..\n");
739         return;
740     }
741     
742     vehicles_exit_running = TRUE;
743     if(self.flags & FL_CLIENT)
744     {
745         _vehicle = self.vehicle;
746             
747         if (_vehicle.vehicle_flags & VHF_PLAYERSLOT)
748         {
749             _vehicle.vehicle_exit(eject);
750             self = _oldself;
751             vehicles_exit_running = FALSE;
752             return;            
753         }
754     }
755     else
756         _vehicle = self;
757     
758     _player = _vehicle.owner;
759     
760     self = _vehicle;
761
762     if (_player)
763     {
764         if (clienttype(_player) == CLIENTTYPE_REAL)
765         {
766             msg_entity = _player;
767             WriteByte (MSG_ONE, SVC_SETVIEWPORT);
768             WriteEntity( MSG_ONE, _player);
769
770             WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
771             WriteAngle(MSG_ONE, 0);
772             WriteAngle(MSG_ONE, _vehicle.angles_y);
773             WriteAngle(MSG_ONE, 0);
774         }
775         
776         setsize(_player, PL_MIN,PL_MAX);
777
778         _player.takedamage     = DAMAGE_AIM;
779         _player.solid          = SOLID_SLIDEBOX;
780         _player.movetype       = MOVETYPE_WALK;
781         _player.effects        &~= EF_NODRAW;
782         _player.alpha          = 1;
783         _player.PlayerPhysplug = func_null;
784         _player.vehicle        = world;
785         _player.view_ofs       = PL_VIEW_OFS;
786         _player.event_damage   = PlayerDamage;
787         _player.hud            = HUD_NORMAL;
788         _player.switchweapon   = _vehicle.switchweapon;
789
790         CSQCVehicleSetup(_player, HUD_NORMAL);
791     }
792     _vehicle.flags |= FL_NOTARGET;
793     
794     if(_vehicle.deadflag == DEAD_NO)
795         _vehicle.avelocity          = '0 0 0';
796     
797     _vehicle.tur_head.nodrawtoclient             = world;
798     
799     if(!teamplay)
800         _vehicle.team = 0;
801     else
802
803     vh_player = _player;
804     vh_vehicle = _vehicle;
805     MUTATOR_CALLHOOK(VehicleExit);
806     _player = vh_player;
807     _vehicle = vh_vehicle;
808
809     _vehicle.team = _vehicle.tur_head.team;
810         
811     sound (_vehicle, CH_TRIGGER_SINGLE, "misc/null.wav", 1, ATTN_NORM);
812     _vehicle.vehicle_hudmodel.viewmodelforclient = _vehicle;    
813     _vehicle.phase = time + 1;
814     
815     _vehicle.vehicle_exit(eject);
816     
817     vehicles_setreturn();
818     vehicles_reset_colors();        
819     _vehicle.owner = world;
820     self = _oldself;
821     
822     vehicles_exit_running = FALSE;
823 }
824
825
826 void vehicles_regen(float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
827 {
828     if(self.regen_field < field_max)
829     if(timer + rpause < time)
830     {
831         if(_healthscale)
832             regen = regen * (self.vehicle_health / self.tur_health);
833             
834         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
835
836         if(self.owner)
837             self.owner.regen_field = (self.regen_field / field_max) * 100;
838     }
839 }
840
841 void shieldhit_think()
842 {
843     self.alpha -= 0.1;
844     if (self.alpha <= 0)
845     {
846         //setmodel(self, "");
847         self.alpha = -1;
848         self.effects |= EF_NODRAW;
849     }
850     else
851     {
852         self.nextthink = time + 0.1;
853     }
854 }
855
856 void vehicles_painframe()
857 {
858     if(self.owner.vehicle_health <= 50)
859     if(self.pain_frame < time)
860     {
861         float _ftmp;
862         _ftmp = self.owner.vehicle_health / 50;
863         self.pain_frame = time + 0.1 + (random() * 0.5 * _ftmp);
864         pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
865
866         if(self.vehicle_flags & VHF_DMGSHAKE)
867             self.velocity += randomvec() * 30;
868
869         if(self.vehicle_flags & VHF_DMGROLL)
870             if(self.vehicle_flags & VHF_DMGHEADROLL)
871                 self.tur_head.angles += randomvec();
872             else
873                 self.angles += randomvec();
874
875     }
876 }
877
878 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
879 {
880     self.dmg_time = time;
881     
882     if(DEATH_ISWEAPON(deathtype, WEP_NEX))
883         damage *= autocvar_g_vehicles_nex_damagerate;
884         
885     if(DEATH_ISWEAPON(deathtype, WEP_UZI))
886         damage *= autocvar_g_vehicles_uzi_damagerate;
887         
888     if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
889         damage *= autocvar_g_vehicles_rifle_damagerate;
890         
891     if(DEATH_ISWEAPON(deathtype, WEP_MINSTANEX))
892         damage *= autocvar_g_vehicles_minstanex_damagerate;
893
894     if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
895         damage *= autocvar_g_vehicles_tag_damagerate;
896     
897     self.enemy = attacker;
898     
899     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
900     {
901         if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
902         {
903             self.vehicle_shieldent = spawn();
904             self.vehicle_shieldent.effects = EF_LOWPRECISION;
905
906             setmodel(self.vehicle_shieldent, "models/vhshield.md3");
907             setattachment(self.vehicle_shieldent, self, "");
908             setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
909             self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
910             self.vehicle_shieldent.think       = shieldhit_think;
911         }
912
913         self.vehicle_shieldent.colormod    = '1 1 1';
914         self.vehicle_shieldent.alpha       = 0.45;
915         self.vehicle_shieldent.angles      = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
916         self.vehicle_shieldent.nextthink   = time;
917         self.vehicle_shieldent.effects &~= EF_NODRAW;
918
919         self.vehicle_shield -= damage;
920
921         if(self.vehicle_shield < 0)
922         {
923             self.vehicle_health            -= fabs(self.vehicle_shield);
924             self.vehicle_shieldent.colormod = '2 0 0';
925             self.vehicle_shield             = 0;
926             self.vehicle_shieldent.alpha    = 0.75;
927
928                 if(sound_allowed(MSG_BROADCAST, attacker))
929                 spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);   // FIXME: PLACEHOLDER
930         }
931         else
932                 if(sound_allowed(MSG_BROADCAST, attacker))
933                 spamsound (self, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
934
935     }
936     else
937     {
938         self.vehicle_health -= damage;
939
940         if(sound_allowed(MSG_BROADCAST, attacker))
941             spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
942     }
943     
944         if(self.damageforcescale < 1 && self.damageforcescale > 0)
945                 self.velocity += force * self.damageforcescale;
946         else
947                 self.velocity += force;
948
949     if(self.vehicle_health <= 0)
950     {
951         if(self.owner)
952             if(self.vehicle_flags & VHF_DEATHEJECT)
953                 vehicles_exit(VHEF_EJECT);
954             else
955                 vehicles_exit(VHEF_RELESE);
956
957
958         antilag_clear(self);
959
960         self.vehicle_die();
961         vehicles_setreturn();
962     }
963 }
964
965 void vehicles_clearrturn()
966 {
967     entity ret;
968     // Remove "return helper", if any.
969     ret = findchain(classname, "vehicle_return");
970     while(ret)
971     {
972         if(ret.wp00 == self)
973         {
974             ret.classname   = "";
975             ret.think       = SUB_Remove;
976             ret.nextthink   = time + 0.1;
977
978             if(ret.waypointsprite_attached)
979                 WaypointSprite_Kill(ret.waypointsprite_attached);
980
981             return;
982         }
983         ret = ret.chain;
984     }
985 }
986
987 void vehicles_return()
988 {
989     pointparticles(particleeffectnum("teleport"), self.wp00.origin + '0 0 64', '0 0 0', 1);
990
991     self.wp00.think     = vehicles_spawn;
992     self.wp00.nextthink = time;
993
994     if(self.waypointsprite_attached)
995         WaypointSprite_Kill(self.waypointsprite_attached);
996
997     remove(self);
998 }
999
1000 void vehicles_showwp_goaway()
1001 {
1002     if(self.waypointsprite_attached)
1003         WaypointSprite_Kill(self.waypointsprite_attached);
1004
1005     remove(self);
1006
1007 }
1008
1009 void vehicles_showwp()
1010 {
1011     entity oldself = world;
1012     vector rgb;
1013
1014     if(self.cnt)
1015     {
1016         self.think      = vehicles_return;
1017         self.nextthink  = self.cnt;
1018     }
1019     else
1020     {
1021         self.think      = vehicles_return;
1022         self.nextthink  = time +1;
1023
1024         oldself = self;
1025         self = spawn();
1026         setmodel(self, "null");
1027         self.team = oldself.wp00.team;
1028         self.wp00 = oldself.wp00;
1029         setorigin(self, oldself.wp00.pos1);
1030
1031         self.nextthink = time + 5;
1032         self.think = vehicles_showwp_goaway;
1033     }
1034
1035     if(teamplay && self.team)
1036             rgb = Team_ColorRGB(self.team);
1037     else
1038             rgb = '1 1 1';
1039     WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE, RADARICON_POWERUP, rgb);
1040     if(self.waypointsprite_attached)
1041     {
1042         WaypointSprite_UpdateRule(self.waypointsprite_attached, self.wp00.team, SPRITERULE_DEFAULT);
1043         if(oldself == world)
1044             WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);
1045         WaypointSprite_Ping(self.waypointsprite_attached);
1046     }
1047
1048     if(oldself != world)
1049         self = oldself;
1050 }
1051
1052 void vehicles_setreturn()
1053 {
1054     entity ret;
1055
1056     vehicles_clearrturn();
1057
1058     ret = spawn();
1059     ret.classname   = "vehicle_return";
1060     ret.wp00       = self;
1061     ret.team        = self.team;
1062     ret.think       = vehicles_showwp;
1063
1064     if(self.deadflag != DEAD_NO)
1065     {
1066         ret.cnt         = time + self.vehicle_respawntime;
1067         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 5);
1068     }
1069     else
1070     {
1071         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 1);
1072     }
1073
1074     setmodel(ret, "null");
1075     setorigin(ret, self.pos1 + '0 0 96');
1076
1077 }
1078
1079 void vehicles_reset_colors()
1080 {
1081     entity e;
1082     float _effects = 0, _colormap;
1083     vector _glowmod, _colormod;
1084
1085     if(autocvar_g_nodepthtestplayers)
1086         _effects |= EF_NODEPTHTEST;
1087
1088     if(autocvar_g_fullbrightplayers)
1089         _effects |= EF_FULLBRIGHT;
1090
1091     if(self.team)
1092         _colormap = 1024 + (self.team - 1) * 17;
1093     else
1094         _colormap = 1024;
1095
1096     _glowmod  = '0 0 0';
1097     _colormod = '0 0 0';
1098
1099     // Find all ents attacked to main model and setup effects, colormod etc.
1100     e = findchainentity(tag_entity, self);
1101     while(e)
1102     {
1103         if(e != self.vehicle_shieldent)
1104         {
1105             e.effects   = _effects; //  | EF_LOWPRECISION;
1106             e.colormod  = _colormod;
1107             e.colormap  = _colormap;
1108             e.alpha     = 1;
1109         }
1110         e = e.chain;
1111     }
1112
1113     self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
1114     self.vehicle_hudmodel.colormod = self.colormod = _colormod;
1115     self.vehicle_hudmodel.colormap = self.colormap = _colormap;
1116     self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
1117
1118     self.alpha     = 1;
1119     self.avelocity = '0 0 0';
1120     self.velocity  = '0 0 0';
1121     self.effects   = _effects;
1122 }
1123
1124 void vehicle_use()
1125 {
1126     dprint("vehicle ",self.netname, " used by ", activator.classname, "\n");
1127
1128     self.tur_head.team = activator.team;
1129
1130     if(self.tur_head.team == 0)
1131         self.active = ACTIVE_NOT;
1132     else
1133         self.active = ACTIVE_ACTIVE;
1134     
1135     if(self.active == ACTIVE_ACTIVE && self.deadflag == DEAD_NO)
1136     {
1137         dprint("^3Eat shit yall!\n");
1138         vehicles_setreturn();
1139         vehicles_reset_colors();
1140     }
1141     else if(self.active == ACTIVE_NOT && self.deadflag != DEAD_NO)
1142     {
1143         
1144     }
1145 }
1146
1147 float vehicle_addplayerslot(    entity _owner, 
1148                                 entity _slot, 
1149                                 float _hud, 
1150                                 string _hud_model,
1151                                 float() _framefunc, 
1152                                 void(float) _exitfunc)
1153 {
1154     if not (_owner.vehicle_flags & VHF_MULTISLOT)
1155         _owner.vehicle_flags |= VHF_MULTISLOT;
1156
1157     _slot.PlayerPhysplug = _framefunc;
1158     _slot.vehicle_exit = _exitfunc;
1159     _slot.hud = _hud;
1160     _slot.vehicle_flags = VHF_PLAYERSLOT;
1161     _slot.vehicle_viewport = spawn();
1162     _slot.vehicle_hudmodel = spawn();
1163     _slot.vehicle_hudmodel.viewmodelforclient = _slot;
1164     _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
1165     
1166     setmodel(_slot.vehicle_hudmodel, _hud_model);
1167     setmodel(_slot.vehicle_viewport, "null");
1168     
1169     setattachment(_slot.vehicle_hudmodel, _slot, "");
1170     setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
1171     
1172     return TRUE;
1173 }
1174
1175 float vehicle_initialize(string  net_name,
1176                          string  bodymodel,
1177                          string  topmodel,
1178                          string  hudmodel,
1179                          string  toptag,
1180                          string  hudtag,
1181                          string  viewtag,
1182                          float   vhud,
1183                          vector  min_s,
1184                          vector  max_s,
1185                          float   nodrop,
1186                          void(float _spawnflag)  spawnproc,
1187                          float   _respawntime,
1188                          float() physproc,
1189                          void()  enterproc,
1190                          void(float extflag) exitfunc,
1191                          void() dieproc,
1192                          void() thinkproc,
1193                          float  use_csqc,
1194                          float _max_health,
1195                          float _max_shield)
1196 {
1197         if(!autocvar_g_vehicles)
1198                 return FALSE;
1199         
1200     if(self.targetname)
1201     {
1202         self.vehicle_controller = find(world, target, self.targetname);
1203         if(!self.vehicle_controller)
1204         {
1205             bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
1206         }
1207         else
1208         {
1209             self.team = self.vehicle_controller.team;        
1210             self.use = vehicle_use;
1211             
1212             if(teamplay)
1213             {
1214                 if(self.vehicle_controller.team == 0)
1215                     self.active = ACTIVE_NOT;
1216                 else
1217                     self.active = ACTIVE_ACTIVE;                
1218             }
1219         }
1220     }
1221     
1222     precache_sound("onslaught/ons_hit2.wav");
1223     precache_sound("onslaught/electricity_explode.wav");
1224
1225
1226     addstat(STAT_HUD, AS_INT,  hud);
1227         addstat(STAT_VEHICLESTAT_HEALTH,  AS_INT, vehicle_health);
1228         addstat(STAT_VEHICLESTAT_SHIELD,  AS_INT, vehicle_shield);
1229         addstat(STAT_VEHICLESTAT_ENERGY,  AS_INT, vehicle_energy);
1230
1231         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT, vehicle_ammo1);
1232         addstat(STAT_VEHICLESTAT_RELOAD1, AS_INT, vehicle_reload1);
1233
1234         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT, vehicle_ammo2);
1235         addstat(STAT_VEHICLESTAT_RELOAD2, AS_INT, vehicle_reload2);
1236
1237     if(bodymodel == "")
1238         error("vehicles: missing bodymodel!");
1239
1240     if(hudmodel == "")
1241         error("vehicles: missing hudmodel!");
1242
1243     if(net_name == "")
1244         self.netname = self.classname;
1245     else
1246         self.netname = net_name;
1247
1248     if(self.team && !teamplay)
1249         self.team = 0;
1250
1251     self.vehicle_flags |= VHF_ISVEHICLE;
1252
1253     setmodel(self, bodymodel);
1254
1255     self.vehicle_viewport   = spawn();
1256     self.vehicle_hudmodel   = spawn();
1257     self.tur_head           = spawn();
1258     self.tur_head.owner     = self;
1259     self.takedamage         = DAMAGE_AIM;
1260     self.bot_attack         = TRUE;
1261     self.iscreature         = TRUE;
1262     self.teleportable       = FALSE; // no teleporting for vehicles, too buggy
1263     self.damagedbycontents      = TRUE;
1264     self.hud                = vhud;
1265     self.tur_health          = _max_health;
1266     self.tur_head.tur_health = _max_shield;
1267     self.vehicle_die         = dieproc;
1268     self.vehicle_exit        = exitfunc;
1269     self.vehicle_enter       = enterproc;
1270     self.PlayerPhysplug      = physproc;
1271     self.event_damage        = func_null;
1272     self.touch               = vehicles_touch;
1273     self.think               = vehicles_spawn;
1274     self.nextthink           = time;
1275     self.vehicle_respawntime = _respawntime;
1276     self.vehicle_spawn       = spawnproc;
1277     self.effects             = EF_NODRAW;
1278     if(!autocvar_g_vehicles_delayspawn)
1279         self.nextthink = time + 0.5;
1280     else
1281         self.nextthink = time + _respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1282
1283
1284     if(autocvar_g_nodepthtestplayers)
1285         self.effects = self.effects | EF_NODEPTHTEST;
1286
1287     if(autocvar_g_fullbrightplayers)
1288         self.effects = self.effects | EF_FULLBRIGHT;
1289
1290     setmodel(self.vehicle_hudmodel, hudmodel);
1291     setmodel(self.vehicle_viewport, "null");
1292
1293     if(topmodel != "")
1294     {
1295         setmodel(self.tur_head, topmodel);
1296         setattachment(self.tur_head, self, toptag);
1297         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
1298         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1299     }
1300     else
1301     {
1302         setattachment(self.tur_head, self, "");
1303         setattachment(self.vehicle_hudmodel, self, hudtag);
1304         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1305     }
1306
1307     setsize(self, min_s, max_s);
1308     if not (nodrop)
1309     {
1310         setorigin(self, self.origin);
1311         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1312         setorigin(self, trace_endpos);
1313     }
1314     
1315     self.pos1 = self.origin;
1316     self.pos2 = self.angles;
1317     self.tur_head.team = self.team;
1318         
1319         if(MUTATOR_CALLHOOK(VehicleSpawn))
1320                 return FALSE;
1321
1322     return TRUE;
1323 }
1324
1325 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname, 
1326                          float _pichlimit_min, float _pichlimit_max, 
1327                          float _rotlimit_min, float _rotlimit_max, float _aimspeed)
1328 {
1329     vector vtmp, vtag;
1330     float ftmp;
1331     vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
1332     vtmp = vectoangles(normalize(_target - vtag));
1333     vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
1334     vtmp = AnglesTransform_Normalize(vtmp, TRUE);
1335     ftmp = _aimspeed * frametime;
1336     vtmp_y = bound(-ftmp, vtmp_y, ftmp);
1337     vtmp_x = bound(-ftmp, vtmp_x, ftmp);
1338     _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);    
1339     _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
1340     return vtag;
1341 }
1342
1343 void vehicles_gib_explode()
1344 {
1345         sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
1346         pointparticles(particleeffectnum("explosion_small"), randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
1347         remove(self);
1348 }
1349
1350 void vehicles_gib_think()
1351 {
1352         self.alpha -= 0.1;
1353         if(self.cnt >= time)
1354                 remove(self);
1355         else
1356                 self.nextthink = time + 0.1;
1357 }
1358
1359 entity vehicle_tossgib(entity _template, vector _vel, string _tag, float _burn, float _explode, float _maxtime, vector _rot)
1360 {
1361         entity _gib = spawn();
1362         setmodel(_gib, _template.model);
1363         setorigin(_gib, gettaginfo(self, gettagindex(self, _tag)));
1364         _gib.velocity = _vel;
1365         _gib.movetype = MOVETYPE_TOSS;
1366         _gib.solid = SOLID_CORPSE;
1367         _gib.colormod = '-0.5 -0.5 -0.5';
1368         _gib.effects = EF_LOWPRECISION; 
1369         _gib.avelocity = _rot;
1370         
1371         if(_burn)
1372                 _gib.effects |= EF_FLAME;
1373         
1374         if(_explode)
1375         {
1376                 _gib.think = vehicles_gib_explode; 
1377                 _gib.nextthink = time + random() * _explode;
1378                 _gib.touch = vehicles_gib_explode;
1379         }
1380         else
1381         {
1382                 _gib.cnt = time + _maxtime;
1383                 _gib.think = vehicles_gib_think; 
1384                 _gib.nextthink = time + _maxtime - 1;           
1385                 _gib.alpha = 1;
1386         }
1387         return _gib;
1388 }
1389
1390 /*
1391 vector predict_target(entity _targ, vector _from, float _shot_speed)
1392 {
1393     float i;                // loop
1394     float _distance;        // How far to target
1395     float _impact_time;     // How long untill projectile impacts
1396     vector _predict_pos;    // Predicted enemy location
1397     vector _original_origin;// Where target is before predicted
1398
1399      _original_origin = real_origin(_targ); // Typicaly center of target BBOX
1400
1401     _predict_pos = _original_origin;
1402     for(i = 0; i < 4; ++i)  // Loop a few times to increase prediction accuracy (increase loop count if accuracy is to low)
1403     {
1404         _distance = vlen(_predict_pos - _from); // Get distance to previos predicted location
1405         _impact_time = _distance / _shot_speed; // Calculate impact time
1406         _predict_pos = _original_origin + _targ.velocity * _impact_time; // Calculate new predicted location
1407     }
1408
1409     return _predict_pos;
1410 }
1411 */