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