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