]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/vehicles/sv_vehicles.qc
Merge branch 't0uYK8Ne/set_slick_friction' into 'master'
[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                 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
781                 {
782                         float wc = vlen(this.velocity - this.oldvelocity);
783                         float take = min(_speedfac * wc, _maxpain);
784                         Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
785                         this.play_time = time + 0.25;
786                 }
787         }
788 }
789
790 // vehicle enter/exit handling
791 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
792 {
793         // TODO: we actually want the player's size here
794         tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
795         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
796                 return prefer_spot;
797
798         float mysize = 1.5 * vlen(this.maxs - this.mins);
799         vector v;
800         vector v2 = 0.5 * (this.absmin + this.absmax);
801         for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
802         {
803                 v = randomvec();
804                 v_z = 0;
805                 v = v2 + normalize(v) * mysize;
806                 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
807                 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
808                         return v;
809         }
810
811         return this.origin;
812 }
813
814 .int old_vehicle_flags;
815 void vehicles_exit(entity vehic, bool eject)
816 {
817         entity player = vehic.owner;
818
819         if(vehicles_exit_running)
820         {
821                 LOG_TRACE("^1vehicles_exit already running! this is not good...");
822                 return;
823         }
824
825         vehicles_exit_running = true;
826
827         if(vehic.vehicle_flags & VHF_PLAYERSLOT)
828         {
829                 vehic.vehicle_exit(vehic, eject);
830                 vehicles_exit_running = false;
831                 return;
832         }
833
834         if (player)
835         {
836                 if (IS_REAL_CLIENT(player))
837                 {
838                         msg_entity = player;
839                         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
840                         WriteEntity( MSG_ONE, player);
841
842                         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
843                         WriteAngle(MSG_ONE, 0);
844                         WriteAngle(MSG_ONE, vehic.angles_y);
845                         WriteAngle(MSG_ONE, 0);
846                 }
847
848                 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
849
850                 player.takedamage               = DAMAGE_AIM;
851                 player.solid                    = SOLID_SLIDEBOX;
852                 set_movetype(player, MOVETYPE_WALK);
853                 player.effects             &= ~EF_NODRAW;
854                 player.teleportable             = TELEPORT_NORMAL;
855                 player.alpha                    = 1;
856                 player.PlayerPhysplug   = func_null;
857                 player.vehicle                  = NULL;
858                 player.view_ofs                 = STAT(PL_VIEW_OFS, player);
859                 player.event_damage             = PlayerDamage;
860                 STAT(HUD, player)               = HUD_NORMAL;
861                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
862                 {
863                         .entity weaponentity = weaponentities[slot];
864                         player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
865                         delete(vehic.(weaponentity)); // no longer needed
866                 }
867                 player.last_vehiclecheck = time + 3;
868                 player.vehicle_enter_delay = time + 2;
869
870                 CSQCVehicleSetup(player, HUD_NORMAL);
871
872                 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
873                 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
874         }
875
876         vehic.flags |= FL_NOTARGET;
877
878         if(!IS_DEAD(vehic))
879                 vehic.avelocity = '0 0 0';
880
881         vehic.tur_head.nodrawtoclient = NULL;
882
883         if(!teamplay)
884                 vehic.team = 0;
885
886         WaypointSprite_Kill(vehic.wps_intruder);
887
888         MUTATOR_CALLHOOK(VehicleExit, player, vehic);
889
890         vehic.team = vehic.tur_head.team;
891
892         if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
893                 vehic.vehicle_flags |= VHF_SHIELDREGEN;
894         vehic.old_vehicle_flags = 0;
895
896         sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
897         vehic.vehicle_hudmodel.viewmodelforclient = vehic;
898         vehic.phase = time + 1;
899
900         vehic.vehicle_exit(vehic, eject);
901
902         vehicles_setreturn(vehic);
903         vehicles_reset_colors(vehic, NULL);
904         vehic.owner = NULL;
905
906         CSQCMODEL_AUTOINIT(vehic);
907
908         if(player)
909                 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
910
911         vehicles_exit_running = false;
912 }
913
914 void vehicles_touch(entity this, entity toucher)
915 {
916         if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
917                 return;
918
919         // Vehicle currently in use
920         if(this.owner)
921         {
922                 if(toucher != NULL)
923                 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
924                 if(vehicles_crushable(toucher))
925                 if(!weaponLocked(this.owner))
926                 {
927                         if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
928                                 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);
929
930                         return; // Dont do selfdamage when hitting "soft targets".
931                 }
932
933                 if(this.play_time < time) {
934                         Vehicle info = Vehicles_from(this.vehicleid);
935                         info.vr_impact(info, this);
936                 }
937
938                 return;
939         }
940
941         if(autocvar_g_vehicles_enter)
942                 return;
943
944         vehicles_enter(toucher, this);
945 }
946
947 bool vehicle_impulse(entity this, int imp)
948 {
949         entity v = this.vehicle;
950         if (!v) return false;
951         if (IS_DEAD(v)) return false;
952         bool(entity,int) f = v.vehicles_impulse;
953         if (f && f(this,imp)) return true;
954         switch (imp)
955         {
956                 case IMP_weapon_drop.impulse:
957                 {
958                         stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
959                         return true;
960                 }
961         }
962         return false;
963 }
964
965 void vehicles_enter(entity pl, entity veh)
966 {
967         // Remove this when bots know how to use vehicles
968         if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
969                 return;
970
971         if((!IS_PLAYER(pl))
972         || (veh.phase >= time)
973         || (pl.vehicle_enter_delay >= time)
974         || (STAT(FROZEN, pl))
975         || (IS_DEAD(pl))
976         || (pl.vehicle)
977         ) { return; }
978
979         Vehicle info = Vehicles_from(veh.vehicleid);
980
981         if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
982         if(veh.vehicle_flags & VHF_MULTISLOT)
983         if(veh.owner && SAME_TEAM(pl, veh))
984         {
985                 // we don't need a return value or anything here
986                 // if successful the owner check below will prevent anything weird
987                 info.vr_gunner_enter(info, veh, pl);
988         }
989
990         if(veh.owner)
991                 return; // got here and didn't enter the gunner, return
992
993         if(teamplay)
994         if(veh.team)
995         if(DIFF_TEAM(pl, veh))
996         if(autocvar_g_vehicles_steal)
997         {
998                 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
999
1000                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1001
1002                 veh.vehicle_shield = 0;
1003                 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1004                 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1005
1006                 if (autocvar_g_vehicles_steal_show_waypoint) {
1007                         entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1008                         wp.colormod = Team_ColorRGB(pl.team);
1009                 }
1010         }
1011         else return;
1012
1013         RemoveGrapplingHooks(pl);
1014
1015         veh.vehicle_ammo1 = 0;
1016         veh.vehicle_ammo2 = 0;
1017         veh.vehicle_reload1 = 0;
1018         veh.vehicle_reload2 = 0;
1019         veh.vehicle_energy = 0;
1020
1021         veh.owner = pl;
1022         pl.vehicle = veh;
1023
1024         // .viewmodelforclient works better.
1025         //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1026
1027         veh.vehicle_hudmodel.viewmodelforclient = pl;
1028
1029         pl.crouch = false;
1030         pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1031         setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1032
1033         veh.event_damage        = vehicles_damage;
1034         veh.event_heal          = vehicles_heal;
1035         veh.nextthink           = 0;
1036         pl.items &= ~IT_USING_JETPACK;
1037         pl.angles                       = veh.angles;
1038         pl.takedamage           = DAMAGE_NO;
1039         pl.solid                        = SOLID_NOT;
1040         pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1041         set_movetype(pl, MOVETYPE_NOCLIP);
1042         pl.teleportable         = false;
1043         pl.alpha                        = -1;
1044         pl.event_damage         = func_null;
1045         pl.view_ofs                     = '0 0 0';
1046         veh.colormap            = pl.colormap;
1047         if(veh.tur_head)
1048                 veh.tur_head.colormap = pl.colormap;
1049         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1050         {
1051                 .entity weaponentity = weaponentities[slot];
1052                 veh.(weaponentity) = new(temp_wepent);
1053                 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1054         }
1055         STAT(HUD, pl) = veh.vehicleid;
1056         pl.PlayerPhysplug = veh.PlayerPhysplug;
1057
1058         pl.vehicle_ammo1 = veh.vehicle_ammo1;
1059         pl.vehicle_ammo2 = veh.vehicle_ammo2;
1060         pl.vehicle_reload1 = veh.vehicle_reload1;
1061         pl.vehicle_reload2 = veh.vehicle_reload2;
1062         pl.vehicle_energy = veh.vehicle_energy;
1063
1064         // Cant do this, hides attached objects too.
1065         //veh.exteriormodeltoclient = veh.owner;
1066         //veh.tur_head.exteriormodeltoclient = veh.owner;
1067
1068         UNSET_ONGROUND(pl);
1069         UNSET_ONGROUND(veh);
1070
1071         veh.team = pl.team;
1072         veh.flags -= FL_NOTARGET;
1073
1074         vehicles_reset_colors(veh, pl);
1075
1076         if (IS_REAL_CLIENT(pl))
1077         {
1078                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1079
1080                 msg_entity = pl;
1081                 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1082                 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1083
1084                 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1085                 if(veh.tur_head)
1086                 {
1087                         WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1088                         WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1089                         WriteAngle(MSG_ONE, 0);                                                                   // roll
1090                 }
1091                 else
1092                 {
1093                         WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1094                         WriteAngle(MSG_ONE, veh.angles_y);        // yaw
1095                         WriteAngle(MSG_ONE, 0);                           // roll
1096                 }
1097         }
1098
1099         vehicles_clearreturn(veh);
1100
1101         CSQCVehicleSetup(pl, veh.vehicleid);
1102
1103         MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1104
1105         CSQCModel_UnlinkEntity(veh);
1106         info.vr_enter(info, veh);
1107
1108         antilag_clear(pl, CS(pl));
1109 }
1110
1111 void vehicles_think(entity this)
1112 {
1113         this.nextthink = time + autocvar_g_vehicles_thinkrate;
1114
1115         if(this.owner)
1116                 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1117
1118         Vehicle info = Vehicles_from(this.vehicleid);
1119         info.vr_think(info, this);
1120
1121         vehicles_painframe(this);
1122
1123         CSQCMODEL_AUTOUPDATE(this);
1124 }
1125
1126 void vehicles_reset(entity this)
1127 {
1128         if(this.owner)
1129                 vehicles_exit(this, VHEF_RELEASE);
1130
1131         vehicles_clearreturn(this);
1132
1133         if(this.active != ACTIVE_NOT)
1134                 vehicles_spawn(this);
1135 }
1136
1137 // initialization
1138 void vehicles_spawn(entity this)
1139 {
1140         LOG_DEBUG("Spawning vehicle: ", this.classname);
1141
1142         // disown & reset
1143         this.vehicle_hudmodel.viewmodelforclient = this;
1144
1145         this.owner                              = NULL;
1146         settouch(this, vehicles_touch);
1147         this.event_damage               = vehicles_damage;
1148         this.event_heal                 = vehicles_heal;
1149         this.reset                              = vehicles_reset;
1150         this.iscreature                 = true;
1151         this.teleportable               = false; // no teleporting for vehicles, too buggy
1152         this.damagedbycontents  = true;
1153         set_movetype(this, MOVETYPE_WALK);
1154         this.solid                              = SOLID_SLIDEBOX;
1155         this.takedamage                 = DAMAGE_AIM;
1156         this.deadflag                   = DEAD_NO;
1157         if(!this.bot_attack)
1158                 IL_PUSH(g_bot_targets, this);
1159         this.bot_attack                 = true;
1160         this.flags                              = FL_NOTARGET;
1161         this.avelocity                  = '0 0 0';
1162         this.velocity                   = '0 0 0';
1163         setthink(this, vehicles_think);
1164         this.nextthink                  = time;
1165
1166         // Reset locking
1167         this.lock_strength = 0;
1168         this.lock_target = NULL;
1169         this.misc_bulletcounter = 0;
1170
1171         // Return to spawn
1172         this.angles = this.pos2;
1173         setorigin(this, this.pos1);
1174         // Show it
1175         Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1176
1177         if(this.vehicle_controller)
1178                 this.team = this.vehicle_controller.team;
1179
1180         FOREACH_CLIENT(IS_PLAYER(it),
1181         {
1182                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1183             {
1184                 .entity weaponentity = weaponentities[slot];
1185                 if(it.(weaponentity).hook.aiment == this)
1186                         RemoveHook(it.(weaponentity).hook);
1187             }
1188         });
1189
1190
1191         Vehicle info = Vehicles_from(this.vehicleid);
1192         info.vr_spawn(info, this);
1193
1194         vehicles_reset_colors(this, NULL);
1195
1196         CSQCMODEL_AUTOINIT(this);
1197 }
1198
1199 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1200 {
1201         if(!autocvar_g_vehicles)
1202                 return false;
1203
1204         if(!info.vehicleid)
1205                 return false;
1206
1207         if(!this.tur_head)
1208         {
1209                 info.vr_precache(info);
1210                 IL_PUSH(g_vehicles, this);
1211         }
1212
1213         if(this.targetname && this.targetname != "")
1214         {
1215                 this.vehicle_controller = find(NULL, target, this.targetname);
1216                 if(!this.vehicle_controller)
1217                 {
1218                         LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1219                         this.active = ACTIVE_ACTIVE;
1220                 }
1221                 else
1222                 {
1223                         this.team = this.vehicle_controller.team;
1224                         this.use = vehicle_use;
1225
1226                         if(teamplay)
1227                         {
1228                                 if(this.vehicle_controller.team == 0)
1229                                         this.active = ACTIVE_NOT;
1230                                 else
1231                                         this.active = ACTIVE_ACTIVE;
1232                         }
1233                 }
1234         }
1235         else { this.active = ACTIVE_ACTIVE; }
1236
1237         if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1238                 this.team = 0;
1239
1240         if(this.mdl == "" || !this.mdl)
1241                 _setmodel(this, info.model);
1242         else
1243                 _setmodel(this, this.mdl);
1244
1245         this.vehicle_flags |= VHF_ISVEHICLE;
1246
1247         this.vehicle_viewport           = new(vehicle_viewport);
1248         this.vehicle_hudmodel           = new(vehicle_hudmodel);
1249         this.tur_head                           = new(tur_head);
1250         this.tur_head.owner                     = this;
1251         this.takedamage                         = DAMAGE_NO;
1252         this.bot_attack                         = true;
1253         IL_PUSH(g_bot_targets, this);
1254         this.iscreature                         = true;
1255         this.teleportable                       = false; // no teleporting for vehicles, too buggy
1256         this.damagedbycontents          = true;
1257         IL_PUSH(g_damagedbycontents, this);
1258         this.vehicleid                          = info.vehicleid;
1259         this.PlayerPhysplug                     = info.PlayerPhysplug;
1260         this.event_damage                       = func_null;
1261         this.event_heal                         = func_null;
1262         settouch(this, vehicles_touch);
1263         setthink(this, vehicles_spawn);
1264         this.nextthink                          = time;
1265         this.effects                            = EF_NODRAW;
1266         this.dphitcontentsmask          = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1267
1268         if(autocvar_g_playerclip_collisions)
1269                 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1270
1271         if(autocvar_g_nodepthtestplayers)
1272                 this.effects |= EF_NODEPTHTEST;
1273
1274         if(autocvar_g_fullbrightplayers)
1275                 this.effects |= EF_FULLBRIGHT;
1276
1277         _setmodel(this.vehicle_hudmodel, info.hud_model);
1278         setmodel(this.vehicle_viewport, MDL_Null);
1279
1280         if(info.head_model != "")
1281         {
1282                 _setmodel(this.tur_head, info.head_model);
1283                 setattachment(this.tur_head, this, info.tag_head);
1284                 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1285                 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1286         }
1287         else
1288         {
1289                 setattachment(this.tur_head, this, "");
1290                 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1291                 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1292         }
1293
1294         setsize(this, info.m_mins, info.m_maxs);
1295
1296         info.vr_setup(info, this);
1297
1298         if(!nodrop)
1299         {
1300                 setorigin(this, this.origin);
1301                 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1302                 setorigin(this, trace_endpos);
1303         }
1304
1305         this.pos1 = this.origin;
1306         this.pos2 = this.angles;
1307         this.tur_head.team = this.team;
1308
1309         if(this.active == ACTIVE_NOT)
1310                 this.nextthink = 0; // wait until activated
1311         else if(autocvar_g_vehicles_delayspawn)
1312                 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1313         else
1314                 this.nextthink = time + game_starttime;
1315
1316         if(MUTATOR_CALLHOOK(VehicleInit, this))
1317                 return false;
1318
1319         return true;
1320 }