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