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