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