]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/vehicles/vehicles.qc
Add mutator hooks for vehicle entrance and exit -- this way CTF code can handle it...
[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 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
7 void vehicles_return();
8 void vehicles_enter();
9 void vehicles_touch();
10 void vehicles_reset_colors();
11 void vehicles_clearrturn();
12 void vehicles_setreturn();
13
14
15 /** AuxiliaryXhair*
16     Send additional points of interest to be drawn, to vehicle owner
17 **/
18 float MAX_AXH = 4;
19 .entity AuxiliaryXhair[MAX_AXH];
20
21 float SendAuxiliaryXhair(entity to, float sf)
22 {
23
24         WriteByte(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
25
26         WriteByte(MSG_ENTITY, self.cnt);
27
28         WriteCoord(MSG_ENTITY, self.origin_x);
29         WriteCoord(MSG_ENTITY, self.origin_y);
30         WriteCoord(MSG_ENTITY, self.origin_z);
31
32     WriteByte(MSG_ENTITY, rint(self.colormod_x * 255));
33     WriteByte(MSG_ENTITY, rint(self.colormod_y * 255));
34     WriteByte(MSG_ENTITY, rint(self.colormod_z * 255));
35
36     return TRUE;
37 }
38
39 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
40 {
41     entity axh;
42
43     axh_id = bound(0, axh_id, MAX_AXH);
44     axh = own.(AuxiliaryXhair[axh_id]);
45
46     if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
47     {
48         axh                     = spawn();
49         axh.cnt                 = axh_id;
50         axh.drawonlytoclient    = own;
51         axh.owner               = own;
52         Net_LinkEntity(axh, FALSE, 0, SendAuxiliaryXhair);
53     }
54
55     setorigin(axh, loc);
56     axh.colormod            = clr;
57     axh.SendFlags           = 0x01;
58     own.(AuxiliaryXhair[axh_id]) = axh;
59 }
60
61 /*
62 // SVC_TEMPENTITY based, horrible with even 50 ping. hm.
63 // WriteByte(MSG_ONE, SVC_TEMPENTITY) uses reliable messagess, never use for thinsg that need continous updates.
64 void SendAuxiliaryXhair2(entity own, vector loc, vector clr, float axh_id)
65 {
66         msg_entity = own;
67
68         WriteByte(MSG_ONE, SVC_TEMPENTITY);
69         WriteByte(MSG_ONE, TE_CSQC_AUXILIARYXHAIR);
70
71         WriteByte(MSG_ONE, axh_id);
72
73         WriteCoord(MSG_ONE, loc_x);
74         WriteCoord(MSG_ONE, loc_y);
75         WriteCoord(MSG_ONE, loc_z);
76
77     WriteByte(MSG_ONE, rint(clr_x * 255));
78     WriteByte(MSG_ONE, rint(clr_y * 255));
79     WriteByte(MSG_ONE, rint(clr_z * 255));
80
81 }
82 */
83 // End AuxiliaryXhair
84
85 /**
86     Notifies the client that he enterd a vehicle, and sends 
87     realavent data.
88     
89     only sends vehicle_id atm (wich is a HUD_* constant, ex. HUD_SPIDERBOT)
90 **/
91 void CSQCVehicleSetup(entity own, float vehicle_id)
92 {
93         msg_entity = own;
94
95         WriteByte(MSG_ONE, SVC_TEMPENTITY);
96         WriteByte(MSG_ONE, TE_CSQC_VEHICLESETUP);
97         WriteByte(MSG_ONE, vehicle_id);
98 }
99
100 /** vehicles_locktarget
101
102     Generic target locking.
103
104     Figure out if what target is "locked" (if any), for missile tracking as such.
105
106     after calling, "if(self.lock_target != world && self.lock_strength == 1)" mean
107     you have a locked in target.
108
109     Exspects a crosshair_trace() or equivalent to be
110     dont before calling.
111
112 **/
113 .entity lock_target;
114 .float  lock_strength;
115 .float  lock_time;
116 .float  lock_soundtime;
117 void vehicles_locktarget(float incr, float decr, float _lock_time)
118 {
119     if(self.lock_target && self.lock_target.deadflag != DEAD_NO)
120     {
121         self.lock_target    = world;
122         self.lock_strength  = 0;
123         self.lock_time      = 0;
124     }
125
126     if(self.lock_time > time)
127     {
128         if(self.lock_target)
129         if(self.lock_soundtime < time)
130         {
131             self.lock_soundtime = time + 0.5;
132             play2(self.owner, "vehicles/locked.wav");
133         }
134         
135         return;
136     }
137
138     if(trace_ent != world)
139     {
140         if(teamplay && trace_ent.team == self.team)
141             trace_ent = world;
142
143         if(trace_ent.deadflag != DEAD_NO)
144             trace_ent = world;
145
146         if not (trace_ent.vehicle_flags & VHF_ISVEHICLE || trace_ent.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
147             trace_ent = world;
148     }
149
150     if(self.lock_target == world && trace_ent != world)
151         self.lock_target = trace_ent;
152     
153     if(self.lock_target && trace_ent == self.lock_target) 
154     {            
155         if(self.lock_strength != 1 && self.lock_strength + incr >= 1)
156         {
157             play2(self.owner, "vehicles/lock.wav");
158             self.lock_soundtime = time + 0.8;
159         }        
160         else if (self.lock_strength != 1 && self.lock_soundtime < time)
161         {            
162             play2(self.owner, "vehicles/locking.wav");
163             self.lock_soundtime = time + 0.3;
164         }
165         
166     }    
167         
168     // Have a locking target
169     // Trace hit current target
170     if(trace_ent == self.lock_target && trace_ent != world)
171     {
172         self.lock_strength = min(self.lock_strength + incr, 1);
173         if(self.lock_strength == 1)
174             self.lock_time = time + _lock_time;
175     }
176     else
177     {
178         if(trace_ent)
179             self.lock_strength = max(self.lock_strength - decr * 2, 0);
180         else
181             self.lock_strength = max(self.lock_strength - decr, 0);
182
183         if(self.lock_strength == 0)
184             self.lock_target = world;
185     }
186 }
187
188 #define VEHICLE_UPDATE_PLAYER(fld,vhname) \
189 self.owner.vehicle_##fld = (self.vehicle_##fld / autocvar_g_vehicle_##vhname##_##fld) * 100
190
191 #define vehicles_sweap_collision(orig,vel,dt,acm,mult) \
192 traceline(orig, orig + vel * dt, MOVE_NORMAL, self); \
193 if(trace_fraction != 1) \
194     acm += normalize(self.origin - trace_endpos) * (vlen(vel) * mult)
195
196 // Hover movement support
197 float  force_fromtag_power;
198 float  force_fromtag_normpower;
199 vector force_fromtag_origin;
200 vector vehicles_force_fromtag_hover(string tag_name, float spring_length, float max_power)
201 {
202     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
203     v_forward  = normalize(v_forward) * -1;
204     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
205
206     force_fromtag_power = (1 - trace_fraction) * max_power;
207     force_fromtag_normpower = force_fromtag_power / max_power;
208
209     return v_forward  * force_fromtag_power;
210 }
211
212 // Experimental hovermode wich uses attraction/repulstion from surface insted of gravity/repulsion
213 // Can possibly be use to move abt any surface (inclusing walls/celings)
214 vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float max_power)
215 {
216
217     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
218     v_forward  = normalize(v_forward) * -1;
219     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
220
221     // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
222     if(trace_fraction == 1.0)
223     {
224         force_fromtag_normpower = -0.25;
225         return '0 0 -200';
226     }
227
228     force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
229     force_fromtag_normpower = force_fromtag_power / max_power;
230
231     return v_forward  * force_fromtag_power;
232 }
233
234 // Generic vehile projectile system
235 void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
236 {
237     // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
238     if(inflictor.owner == self.owner)
239         return; 
240     
241     self.health -= damage;
242     self.velocity += force;
243     if(self.health < 1)
244     {
245         self.takedamage = DAMAGE_NO;
246         self.event_damage = SUB_Null;
247         self.think = self.use;
248         self.nextthink = time;
249     }
250
251 }
252
253 void vehicles_projectile_explode()
254 {
255     if(self.owner && other != world)
256     {
257         if(other == self.owner.vehicle)
258             return;
259
260         if(other == self.owner.vehicle.tur_head)
261             return;
262     }
263
264         PROJECTILE_TOUCH;
265
266         self.event_damage = SUB_Null;
267     RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, self.shot_force, self.totalfrags, other);
268
269     remove (self);
270 }
271
272 entity vehicles_projectile(string _mzlfx, string _mzlsound,
273                            vector _org, vector _vel,
274                            float _dmg, float _radi, float _force,  float _size,
275                            float _deahtype, float _projtype, float _health,
276                            float _cull, float _clianim)
277 {
278     entity proj;
279
280     proj = spawn();
281
282     PROJECTILE_MAKETRIGGER(proj);
283     setorigin(proj, _org);
284
285     proj.shot_dmg         = _dmg;
286     proj.shot_radius      = _radi;
287     proj.shot_force       = _force;
288     proj.totalfrags       = _deahtype;
289     proj.solid            = SOLID_BBOX;
290     proj.movetype         = MOVETYPE_FLYMISSILE;
291     proj.flags            = FL_PROJECTILE;
292     proj.bot_dodge        = TRUE;
293     proj.bot_dodgerating  = _dmg;
294     proj.velocity         = _vel;
295     proj.touch            = vehicles_projectile_explode;
296     proj.use              = vehicles_projectile_explode;
297     proj.owner            = self;
298     proj.realowner        = self.owner;
299     proj.think            = SUB_Remove;
300     proj.nextthink        = time + 30;
301
302     if(_health)
303     {
304         proj.takedamage       = DAMAGE_AIM;
305         proj.event_damage     = vehicles_projectile_damage;
306         proj.health           = _health;
307     }
308     else
309         proj.flags           = FL_PROJECTILE | FL_NOTARGET;
310
311     if(_mzlsound)
312         sound (self, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTN_NORM);
313
314     if(_mzlfx)
315         pointparticles(particleeffectnum(_mzlfx), proj.origin, proj.velocity, 1);
316
317
318     setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
319
320     CSQCProjectile(proj, _clianim, _projtype, _cull);
321
322     return proj;
323 }
324 // End generic vehile projectile system
325
326 /** vehicles_spawn
327     Exetuted for all vehicles on (re)spawn.
328     Sets defaults for newly spawned units.
329 **/
330 void vehicles_spawn()
331 {
332     dprint("Spawning vehicle: ", self.netname, "\n");
333
334     // De-own & reset
335     self.vehicle_hudmodel.viewmodelforclient = self;
336
337     self.owner              = world;
338     self.touch              = vehicles_touch;
339     self.event_damage       = vehicles_damage;
340     self.iscreature         = TRUE;
341     self.damagedbycontents      = TRUE;
342     self.movetype           = MOVETYPE_WALK;
343     self.solid              = SOLID_SLIDEBOX;
344     self.takedamage         = DAMAGE_AIM;
345         self.deadflag           = DEAD_NO;
346     self.bot_attack         = TRUE;
347     self.flags              = FL_NOTARGET;
348     self.avelocity          = '0 0 0';
349     self.velocity           = '0 0 0';
350
351     // Reset locking
352     self.lock_strength      = 0;
353     self.lock_target        = world;
354     self.misc_bulletcounter = 0;
355
356     // Return to spawn
357     self.angles             = self.pos2;
358     setorigin(self, self.pos1 + '0 0 0');
359     // Show it
360     pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
361
362     vehicles_reset_colors();
363     self.vehicle_spawn();
364 }
365
366 // Better way of determening whats crushable needed! (fl_crushable?)
367 float vehicles_crushable(entity e)
368 {
369     if(e.classname == "player")
370         return TRUE;
371
372     if(e.classname == "monster_zombie")
373         return TRUE;
374
375     return FALSE;
376 }
377
378 void vehilces_impact(float _minspeed, float _speedfac, float _maxpain)
379 {    
380     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
381         return;
382     
383     if(self.play_time < time)
384     {                    
385         float wc = vlen(self.velocity - self.oldvelocity);
386         //dprint("oldvel: ", vtos(self.oldvelocity), "\n");
387         //dprint("vel: ", vtos(self.velocity), "\n");
388         if(_minspeed < wc)
389         {
390             float take = take = min(_speedfac * wc, _maxpain);
391             Damage (self, world, world, take, DEATH_FALL, self.origin, '0 0 0');
392             self.play_time = time + 0.25;
393             
394             //dprint("wc: ", ftos(wc), "\n");
395             //dprint("take: ", ftos(take), "\n");
396         }
397     }
398 }
399
400 .void() vehicle_impact;
401 void vehicles_touch()
402 {
403     // Vehicle currently in use
404     if(self.owner)
405     {
406         if(other != world)
407         if(vehicles_crushable(other))
408         {
409             if(vlen(self.velocity) != 0)
410                 Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_VHCRUSH, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
411             
412             return; // Dont do selfdamage when hitting "soft targets".
413         }
414         
415         if(self.play_time < time)
416         if(self.vehicle_impact)
417             self.vehicle_impact();
418         
419         return;
420     }
421
422     if(other.classname != "player")
423         return;
424
425     if(other.deadflag != DEAD_NO)
426         return;
427
428     if(other.vehicle != world)
429         return;
430
431     // Remove this when bots know how to use vehicles.
432     if (clienttype(other) != CLIENTTYPE_REAL)
433         return;
434
435     vehicles_enter();
436 }
437
438 void vehicles_enter()
439 {
440    // Remove this when bots know how to use vehicles
441     if (clienttype(other) != CLIENTTYPE_REAL)
442         return;
443
444     if(self.phase > time)
445         return;
446
447     if(teamplay)
448     if(self.team)
449     if(self.team != other.team)
450         return;
451         
452     RemoveGrapplingHook(other);
453
454     self.vehicle_ammo1   = 0;
455     self.vehicle_ammo2   = 0;
456     self.vehicle_reload1 = 0;
457     self.vehicle_reload2 = 0;
458     self.vehicle_energy  = 0;
459
460     self.owner          = other;
461     self.switchweapon   = other.switchweapon;
462
463     // .viewmodelforclient works better.
464     //self.vehicle_hudmodel.drawonlytoclient = self.owner;
465
466     self.vehicle_hudmodel.viewmodelforclient = self.owner;
467
468     self.event_damage         = vehicles_damage;
469     self.nextthink            = 0;
470     self.owner.angles         = self.angles;
471     self.owner.takedamage     = DAMAGE_NO;
472     self.owner.solid          = SOLID_NOT;
473     self.owner.movetype       = MOVETYPE_NOCLIP;
474     self.owner.alpha          = -1;
475     self.owner.vehicle        = self;
476     self.owner.event_damage   = SUB_Null;
477     self.owner.view_ofs       = '0 0 0';
478     self.colormap             = self.owner.colormap;
479     if(self.tur_head)
480         self.tur_head.colormap    = self.owner.colormap;
481
482     self.owner.hud            = self.hud;
483     self.owner.PlayerPhysplug = self.PlayerPhysplug;
484
485     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
486     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
487     self.owner.vehicle_reload1  = self.vehicle_reload1;
488     self.owner.vehicle_reload2  = self.vehicle_reload2;
489
490     // Cant do this, hides attached objects too.
491     //self.exteriormodeltoclient = self.owner;
492     //self.tur_head.exteriormodeltoclient = self.owner;
493
494     other.flags &~= FL_ONGROUND;
495     self.flags  &~= FL_ONGROUND;
496
497     self.team                 = self.owner.team;
498     self.flags               -= FL_NOTARGET;
499
500     msg_entity = other;
501     WriteByte (MSG_ONE, SVC_SETVIEWPORT);
502     WriteEntity(MSG_ONE, self.vehicle_viewport);
503
504     WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
505     if(self.tur_head)
506     {
507         WriteAngle(MSG_ONE, self.tur_head.angles_x + self.angles_x); // tilt
508         WriteAngle(MSG_ONE, self.tur_head.angles_y + self.angles_y); // yaw
509         WriteAngle(MSG_ONE, 0);                                      // roll
510     }
511     else
512     {
513         WriteAngle(MSG_ONE,  self.angles_x * -1); // tilt
514         WriteAngle(MSG_ONE,  self.angles_y);      // yaw
515         WriteAngle(MSG_ONE,  0);                  // roll
516     }
517
518     vehicles_clearrturn();
519
520     CSQCVehicleSetup(self.owner, self.hud);
521     
522     MUTATOR_CALLHOOK(VehicleEnter);
523     
524     self.vehicle_enter();
525     antilag_clear(other);
526 }
527
528 /** vehicles_findgoodexit
529     Locates a valid location for the player to exit the vehicle.
530     Will first try prefer_spot, then up 100 random spots arround the vehicle
531     wich are in direct line of sight and empty enougth to hold a players bbox
532 **/
533 vector vehicles_findgoodexit(vector prefer_spot)
534 {
535     //vector exitspot;
536     float mysize;
537     
538     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
539     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
540         return prefer_spot;
541     
542     mysize = vlen(self.maxs - self.mins);
543     float i;
544     vector v, v2;
545     v2 = 0.5 * (self.absmin + self.absmax);
546     for(i = 0; i < 100; ++i)
547     {        
548         v = randomvec();
549         v_z = 0;
550         v = v2 + normalize(v) * mysize;
551         tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
552         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
553             return v;
554     }
555     
556     /*
557     exitspot = (self.origin + '0 0 48') + v_forward * mysize;
558     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
559     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
560         return exitspot;
561     
562     exitspot = (self.origin + '0 0 48') - v_forward * mysize;
563     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
564     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
565         return exitspot;
566
567     exitspot = (self.origin + '0 0 48') + v_right * mysize;
568     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
569     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
570         return exitspot;
571     
572     exitspot = (self.origin + '0 0 48') - v_right * mysize;
573     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
574     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
575         return exitspot;
576     */
577     
578     return self.origin;
579 }
580
581 /** vehicles_exit
582     Standarrd vehicle release fucntion.
583     custom code goes in self.vehicle_exit
584 **/
585 void vehicles_exit(float eject)
586 {       
587     entity oldself;
588     if(self.flags & FL_CLIENT)
589     {
590         oldself = self;
591         self = self.vehicle;
592     }
593     
594         self.flags |= FL_NOTARGET;
595
596     if (self.owner)
597     {
598         msg_entity = self.owner;
599         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
600         WriteEntity( MSG_ONE, self.owner);
601
602         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
603         WriteAngle(MSG_ONE, 0);                 // pich
604         WriteAngle(MSG_ONE, self.angles_y);     // yaw
605         WriteAngle(MSG_ONE, 0);                 // roll
606
607         setsize(self.owner, PL_MIN,PL_MAX);
608
609         self.owner.takedamage     = DAMAGE_AIM;
610         self.owner.solid          = SOLID_SLIDEBOX;
611         self.owner.movetype       = MOVETYPE_WALK;
612         self.owner.effects        &~= EF_NODRAW;
613         self.owner.alpha          = 1;
614         self.owner.PlayerPhysplug = SUB_Null;
615         self.owner.vehicle        = world;
616         self.owner.view_ofs       = PL_VIEW_OFS;
617         self.owner.event_damage   = PlayerDamage;
618         self.owner.hud            = HUD_NORMAL;
619         self.owner.switchweapon   = self.switchweapon;
620         //self.owner.BUTTON_USE     = 0;
621         
622         CSQCVehicleSetup(self.owner, HUD_NORMAL);
623     }
624
625     if(self.deadflag == DEAD_NO)
626         self.avelocity          = '0 0 0';
627
628     self.vehicle_hudmodel.viewmodelforclient = self;
629         self.tur_head.nodrawtoclient             = world;
630     vehicles_setreturn();
631
632     self.phase = time + 1;
633
634     if(!teamplay)
635         self.team = 0;
636     else
637         self.team = self.tur_head.team;
638    
639     MUTATOR_CALLHOOK(VehicleExit);
640     
641     sound (self, CH_TRIGGER_SINGLE, "misc/null.wav", 1, ATTN_NORM);
642     self.vehicle_exit(eject);
643     self.owner = world;
644     vehicles_reset_colors();
645     
646     if(oldself)
647         self = oldself;
648 }
649
650
651 void vehicles_regen(.float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time)
652 {
653     if(self.regen_field < field_max)
654     if(self.timer + rpause < time)
655     {
656         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
657
658         if(self.owner)
659             self.owner.regen_field = (self.regen_field / field_max) * 100;
660     }
661 }
662
663 void shieldhit_think()
664 {
665     self.alpha -= 0.1;
666     if (self.alpha <= 0)
667     {
668         //setmodel(self, "");
669         self.alpha = -1;
670     }
671     else
672     {
673         self.nextthink = time + 0.1;
674     }
675 }
676
677 void vehicles_painframe()
678 {    
679     if(self.owner.vehicle_health <= 50)
680     if(self.pain_frame < time)
681     {  
682         float _ftmp;  
683         _ftmp = self.owner.vehicle_health / 50;
684         self.pain_frame = time + 0.1 + (random() * 0.5 * _ftmp);
685         pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
686         
687         if(self.vehicle_flags & VHF_DMGSHAKE)
688             self.velocity += randomvec() * 30;
689         
690         if(self.vehicle_flags & VHF_DMGROLL)
691             if(self.vehicle_flags & VHF_DMGHEADROLL)
692                 self.tur_head.angles += randomvec();
693             else
694                 self.angles += randomvec();
695         
696     }    
697 }
698
699 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
700 {
701     self.dmg_time = time;
702
703     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
704     {
705         if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
706         {
707             self.vehicle_shieldent = spawn();
708             self.vehicle_shieldent.effects = EF_LOWPRECISION;
709
710             setmodel(self.vehicle_shieldent, "models/vhshield.md3");
711             setattachment(self.vehicle_shieldent, self, "");
712             setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
713             self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
714             self.vehicle_shieldent.think       = shieldhit_think;
715         }
716
717         self.vehicle_shieldent.colormod    = '1 1 1';
718         self.vehicle_shieldent.alpha       = 0.45;
719         self.vehicle_shieldent.angles      = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
720         self.vehicle_shieldent.nextthink   = time;
721
722         self.vehicle_shield -= damage;
723
724         if(self.vehicle_shield < 0)
725         {
726             self.vehicle_health            -= fabs(self.vehicle_shield);
727             self.vehicle_shieldent.colormod = '2 0 0';
728             self.vehicle_shield             = 0;
729             self.vehicle_shieldent.alpha    = 0.75;
730             
731                 if(sound_allowed(MSG_BROADCAST, attacker))
732                 spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);   // FIXME: PLACEHOLDER
733         }
734         else
735                 if(sound_allowed(MSG_BROADCAST, attacker))
736                 spamsound (self, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
737
738     }
739     else
740     {
741         self.vehicle_health -= damage;
742
743         if(sound_allowed(MSG_BROADCAST, attacker))
744             spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
745     }
746
747     self.velocity += force; // * (vlen(force) / self.mass);
748
749     if(self.vehicle_health <= 0)
750     {
751         if(self.owner)
752             if(self.vehicle_flags & VHF_DEATHEJECT)
753                 vehicles_exit(VHEF_EJECT);
754             else
755                 vehicles_exit(VHEF_RELESE);
756
757         self.vehicle_die();
758         vehicles_setreturn();
759     }
760 }
761
762 void vehicles_clearrturn()
763 {
764     entity ret;
765     // Remove "return helper", if any.
766     ret = findchain(classname, "vehicle_return");
767     while(ret)
768     {
769         if(ret.enemy == self)
770         {
771             ret.classname   = "";
772             ret.think       = SUB_Remove;
773             ret.nextthink   = time + 0.1;            
774             
775             if(ret.waypointsprite_attached)
776                 WaypointSprite_Kill(ret.waypointsprite_attached);
777             
778             return;
779         }
780         ret = ret.chain;
781     }
782 }
783
784 void vehicles_return()
785 {
786     pointparticles(particleeffectnum("teleport"), self.enemy.origin + '0 0 64', '0 0 0', 1);
787
788     self.enemy.think     = vehicles_spawn;
789     self.enemy.nextthink = time;
790
791     if(self.waypointsprite_attached)
792         WaypointSprite_Kill(self.waypointsprite_attached);
793             
794     remove(self);
795 }
796
797 void vehicles_showwp_goaway()
798 {
799     if(self.waypointsprite_attached)
800         WaypointSprite_Kill(self.waypointsprite_attached);
801             
802     remove(self);
803     
804 }
805
806 void vehicles_showwp()
807 {
808     entity oldself;
809     vector rgb;
810     
811     if(self.cnt)
812     {        
813         self.think      = vehicles_return;
814         self.nextthink  = self.cnt;
815     }    
816     else
817     {
818         self.think      = vehicles_return;
819         self.nextthink  = time +1;
820         
821         oldself = self;
822         self = spawn();
823         setmodel(self, "null");
824         self.team = oldself.enemy.team;
825         self.enemy = oldself.enemy;
826         setorigin(self, oldself.enemy.pos1);
827         
828         self.nextthink = time + 5;
829         self.think = vehicles_showwp_goaway;
830     }
831     
832     if(teamplay && self.team)
833             rgb = TeamColor(self.team);
834     else
835             rgb = '1 1 1';
836     WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE, RADARICON_POWERUP, rgb);
837     if(self.waypointsprite_attached)
838     {        
839         WaypointSprite_UpdateRule(self.waypointsprite_attached, self.enemy.team, SPRITERULE_DEFAULT);        
840         if(oldself == world)
841             WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);        
842         WaypointSprite_Ping(self.waypointsprite_attached);
843     }    
844     
845     if(oldself != world)
846         self = oldself;
847 }
848
849 void vehicles_setreturn()
850 {
851     entity ret;
852     
853     vehicles_clearrturn();
854
855     ret = spawn();
856     ret.classname   = "vehicle_return";
857     ret.enemy       = self;    
858     ret.team        = self.team;
859     ret.think       = vehicles_showwp;
860     
861     if(self.deadflag != DEAD_NO)
862     {
863         ret.cnt         = time + self.vehicle_respawntime;
864         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 5);        
865     }        
866     else
867     {
868         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 1);        
869     }
870     
871     setmodel(ret, "null");
872     setorigin(ret, self.pos1 + '0 0 96');
873         
874 }
875
876 void vehicles_configcheck(string  configname, float check_cvar)
877 {
878     if(check_cvar == 0)
879         localcmd(strcat("exec ", configname, "\n"));
880 }
881
882 void vehicles_reset_colors()
883 {
884     entity e;
885     float _effects, _colormap;
886     vector _glowmod, _colormod;
887
888     if(autocvar_g_nodepthtestplayers)
889         _effects = EF_NODEPTHTEST;
890
891     if(autocvar_g_fullbrightplayers)
892         _effects |= EF_FULLBRIGHT;
893
894     if(self.team)
895         _colormap = 1024 + (self.team - 1) * 17;
896     else
897         _colormap = 1024;
898
899     _glowmod  = '0 0 0';
900     _colormod = '0 0 0';
901
902     // Find all ents attacked to main model and setup effects, colormod etc.
903     e = findchainentity(tag_entity, self);
904     while(e)
905     {
906         if(e != self.vehicle_shieldent)
907         {
908             e.effects   = _effects; //  | EF_LOWPRECISION;
909             e.colormod  = _colormod;
910             e.colormap  = _colormap;
911             e.alpha     = 1;
912         }
913         e = e.chain;
914     }
915
916     self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
917     self.vehicle_hudmodel.colormod = self.colormod = _colormod;
918     self.vehicle_hudmodel.colormap = self.colormap = _colormap;
919     self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
920
921     self.alpha     = 1;
922     self.avelocity = '0 0 0';
923     self.velocity  = '0 0 0';
924     self.effects   = _effects;
925 }
926
927 float vehicle_initialize(string  net_name,
928                          string  bodymodel,
929                          string  topmodel,
930                          string  hudmodel,
931                          string  toptag,
932                          string  hudtag,
933                          string  viewtag,
934                          float   vhud,
935                          vector  min_s,
936                          vector  max_s,
937                          float   nodrop,
938                          void()  spawnproc,
939                          float   _respawntime,
940                          float() physproc,
941                          void()  enterproc,
942                          void(float extflag) exitfunc,
943                          void() dieproc,
944                          void() thinkproc,
945                          float  use_csqc)
946 {
947     addstat(STAT_HUD, AS_INT,  hud);
948         addstat(STAT_VEHICLESTAT_HEALTH,  AS_INT, vehicle_health);
949         addstat(STAT_VEHICLESTAT_SHIELD,  AS_INT, vehicle_shield);
950         addstat(STAT_VEHICLESTAT_ENERGY,  AS_INT, vehicle_energy);
951
952         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT,   vehicle_ammo1);
953         addstat(STAT_VEHICLESTAT_RELOAD1, AS_INT, vehicle_reload1);
954
955         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT,   vehicle_ammo2);
956         addstat(STAT_VEHICLESTAT_RELOAD2, AS_INT, vehicle_reload2);
957
958     if(bodymodel == "")
959         error("vehicles: missing bodymodel!");
960
961     if(hudmodel == "")
962         error("vehicles: missing hudmodel!");
963
964     if(net_name == "")
965         self.netname = self.classname;
966     else
967         self.netname = net_name;
968
969     if(self.team && !teamplay)
970         self.team = 0;
971         
972     self.vehicle_flags |= VHF_ISVEHICLE;
973     
974     setmodel(self, bodymodel);
975
976     self.vehicle_viewport   = spawn();
977     self.vehicle_hudmodel   = spawn();
978     self.tur_head           = spawn();
979     self.tur_head.owner     = self;
980     self.takedamage         = DAMAGE_AIM;
981     self.bot_attack         = TRUE;
982     self.iscreature         = TRUE;
983     self.damagedbycontents      = TRUE;
984     self.hud                = vhud;
985
986     self.vehicle_die         = dieproc;
987     self.vehicle_exit        = exitfunc;
988     self.vehicle_enter       = enterproc;
989     self.PlayerPhysplug      = physproc;
990     self.event_damage        = vehicles_damage;
991     self.touch               = vehicles_touch;
992     self.think               = vehicles_spawn;    
993     self.nextthink           = time;        
994     self.vehicle_respawntime = _respawntime;
995     self.vehicle_spawn       = spawnproc;
996
997     if(autocvar_g_nodepthtestplayers)
998         self.effects = self.effects | EF_NODEPTHTEST;
999
1000     if(autocvar_g_fullbrightplayers)
1001         self.effects = self.effects | EF_FULLBRIGHT;
1002
1003     setmodel(self.vehicle_hudmodel, hudmodel);
1004     setmodel(self.vehicle_viewport, "null");
1005
1006
1007     if(topmodel != "")
1008     {
1009         setmodel(self.tur_head, topmodel);
1010         setattachment(self.tur_head, self, toptag);
1011         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
1012         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1013     }
1014     else
1015     {
1016         setattachment(self.tur_head, self, "");
1017         setattachment(self.vehicle_hudmodel, self, hudtag);
1018         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1019     }
1020
1021     setsize(self, min_s, max_s);
1022     if not (nodrop)
1023     {
1024         setorigin(self, self.origin);
1025         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1026         setorigin(self, trace_endpos);
1027     }
1028
1029     self.pos1 = self.origin;
1030     self.pos2 = self.angles;
1031     self.tur_head.team = self.team;
1032     
1033     return TRUE;
1034 }
1035
1036 void bugmenot()
1037 {
1038     self.vehicle_exit       = self.vehicle_exit;
1039     self.vehicle_enter      = self.vehicle_exit;
1040     self.vehicle_die        = self.vehicle_exit;
1041     self.vehicle_spawn      = self.vehicle_exit;
1042     self.AuxiliaryXhair     = self.AuxiliaryXhair;
1043 }