]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/vehicles/vehicles.qc
Merge remote-tracking branch 'origin/atheros/item_keys'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / vehicles / vehicles.qc
1 float autocvar_g_vehicles_crush_dmg;
2 float autocvar_g_vehicles_crush_force;
3 float autocvar_g_vehicles_delayspawn;
4 float autocvar_g_vehicles_delayspawn_jitter;
5 float autocvar_g_vehicles_allow_flagcarry;
6
7 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
8 void vehicles_return();
9 void vehicles_enter();
10 void vehicles_touch();
11 void vehicles_reset_colors();
12 void vehicles_clearrturn();
13 void vehicles_setreturn();
14
15
16 /** AuxiliaryXhair*
17     Send additional points of interest to be drawn, to vehicle owner
18 **/
19 float MAX_AXH = 4;
20 .entity AuxiliaryXhair[MAX_AXH];
21
22 float SendAuxiliaryXhair(entity to, float sf)
23 {
24
25         WriteByte(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
26
27         WriteByte(MSG_ENTITY, self.cnt);
28
29         WriteCoord(MSG_ENTITY, self.origin_x);
30         WriteCoord(MSG_ENTITY, self.origin_y);
31         WriteCoord(MSG_ENTITY, self.origin_z);
32
33     WriteByte(MSG_ENTITY, rint(self.colormod_x * 255));
34     WriteByte(MSG_ENTITY, rint(self.colormod_y * 255));
35     WriteByte(MSG_ENTITY, rint(self.colormod_z * 255));
36
37     return TRUE;
38 }
39
40 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
41 {
42     entity axh;
43
44     axh_id = bound(0, axh_id, MAX_AXH);
45     axh = own.(AuxiliaryXhair[axh_id]);
46
47     if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
48     {
49         axh                     = spawn();
50         axh.cnt                 = axh_id;
51         axh.drawonlytoclient    = own;
52         axh.owner               = own;
53         Net_LinkEntity(axh, FALSE, 0, SendAuxiliaryXhair);
54     }
55
56     setorigin(axh, loc);
57     axh.colormod            = clr;
58     axh.SendFlags           = 0x01;
59     own.(AuxiliaryXhair[axh_id]) = axh;
60 }
61
62 /*
63 // SVC_TEMPENTITY based, horrible with even 50 ping. hm.
64 // WriteByte(MSG_ONE, SVC_TEMPENTITY) uses reliable messagess, never use for thinsg that need continous updates.
65 void SendAuxiliaryXhair2(entity own, vector loc, vector clr, float axh_id)
66 {
67         msg_entity = own;
68
69         WriteByte(MSG_ONE, SVC_TEMPENTITY);
70         WriteByte(MSG_ONE, TE_CSQC_AUXILIARYXHAIR);
71
72         WriteByte(MSG_ONE, axh_id);
73
74         WriteCoord(MSG_ONE, loc_x);
75         WriteCoord(MSG_ONE, loc_y);
76         WriteCoord(MSG_ONE, loc_z);
77
78     WriteByte(MSG_ONE, rint(clr_x * 255));
79     WriteByte(MSG_ONE, rint(clr_y * 255));
80     WriteByte(MSG_ONE, rint(clr_z * 255));
81
82 }
83 */
84 // End AuxiliaryXhair
85
86 /**
87     Notifies the client that he enterd a vehicle, and sends 
88     realavent data.
89     
90     only sends vehicle_id atm (wich is a HUD_* constant, ex. HUD_SPIDERBOT)
91 **/
92 void CSQCVehicleSetup(entity own, float vehicle_id)
93 {
94         msg_entity = own;
95
96         WriteByte(MSG_ONE, SVC_TEMPENTITY);
97         WriteByte(MSG_ONE, TE_CSQC_VEHICLESETUP);
98         WriteByte(MSG_ONE, vehicle_id);
99 }
100
101 /** vehicles_locktarget
102
103     Generic target locking.
104
105     Figure out if what target is "locked" (if any), for missile tracking as such.
106
107     after calling, "if(self.lock_target != world && self.lock_strength == 1)" mean
108     you have a locked in target.
109
110     Exspects a crosshair_trace() or equivalent to be
111     dont before calling.
112
113 **/
114 void vh_crosshair_trace(entity player)
115 {
116     float lag; 
117     entity pl;
118     
119     lag = ANTILAG_LATENCY(self);
120     if(lag < 0.001)
121         lag = 0;
122     if(clienttype(self) != CLIENTTYPE_REAL)
123         lag = 0;
124     if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
125         lag = 0; // only do hitscan, but no antilag
126
127     if(lag)
128         FOR_EACH_PLAYER(pl)
129             if(pl != self)
130             {
131                 antilag_takeback(pl, time - lag);
132                 if(pl.vehicle)
133                     antilag_takeback(pl.vehicle, time - lag);
134
135             }
136
137     crosshair_trace(player);
138
139     if(lag)
140         FOR_EACH_PLAYER(pl)
141             if(pl != self)
142             {
143                 antilag_restore(pl);
144                 if(pl.vehicle)
145                     antilag_restore(pl.vehicle);
146             }
147 }
148 .entity lock_target;
149 .float  lock_strength;
150 .float  lock_time;
151 .float  lock_soundtime;
152 void vehicles_locktarget(float incr, float decr, float _lock_time)
153 {
154     if(self.lock_target && self.lock_target.deadflag != DEAD_NO)
155     {
156         self.lock_target    = world;
157         self.lock_strength  = 0;
158         self.lock_time      = 0;
159     }
160
161     if(self.lock_time > time)
162     {
163         if(self.lock_target)
164         if(self.lock_soundtime < time)
165         {
166             self.lock_soundtime = time + 0.5;
167             play2(self.owner, "vehicles/locked.wav");
168         }
169         
170         return;
171     }
172
173     if(trace_ent != world)
174     {
175         if(teamplay && trace_ent.team == self.team)
176             trace_ent = world;
177
178         if(trace_ent.deadflag != DEAD_NO)
179             trace_ent = world;
180
181         if not (trace_ent.vehicle_flags & VHF_ISVEHICLE || trace_ent.turrcaps_flags & TFL_TURRCAPS_ISTURRET || trace_ent.classname == "player")
182             trace_ent = world;
183     }
184
185     if(self.lock_target == world && trace_ent != world)
186         self.lock_target = trace_ent;
187     
188     if(self.lock_target && trace_ent == self.lock_target) 
189     {            
190         if(self.lock_strength != 1 && self.lock_strength + incr >= 1)
191         {
192             play2(self.owner, "vehicles/lock.wav");
193             self.lock_soundtime = time + 0.8;
194         }        
195         else if (self.lock_strength != 1 && self.lock_soundtime < time)
196         {            
197             play2(self.owner, "vehicles/locking.wav");
198             self.lock_soundtime = time + 0.3;
199         }
200         
201     }    
202         
203     // Have a locking target
204     // Trace hit current target
205     if(trace_ent == self.lock_target && trace_ent != world)
206     {
207         self.lock_strength = min(self.lock_strength + incr, 1);
208         if(self.lock_strength == 1)
209             self.lock_time = time + _lock_time;
210     }
211     else
212     {
213         if(trace_ent)
214             self.lock_strength = max(self.lock_strength - decr * 2, 0);
215         else
216             self.lock_strength = max(self.lock_strength - decr, 0);
217
218         if(self.lock_strength == 0)
219             self.lock_target = world;
220     }
221 }
222
223 #define VEHICLE_UPDATE_PLAYER(fld,vhname) \
224 self.owner.vehicle_##fld = (self.vehicle_##fld / autocvar_g_vehicle_##vhname##_##fld) * 100
225
226 #define vehicles_sweap_collision(orig,vel,dt,acm,mult) \
227 traceline(orig, orig + vel * dt, MOVE_NORMAL, self); \
228 if(trace_fraction != 1) \
229     acm += normalize(self.origin - trace_endpos) * (vlen(vel) * mult)
230
231 // Hover movement support
232 float  force_fromtag_power;
233 float  force_fromtag_normpower;
234 vector force_fromtag_origin;
235 vector vehicles_force_fromtag_hover(string tag_name, float spring_length, float max_power)
236 {
237     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
238     v_forward  = normalize(v_forward) * -1;
239     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
240
241     force_fromtag_power = (1 - trace_fraction) * max_power;
242     force_fromtag_normpower = force_fromtag_power / max_power;
243
244     return v_forward  * force_fromtag_power;
245 }
246
247 // Experimental hovermode wich uses attraction/repulstion from surface insted of gravity/repulsion
248 // Can possibly be use to move abt any surface (inclusing walls/celings)
249 vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float max_power)
250 {
251
252     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
253     v_forward  = normalize(v_forward) * -1;
254     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
255
256     // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
257     if(trace_fraction == 1.0)
258     {
259         force_fromtag_normpower = -0.25;
260         return '0 0 -200';
261     }
262
263     force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
264     force_fromtag_normpower = force_fromtag_power / max_power;
265
266     return v_forward  * force_fromtag_power;
267 }
268
269 // Generic vehile projectile system
270 void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
271 {
272     // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
273     if(inflictor.owner == self.owner)
274         return; 
275     
276     self.health -= damage;
277     self.velocity += force;
278     if(self.health < 1)
279     {
280         self.takedamage = DAMAGE_NO;
281         self.event_damage = SUB_Null;
282         self.think = self.use;
283         self.nextthink = time;
284     }
285
286 }
287
288 void vehicles_projectile_explode()
289 {
290     if(self.owner && other != world)
291     {
292         if(other == self.owner.vehicle)
293             return;
294
295         if(other == self.owner.vehicle.tur_head)
296             return;
297     }
298
299         PROJECTILE_TOUCH;
300
301         self.event_damage = SUB_Null;
302     RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, self.shot_force, self.totalfrags, other);
303
304     remove (self);
305 }
306
307 entity vehicles_projectile(string _mzlfx, string _mzlsound,
308                            vector _org, vector _vel,
309                            float _dmg, float _radi, float _force,  float _size,
310                            float _deahtype, float _projtype, float _health,
311                            float _cull, float _clianim)
312 {
313     entity proj;
314
315     proj = spawn();
316
317     PROJECTILE_MAKETRIGGER(proj);
318     setorigin(proj, _org);
319
320     proj.shot_dmg         = _dmg;
321     proj.shot_radius      = _radi;
322     proj.shot_force       = _force;
323     proj.totalfrags       = _deahtype;
324     proj.solid            = SOLID_BBOX;
325     proj.movetype         = MOVETYPE_FLYMISSILE;
326     proj.flags            = FL_PROJECTILE;
327     proj.bot_dodge        = TRUE;
328     proj.bot_dodgerating  = _dmg;
329     proj.velocity         = _vel;
330     proj.touch            = vehicles_projectile_explode;
331     proj.use              = vehicles_projectile_explode;
332     proj.owner            = self;
333     proj.realowner        = self.owner;
334     proj.think            = SUB_Remove;
335     proj.nextthink        = time + 30;
336
337     if(_health)
338     {
339         proj.takedamage       = DAMAGE_AIM;
340         proj.event_damage     = vehicles_projectile_damage;
341         proj.health           = _health;
342     }
343     else
344         proj.flags           = FL_PROJECTILE | FL_NOTARGET;
345
346     if(_mzlsound)
347         sound (self, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTN_NORM);
348
349     if(_mzlfx)
350         pointparticles(particleeffectnum(_mzlfx), proj.origin, proj.velocity, 1);
351
352
353     setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
354
355     CSQCProjectile(proj, _clianim, _projtype, _cull);
356
357     return proj;
358 }
359 // End generic vehile projectile system
360
361 /** vehicles_spawn
362     Exetuted for all vehicles on (re)spawn.
363     Sets defaults for newly spawned units.
364 **/
365 void vehicles_spawn()
366 {
367     dprint("Spawning vehicle: ", self.netname, "\n");
368
369     // De-own & reset
370     self.vehicle_hudmodel.viewmodelforclient = self;
371
372     self.owner              = world;
373     self.touch              = vehicles_touch;
374     self.event_damage       = vehicles_damage;
375     self.iscreature         = TRUE;
376     self.damagedbycontents      = TRUE;
377     self.movetype           = MOVETYPE_WALK;
378     self.solid              = SOLID_SLIDEBOX;
379     self.takedamage         = DAMAGE_AIM;
380         self.deadflag           = DEAD_NO;
381     self.bot_attack         = TRUE;
382     self.flags              = FL_NOTARGET;
383     self.avelocity          = '0 0 0';
384     self.velocity           = '0 0 0';
385
386     // Reset locking
387     self.lock_strength      = 0;
388     self.lock_target        = world;
389     self.misc_bulletcounter = 0;
390
391     // Return to spawn
392     self.angles             = self.pos2;
393     setorigin(self, self.pos1 + '0 0 128');
394     // Show it
395     pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
396
397     vehicles_reset_colors();
398     self.vehicle_spawn();
399 }
400
401 // Better way of determening whats crushable needed! (fl_crushable?)
402 float vehicles_crushable(entity e)
403 {
404     if(e.classname == "player")
405         return TRUE;
406
407     if(e.classname == "monster_zombie")
408         return TRUE;
409
410     return FALSE;
411 }
412
413 void vehicles_touch()
414 {
415     // Vehicle currently in use
416     if(self.owner)
417     {
418         // Colided with world?
419         if(other == world)
420         {
421         }
422         else
423         {
424             if(other.vehicle_flags & VHF_ISVEHICLE)
425             {
426                 //other.velocity += self.velocity * (self.mass / other.mass);
427             }
428             else if(vehicles_crushable(other))
429             {
430                 if(vlen(self.velocity) != 0)
431                     Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_VHCRUSH, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
432             }
433         }
434         return;
435     }
436
437     if(other.classname != "player")
438         return;
439
440     if(other.deadflag != DEAD_NO)
441         return;
442
443     if(other.vehicle != world)
444         return;
445
446     // Remove this when bots know how to use vehicles.
447     if (clienttype(other) != CLIENTTYPE_REAL)
448         return;
449
450     vehicles_enter();
451 }
452
453 void vehicles_enter()
454 {
455    // Remove this when bots know how to use vehicles
456     if (clienttype(other) != CLIENTTYPE_REAL)
457         return;
458
459     if(self.phase > time)
460         return;
461
462     if(teamplay)
463     if(self.team)
464     if(self.team != other.team)
465         return;
466         
467     RemoveGrapplingHook(other);
468
469     self.vehicle_ammo1   = 0;
470     self.vehicle_ammo2   = 0;
471     self.vehicle_reload1 = 0;
472     self.vehicle_reload2 = 0;
473     self.vehicle_energy  = 0;
474
475     self.owner          = other;
476     self.switchweapon   = other.switchweapon;
477
478     // .viewmodelforclient works better.
479     //self.vehicle_hudmodel.drawonlytoclient = self.owner;
480
481     self.vehicle_hudmodel.viewmodelforclient = self.owner;
482
483     self.event_damage         = vehicles_damage;
484     self.nextthink            = 0;
485     self.owner.angles         = self.angles;
486     self.owner.takedamage     = DAMAGE_NO;
487     self.owner.solid          = SOLID_NOT;
488     self.owner.movetype       = MOVETYPE_NOCLIP;
489     self.owner.alpha          = -1;
490     self.owner.vehicle        = self;
491     self.owner.event_damage   = SUB_Null;
492     self.owner.view_ofs       = '0 0 0';
493     self.colormap             = self.owner.colormap;
494     if(self.tur_head)
495         self.tur_head.colormap    = self.owner.colormap;
496
497     self.owner.hud            = self.hud;
498     self.owner.PlayerPhysplug = self.PlayerPhysplug;
499
500     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
501     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
502     self.owner.vehicle_reload1  = self.vehicle_reload1;
503     self.owner.vehicle_reload2  = self.vehicle_reload2;
504
505     // Cant do this, hides attached objects too.
506     //self.exteriormodeltoclient = self.owner;
507     //self.tur_head.exteriormodeltoclient = self.owner;
508
509     other.flags &~= FL_ONGROUND;
510     self.flags  &~= FL_ONGROUND;
511
512     self.team                 = self.owner.team;
513     self.flags               -= FL_NOTARGET;
514
515     msg_entity = other;
516     WriteByte (MSG_ONE, SVC_SETVIEWPORT);
517     WriteEntity(MSG_ONE, self.vehicle_viewport);
518
519     WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
520     if(self.tur_head)
521     {
522         WriteAngle(MSG_ONE, self.tur_head.angles_x + self.angles_x); // tilt
523         WriteAngle(MSG_ONE, self.tur_head.angles_y + self.angles_y); // yaw
524         WriteAngle(MSG_ONE, 0);                                      // roll
525     }
526     else
527     {
528         WriteAngle(MSG_ONE,  self.angles_x * -1); // tilt
529         WriteAngle(MSG_ONE,  self.angles_y);      // yaw
530         WriteAngle(MSG_ONE,  0);                  // roll
531     }
532
533     vehicles_clearrturn();
534
535     CSQCVehicleSetup(self.owner, self.hud);
536     
537     if(other.flagcarried)
538     {
539         if(!autocvar_g_vehicles_allow_flagcarry)
540             DropFlag(other.flagcarried, world, world);
541         else
542         {            
543             other.flagcarried.scale = 1;
544             setattachment(other.flagcarried, self, ""); 
545             setorigin(other, '0 0 96');
546         }
547     }
548     
549     self.vehicle_enter();
550 }
551
552 /** vehicles_findgoodexit
553     Locates a valid location for the player to exit the vehicle.
554     Will first try prefer_spot, then up 100 random spots arround the vehicle
555     wich are in direct line of sight and empty enougth to hold a players bbox
556 **/
557 vector vehicles_findgoodexit(vector prefer_spot)
558 {
559     //vector exitspot;
560     float mysize;
561     
562     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
563     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
564         return prefer_spot;
565     
566     mysize = vlen(self.maxs - self.mins);
567     float i;
568     vector v, v2;
569     v2 = 0.5 * (self.absmin + self.absmax);
570     for(i = 0; i < 100; ++i)
571     {        
572         v = randomvec();
573         v_z = 0;
574         v = v2 + normalize(v) * mysize;
575         tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
576         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
577             return v;
578     }
579     
580     /*
581     exitspot = (self.origin + '0 0 48') + v_forward * mysize;
582     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
583     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
584         return exitspot;
585     
586     exitspot = (self.origin + '0 0 48') - v_forward * mysize;
587     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
588     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
589         return exitspot;
590
591     exitspot = (self.origin + '0 0 48') + v_right * mysize;
592     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
593     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
594         return exitspot;
595     
596     exitspot = (self.origin + '0 0 48') - v_right * mysize;
597     tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
598     if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
599         return exitspot;
600     */
601     
602     return self.origin;
603 }
604
605 /** vehicles_exit
606     Standarrd vehicle release fucntion.
607     custom code goes in self.vehicle_exit
608 **/
609 void vehicles_exit(float eject)
610 {       
611     entity oldself;
612     if(self.flags & FL_CLIENT)
613     {
614         oldself = self;
615         self = self.vehicle;
616     }
617     
618         self.flags |= FL_NOTARGET;
619
620     if (self.owner)
621     {
622         msg_entity = self.owner;
623         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
624         WriteEntity( MSG_ONE, self.owner);
625
626         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
627         WriteAngle(MSG_ONE, 0);                 // pich
628         WriteAngle(MSG_ONE, self.angles_y);     // yaw
629         WriteAngle(MSG_ONE, 0);                 // roll
630
631         setsize(self.owner, PL_MIN,PL_MAX);
632
633         self.owner.takedamage     = DAMAGE_AIM;
634         self.owner.solid          = SOLID_SLIDEBOX;
635         self.owner.movetype       = MOVETYPE_WALK;
636         self.owner.effects        &~= EF_NODRAW;
637         self.owner.alpha          = 1;
638         self.owner.PlayerPhysplug = SUB_Null;
639         self.owner.vehicle        = world;
640         self.owner.view_ofs       = PL_VIEW_OFS;
641         self.owner.event_damage   = PlayerDamage;
642         self.owner.hud            = HUD_NORMAL;
643         self.owner.switchweapon   = self.switchweapon;
644         //self.owner.BUTTON_USE     = 0;
645     }
646
647     if(self.deadflag == DEAD_NO)
648         self.avelocity          = '0 0 0';
649
650     self.vehicle_hudmodel.viewmodelforclient = self;
651         self.tur_head.nodrawtoclient             = world;
652     vehicles_setreturn();
653
654     self.phase = time + 1;
655
656     if(!teamplay)
657         self.team = 0;
658     else
659         self.team = self.tur_head.team;
660     
661     if(self.owner.flagcarried)
662     {
663         self.owner.flagcarried.scale = 0.6;
664         setattachment(self.owner.flagcarried, self.owner, ""); 
665         setorigin(self.owner.flagcarried, FLAG_CARRY_POS);
666     }
667     
668     sound (self, CH_TRIGGER_SINGLE, "misc/null.wav", 1, ATTN_NORM);
669     self.vehicle_exit(eject);
670     self.owner = world;
671     vehicles_reset_colors();
672     
673     if(oldself)
674         self = oldself;
675 }
676
677
678 void vehicles_regen(.float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time)
679 {
680     if(self.regen_field < field_max)
681     if(self.timer + rpause < time)
682     {
683         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
684
685         if(self.owner)
686             self.owner.regen_field = (self.regen_field / field_max) * 100;
687     }
688 }
689
690 void shieldhit_think()
691 {
692     self.alpha -= 0.1;
693     if (self.alpha <= 0)
694     {
695         //setmodel(self, "");
696         self.alpha = -1;
697     }
698     else
699     {
700         self.nextthink = time + 0.1;
701     }
702 }
703
704 void vehicles_painframe()
705 {
706 //.float        pain_finished;                  //Added by Supajoe
707     
708     if(self.owner.vehicle_health <= 50)
709     if(self.pain_frame < time)
710     {  
711         float _ftmp;  
712         _ftmp = self.owner.vehicle_health / 50;
713         self.pain_frame = time + 0.1 + (random() * 0.5 * _ftmp);
714         pointparticles(particleeffectnum("smoke_small"), (self.origin + (randomvec() * 80)), '0 0 0', 1);
715         
716         if(self.vehicle_flags & VHF_DMGSHAKE)
717             self.velocity += randomvec() * 30;
718         
719         if(self.vehicle_flags & VHF_DMGROLL)
720             if(self.vehicle_flags & VHF_DMGHEADROLL)
721                 self.tur_head.angles += randomvec();
722             else
723                 self.angles += randomvec();
724         
725     }    
726 }
727
728 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
729 {
730     self.dmg_time = time;
731
732     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
733     {
734         if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
735         {
736             self.vehicle_shieldent = spawn();
737             self.vehicle_shieldent.effects = EF_LOWPRECISION;
738
739             setmodel(self.vehicle_shieldent, "models/vhshield.md3");
740             setattachment(self.vehicle_shieldent, self, "");
741             setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
742             self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
743             self.vehicle_shieldent.think       = shieldhit_think;
744         }
745
746         self.vehicle_shieldent.colormod    = '1 1 1';
747         self.vehicle_shieldent.alpha       = 0.45;
748         self.vehicle_shieldent.angles      = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
749         self.vehicle_shieldent.nextthink   = time;
750
751         self.vehicle_shield -= damage;
752
753         if(self.vehicle_shield < 0)
754         {
755             self.vehicle_health            -= fabs(self.vehicle_shield);
756             self.vehicle_shieldent.colormod = '2 0 0';
757             self.vehicle_shield             = 0;
758             self.vehicle_shieldent.alpha    = 0.75;
759             
760                 if(sound_allowed(MSG_BROADCAST, attacker))
761                 spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);   // FIXME: PLACEHOLDER
762         }
763         else
764                 if(sound_allowed(MSG_BROADCAST, attacker))
765                 spamsound (self, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
766
767     }
768     else
769     {
770         self.vehicle_health -= damage;
771
772         if(sound_allowed(MSG_BROADCAST, attacker))
773             spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTN_NORM);  // FIXME: PLACEHOLDER
774     }
775
776     self.velocity += force; // * (vlen(force) / self.mass);
777
778     if(self.vehicle_health <= 0)
779     {
780         if(self.owner)
781             if(self.vehicle_flags & VHF_DEATHEJECT)
782                 vehicles_exit(VHEF_EJECT);
783             else
784                 vehicles_exit(VHEF_RELESE);
785
786         self.vehicle_die();
787         vehicles_setreturn();
788     }
789 }
790
791 void vehicles_clearrturn()
792 {
793     entity ret;
794     // Remove "return helper", if any.
795     ret = findchain(classname, "vehicle_return");
796     while(ret)
797     {
798         if(ret.enemy == self)
799         {
800             ret.classname   = "";
801             ret.think       = SUB_Remove;
802             ret.nextthink   = time + 0.1;            
803             
804             if(ret.waypointsprite_attached)
805                 WaypointSprite_Kill(ret.waypointsprite_attached);
806             
807             return;
808         }
809         ret = ret.chain;
810     }
811 }
812
813 void vehicles_return()
814 {
815     pointparticles(particleeffectnum("teleport"), self.enemy.origin + '0 0 64', '0 0 0', 1);
816
817     self.enemy.think     = vehicles_spawn;
818     self.enemy.nextthink = time;
819
820     if(self.waypointsprite_attached)
821         WaypointSprite_Kill(self.waypointsprite_attached);
822             
823     remove(self);
824 }
825
826 void vehicles_showwp_goaway()
827 {
828     if(self.waypointsprite_attached)
829         WaypointSprite_Kill(self.waypointsprite_attached);
830             
831     remove(self);
832     
833 }
834
835 void vehicles_showwp()
836 {
837     entity oldself;
838     vector rgb;
839     
840     if(self.cnt)
841     {        
842         self.think      = vehicles_return;
843         self.nextthink  = self.cnt;
844     }    
845     else
846     {
847         self.think      = vehicles_return;
848         self.nextthink  = time +1;
849         
850         oldself = self;
851         self = spawn();
852         setmodel(self, "null");
853         self.team = oldself.enemy.team;
854         self.enemy = oldself.enemy;
855         setorigin(self, oldself.enemy.pos1);
856         
857         self.nextthink = time + 5;
858         self.think = vehicles_showwp_goaway;
859     }
860     
861     if(teamplay && self.team)
862             rgb = TeamColor(self.team);
863     else
864             rgb = '1 1 1';
865     WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE, RADARICON_POWERUP, rgb);
866     if(self.waypointsprite_attached)
867     {        
868         WaypointSprite_UpdateRule(self.waypointsprite_attached, self.enemy.team, SPRITERULE_DEFAULT);        
869         if(oldself == world)
870             WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);        
871         WaypointSprite_Ping(self.waypointsprite_attached);
872     }    
873     
874     if(oldself != world)
875         self = oldself;
876 }
877
878 void vehicles_setreturn()
879 {
880     entity ret;
881     
882     vehicles_clearrturn();
883
884     ret = spawn();
885     ret.classname   = "vehicle_return";
886     ret.enemy       = self;    
887     ret.team        = self.team;
888     ret.think       = vehicles_showwp;
889     
890     if(self.deadflag != DEAD_NO)
891     {
892         ret.cnt         = time + self.vehicle_respawntime;
893         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 5);        
894     }        
895     else
896     {
897         ret.nextthink   = min(time + self.vehicle_respawntime, time + self.vehicle_respawntime - 1);        
898     }
899     
900     setmodel(ret, "null");
901     setorigin(ret, self.pos1 + '0 0 96');
902         
903 }
904
905 void vehicles_configcheck(string  configname, float check_cvar)
906 {
907     if(check_cvar == 0)
908         localcmd(strcat("exec ", configname, "\n"));
909 }
910
911 void vehicles_reset_colors()
912 {
913     entity e;
914     float _effects, _colormap;
915     vector _glowmod, _colormod;
916
917     if(autocvar_g_nodepthtestplayers)
918         _effects = EF_NODEPTHTEST;
919
920     if(autocvar_g_fullbrightplayers)
921         _effects |= EF_FULLBRIGHT;
922
923     if(self.team)
924         _colormap = 1024 + (self.team - 1) * 17;
925     else
926         _colormap = 1024;
927
928     _glowmod  = '0 0 0';
929     _colormod = '0 0 0';
930
931     // Find all ents attacked to main model and setup effects, colormod etc.
932     e = findchainentity(tag_entity, self);
933     while(e)
934     {
935         if(e != self.vehicle_shieldent)
936         {
937             e.effects   = _effects; //  | EF_LOWPRECISION;
938             e.colormod  = _colormod;
939             e.colormap  = _colormap;
940             e.alpha     = 1;
941         }
942         e = e.chain;
943     }
944
945     self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
946     self.vehicle_hudmodel.colormod = self.colormod = _colormod;
947     self.vehicle_hudmodel.colormap = self.colormap = _colormap;
948     self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
949
950     self.alpha     = 1;
951     self.avelocity = '0 0 0';
952     self.velocity  = '0 0 0';
953     self.effects   = _effects;
954 }
955
956 float vehicle_initialize(string  net_name,
957                          string  bodymodel,
958                          string  topmodel,
959                          string  hudmodel,
960                          string  toptag,
961                          string  hudtag,
962                          string  viewtag,
963                          float   vhud,
964                          vector  min_s,
965                          vector  max_s,
966                          float   nodrop,
967                          void()  spawnproc,
968                          float   _respawntime,
969                          float() physproc,
970                          void()  enterproc,
971                          void(float extflag) exitfunc,
972                          void() dieproc,
973                          void() thinkproc,
974                          float  use_csqc)
975 {
976     addstat(STAT_HUD, AS_INT,  hud);
977         addstat(STAT_VEHICLESTAT_HEALTH,  AS_INT, vehicle_health);
978         addstat(STAT_VEHICLESTAT_SHIELD,  AS_INT, vehicle_shield);
979         addstat(STAT_VEHICLESTAT_ENERGY,  AS_INT, vehicle_energy);
980
981         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT,   vehicle_ammo1);
982         addstat(STAT_VEHICLESTAT_RELOAD1, AS_INT, vehicle_reload1);
983
984         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT,   vehicle_ammo2);
985         addstat(STAT_VEHICLESTAT_RELOAD2, AS_INT, vehicle_reload2);
986
987     if(bodymodel == "")
988         error("vehicles: missing bodymodel!");
989
990     if(hudmodel == "")
991         error("vehicles: missing hudmodel!");
992
993     if(net_name == "")
994         self.netname = self.classname;
995     else
996         self.netname = net_name;
997
998     if(self.team && !teamplay)
999         self.team = 0;
1000         
1001     self.vehicle_flags |= VHF_ISVEHICLE;
1002     
1003     setmodel(self, bodymodel);
1004
1005     self.vehicle_viewport   = spawn();
1006     self.vehicle_hudmodel   = spawn();
1007     self.tur_head           = spawn();
1008     self.tur_head.owner     = self;
1009     self.takedamage         = DAMAGE_AIM;
1010     self.bot_attack         = TRUE;
1011     self.iscreature         = TRUE;
1012     self.damagedbycontents      = TRUE;
1013     self.hud                = vhud;
1014
1015     self.vehicle_die         = dieproc;
1016     self.vehicle_exit        = exitfunc;
1017     self.vehicle_enter       = enterproc;
1018     self.PlayerPhysplug      = physproc;
1019     self.event_damage        = vehicles_damage;
1020     self.touch               = vehicles_touch;
1021     self.think               = vehicles_spawn;    
1022     self.nextthink           = time;        
1023     self.vehicle_respawntime = _respawntime;
1024     self.vehicle_spawn       = spawnproc;
1025
1026     if(autocvar_g_nodepthtestplayers)
1027         self.effects = self.effects | EF_NODEPTHTEST;
1028
1029     if(autocvar_g_fullbrightplayers)
1030         self.effects = self.effects | EF_FULLBRIGHT;
1031
1032     setmodel(self.vehicle_hudmodel, hudmodel);
1033     setmodel(self.vehicle_viewport, "null");
1034
1035
1036     if(topmodel != "")
1037     {
1038         setmodel(self.tur_head, topmodel);
1039         setattachment(self.tur_head, self, toptag);
1040         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
1041         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1042     }
1043     else
1044     {
1045         setattachment(self.tur_head, self, "");
1046         setattachment(self.vehicle_hudmodel, self, hudtag);
1047         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
1048     }
1049
1050     setsize(self, min_s, max_s);
1051     if not (nodrop)
1052     {
1053         setorigin(self, self.origin);
1054         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1055         setorigin(self, trace_endpos);
1056     }
1057
1058     self.pos1 = self.origin;
1059     self.pos2 = self.angles;
1060     self.tur_head.team = self.team;
1061     
1062     return TRUE;
1063 }
1064
1065 void bugmenot()
1066 {
1067     self.vehicle_exit       = self.vehicle_exit;
1068     self.vehicle_enter      = self.vehicle_exit;
1069     self.vehicle_die        = self.vehicle_exit;
1070     self.vehicle_spawn      = self.vehicle_exit;
1071     self.AuxiliaryXhair     = self.AuxiliaryXhair;
1072 }