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