]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/vehicles/vehicles.qc
Support g_vehicles_delayspawn (makes vehciles spawn after their set respawntime at...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / vehicles / vehicles.qc
1 float autocvar_g_vehicles_crush_dmg;
2 float autocvar_g_vehicles_crush_force;
3 float autocvar_g_vehicles_delayspawn;
4 float autocvar_g_vehicles_delayspawn_jitter;
5
6 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
7 void vehicles_return();
8 void vehicles_enter();
9 void vehicles_touch();
10 void vehicles_reset_colors();
11 void vehicles_clearrturn();
12 void vehicles_setreturn();
13
14
15 /** AuxiliaryXhair*
16     Send addictional points of interest to be drawn, to vehicle owner
17 **/
18 float MAX_AXH = 4;
19 .entity AuxiliaryXhair[MAX_AXH];
20
21 float SendAuxiliaryXhair(entity to, float sf)
22 {
23
24         WriteByte(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
25
26         WriteByte(MSG_ENTITY, self.cnt);
27
28         WriteCoord(MSG_ENTITY, self.origin_x);
29         WriteCoord(MSG_ENTITY, self.origin_y);
30         WriteCoord(MSG_ENTITY, self.origin_z);
31
32     WriteByte(MSG_ENTITY, rint(self.colormod_x * 255));
33     WriteByte(MSG_ENTITY, rint(self.colormod_y * 255));
34     WriteByte(MSG_ENTITY, rint(self.colormod_z * 255));
35
36     return TRUE;
37 }
38
39 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
40 {
41     entity axh;
42
43     axh_id = bound(0, axh_id, MAX_AXH);
44     axh = own.AuxiliaryXhair[axh_id];
45
46     if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
47     {
48         axh                     = spawn();
49         axh.cnt                 = axh_id;
50         axh.drawonlytoclient    = own;
51         axh.owner               = own;
52         Net_LinkEntity(axh, FALSE, 0, SendAuxiliaryXhair);
53     }
54
55     setorigin(axh, loc);
56     axh.colormod            = clr;
57     axh.SendFlags           = 0x01;
58     own.AuxiliaryXhair[axh_id] = axh;
59 }
60
61 /*
62 // SVC_TEMPENTITY based, horrible with even 50 ping. hm.
63 // WriteByte(MSG_ONE, SVC_TEMPENTITY) uses reliable messagess, never use for thinsg that need continous updates.
64 void SendAuxiliaryXhair2(entity own, vector loc, vector clr, float axh_id)
65 {
66         msg_entity = own;
67
68         WriteByte(MSG_ONE, SVC_TEMPENTITY);
69         WriteByte(MSG_ONE, TE_CSQC_AUXILIARYXHAIR);
70
71         WriteByte(MSG_ONE, axh_id);
72
73         WriteCoord(MSG_ONE, loc_x);
74         WriteCoord(MSG_ONE, loc_y);
75         WriteCoord(MSG_ONE, loc_z);
76
77     WriteByte(MSG_ONE, rint(clr_x * 255));
78     WriteByte(MSG_ONE, rint(clr_y * 255));
79     WriteByte(MSG_ONE, rint(clr_z * 255));
80
81 }
82 */
83 // End AuxiliaryXhair
84
85 void CSQCVehicleSetup(entity own, float vehicle_id)
86 {
87         msg_entity = own;
88
89         WriteByte(MSG_ONE, SVC_TEMPENTITY);
90         WriteByte(MSG_ONE, TE_CSQC_VEHICLESETUP);
91         WriteByte(MSG_ONE, vehicle_id);
92 }
93
94 /** vehicles_locktarget
95
96     Generic target locking.
97
98     Figure out if what target is "locked" (if any), for missile tracking as such.
99
100     after calling, "if(self.lock_target != world && self.lock_strength == 1)" mean
101     you have a locked in target.
102
103     Exspects a crosshair_trace() or equivalent to be
104     dont before calling.
105
106 **/
107 .entity lock_target;
108 .float  lock_strength;
109 .float  lock_time;
110 .float  lock_soundtime;
111 void vehicles_locktarget(float incr, float decr, float _lock_time)
112 {
113     if(self.lock_target && self.lock_target.deadflag != DEAD_NO)
114     {
115         self.lock_target    = world;
116         self.lock_strength  = 0;
117         self.lock_time      = 0;
118     }
119
120     if(self.lock_time > time)
121     {
122         if(self.lock_target)
123         if(self.lock_soundtime < time)
124         {
125             self.lock_soundtime = time + 0.5;
126             play2(self.owner, "vehicles/locked.wav");
127         }
128         
129         return;
130     }
131         
132
133     if(trace_ent != world)
134     {
135         if(teams_matter && trace_ent.team == self.team)
136             trace_ent = world;
137
138         if(trace_ent.deadflag != DEAD_NO)
139             trace_ent = world;
140
141         if not (trace_ent.vehicle_flags & VHF_ISVEHICLE || trace_ent.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
142             trace_ent = world;
143     }
144
145     if(self.lock_target == world && trace_ent != world)
146         self.lock_target = trace_ent;
147     
148     if(self.lock_target && trace_ent == self.lock_target) 
149     {            
150         if(self.lock_strength != 1 && self.lock_strength + incr >= 1)
151         {
152             play2(self.owner, "vehicles/lock.wav");
153             self.lock_soundtime = time + 0.8;
154         }        
155         else if (self.lock_strength != 1 && self.lock_soundtime < time)
156         {            
157             play2(self.owner, "vehicles/locking.wav");
158             self.lock_soundtime = time + 0.3;
159         }
160         
161     }    
162         
163     // Have a locking target
164     // Trace hit current target
165     if(trace_ent == self.lock_target && trace_ent != world)
166     {
167         self.lock_strength = min(self.lock_strength + incr, 1);
168         if(self.lock_strength == 1)
169             self.lock_time = time + _lock_time;
170     }
171     else
172     {
173         if(trace_ent)
174             self.lock_strength = max(self.lock_strength - decr * 2, 0);
175         else
176             self.lock_strength = max(self.lock_strength - decr, 0);
177
178         if(self.lock_strength == 0)
179             self.lock_target = world;
180     }
181 }
182
183 #define VEHICLE_UPDATE_PLAYER(fld,vhname) \
184 self.owner.vehicle_##fld = (self.vehicle_##fld / autocvar_g_vehicle_##vhname##_##fld) * 100
185
186 #define vehicles_sweap_collision(orig,vel,dt,acm,mult) \
187 traceline(orig, orig + vel * dt, MOVE_NORMAL, self); \
188 if(trace_fraction != 1) \
189     acm += normalize(self.origin - trace_endpos) * (vlen(vel) * mult)
190
191 // Hover movement support
192 float  force_fromtag_power;
193 float  force_fromtag_normpower;
194 vector force_fromtag_origin;
195 vector vehicles_force_fromtag_hover(string tag_name, float spring_length, float max_power)
196 {
197     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
198     v_forward  = normalize(v_forward) * -1;
199     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
200
201     force_fromtag_power = (1 - trace_fraction) * max_power;
202     force_fromtag_normpower = force_fromtag_power / max_power;
203
204     return v_forward  * force_fromtag_power;
205 }
206
207 // Experimental hovermode wich uses attraction/repulstion from surface insted of gravity/repulsion
208 // Can possibly be use to move abt any surface (inclusing walls/celings)
209 vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float max_power)
210 {
211
212     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
213     v_forward  = normalize(v_forward) * -1;
214     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
215
216     // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
217     if(trace_fraction == 1.0)
218     {
219         force_fromtag_normpower = -0.25;
220         return '0 0 -200';
221     }
222
223     force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
224     force_fromtag_normpower = force_fromtag_power / max_power;
225
226     return v_forward  * force_fromtag_power;
227 }
228
229 // Generic vehile projectile system
230 void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
231 {
232     self.health -= damage;
233     self.velocity += force;
234     if(self.health < 1)
235     {
236         self.takedamage = DAMAGE_NO;
237         self.event_damage = SUB_Null;
238         self.think = self.use;
239         self.nextthink = time;
240     }
241
242 }
243
244 void vehicles_projectile_explode()
245 {
246     if(self.owner && other != world)
247     {
248         if(other == self.owner.vehicle)
249             return;
250
251         if(other == self.owner.vehicle.tur_head)
252             return;
253     }
254
255         PROJECTILE_TOUCH;
256
257         self.event_damage = SUB_Null;
258     RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, self.shot_force, self.totalfrags, other);
259
260     remove (self);
261 }
262
263 entity vehicles_projectile(string _mzlfx, string _mzlsound,
264                            vector _org, vector _vel,
265                            float _dmg, float _radi, float _force,  float _size,
266                            float _deahtype, float _projtype, float _health,
267                            float _cull, float _clianim)
268 {
269     entity proj;
270
271     proj = spawn();
272
273     PROJECTILE_MAKETRIGGER(proj);
274     setorigin(proj, _org);
275
276     proj.shot_dmg         = _dmg;
277     proj.shot_radius      = _radi;
278     proj.shot_force       = _force;
279     proj.totalfrags       = _deahtype;
280     proj.solid            = SOLID_BBOX;
281     proj.movetype         = MOVETYPE_FLYMISSILE;
282     proj.flags            = FL_PROJECTILE;
283     proj.bot_dodge        = TRUE;
284     proj.bot_dodgerating  = _dmg;
285     proj.velocity         = _vel;
286     proj.touch            = vehicles_projectile_explode;
287     proj.use              = vehicles_projectile_explode;
288     proj.owner            = self;
289     proj.realowner        = self.owner;
290     proj.think            = SUB_Remove;
291     proj.nextthink        = time + 30;
292
293     if(_health)
294     {
295         proj.takedamage       = DAMAGE_AIM;
296         proj.event_damage     = vehicles_projectile_damage;
297         proj.health           = _health;
298     }
299     else
300         proj.flags           = FL_PROJECTILE | FL_NOTARGET;
301
302     if(_mzlsound)
303         sound (self, CHAN_WEAPON, _mzlsound, VOL_BASE, ATTN_NORM);
304
305     if(_mzlfx)
306         pointparticles(particleeffectnum(_mzlfx), proj.origin, proj.velocity, 1);
307
308
309     setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
310
311     CSQCProjectile(proj, _clianim, _projtype, _cull);
312
313     return proj;
314 }
315 // End generic vehile projectile system
316
317 /** vehicles_spawn
318     Exetuted for all vehiles on (re)spawn.
319     Sets defaults for newly spawned units.
320 **/
321 void vehicles_spawn()
322 {
323     dprint("Spawning vehicle: ", self.netname, "\n");
324
325     // De-own & reset
326     self.vehicle_hudmodel.viewmodelforclient = self;
327
328     self.owner              = world;
329     self.touch              = vehicles_touch;
330     self.event_damage       = vehicles_damage;
331     self.iscreature         = TRUE;
332     self.movetype           = MOVETYPE_WALK;
333     self.solid              = SOLID_SLIDEBOX;
334     self.takedamage         = DAMAGE_AIM;
335         self.deadflag           = DEAD_NO;
336     self.bot_attack         = TRUE;
337     self.flags              = FL_NOTARGET;
338     self.avelocity          = '0 0 0';
339     self.velocity           = '0 0 0';
340
341     // Reset locking
342     self.lock_strength      = 0;
343     self.lock_target        = world;
344     self.misc_bulletcounter = 0;
345
346     // Return to spawn
347     self.angles             = self.pos2;
348     setorigin(self, self.pos1 + '0 0 128');
349     // Show it
350     pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
351
352     vehicles_reset_colors();
353     self.vehicle_spawn();
354 }
355
356 // Better way of determening whats crushable needed! (fl_crushable?)
357 float vehicles_crushable(entity e)
358 {
359     if(e.classname == "player")
360         return TRUE;
361
362     if(e.classname == "monster_zombie")
363         return TRUE;
364
365     return FALSE;
366 }
367
368 void vehicles_touch()
369 {
370     // Vehicle currently in use
371     if(self.owner)
372     {
373         // Colided with world?
374         if(other == world)
375         {
376             // Apply velocity based self damage here
377         }
378         else
379         {
380             if(other.vehicle_flags & VHF_ISVEHICLE)
381             {
382                 //other.velocity += self.velocity * (self.mass / other.mass);
383             }
384             else if(vehicles_crushable(other))
385             {
386                 if(vlen(self.velocity) != 0)
387                     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);
388             }
389         }
390         return;
391     }
392
393     if(other.classname != "player")
394         return;
395
396     if(other.deadflag != DEAD_NO)
397         return;
398
399     if(other.vehicle != world)
400         return;
401
402     // Remove this when bots know how to use vehicles.
403     if (clienttype(other) != CLIENTTYPE_REAL)
404         return;
405
406     vehicles_enter();
407 }
408
409 void vehicles_enter()
410 {
411    // Remove this when bots know how to use vehicles
412     if (clienttype(other) != CLIENTTYPE_REAL)
413         return;
414
415     if(self.phase > time)
416         return;
417
418     if(teams_matter)
419     if(self.team)
420     if(self.team != other.team)
421         return;
422
423     self.vehicle_ammo1   = 0;
424     self.vehicle_ammo2   = 0;
425     self.vehicle_reload1 = 0;
426     self.vehicle_reload2 = 0;
427     self.vehicle_energy  = 0;
428
429     self.owner          = other;
430     self.switchweapon   = other.switchweapon;
431
432     // .viewmodelforclient works better.
433     //self.vehicle_hudmodel.drawonlytoclient = self.owner;
434
435 #ifndef VEHICLES_CSQC
436         self.vehicle_hudmodel.viewmodelforclient = self.owner;
437 #endif
438
439     self.event_damage         = vehicles_damage;
440     self.nextthink            = 0;
441     self.owner.angles         = self.angles;
442     self.owner.takedamage     = DAMAGE_NO;
443     self.owner.solid          = SOLID_NOT;
444     self.owner.movetype       = MOVETYPE_NOCLIP;
445     self.owner.alpha          = -1;
446     self.owner.vehicle        = self;
447     self.owner.event_damage   = SUB_Null;
448     self.owner.view_ofs       = '0 0 0';
449     self.colormap             = self.owner.colormap;
450     if(self.tur_head)
451         self.tur_head.colormap    = self.owner.colormap;
452
453     self.owner.hud            = self.hud;
454     self.owner.PlayerPhysplug = self.PlayerPhysplug;
455
456     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
457     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
458     self.owner.vehicle_reload1  = self.vehicle_reload1;
459     self.owner.vehicle_reload2  = self.vehicle_reload2;
460
461     // Cant do this, hides attached objects too.
462     //self.exteriormodeltoclient = self.owner;
463     //self.tur_head.exteriormodeltoclient = self.owner;
464
465     other.flags &~= FL_ONGROUND;
466     self.flags  &~= FL_ONGROUND;
467
468     self.team                 = self.owner.team;
469     self.flags               -= FL_NOTARGET;
470
471 #ifndef VEHICLES_CSQC
472     msg_entity = other;
473     WriteByte (MSG_ONE, SVC_SETVIEWPORT);
474     WriteEntity(MSG_ONE, self.vehicle_viewport);
475
476     WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
477     if(self.tur_head)
478     {
479         WriteAngle(MSG_ONE, self.tur_head.angles_x + self.angles_x); // tilt
480         WriteAngle(MSG_ONE, self.tur_head.angles_y + self.angles_y); // yaw
481         WriteAngle(MSG_ONE, 0);                                      // roll
482     }
483     else
484     {
485         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
486         WriteAngle(MSG_ONE,  self.angles_x * -1); // tilt
487         WriteAngle(MSG_ONE,  self.angles_y);      // yaw
488         WriteAngle(MSG_ONE,  0);                  // roll
489     }
490 #endif
491 //#endif
492
493     vehicles_clearrturn();
494
495     CSQCVehicleSetup(self.owner, self.hud);
496
497     self.vehicle_enter();
498 }
499
500 vector vehicles_findgoodexit(vector prefer_spot)
501 {
502     vector exitspot;
503     float mysize;
504     
505     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
506     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
507         return prefer_spot;
508     
509     mysize = vlen(self.maxs - self.mins);
510     float i;
511     vector v, v2;
512     v2 = 0.5 * (self.absmin + self.absmax);
513     for(i = 0; i < 100; ++i)
514     {        
515         v = randomvec();
516         v_z = 0;
517         v = v2 + normalize(v) * mysize;
518         tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
519         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
520             return v;
521     }
522     
523     /*
524     exitspot = (self.origin + '0 0 48') + v_forward * mysize;
525     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
526     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
527         return exitspot;
528     
529     exitspot = (self.origin + '0 0 48') - v_forward * mysize;
530     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
531     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
532         return exitspot;
533
534     exitspot = (self.origin + '0 0 48') + v_right * mysize;
535     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
536     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
537         return exitspot;
538     
539     exitspot = (self.origin + '0 0 48') - v_right * mysize;
540     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
541     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
542         return exitspot;
543     */
544     
545     return self.origin;
546 }
547
548 void vehicles_exit(float eject)
549 {       
550         self.flags |= FL_NOTARGET;
551
552     if (self.owner)
553     {
554         msg_entity = self.owner;
555         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
556         WriteEntity( MSG_ONE, self.owner);
557
558         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
559         WriteAngle(MSG_ONE, 0);                 // pich
560         WriteAngle(MSG_ONE, self.angles_y);     // yaw
561         WriteAngle(MSG_ONE, 0);                 // roll
562
563         setsize(self.owner, PL_MIN,PL_MAX);
564
565         self.owner.takedamage     = DAMAGE_AIM;
566         self.owner.solid          = SOLID_SLIDEBOX;
567         self.owner.movetype       = MOVETYPE_WALK;
568         self.owner.effects        &~= EF_NODRAW;
569         self.owner.alpha          = 1;
570         self.owner.PlayerPhysplug = SUB_Null;
571         self.owner.vehicle        = world;
572         self.owner.view_ofs       = PL_VIEW_OFS;
573         self.owner.event_damage   = PlayerDamage;
574         self.owner.hud            = HUD_NORMAL;
575         self.owner.switchweapon   = self.switchweapon;
576         self.owner.BUTTON_USE     = 0;
577     }
578
579     if(self.deadflag == DEAD_NO)
580         self.avelocity          = '0 0 0';
581
582     self.vehicle_hudmodel.viewmodelforclient = self;
583         self.tur_head.nodrawtoclient             = world;
584     vehicles_setreturn();
585
586     self.phase = time + 1;
587
588     if(!teams_matter)
589         self.team = 0;
590
591     self.vehicle_exit(eject);
592     self.owner = world;
593 }
594
595
596 void vehicles_regen(.float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time)
597 {
598     if(self.regen_field < field_max)
599     if(self.timer + rpause < time)
600     {
601         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
602
603         if(self.owner)
604             self.owner.regen_field = (self.regen_field / field_max) * 100;
605     }
606 }
607
608 void shieldhit_think()
609 {
610     self.alpha -= 0.1;
611     if (self.alpha <= 0)
612     {
613         //setmodel(self, "");
614         self.alpha = -1;
615     }
616     else
617     {
618         self.nextthink = time + 0.1;
619     }
620 }
621
622 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
623 {
624     self.dmg_time = time;
625
626     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
627     {
628         if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
629         {
630             self.vehicle_shieldent = spawn();
631             self.vehicle_shieldent.effects = EF_LOWPRECISION;
632
633             setmodel(self.vehicle_shieldent, "models/vhshield.md3");
634             setattachment(self.vehicle_shieldent, self, "");
635             setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
636             self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
637             self.vehicle_shieldent.think       = shieldhit_think;
638         }
639
640
641         self.vehicle_shieldent.colormod    = '1 1 1';
642         self.vehicle_shieldent.alpha       = 0.45;
643         self.vehicle_shieldent.angles      = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
644         self.vehicle_shieldent.nextthink   = time;
645
646         self.vehicle_shield -= damage;
647
648         if(self.vehicle_shield < 0)
649         {
650             self.vehicle_shieldent.colormod = '2 0 0';
651             self.vehicle_shield             = 0;
652             self.vehicle_shieldent.alpha    = 0.75;
653             self.vehicle_health            -= fabs(self.vehicle_shield);
654         }
655     }
656     else
657         self.vehicle_health -= damage;
658
659     self.velocity += force; // * (vlen(force) / self.mass);
660
661     if(self.vehicle_health <= 0)
662     {
663         if(self.owner)
664             if(self.vehicle_flags & VHF_DEATHEJECT)
665                 vehicles_exit(VHEF_EJECT);
666             else
667                 vehicles_exit(VHEF_RELESE);
668
669         self.vehicle_die();
670         vehicles_setreturn();
671     }
672 }
673
674 void vehicles_return()
675 {
676     pointparticles(particleeffectnum("teleport"), self.enemy.origin + '0 0 64', '0 0 0', 1);
677
678     self.enemy.think     = vehicles_spawn;
679     self.enemy.nextthink = time;
680
681     remove(self);
682 }
683
684 void vehicles_clearrturn()
685 {
686     entity ret;
687     // Remove "return helper", if any.
688     ret = findchain(classname, "vehicle_return");
689     while(ret)
690     {
691         if(ret.enemy == self)
692         {
693             ret.classname   = "";
694             ret.think       = SUB_Remove;
695             ret.nextthink   = time + 0.1;
696             return;
697         }
698
699         ret = ret.chain;
700     }
701 }
702
703 void vehicles_setreturn()
704 {
705     entity ret;
706
707     vehicles_clearrturn();
708
709     ret = spawn();
710     ret.classname   = "vehicle_return";
711     ret.enemy       = self;
712     ret.think       = vehicles_return;
713     ret.nextthink   = time + self.vehicle_respawntime;
714 }
715
716 float vehicles_customizeentityforclient()
717 {
718     if(self.deadflag  == DEAD_DEAD)
719         return FALSE;
720     else
721         return TRUE;
722 }
723
724 void vehicles_configcheck(string  configname, float check_cvar)
725 {
726     if(check_cvar == 0)
727         localcmd(strcat("exec ", configname, "\n"));
728 }
729
730 void vehicles_reset_colors()
731 {
732     entity e;
733     float _effects, _colormap;
734     vector _glowmod, _colormod;
735
736     if(autocvar_g_nodepthtestplayers)
737         _effects = EF_NODEPTHTEST;
738
739     if(autocvar_g_fullbrightplayers)
740         _effects |= EF_FULLBRIGHT;
741
742     if(self.team)
743         _colormap = 1024 + (self.team - 1) * 17;
744     else
745         _colormap = 1024;
746
747     _glowmod  = '0 0 0';
748     _colormod = '0 0 0';
749
750     // Find all ents attacked to main model and setup effects, colormod etc.
751     e = findchainentity(tag_entity, self);
752     while(e)
753     {
754         if(e != self.vehicle_shieldent)
755         {
756             e.effects   = _effects  | EF_LOWPRECISION;
757             e.colormod  = _colormod;
758             e.colormap  = _colormap;
759             e.alpha     = 1;
760         }
761         e = e.chain;
762     }
763
764     self.vehicle_hudmodel.effects  = self.effects  = _effects | EF_LOWPRECISION;
765     self.vehicle_hudmodel.colormod = self.colormod = _colormod;
766     self.vehicle_hudmodel.colormap = self.colormap = _colormap;
767     self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
768
769     self.alpha     = 1;
770     self.avelocity = '0 0 0';
771     self.velocity  = '0 0 0';
772     self.effects   = _effects;
773 }
774
775 float vehicle_initialize(string  net_name,
776                          string  bodymodel,
777                          string  topmodel,
778                          string  hudmodel,
779                          string  toptag,
780                          string  hudtag,
781                          string  viewtag,
782                          float   vhud,
783                          vector  min_s,
784                          vector  max_s,
785                          float   nodrop,
786                          void()  spawnproc,
787                          float   _respawntime,
788                          float() physproc,
789                          void()  enterproc,
790                          void(float extflag) exitfunc,
791                          void() dieproc,
792                          void() thinkproc,
793                          float  use_csqc)
794 {
795     addstat(STAT_HUD, AS_INT,  hud);
796         addstat(STAT_VEHICLESTAT_HEALTH,  AS_INT, vehicle_health);
797         addstat(STAT_VEHICLESTAT_SHIELD,  AS_INT, vehicle_shield);
798         addstat(STAT_VEHICLESTAT_ENERGY,  AS_INT, vehicle_energy);
799
800         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT,   vehicle_ammo1);
801         addstat(STAT_VEHICLESTAT_RELOAD1, AS_INT, vehicle_reload1);
802
803         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT,   vehicle_ammo2);
804         addstat(STAT_VEHICLESTAT_RELOAD2, AS_INT, vehicle_reload2);
805
806     if(bodymodel == "")
807         error("vehicles: missing bodymodel!");
808
809     if(hudmodel == "")
810         error("vehicles: missing hudmodel!");
811
812     if(net_name == "")
813         self.netname = self.classname;
814     else
815         self.netname = net_name;
816
817     if(self.team && !teams_matter)
818         self.team = 0;
819
820     self.vehicle_flags |= VHF_ISVEHICLE;
821
822     setmodel(self, bodymodel);
823
824     self.vehicle_viewport   = spawn();
825     self.vehicle_hudmodel   = spawn();
826     self.tur_head           = spawn();
827     self.tur_head.owner     = self;
828     self.takedamage         = DAMAGE_AIM;
829     self.bot_attack         = TRUE;
830     self.iscreature         = TRUE;
831     self.hud                = vhud;
832
833     //self.customizeentityforclient = vehicles_customizeentityforclient;
834     self.vehicle_die         = dieproc;
835     self.vehicle_exit        = exitfunc;
836     self.vehicle_enter       = enterproc;
837     self.PlayerPhysplug      = physproc;
838     self.event_damage        = vehicles_damage;
839     self.touch               = vehicles_touch;
840     self.think               = vehicles_spawn;    
841     self.nextthink           = time;        
842     self.vehicle_respawntime = _respawntime;
843     self.vehicle_spawn       = spawnproc;
844
845     if(autocvar_g_nodepthtestplayers)
846         self.effects = self.effects | EF_NODEPTHTEST;
847
848     if(autocvar_g_fullbrightplayers)
849         self.effects = self.effects | EF_FULLBRIGHT;
850
851     setmodel(self.vehicle_hudmodel, hudmodel);
852     setmodel(self.vehicle_viewport, "null");
853
854
855     if(topmodel != "")
856     {
857         setmodel(self.tur_head, topmodel);
858         setattachment(self.tur_head, self, toptag);
859         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
860         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
861     }
862     else
863     {
864         setattachment(self.tur_head, self, "");
865         setattachment(self.vehicle_hudmodel, self, hudtag);
866         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
867     }
868
869     setsize(self, min_s, max_s);
870     if not (nodrop)
871     {
872         setorigin(self, self.origin);
873         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
874         setorigin(self, trace_endpos);
875     }
876
877     self.pos1 = self.origin;
878     self.pos2 = self.angles;
879 #ifdef VEHICLES_CSQC
880     if(use_csqc)
881         net_link_vehile();
882 #endif
883     return TRUE;
884 }
885
886
887 void bugmenot()
888 {
889     self.vehicle_exit       = self.vehicle_exit;
890     self.vehicle_enter      = self.vehicle_exit;
891     self.vehicle_die        = self.vehicle_exit;
892     self.vehicle_spawn      = self.vehicle_exit;
893     self.AuxiliaryXhair     = self.AuxiliaryXhair;
894 }