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