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