]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/vehicles/vehicles.qc
Merge remote-tracking 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
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.teleportable       = FALSE; // no teleporting for vehicles, too buggy
342     self.damagedbycontents      = TRUE;
343     self.movetype           = MOVETYPE_WALK;
344     self.solid              = SOLID_SLIDEBOX;
345     self.takedamage         = DAMAGE_AIM;
346         self.deadflag           = DEAD_NO;
347     self.bot_attack         = TRUE;
348     self.flags              = FL_NOTARGET;
349     self.avelocity          = '0 0 0';
350     self.velocity           = '0 0 0';
351
352     // Reset locking
353     self.lock_strength      = 0;
354     self.lock_target        = world;
355     self.misc_bulletcounter = 0;
356
357     // Return to spawn
358     self.angles             = self.pos2;
359     setorigin(self, self.pos1 + '0 0 0');
360     // Show it
361     pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
362
363     vehicles_reset_colors();
364     self.vehicle_spawn();
365 }
366
367 // Better way of determening whats crushable needed! (fl_crushable?)
368 float vehicles_crushable(entity e)
369 {
370     if(e.classname == "player")
371         return TRUE;
372
373     if(e.classname == "monster_zombie")
374         return TRUE;
375
376     return FALSE;
377 }
378
379 void vehilces_impact(float _minspeed, float _speedfac, float _maxpain)
380 {    
381     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
382         return;
383     
384     if(self.play_time < time)
385     {                    
386         float wc = vlen(self.velocity - self.oldvelocity);
387         //dprint("oldvel: ", vtos(self.oldvelocity), "\n");
388         //dprint("vel: ", vtos(self.velocity), "\n");
389         if(_minspeed < wc)
390         {
391             float take = take = min(_speedfac * wc, _maxpain);
392             Damage (self, world, world, take, DEATH_FALL, self.origin, '0 0 0');
393             self.play_time = time + 0.25;
394             
395             //dprint("wc: ", ftos(wc), "\n");
396             //dprint("take: ", ftos(take), "\n");
397         }
398     }
399 }
400
401 .void() vehicle_impact;
402 void vehicles_touch()
403 {
404     // Vehicle currently in use
405     if(self.owner)
406     {
407         if(other != world)
408         if(vehicles_crushable(other))
409         {
410             if(vlen(self.velocity) != 0)
411                 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);
412             
413             return; // Dont do selfdamage when hitting "soft targets".
414         }
415         
416         if(self.play_time < time)
417         if(self.vehicle_impact)
418             self.vehicle_impact();
419         
420         return;
421     }
422
423     if(other.classname != "player")
424         return;
425
426     if(other.deadflag != DEAD_NO)
427         return;
428
429     if(other.vehicle != world)
430         return;
431
432     // Remove this when bots know how to use vehicles.
433     if (clienttype(other) != CLIENTTYPE_REAL)
434         return;
435
436     vehicles_enter();
437 }
438
439 void vehicles_enter()
440 {
441    // Remove this when bots know how to use vehicles
442     if (clienttype(other) != CLIENTTYPE_REAL)
443         return;
444
445     if(self.phase > time)
446         return;
447
448     if(teamplay)
449     if(self.team)
450     if(self.team != other.team)
451         return;
452         
453     RemoveGrapplingHook(other);
454
455     self.vehicle_ammo1   = 0;
456     self.vehicle_ammo2   = 0;
457     self.vehicle_reload1 = 0;
458     self.vehicle_reload2 = 0;
459     self.vehicle_energy  = 0;
460
461     self.owner          = other;
462     self.switchweapon   = other.switchweapon;
463
464     // .viewmodelforclient works better.
465     //self.vehicle_hudmodel.drawonlytoclient = self.owner;
466
467     self.vehicle_hudmodel.viewmodelforclient = self.owner;
468
469     self.event_damage         = vehicles_damage;
470     self.nextthink            = 0;
471     self.owner.angles         = self.angles;
472     self.owner.takedamage     = DAMAGE_NO;
473     self.owner.solid          = SOLID_NOT;
474     self.owner.movetype       = MOVETYPE_NOCLIP;
475     self.owner.alpha          = -1;
476     self.owner.vehicle        = self;
477     self.owner.event_damage   = SUB_Null;
478     self.owner.view_ofs       = '0 0 0';
479     self.colormap             = self.owner.colormap;
480     if(self.tur_head)
481         self.tur_head.colormap    = self.owner.colormap;
482
483     self.owner.hud            = self.hud;
484     self.owner.PlayerPhysplug = self.PlayerPhysplug;
485
486     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
487     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
488     self.owner.vehicle_reload1  = self.vehicle_reload1;
489     self.owner.vehicle_reload2  = self.vehicle_reload2;
490
491     // Cant do this, hides attached objects too.
492     //self.exteriormodeltoclient = self.owner;
493     //self.tur_head.exteriormodeltoclient = self.owner;
494
495     other.flags &~= FL_ONGROUND;
496     self.flags  &~= FL_ONGROUND;
497
498     self.team                 = self.owner.team;
499     self.flags               -= FL_NOTARGET;
500
501     msg_entity = other;
502     WriteByte (MSG_ONE, SVC_SETVIEWPORT);
503     WriteEntity(MSG_ONE, self.vehicle_viewport);
504
505     WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
506     if(self.tur_head)
507     {
508         WriteAngle(MSG_ONE, self.tur_head.angles_x + self.angles_x); // tilt
509         WriteAngle(MSG_ONE, self.tur_head.angles_y + self.angles_y); // yaw
510         WriteAngle(MSG_ONE, 0);                                      // roll
511     }
512     else
513     {
514         WriteAngle(MSG_ONE,  self.angles_x * -1); // tilt
515         WriteAngle(MSG_ONE,  self.angles_y);      // yaw
516         WriteAngle(MSG_ONE,  0);                  // roll
517     }
518
519     vehicles_clearrturn();
520
521     CSQCVehicleSetup(self.owner, self.hud);
522     
523     MUTATOR_CALLHOOK(VehicleEnter);
524     
525     self.vehicle_enter();
526     antilag_clear(other);
527 }
528
529 /** vehicles_findgoodexit
530     Locates a valid location for the player to exit the vehicle.
531     Will first try prefer_spot, then up 100 random spots arround the vehicle
532     wich are in direct line of sight and empty enougth to hold a players bbox
533 **/
534 vector vehicles_findgoodexit(vector prefer_spot)
535 {
536     //vector exitspot;
537     float mysize;
538     
539     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
540     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
541         return prefer_spot;
542     
543     mysize = vlen(self.maxs - self.mins);
544     float i;
545     vector v, v2;
546     v2 = 0.5 * (self.absmin + self.absmax);
547     for(i = 0; i < 100; ++i)
548     {        
549         v = randomvec();
550         v_z = 0;
551         v = v2 + normalize(v) * mysize;
552         tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
553         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
554             return v;
555     }
556     
557     /*
558     exitspot = (self.origin + '0 0 48') + v_forward * mysize;
559     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
560     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
561         return exitspot;
562     
563     exitspot = (self.origin + '0 0 48') - v_forward * mysize;
564     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
565     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
566         return exitspot;
567
568     exitspot = (self.origin + '0 0 48') + v_right * mysize;
569     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
570     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
571         return exitspot;
572     
573     exitspot = (self.origin + '0 0 48') - v_right * mysize;
574     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
575     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
576         return exitspot;
577     */
578     
579     return self.origin;
580 }
581
582 /** vehicles_exit
583     Standarrd vehicle release fucntion.
584     custom code goes in self.vehicle_exit
585 **/
586 void vehicles_exit(float eject)
587 {       
588     entity oldself;
589     if(self.flags & FL_CLIENT)
590     {
591         oldself = self;
592         self = self.vehicle;
593     }
594     
595         self.flags |= FL_NOTARGET;
596
597     if (self.owner)
598     {
599         msg_entity = self.owner;
600         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
601         WriteEntity( MSG_ONE, self.owner);
602
603         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
604         WriteAngle(MSG_ONE, 0);                 // pich
605         WriteAngle(MSG_ONE, self.angles_y);     // yaw
606         WriteAngle(MSG_ONE, 0);                 // roll
607
608         setsize(self.owner, PL_MIN,PL_MAX);
609
610         self.owner.takedamage     = DAMAGE_AIM;
611         self.owner.solid          = SOLID_SLIDEBOX;
612         self.owner.movetype       = MOVETYPE_WALK;
613         self.owner.effects        &~= EF_NODRAW;
614         self.owner.alpha          = 1;
615         self.owner.PlayerPhysplug = SUB_Null;
616         self.owner.vehicle        = world;
617         self.owner.view_ofs       = PL_VIEW_OFS;
618         self.owner.event_damage   = PlayerDamage;
619         self.owner.hud            = HUD_NORMAL;
620         self.owner.switchweapon   = self.switchweapon;
621         //self.owner.BUTTON_USE     = 0;
622         
623         CSQCVehicleSetup(self.owner, HUD_NORMAL);
624     }
625
626     if(self.deadflag == DEAD_NO)
627         self.avelocity          = '0 0 0';
628
629     self.vehicle_hudmodel.viewmodelforclient = self;
630         self.tur_head.nodrawtoclient             = world;
631     vehicles_setreturn();
632
633     self.phase = time + 1;
634
635     if(!teamplay)
636         self.team = 0;
637     else
638         self.team = self.tur_head.team;
639    
640     MUTATOR_CALLHOOK(VehicleExit);
641     
642     sound (self, CH_TRIGGER_SINGLE, "misc/null.wav", 1, ATTN_NORM);
643     self.vehicle_exit(eject);
644     self.owner = world;
645     vehicles_reset_colors();
646     
647     if(oldself)
648         self = oldself;
649 }
650
651
652 void vehicles_regen(.float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time)
653 {
654     if(self.regen_field < field_max)
655     if(self.timer + rpause < time)
656     {
657         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
658
659         if(self.owner)
660             self.owner.regen_field = (self.regen_field / field_max) * 100;
661     }
662 }
663
664 void shieldhit_think()
665 {
666     self.alpha -= 0.1;
667     if (self.alpha <= 0)
668     {
669         //setmodel(self, "");
670         self.alpha = -1;
671     }
672     else
673     {
674         self.nextthink = time + 0.1;
675     }
676 }
677
678 void vehicles_painframe()
679 {    
680     if(self.owner.vehicle_health <= 50)
681     if(self.pain_frame < time)
682     {  
683         float _ftmp;  
684         _ftmp = self.owner.vehicle_health / 50;
685         self.pain_frame = time + 0.1 + (random() * 0.5 * _ftmp);
686         pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
687         
688         if(self.vehicle_flags & VHF_DMGSHAKE)
689             self.velocity += randomvec() * 30;
690         
691         if(self.vehicle_flags & VHF_DMGROLL)
692             if(self.vehicle_flags & VHF_DMGHEADROLL)
693                 self.tur_head.angles += randomvec();
694             else
695                 self.angles += randomvec();
696         
697     }    
698 }
699
700 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
701 {
702     self.dmg_time = time;
703
704     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
705     {
706         if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
707         {
708             self.vehicle_shieldent = spawn();
709             self.vehicle_shieldent.effects = EF_LOWPRECISION;
710
711             setmodel(self.vehicle_shieldent, "models/vhshield.md3");
712             setattachment(self.vehicle_shieldent, self, "");
713             setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
714             self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
715             self.vehicle_shieldent.think       = shieldhit_think;
716         }
717
718         self.vehicle_shieldent.colormod    = '1 1 1';
719         self.vehicle_shieldent.alpha       = 0.45;
720         self.vehicle_shieldent.angles      = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
721         self.vehicle_shieldent.nextthink   = time;
722
723         self.vehicle_shield -= damage;
724
725         if(self.vehicle_shield < 0)
726         {
727             self.vehicle_health            -= fabs(self.vehicle_shield);
728             self.vehicle_shieldent.colormod = '2 0 0';
729             self.vehicle_shield             = 0;
730             self.vehicle_shieldent.alpha    = 0.75;
731             
732                 if(sound_allowed(MSG_BROADCAST, attacker))
733                 spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);   // FIXME: PLACEHOLDER
734         }
735         else
736                 if(sound_allowed(MSG_BROADCAST, attacker))
737                 spamsound (self, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
738
739     }
740     else
741     {
742         self.vehicle_health -= damage;
743
744         if(sound_allowed(MSG_BROADCAST, attacker))
745             spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
746     }
747
748     self.velocity += force; // * (vlen(force) / self.mass);
749
750     if(self.vehicle_health <= 0)
751     {
752         if(self.owner)
753             if(self.vehicle_flags & VHF_DEATHEJECT)
754                 vehicles_exit(VHEF_EJECT);
755             else
756                 vehicles_exit(VHEF_RELESE);
757
758         self.vehicle_die();
759         vehicles_setreturn();
760     }
761 }
762
763 void vehicles_clearrturn()
764 {
765     entity ret;
766     // Remove "return helper", if any.
767     ret = findchain(classname, "vehicle_return");
768     while(ret)
769     {
770         if(ret.enemy == self)
771         {
772             ret.classname   = "";
773             ret.think       = SUB_Remove;
774             ret.nextthink   = time + 0.1;            
775             
776             if(ret.waypointsprite_attached)
777                 WaypointSprite_Kill(ret.waypointsprite_attached);
778             
779             return;
780         }
781         ret = ret.chain;
782     }
783 }
784
785 void vehicles_return()
786 {
787     pointparticles(particleeffectnum("teleport"), self.enemy.origin + '0 0 64', '0 0 0', 1);
788
789     self.enemy.think     = vehicles_spawn;
790     self.enemy.nextthink = time;
791
792     if(self.waypointsprite_attached)
793         WaypointSprite_Kill(self.waypointsprite_attached);
794             
795     remove(self);
796 }
797
798 void vehicles_showwp_goaway()
799 {
800     if(self.waypointsprite_attached)
801         WaypointSprite_Kill(self.waypointsprite_attached);
802             
803     remove(self);
804     
805 }
806
807 void vehicles_showwp()
808 {
809     entity oldself;
810     vector rgb;
811     
812     if(self.cnt)
813     {        
814         self.think      = vehicles_return;
815         self.nextthink  = self.cnt;
816     }    
817     else
818     {
819         self.think      = vehicles_return;
820         self.nextthink  = time +1;
821         
822         oldself = self;
823         self = spawn();
824         setmodel(self, "null");
825         self.team = oldself.enemy.team;
826         self.enemy = oldself.enemy;
827         setorigin(self, oldself.enemy.pos1);
828         
829         self.nextthink = time + 5;
830         self.think = vehicles_showwp_goaway;
831     }
832     
833     if(teamplay && self.team)
834             rgb = TeamColor(self.team);
835     else
836             rgb = '1 1 1';
837     WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE, RADARICON_POWERUP, rgb);
838     if(self.waypointsprite_attached)
839     {        
840         WaypointSprite_UpdateRule(self.waypointsprite_attached, self.enemy.team, SPRITERULE_DEFAULT);        
841         if(oldself == world)
842             WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);        
843         WaypointSprite_Ping(self.waypointsprite_attached);
844     }    
845     
846     if(oldself != world)
847         self = oldself;
848 }
849
850 void vehicles_setreturn()
851 {
852     entity ret;
853     
854     vehicles_clearrturn();
855
856     ret = spawn();
857     ret.classname   = "vehicle_return";
858     ret.enemy       = self;    
859     ret.team        = self.team;
860     ret.think       = vehicles_showwp;
861     
862     if(self.deadflag != DEAD_NO)
863     {
864         ret.cnt         = time + self.vehicle_respawntime;
865         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 5);        
866     }        
867     else
868     {
869         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 1);        
870     }
871     
872     setmodel(ret, "null");
873     setorigin(ret, self.pos1 + '0 0 96');
874         
875 }
876
877 void vehicles_configcheck(string  configname, float check_cvar)
878 {
879     if(check_cvar == 0)
880         localcmd(strcat("exec ", configname, "\n"));
881 }
882
883 void vehicles_reset_colors()
884 {
885     entity e;
886     float _effects, _colormap;
887     vector _glowmod, _colormod;
888
889     if(autocvar_g_nodepthtestplayers)
890         _effects = EF_NODEPTHTEST;
891
892     if(autocvar_g_fullbrightplayers)
893         _effects |= EF_FULLBRIGHT;
894
895     if(self.team)
896         _colormap = 1024 + (self.team - 1) * 17;
897     else
898         _colormap = 1024;
899
900     _glowmod  = '0 0 0';
901     _colormod = '0 0 0';
902
903     // Find all ents attacked to main model and setup effects, colormod etc.
904     e = findchainentity(tag_entity, self);
905     while(e)
906     {
907         if(e != self.vehicle_shieldent)
908         {
909             e.effects   = _effects; //  | EF_LOWPRECISION;
910             e.colormod  = _colormod;
911             e.colormap  = _colormap;
912             e.alpha     = 1;
913         }
914         e = e.chain;
915     }
916
917     self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
918     self.vehicle_hudmodel.colormod = self.colormod = _colormod;
919     self.vehicle_hudmodel.colormap = self.colormap = _colormap;
920     self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
921
922     self.alpha     = 1;
923     self.avelocity = '0 0 0';
924     self.velocity  = '0 0 0';
925     self.effects   = _effects;
926 }
927
928 float vehicle_initialize(string  net_name,
929                          string  bodymodel,
930                          string  topmodel,
931                          string  hudmodel,
932                          string  toptag,
933                          string  hudtag,
934                          string  viewtag,
935                          float   vhud,
936                          vector  min_s,
937                          vector  max_s,
938                          float   nodrop,
939                          void()  spawnproc,
940                          float   _respawntime,
941                          float() physproc,
942                          void()  enterproc,
943                          void(float extflag) exitfunc,
944                          void() dieproc,
945                          void() thinkproc,
946                          float  use_csqc)
947 {
948     addstat(STAT_HUD, AS_INT,  hud);
949         addstat(STAT_VEHICLESTAT_HEALTH,  AS_INT, vehicle_health);
950         addstat(STAT_VEHICLESTAT_SHIELD,  AS_INT, vehicle_shield);
951         addstat(STAT_VEHICLESTAT_ENERGY,  AS_INT, vehicle_energy);
952
953         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT,   vehicle_ammo1);
954         addstat(STAT_VEHICLESTAT_RELOAD1, AS_INT, vehicle_reload1);
955
956         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT,   vehicle_ammo2);
957         addstat(STAT_VEHICLESTAT_RELOAD2, AS_INT, vehicle_reload2);
958
959     if(bodymodel == "")
960         error("vehicles: missing bodymodel!");
961
962     if(hudmodel == "")
963         error("vehicles: missing hudmodel!");
964
965     if(net_name == "")
966         self.netname = self.classname;
967     else
968         self.netname = net_name;
969
970     if(self.team && !teamplay)
971         self.team = 0;
972         
973     self.vehicle_flags |= VHF_ISVEHICLE;
974     
975     setmodel(self, bodymodel);
976
977     self.vehicle_viewport   = spawn();
978     self.vehicle_hudmodel   = spawn();
979     self.tur_head           = spawn();
980     self.tur_head.owner     = self;
981     self.takedamage         = DAMAGE_AIM;
982     self.bot_attack         = TRUE;
983     self.iscreature         = TRUE;
984     self.teleportable       = FALSE; // no teleporting for vehicles, too buggy
985     self.damagedbycontents      = TRUE;
986     self.hud                = vhud;
987
988     self.vehicle_die         = dieproc;
989     self.vehicle_exit        = exitfunc;
990     self.vehicle_enter       = enterproc;
991     self.PlayerPhysplug      = physproc;
992     self.event_damage        = vehicles_damage;
993     self.touch               = vehicles_touch;
994     self.think               = vehicles_spawn;    
995     self.nextthink           = time;        
996     self.vehicle_respawntime = _respawntime;
997     self.vehicle_spawn       = spawnproc;
998
999     if(autocvar_g_nodepthtestplayers)
1000         self.effects = self.effects | EF_NODEPTHTEST;
1001
1002     if(autocvar_g_fullbrightplayers)
1003         self.effects = self.effects | EF_FULLBRIGHT;
1004
1005     setmodel(self.vehicle_hudmodel, hudmodel);
1006     setmodel(self.vehicle_viewport, "null");
1007
1008
1009     if(topmodel != "")
1010     {
1011         setmodel(self.tur_head, topmodel);
1012         setattachment(self.tur_head, self, toptag);
1013         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
1014         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1015     }
1016     else
1017     {
1018         setattachment(self.tur_head, self, "");
1019         setattachment(self.vehicle_hudmodel, self, hudtag);
1020         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1021     }
1022
1023     setsize(self, min_s, max_s);
1024     if not (nodrop)
1025     {
1026         setorigin(self, self.origin);
1027         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1028         setorigin(self, trace_endpos);
1029     }
1030
1031     self.pos1 = self.origin;
1032     self.pos2 = self.angles;
1033     self.tur_head.team = self.team;
1034     
1035     return TRUE;
1036 }
1037
1038 void bugmenot()
1039 {
1040     self.vehicle_exit       = self.vehicle_exit;
1041     self.vehicle_enter      = self.vehicle_exit;
1042     self.vehicle_die        = self.vehicle_exit;
1043     self.vehicle_spawn      = self.vehicle_exit;
1044     self.AuxiliaryXhair     = self.AuxiliaryXhair;
1045 }