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