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