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