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