]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/vehicles/sv_vehicles.qc
Tidy up classnames
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / sv_vehicles.qc
1 #include "sv_vehicles.qh"
2
3 #include <common/items/_mod.qh>
4 #include <common/mapobjects/defs.qh>
5 #include <common/mapobjects/teleporters.qh>
6 #include <common/mapobjects/triggers.qh>
7 #include <server/bot/api.qh>
8 #include <server/damage.qh>
9 #include <server/items/items.qh>
10 #include <server/weapons/common.qh>
11 #include <server/world.qh>
12
13 bool SendAuxiliaryXhair(entity this, entity to, int sf)
14 {
15         WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
16         WriteByte(MSG_ENTITY, sf);
17
18         WriteByte(MSG_ENTITY, this.cnt);
19
20         if(sf & 2)
21         {
22                 WriteVector(MSG_ENTITY, this.origin);
23         }
24
25         if(sf & 4)
26         {
27                 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
28                 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
29                 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
30         }
31
32         return true;
33 }
34
35 bool AuxiliaryXhair_customize(entity this, entity client)
36 {
37         entity e = WaypointSprite_getviewentity(client);
38         entity axh = e.(AuxiliaryXhair[this.cnt]);
39         return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
40 }
41
42 .vector axh_prevorigin;
43 .vector axh_prevcolors;
44
45 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
46 {
47         if(!IS_REAL_CLIENT(own))
48                 return;
49
50         axh_id = bound(0, axh_id, MAX_AXH);
51         entity axh = own.(AuxiliaryXhair[axh_id]);
52
53         if(axh == NULL || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
54         {
55                 axh = new(auxiliary_xhair);
56                 axh.cnt = axh_id;
57                 //axh.drawonlytoclient = own; // not spectatable
58                 setcefc(axh, AuxiliaryXhair_customize);
59                 axh.owner = own;
60                 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
61         }
62
63         if(loc != axh.axh_prevorigin)
64         {
65                 setorigin(axh, loc);
66                 axh.SendFlags |= 2;
67         }
68
69         if(clr != axh.axh_prevcolors)
70         {
71                 axh.colormod = clr;
72                 axh.SendFlags |= 4;
73         }
74
75         own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
76 }
77
78 void CSQCVehicleSetup(entity own, int vehicle_id)
79 {
80         if(!IS_REAL_CLIENT(own))
81                 return;
82
83         msg_entity = own;
84
85         WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
86         WriteByte(MSG_ONE, vehicle_id);
87
88         if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
89         {
90                 for(int i = 0; i < MAX_AXH; ++i)
91                 {
92                         entity axh = own.(AuxiliaryXhair[i]);
93                         own.(AuxiliaryXhair[i]) = NULL;
94
95                         if(axh.owner == own && axh != NULL && !wasfreed(axh))
96                                 delete(axh);
97                 }
98         }
99 }
100
101 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
102 {
103         if(this.lock_target && IS_DEAD(this.lock_target))
104         {
105                 this.lock_target        = NULL;
106                 this.lock_strength  = 0;
107                 this.lock_time    = 0;
108         }
109
110         if(this.lock_time > time)
111         {
112                 if(this.lock_target)
113                 if(this.lock_soundtime < time)
114                 {
115                         this.lock_soundtime = time + 0.5;
116                         play2(this.owner, "vehicles/locked.wav");
117                 }
118
119                 return;
120         }
121
122         if(trace_ent != NULL)
123         {
124                 if(SAME_TEAM(trace_ent, this))
125                         trace_ent = NULL;
126
127                 if(IS_DEAD(trace_ent))
128                         trace_ent = NULL;
129
130                 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
131                         trace_ent = NULL;
132
133                 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
134                         trace_ent = NULL; // invisible
135         }
136
137         if(this.lock_target == NULL && trace_ent != NULL)
138                 this.lock_target = trace_ent;
139
140         if(this.lock_target && trace_ent == this.lock_target)
141         {
142                 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
143                 {
144                         play2(this.owner, "vehicles/lock.wav");
145                         this.lock_soundtime = time + 0.8;
146                 }
147                 else if (this.lock_strength != 1 && this.lock_soundtime < time)
148                 {
149                         play2(this.owner, "vehicles/locking.wav");
150                         this.lock_soundtime = time + 0.3;
151                 }
152         }
153
154         // Have a locking target
155         // Trace hit current target
156         if(trace_ent == this.lock_target && trace_ent != NULL)
157         {
158                 this.lock_strength = min(this.lock_strength + incr, 1);
159                 if(this.lock_strength == 1)
160                         this.lock_time = time + _lock_time;
161         }
162         else
163         {
164                 if(trace_ent)
165                         this.lock_strength = max(this.lock_strength - decr * 2, 0);
166                 else
167                         this.lock_strength = max(this.lock_strength - decr, 0);
168
169                 if(this.lock_strength == 0)
170                         this.lock_target = NULL;
171         }
172 }
173
174 float vehicle_altitude(entity this, float amax)
175 {
176         tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
177         return vlen(this.origin - trace_endpos);
178 }
179
180 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
181 {
182         force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
183         v_forward  = normalize(v_forward) * -1;
184         traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, this);
185
186         force_fromtag_power = (1 - trace_fraction) * max_power;
187         force_fromtag_normpower = force_fromtag_power / max_power;
188
189         return v_forward  * force_fromtag_power;
190 }
191
192 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
193 {
194         force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
195         v_forward  = normalize(v_forward) * -1;
196         traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, this);
197
198         // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
199         if(trace_fraction == 1.0)
200         {
201                 force_fromtag_normpower = -0.25;
202                 return '0 0 -200';
203         }
204
205         force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
206         force_fromtag_normpower = force_fromtag_power / max_power;
207
208         return v_forward  * force_fromtag_power;
209 }
210
211 // projectile handling
212 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
213 {
214         // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
215         if(inflictor.owner == this.owner)
216                 return;
217
218         TakeResource(this, RES_HEALTH, damage);
219         this.velocity += force;
220         if(GetResource(this, RES_HEALTH) < 1)
221         {
222                 this.takedamage = DAMAGE_NO;
223                 this.event_damage = func_null;
224                 setthink(this, adaptor_think2use);
225                 this.nextthink = time;
226         }
227 }
228
229 void vehicles_projectile_explode(entity this, entity toucher)
230 {
231         if(this.owner && toucher != NULL)
232         {
233                 if(toucher == this.owner.vehicle)
234                         return;
235
236                 if(toucher == this.owner.vehicle.tur_head)
237                         return;
238         }
239
240         PROJECTILE_TOUCH(this, toucher);
241
242         this.event_damage = func_null;
243         RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.projectiledeathtype, DMG_NOWEP, toucher);
244
245         delete(this);
246 }
247
248 void vehicles_projectile_explode_think(entity this)
249 {
250         vehicles_projectile_explode(this, NULL);
251 }
252
253 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
254 {
255         vehicles_projectile_explode(this, trigger);
256 }
257
258 entity vehicles_projectile(entity this, entity _mzlfx, Sound _mzlsound,
259                                                    vector _org, vector _vel,
260                                                    float _dmg, float _radi, float _force,  float _size,
261                                                    int _deahtype, float _projtype, float _health,
262                                                    bool _cull, bool _clianim, entity _owner)
263 {
264     TC(Sound, _mzlsound);
265         entity proj = new(vehicles_projectile);
266
267         PROJECTILE_MAKETRIGGER(proj);
268         setorigin(proj, _org);
269
270         proj.shot_dmg            = _dmg;
271         proj.shot_radius          = _radi;
272         proj.shot_force    = _force;
273         proj.projectiledeathtype           = _deahtype;
274         proj.solid                      = SOLID_BBOX;
275         set_movetype(proj, MOVETYPE_FLYMISSILE);
276         proj.flags = FL_PROJECTILE;
277         IL_PUSH(g_projectiles, proj);
278         IL_PUSH(g_bot_dodge, proj);
279         proj.bot_dodge          = true;
280         proj.bot_dodgerating  = _dmg;
281         proj.velocity            = _vel;
282         settouch(proj, vehicles_projectile_explode);
283         proj.use                          = vehicles_projectile_explode_use;
284         proj.owner                      = this;
285         proj.realowner          = _owner;
286         setthink(proj, SUB_Remove);
287         proj.nextthink          = time + 30;
288
289         if(_health)
290         {
291                 proj.takedamage    = DAMAGE_AIM;
292                 proj.event_damage        = vehicles_projectile_damage;
293                 SetResourceExplicit(proj, RES_HEALTH, _health);
294         }
295         else
296                 proj.flags |= FL_NOTARGET;
297
298         if(_mzlsound != SND_Null)
299                 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
300
301         if(_mzlfx != EFFECT_Null)
302                 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
303
304         setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
305
306         CSQCProjectile(proj, _clianim, _projtype, _cull);
307
308         return proj;
309 }
310
311 void vehicles_gib_explode(entity this)
312 {
313         sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
314         Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
315         Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
316         delete(this);
317 }
318
319 void vehicles_gib_touch(entity this, entity toucher)
320 {
321         vehicles_gib_explode(this);
322 }
323
324 void vehicles_gib_think(entity this)
325 {
326         this.alpha -= 0.1;
327         if(this.cnt >= time)
328                 delete(this);
329         else
330                 this.nextthink = time + 0.1;
331 }
332
333 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
334 {
335         entity _gib = new(vehicle_gib);
336         _setmodel(_gib, _template.model);
337         vector org = gettaginfo(this, gettagindex(this, _tag));
338         setorigin(_gib, org);
339         _gib.velocity = _vel;
340         set_movetype(_gib, MOVETYPE_TOSS);
341         _gib.solid = SOLID_CORPSE;
342         _gib.colormod = '-0.5 -0.5 -0.5';
343         _gib.effects = EF_LOWPRECISION;
344         _gib.avelocity = _rot;
345
346         if(_burn)
347                 _gib.effects |= EF_FLAME;
348
349         if(_explode)
350         {
351                 setthink(_gib, vehicles_gib_explode);
352                 _gib.nextthink = time + random() * _explode;
353                 settouch(_gib, vehicles_gib_touch);
354         }
355         else
356         {
357                 _gib.cnt = time + _maxtime;
358                 setthink(_gib, vehicles_gib_think);
359                 _gib.nextthink = time + _maxtime - 1;
360                 _gib.alpha = 1;
361         }
362         return _gib;
363 }
364
365 bool vehicle_addplayerslot(     entity _owner,
366                                                                 entity _slot,
367                                                                 int _hud,
368                                                                 Model _hud_model,
369                                                                 bool(entity,float) _framefunc,
370                                                                 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
371 {
372         if(!(_owner.vehicle_flags & VHF_MULTISLOT))
373                 _owner.vehicle_flags |= VHF_MULTISLOT;
374
375         _slot.PlayerPhysplug = _framefunc;
376         _slot.vehicle_exit = _exitfunc;
377         _slot.vehicle_enter = _enterfunc;
378         STAT(HUD, _slot) = _hud;
379         _slot.vehicle_flags = VHF_PLAYERSLOT;
380         _slot.vehicle_viewport = new(vehicle_viewport);
381         _slot.vehicle_hudmodel = new(vehicle_hudmodel);
382         _slot.vehicle_hudmodel.viewmodelforclient = _slot;
383         _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
384
385         setmodel(_slot.vehicle_hudmodel, _hud_model);
386         setmodel(_slot.vehicle_viewport, MDL_Null);
387
388         setattachment(_slot.vehicle_hudmodel, _slot, "");
389         setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
390
391         return true;
392 }
393
394 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
395                                                  float _pichlimit_min, float _pichlimit_max,
396                                                  float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
397 {
398         vector vtmp, vtag;
399         float ftmp;
400         vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
401         vtmp = vectoangles(normalize(_target - vtag));
402         vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
403         vtmp = AnglesTransform_Normalize(vtmp, true);
404         ftmp = _aimspeed * dt;
405         vtmp_y = bound(-ftmp, vtmp_y, ftmp);
406         vtmp_x = bound(-ftmp, vtmp_x, ftmp);
407         _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
408         _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
409         return vtag;
410 }
411
412 void vehicles_reset_colors(entity this, entity player)
413 {
414         int eff = 0, cmap;
415         const vector cmod = '0 0 0';
416         if(this.team && teamplay)
417                 cmap = 1024 + (this.team - 1) * 17;
418         else if(player)
419                 cmap = player.colormap;
420         else
421                 cmap = 1024;
422         if(autocvar_g_nodepthtestplayers)
423                 eff |= EF_NODEPTHTEST;
424         if(autocvar_g_fullbrightplayers)
425                 eff |= EF_FULLBRIGHT;
426
427         // Find all ents attacked to main model and setup effects, colormod etc.
428         FOREACH_ENTITY_ENT(tag_entity, this,
429         {
430                 if(it == this.vehicle_shieldent)
431                         continue;
432
433                 it.effects = eff;
434                 it.colormod = cmod;
435                 it.colormap = cmap;
436                 it.alpha = 1;
437         });
438
439         // Also check head tags
440         FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
441         {
442                 if(it == this.vehicle_shieldent)
443                         continue;
444
445                 it.effects = eff;
446                 it.colormod = cmod;
447                 it.colormap = cmap;
448                 it.alpha = 1;
449         });
450
451         this.vehicle_hudmodel.effects  = this.effects  = eff; // | EF_LOWPRECISION;
452         this.vehicle_hudmodel.colormod = this.colormod = cmod;
453         this.vehicle_hudmodel.colormap = this.colormap = cmap;
454         this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
455
456         this.alpha       = 1;
457         this.avelocity = '0 0 0';
458         this.velocity  = '0 0 0';
459         this.effects   = eff;
460
461         Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
462         info.vr_setcolors(info, this);
463 }
464
465 void vehicles_clearreturn(entity veh)
466 {
467         // Remove "return helper" entities, if any.
468         IL_EACH(g_vehicle_returners, it.wp00 == veh,
469         {
470                 it.classname = "";
471                 setthink(it, SUB_Remove);
472                 it.nextthink = time + 0.1;
473                 IL_REMOVE(g_vehicle_returners, it);
474
475                 if(it.waypointsprite_attached)
476                         WaypointSprite_Kill(it.waypointsprite_attached);
477         });
478 }
479
480 void vehicles_spawn(entity this);
481 void vehicles_return(entity this)
482 {
483         Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
484
485         setthink(this.wp00, vehicles_spawn);
486         this.wp00.nextthink = time;
487
488         if(this.waypointsprite_attached)
489                 WaypointSprite_Kill(this.waypointsprite_attached);
490
491         delete(this);
492 }
493
494 void vehicles_showwp_goaway(entity this)
495 {
496         if(this.waypointsprite_attached)
497                 WaypointSprite_Kill(this.waypointsprite_attached);
498
499         delete(this);
500 }
501
502 void vehicles_showwp(entity this)
503 {
504         entity ent = this;
505
506         if(ent.cnt)
507         {
508                 setthink(ent, vehicles_return);
509                 ent.nextthink = ent.cnt;
510         }
511         else
512         {
513                 setthink(ent, vehicles_return);
514                 ent.nextthink  = time + 1;
515
516                 ent = spawn();
517                 ent.team = this.wp00.team;
518                 ent.wp00 = this.wp00;
519                 setorigin(ent, this.wp00.pos1);
520
521                 ent.nextthink = time + 5;
522                 setthink(ent, vehicles_showwp_goaway);
523         }
524
525         vector rgb;
526         if(teamplay && ent.team)
527                 rgb = Team_ColorRGB(ent.team);
528         else
529                 rgb = '1 1 1';
530         entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
531         wp.wp_extra = ent.wp00.vehicleid;
532         wp.colormod = rgb;
533         if(ent.waypointsprite_attached)
534         {
535                 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
536                 if(this == NULL)
537                         WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
538                 WaypointSprite_Ping(ent.waypointsprite_attached);
539         }
540 }
541
542 void vehicles_setreturn(entity veh)
543 {
544         vehicles_clearreturn(veh);
545
546         entity ret = new(vehicle_return);
547         IL_PUSH(g_vehicle_returners, ret);
548         ret.wp00           = veh;
549         ret.team                = veh.team;
550         setthink(ret, vehicles_showwp);
551
552         if(IS_DEAD(veh))
553         {
554                 ret.cnt          = time + veh.respawntime;
555                 ret.nextthink   = min(time + veh.respawntime, time + veh.respawntime - 5);
556         }
557         else
558         {
559                 ret.nextthink   = min(time + veh.respawntime, time + veh.respawntime - 1);
560         }
561
562         setorigin(ret, veh.pos1 + '0 0 96');
563 }
564
565 void vehicle_use(entity this, entity actor, entity trigger)
566 {
567         LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
568
569         this.tur_head.team = actor.team;
570
571         if(this.tur_head.team == 0)
572                 this.active = ACTIVE_NOT;
573         else
574                 this.active = ACTIVE_ACTIVE;
575
576         if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
577         {
578                 LOG_DEBUG("Respawning vehicle: ", this.netname);
579                 if(this.effects & EF_NODRAW)
580                 {
581                         setthink(this, vehicles_spawn);
582                         this.nextthink = time + 3;
583                 }
584                 else
585                 {
586                         vehicles_setreturn(this);
587                         vehicles_reset_colors(this, actor);
588                 }
589         }
590 }
591
592 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
593 {
594         if(this.(regen_field) < field_max)
595         if(timer + rpause < time)
596         {
597                 if(_healthscale)
598                         regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
599
600                 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
601
602                 if(this.owner)
603                         this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
604         }
605 }
606
607 void vehicles_regen_resource(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale, int resource)
608 {
609         float resource_amount = GetResource(this, resource);
610
611         if(resource_amount < field_max)
612         if(timer + rpause < time)
613         {
614                 if(_healthscale)
615                         regen = regen * (resource_amount / this.max_health);
616
617                 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
618
619                 if(this.owner)
620                         this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
621         }
622 }
623
624 void shieldhit_think(entity this)
625 {
626         this.alpha -= 0.1;
627         if (this.alpha <= 0)
628         {
629                 // setmodel(this, MDL_Null);
630                 this.alpha = -1;
631                 this.effects |= EF_NODRAW;
632         }
633         else
634         {
635                 this.nextthink = time + 0.1;
636         }
637 }
638
639 void vehicles_painframe(entity this)
640 {
641         int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
642
643         if(myhealth <= 50)
644         if(this.pain_frame < time)
645         {
646                 float _ftmp = myhealth / 50;
647                 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
648                 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
649
650                 if(this.vehicle_flags & VHF_DMGSHAKE)
651                         this.velocity += randomvec() * 30;
652
653                 if(this.vehicle_flags & VHF_DMGROLL)
654                 {
655                         if(this.vehicle_flags & VHF_DMGHEADROLL)
656                                 this.tur_head.angles += randomvec();
657                         else
658                                 this.angles += randomvec();
659                 }
660         }
661 }
662
663 void vehicles_frame(entity this, entity actor)
664 {
665         vehicles_painframe(this);
666 }
667
668 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
669 {
670         this.dmg_time = time;
671
672         // WEAPONTODO
673         if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
674                 damage *= autocvar_g_vehicles_vortex_damagerate;
675         else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
676                 damage *= autocvar_g_vehicles_machinegun_damagerate;
677         else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
678                 damage *= autocvar_g_vehicles_rifle_damagerate;
679         else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
680                 damage *= autocvar_g_vehicles_vaporizer_damagerate;
681         else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
682                 damage *= autocvar_g_vehicles_tag_damagerate;
683         else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
684                 damage *= autocvar_g_vehicles_weapon_damagerate;
685
686         this.enemy = attacker;
687
688         this.pain_finished = time;
689
690         if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
691         {
692                 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
693                 {
694                         this.vehicle_shieldent = new(vehicle_shieldent);
695                         this.vehicle_shieldent.effects = EF_LOWPRECISION;
696
697                         setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
698                         setattachment(this.vehicle_shieldent, this, "");
699                         setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
700                         this.vehicle_shieldent.scale       = 256 / vlen(this.maxs - this.mins);
701                         setthink(this.vehicle_shieldent, shieldhit_think);
702                 }
703
704                 this.vehicle_shieldent.colormod = '1 1 1';
705                 this.vehicle_shieldent.alpha = 0.45;
706                 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
707                 this.vehicle_shieldent.nextthink = time;
708                 this.vehicle_shieldent.effects &= ~EF_NODRAW;
709
710                 this.vehicle_shield -= damage;
711
712                 if(this.vehicle_shield < 0)
713                 {
714                         TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
715                         this.vehicle_shieldent.colormod = '2 0 0';
716                         this.vehicle_shield = 0;
717                         this.vehicle_shieldent.alpha = 0.75;
718
719                         if(sound_allowed(MSG_BROADCAST, attacker))
720                                 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM);   // FIXME: PLACEHOLDER
721                 }
722                 else
723                         if(sound_allowed(MSG_BROADCAST, attacker))
724                                 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
725
726         }
727         else
728         {
729                 TakeResource(this, RES_HEALTH, damage);
730
731                 if(sound_allowed(MSG_BROADCAST, attacker))
732                         spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
733         }
734
735         if(this.damageforcescale < 1 && this.damageforcescale > 0)
736                 this.velocity += force * this.damageforcescale;
737         else
738                 this.velocity += force;
739
740         if(GetResource(this, RES_HEALTH) <= 0)
741         {
742                 if(this.owner)
743                 {
744                         if(this.vehicle_flags & VHF_DEATHEJECT)
745                                 vehicles_exit(this, VHEF_EJECT);
746                         else
747                                 vehicles_exit(this, VHEF_RELEASE);
748                 }
749
750                 antilag_clear(this, this);
751
752                 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
753                 info.vr_death(info, this);
754                 vehicles_setreturn(this);
755         }
756 }
757
758 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
759 {
760         float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
761         if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
762                 return false;
763
764         GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
765         if(targ.owner)
766                 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
767         return true;
768 }
769
770 bool vehicles_crushable(entity e)
771 {
772         if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
773                 return true;
774
775         if(IS_MONSTER(e))
776                 return true;
777
778         return false;
779 }
780
781 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
782 {
783         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
784                 return;
785
786         if(this.play_time < time)
787         {
788                 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
789                 {
790                         float wc = vlen(this.velocity - this.oldvelocity);
791                         float take = min(_speedfac * wc, _maxpain);
792                         Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
793                         this.play_time = time + 0.25;
794                 }
795         }
796 }
797
798 // vehicle enter/exit handling
799 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
800 {
801         // TODO: we actually want the player's size here
802         tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
803         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
804                 return prefer_spot;
805
806         float mysize = 1.5 * vlen(this.maxs - this.mins);
807         vector v;
808         vector v2 = 0.5 * (this.absmin + this.absmax);
809         for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
810         {
811                 v = randomvec();
812                 v_z = 0;
813                 v = v2 + normalize(v) * mysize;
814                 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
815                 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
816                         return v;
817         }
818
819         return this.origin;
820 }
821
822 .int old_vehicle_flags;
823 void vehicles_exit(entity vehic, bool eject)
824 {
825         entity player = vehic.owner;
826
827         if(vehicles_exit_running)
828         {
829                 LOG_TRACE("^1vehicles_exit already running! this is not good...");
830                 return;
831         }
832
833         vehicles_exit_running = true;
834
835         if(vehic.vehicle_flags & VHF_PLAYERSLOT)
836         {
837                 vehic.vehicle_exit(vehic, eject);
838                 vehicles_exit_running = false;
839                 return;
840         }
841
842         if (player)
843         {
844                 if (IS_REAL_CLIENT(player))
845                 {
846                         msg_entity = player;
847                         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
848                         WriteEntity( MSG_ONE, player);
849
850                         // NOTE: engine networked
851                         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
852                         WriteAngle(MSG_ONE, 0);
853                         WriteAngle(MSG_ONE, vehic.angles_y);
854                         WriteAngle(MSG_ONE, 0);
855                 }
856
857                 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
858
859                 player.takedamage               = DAMAGE_AIM;
860                 player.solid                    = SOLID_SLIDEBOX;
861                 set_movetype(player, MOVETYPE_WALK);
862                 player.effects             &= ~EF_NODRAW;
863                 player.teleportable             = TELEPORT_NORMAL;
864                 player.alpha                    = default_player_alpha;
865                 player.PlayerPhysplug   = func_null;
866                 player.vehicle                  = NULL;
867                 player.view_ofs                 = STAT(PL_VIEW_OFS, player);
868                 player.event_damage             = PlayerDamage;
869                 STAT(HUD, player)               = HUD_NORMAL;
870                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
871                 {
872                         .entity weaponentity = weaponentities[slot];
873                         player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
874                         delete(vehic.(weaponentity)); // no longer needed
875                 }
876                 player.last_vehiclecheck = time + 3;
877                 player.vehicle_enter_delay = time + 2;
878
879                 CSQCVehicleSetup(player, HUD_NORMAL);
880
881                 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
882                 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
883         }
884
885         vehic.flags |= FL_NOTARGET;
886
887         if(!IS_DEAD(vehic))
888                 vehic.avelocity = '0 0 0';
889
890         vehic.tur_head.nodrawtoclient = NULL;
891
892         if(!teamplay)
893                 vehic.team = 0;
894
895         WaypointSprite_Kill(vehic.wps_intruder);
896
897         MUTATOR_CALLHOOK(VehicleExit, player, vehic);
898
899         vehic.team = vehic.tur_head.team;
900
901         if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
902                 vehic.vehicle_flags |= VHF_SHIELDREGEN;
903         vehic.old_vehicle_flags = 0;
904
905         sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
906         vehic.vehicle_hudmodel.viewmodelforclient = vehic;
907         vehic.phase = time + 1;
908
909         vehic.vehicle_exit(vehic, eject);
910
911         vehicles_setreturn(vehic);
912         vehicles_reset_colors(vehic, NULL);
913         vehic.owner = NULL;
914
915         CSQCMODEL_AUTOINIT(vehic);
916
917         if(player)
918                 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
919
920         vehicles_exit_running = false;
921 }
922
923 void vehicles_touch(entity this, entity toucher)
924 {
925         if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
926                 return;
927
928         // Vehicle currently in use
929         if(this.owner)
930         {
931                 if(toucher != NULL)
932                 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
933                 if(vehicles_crushable(toucher))
934                 if(!weaponLocked(this.owner))
935                 {
936                         if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
937                                 Damage(toucher, this, this.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH.m_id, DMG_NOWEP, '0 0 0', normalize(toucher.origin - this.origin) * autocvar_g_vehicles_crush_force);
938
939                         return; // Dont do selfdamage when hitting "soft targets".
940                 }
941
942                 if(this.play_time < time) {
943                         Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
944                         info.vr_impact(info, this);
945                 }
946
947                 return;
948         }
949
950         if(autocvar_g_vehicles_enter)
951                 return;
952
953         vehicles_enter(toucher, this);
954 }
955
956 bool vehicle_impulse(entity this, int imp)
957 {
958         entity v = this.vehicle;
959         if (!v) return false;
960         if (IS_DEAD(v)) return false;
961         bool(entity,int) f = v.vehicles_impulse;
962         if (f && f(this,imp)) return true;
963         switch (imp)
964         {
965                 case IMP_weapon_drop.impulse:
966                 {
967                         stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
968                         return true;
969                 }
970         }
971         return false;
972 }
973
974 void vehicles_enter(entity pl, entity veh)
975 {
976         // Remove this when bots know how to use vehicles
977         if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
978                 return;
979
980         if((!IS_PLAYER(pl))
981         || (veh.phase >= time)
982         || (pl.vehicle_enter_delay >= time)
983         || (STAT(FROZEN, pl))
984         || (IS_DEAD(pl))
985         || (pl.vehicle)
986         ) { return; }
987
988         Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
989
990         if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
991         if(veh.vehicle_flags & VHF_MULTISLOT)
992         if(veh.owner && SAME_TEAM(pl, veh))
993         {
994                 // we don't need a return value or anything here
995                 // if successful the owner check below will prevent anything weird
996                 info.vr_gunner_enter(info, veh, pl);
997         }
998
999         if(veh.owner)
1000                 return; // got here and didn't enter the gunner, return
1001
1002         if(teamplay)
1003         if(veh.team)
1004         if(DIFF_TEAM(pl, veh))
1005         if(autocvar_g_vehicles_steal)
1006         {
1007                 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
1008
1009                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1010
1011                 veh.vehicle_shield = 0;
1012                 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1013                 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1014
1015                 if (autocvar_g_vehicles_steal_show_waypoint) {
1016                         entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1017                         wp.colormod = Team_ColorRGB(pl.team);
1018                 }
1019         }
1020         else return;
1021
1022         RemoveGrapplingHooks(pl);
1023
1024         veh.vehicle_ammo1 = 0;
1025         veh.vehicle_ammo2 = 0;
1026         veh.vehicle_reload1 = 0;
1027         veh.vehicle_reload2 = 0;
1028         veh.vehicle_energy = 0;
1029
1030         veh.owner = pl;
1031         pl.vehicle = veh;
1032
1033         // .viewmodelforclient works better.
1034         //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1035
1036         veh.vehicle_hudmodel.viewmodelforclient = pl;
1037
1038         UNSET_DUCKED(pl);
1039         pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1040         setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1041
1042         veh.event_damage        = vehicles_damage;
1043         veh.event_heal          = vehicles_heal;
1044         veh.nextthink           = 0;
1045         pl.items &= ~IT_USING_JETPACK;
1046         pl.angles                       = veh.angles;
1047         pl.takedamage           = DAMAGE_NO;
1048         pl.solid                        = SOLID_NOT;
1049         pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1050         set_movetype(pl, MOVETYPE_NOCLIP);
1051         pl.teleportable         = false;
1052         pl.alpha                        = -1;
1053         pl.event_damage         = func_null;
1054         pl.view_ofs                     = '0 0 0';
1055         veh.colormap            = pl.colormap;
1056         if(veh.tur_head)
1057                 veh.tur_head.colormap = pl.colormap;
1058         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1059         {
1060                 .entity weaponentity = weaponentities[slot];
1061                 veh.(weaponentity) = new(temp_wepent);
1062                 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1063         }
1064         STAT(HUD, pl) = veh.vehicleid;
1065         pl.PlayerPhysplug = veh.PlayerPhysplug;
1066
1067         pl.vehicle_ammo1 = veh.vehicle_ammo1;
1068         pl.vehicle_ammo2 = veh.vehicle_ammo2;
1069         pl.vehicle_reload1 = veh.vehicle_reload1;
1070         pl.vehicle_reload2 = veh.vehicle_reload2;
1071         pl.vehicle_energy = veh.vehicle_energy;
1072
1073         // Cant do this, hides attached objects too.
1074         //veh.exteriormodeltoclient = veh.owner;
1075         //veh.tur_head.exteriormodeltoclient = veh.owner;
1076
1077         UNSET_ONGROUND(pl);
1078         UNSET_ONGROUND(veh);
1079
1080         veh.team = pl.team;
1081         veh.flags -= FL_NOTARGET;
1082
1083         vehicles_reset_colors(veh, pl);
1084
1085         if (IS_REAL_CLIENT(pl))
1086         {
1087                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1088
1089                 msg_entity = pl;
1090                 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1091                 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1092
1093                 // NOTE: engine networked
1094                 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1095                 if(veh.tur_head)
1096                 {
1097                         WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1098                         WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1099                         WriteAngle(MSG_ONE, 0);                                                                   // roll
1100                 }
1101                 else
1102                 {
1103                         WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1104                         WriteAngle(MSG_ONE, veh.angles_y);        // yaw
1105                         WriteAngle(MSG_ONE, 0);                           // roll
1106                 }
1107         }
1108
1109         vehicles_clearreturn(veh);
1110
1111         CSQCVehicleSetup(pl, veh.vehicleid);
1112
1113         MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1114
1115         CSQCModel_UnlinkEntity(veh);
1116         info.vr_enter(info, veh);
1117
1118         antilag_clear(pl, CS(pl));
1119 }
1120
1121 void vehicles_think(entity this)
1122 {
1123         this.nextthink = time + autocvar_g_vehicles_thinkrate;
1124
1125         if(this.owner)
1126                 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1127
1128         Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1129         info.vr_think(info, this);
1130
1131         vehicles_painframe(this);
1132
1133         CSQCMODEL_AUTOUPDATE(this);
1134 }
1135
1136 void vehicles_reset(entity this)
1137 {
1138         if(this.owner)
1139                 vehicles_exit(this, VHEF_RELEASE);
1140
1141         vehicles_clearreturn(this);
1142
1143         if(this.active != ACTIVE_NOT)
1144                 vehicles_spawn(this);
1145 }
1146
1147 // initialization
1148 void vehicles_spawn(entity this)
1149 {
1150         LOG_DEBUG("Spawning vehicle: ", this.classname);
1151
1152         // disown & reset
1153         this.vehicle_hudmodel.viewmodelforclient = this;
1154
1155         this.owner                              = NULL;
1156         settouch(this, vehicles_touch);
1157         this.event_damage               = vehicles_damage;
1158         this.event_heal                 = vehicles_heal;
1159         this.reset                              = vehicles_reset;
1160         this.iscreature                 = true;
1161         this.teleportable               = false; // no teleporting for vehicles, too buggy
1162         this.damagedbycontents  = true;
1163         set_movetype(this, MOVETYPE_WALK);
1164         this.solid                              = SOLID_SLIDEBOX;
1165         this.takedamage                 = DAMAGE_AIM;
1166         this.deadflag                   = DEAD_NO;
1167         if(!this.bot_attack)
1168                 IL_PUSH(g_bot_targets, this);
1169         this.bot_attack                 = true;
1170         this.flags                              = FL_NOTARGET;
1171         this.avelocity                  = '0 0 0';
1172         this.velocity                   = '0 0 0';
1173         setthink(this, vehicles_think);
1174         this.nextthink                  = time;
1175
1176         // Reset locking
1177         this.lock_strength = 0;
1178         this.lock_target = NULL;
1179         this.misc_bulletcounter = 0;
1180
1181         // Return to spawn
1182         this.angles = this.pos2;
1183         setorigin(this, this.pos1);
1184         // Show it
1185         Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1186
1187         if(this.vehicle_controller)
1188                 this.team = this.vehicle_controller.team;
1189
1190         FOREACH_CLIENT(IS_PLAYER(it),
1191         {
1192                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1193             {
1194                 .entity weaponentity = weaponentities[slot];
1195                 if(it.(weaponentity).hook.aiment == this)
1196                         RemoveHook(it.(weaponentity).hook);
1197             }
1198         });
1199
1200
1201         Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1202         info.vr_spawn(info, this);
1203
1204         vehicles_reset_colors(this, NULL);
1205
1206         CSQCMODEL_AUTOINIT(this);
1207 }
1208
1209 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1210 {
1211         if(!autocvar_g_vehicles)
1212                 return false;
1213
1214         if(!info.vehicleid)
1215                 return false;
1216
1217         if(!this.tur_head)
1218                 info.vr_precache(info);
1219
1220         if(this.targetname && this.targetname != "")
1221         {
1222                 this.vehicle_controller = find(NULL, target, this.targetname);
1223                 if(!this.vehicle_controller)
1224                 {
1225                         LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1226                         this.active = ACTIVE_ACTIVE;
1227                 }
1228                 else
1229                 {
1230                         this.team = this.vehicle_controller.team;
1231                         this.use = vehicle_use;
1232
1233                         if(teamplay)
1234                         {
1235                                 if(this.vehicle_controller.team == 0)
1236                                         this.active = ACTIVE_NOT;
1237                                 else
1238                                         this.active = ACTIVE_ACTIVE;
1239                         }
1240                 }
1241         }
1242         else { this.active = ACTIVE_ACTIVE; }
1243
1244         if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1245                 this.team = 0;
1246
1247         if(this.mdl == "" || !this.mdl)
1248                 _setmodel(this, info.model);
1249         else
1250                 _setmodel(this, this.mdl);
1251
1252         this.vehicle_flags |= VHF_ISVEHICLE;
1253
1254         this.vehicle_viewport           = new(vehicle_viewport);
1255         this.vehicle_hudmodel           = new(vehicle_hudmodel);
1256         this.tur_head                           = new(tur_head);
1257         this.tur_head.owner                     = this;
1258         this.takedamage                         = DAMAGE_NO;
1259         this.bot_attack                         = true;
1260         IL_PUSH(g_bot_targets, this);
1261         this.iscreature                         = true;
1262         this.teleportable                       = false; // no teleporting for vehicles, too buggy
1263         this.damagedbycontents          = true;
1264         IL_PUSH(g_damagedbycontents, this);
1265         this.vehicleid                          = info.vehicleid;
1266         this.vehicledef                         = info;
1267         this.PlayerPhysplug                     = info.PlayerPhysplug;
1268         this.event_damage                       = func_null;
1269         this.event_heal                         = func_null;
1270         settouch(this, vehicles_touch);
1271         setthink(this, vehicles_spawn);
1272         this.nextthink                          = time;
1273         this.effects                            = EF_NODRAW;
1274         this.dphitcontentsmask          = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1275
1276         if(autocvar_g_playerclip_collisions)
1277                 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1278
1279         if(autocvar_g_nodepthtestplayers)
1280                 this.effects |= EF_NODEPTHTEST;
1281
1282         if(autocvar_g_fullbrightplayers)
1283                 this.effects |= EF_FULLBRIGHT;
1284
1285         _setmodel(this.vehicle_hudmodel, info.hud_model);
1286         setmodel(this.vehicle_viewport, MDL_Null);
1287
1288         if(info.head_model != "")
1289         {
1290                 _setmodel(this.tur_head, info.head_model);
1291                 setattachment(this.tur_head, this, info.tag_head);
1292                 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1293                 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1294         }
1295         else
1296         {
1297                 setattachment(this.tur_head, this, "");
1298                 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1299                 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1300         }
1301
1302         setsize(this, info.m_mins, info.m_maxs);
1303
1304         info.vr_setup(info, this);
1305
1306         if(!nodrop)
1307         {
1308                 setorigin(this, this.origin);
1309                 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1310                 setorigin(this, trace_endpos);
1311         }
1312
1313         this.pos1 = this.origin;
1314         this.pos2 = this.angles;
1315         this.tur_head.team = this.team;
1316
1317         if(this.active == ACTIVE_NOT)
1318                 this.nextthink = 0; // wait until activated
1319         else if(autocvar_g_vehicles_delayspawn)
1320                 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1321         else
1322                 this.nextthink = time + game_starttime;
1323
1324         if(MUTATOR_CALLHOOK(VehicleInit, this))
1325                 return false;
1326
1327         return true;
1328 }