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