]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/vehicles/vehicles.qc
Bring back raptor straferoll, fix raptor mode 1 movement, fix raptor bomb reload...
[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
4 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force);
5 void vehicles_return();
6 void vehicles_enter();
7 void vehicles_touch();
8 void vehicles_reset_colors();
9 void vehicles_clearrturn();
10 void vehicles_setreturn();
11
12
13 #define MAX_AXH 4
14 .entity AuxiliaryXhair[MAX_AXH];
15
16 float SendAuxiliaryXhair(entity to, float sf)
17 {
18
19         WriteByte(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
20
21         WriteByte(MSG_ENTITY, self.cnt);
22
23         WriteCoord(MSG_ENTITY, self.origin_x);
24         WriteCoord(MSG_ENTITY, self.origin_y);
25         WriteCoord(MSG_ENTITY, self.origin_z);
26
27     WriteByte(MSG_ENTITY, rint(self.colormod_x * 255));
28     WriteByte(MSG_ENTITY, rint(self.colormod_y * 255));
29     WriteByte(MSG_ENTITY, rint(self.colormod_z * 255));
30
31     return TRUE;
32 }
33
34 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
35 {
36     entity axh;
37
38     axh_id = bound(0, axh_id, MAX_AXH);
39     axh = own.AuxiliaryXhair[axh_id];
40
41     if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
42     {
43         axh                     = spawn();
44         axh.cnt                 = axh_id;
45         axh.drawonlytoclient    = own;
46         axh.owner               = own;
47         Net_LinkEntity(axh, FALSE, 0, SendAuxiliaryXhair);
48     }
49
50     setorigin(axh, loc);
51     axh.colormod            = clr;
52     axh.SendFlags           = 0x01;
53     own.AuxiliaryXhair[axh_id] = axh;
54 }
55
56 /*
57 // SVC_TEMPENTITY based, horrible with even 50 ping. hm.
58 // WriteByte(MSG_ONE, SVC_TEMPENTITY) uses reliable messagess, never use for thinsg that need continous updates.
59 void SendAuxiliaryXhair2(entity own, vector loc, vector clr, float axh_id)
60 {
61         msg_entity = own;
62
63         WriteByte(MSG_ONE, SVC_TEMPENTITY);
64         WriteByte(MSG_ONE, TE_CSQC_AUXILIARYXHAIR);
65
66         WriteByte(MSG_ONE, axh_id);
67
68         WriteCoord(MSG_ONE, loc_x);
69         WriteCoord(MSG_ONE, loc_y);
70         WriteCoord(MSG_ONE, loc_z);
71
72     WriteByte(MSG_ONE, rint(clr_x * 255));
73     WriteByte(MSG_ONE, rint(clr_y * 255));
74     WriteByte(MSG_ONE, rint(clr_z * 255));
75
76 }
77 */
78
79 void CSQCVehicleSetup(entity own, float vehicle_id)
80 {
81         msg_entity = own;
82
83         WriteByte(MSG_ONE, SVC_TEMPENTITY);
84         WriteByte(MSG_ONE, TE_CSQC_VEHICLESETUP);
85         WriteByte(MSG_ONE, vehicle_id);
86 }
87
88 .entity lock_target;
89 .float  lock_strength;
90 .float  lock_time;
91 void vehicles_locktarget(float incr, float decr, float _lock_time)
92 {
93     if(self.lock_target && self.lock_target.deadflag != DEAD_NO)
94     {
95         self.lock_target    = world;
96         self.lock_strength  = 0;
97         self.lock_time      = 0;
98     }
99
100     if(self.lock_time > time)
101         return;
102
103     if(trace_ent != world)
104     {
105         if(teams_matter && trace_ent.team == self.team)
106             trace_ent = world;
107
108         if(trace_ent.deadflag != DEAD_NO)
109             trace_ent = world;
110
111         if not (trace_ent.vehicle_flags & VHF_ISVEHICLE || trace_ent.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
112             trace_ent = world;
113     }
114
115     if(self.lock_target == world && trace_ent != world)
116         self.lock_target = trace_ent;
117
118     // Have a locking target
119     // Trace hit current target
120     if(trace_ent == self.lock_target && trace_ent != world)
121     {
122         self.lock_strength = min(self.lock_strength + incr, 1);
123         if(self.lock_strength == 1)
124             self.lock_time = time + _lock_time;
125     }
126     else
127     {
128         if(trace_ent)
129             self.lock_strength = max(self.lock_strength - decr * 2, 0);
130         else
131             self.lock_strength = max(self.lock_strength - decr, 0);
132
133         if(self.lock_strength == 0)
134             self.lock_target = world;
135     }
136 }
137
138
139 #define VEHICLE_UPDATE_PLAYER(fld,vhname) \
140 self.owner.vehicle_##fld = self.vehicle_##fld / autocvar_g_vehicle_##vhname##_##fld
141
142 #define vehicles_sweap_collision(orig,vel,dt,acm,mult) \
143 traceline(orig, orig + vel * dt, MOVE_NORMAL, self); \
144 if(trace_fraction != 1) \
145     acm += normalize(self.origin - trace_endpos) * (vlen(vel) * mult)
146
147 float  force_fromtag_power;
148 float  force_fromtag_normpower;
149 vector force_fromtag_origin;
150 vector vehicles_force_fromtag_hover(string tag_name, float spring_length, float max_power)
151 {
152     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
153     v_forward  = normalize(v_forward) * -1;
154     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
155
156     force_fromtag_power = (1 - trace_fraction) * max_power;
157     force_fromtag_normpower = force_fromtag_power / max_power;
158
159     return v_forward  * force_fromtag_power;
160 }
161
162 vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float max_power)
163 {
164
165     force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
166     v_forward  = normalize(v_forward) * -1;
167     traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
168
169     if(trace_fraction == 1.0)
170     {
171         force_fromtag_normpower = -0.25;
172         return '0 0 -200';
173     }
174
175     force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
176     force_fromtag_normpower = force_fromtag_power / max_power;
177
178     return v_forward  * force_fromtag_power;
179 }
180
181
182
183 void vehicles_spawn()
184 {
185     dprint("Spawning vehicle: ", self.netname, "\n");
186
187     // De-own & reset
188     self.vehicle_hudmodel.viewmodelforclient = self;
189     self.owner              = world;
190     self.touch              = vehicles_touch;
191     self.event_damage       = vehicles_damage;
192     self.iscreature         = TRUE;
193     self.movetype           = MOVETYPE_WALK;
194     self.solid              = SOLID_SLIDEBOX;
195     self.takedamage         = DAMAGE_AIM;
196         self.deadflag           = DEAD_NO;
197     self.bot_attack         = TRUE;
198     self.flags              = FL_NOTARGET;
199     self.avelocity          = '0 0 0';
200
201     // Reset locking
202     self.lock_strength      = 0;
203     self.lock_target        = world;
204     self.misc_bulletcounter = 0;
205
206     // Return to spawn
207     self.angles             = self.pos2;
208     setorigin(self, self.pos1 + '0 0 128');
209     // Show it
210     pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
211
212     vehicles_reset_colors();
213     self.vehicle_spawn();
214 }
215
216 // Better way of determening whats crushable needed! (fl_crushable?)
217 float vehicles_crushable(entity e)
218 {
219     if(e.classname == "player")
220         return TRUE;
221
222     if(e.classname == "monster_zombie")
223         return TRUE;
224
225     return FALSE;
226 }
227
228 void vehicles_touch()
229 {
230     // Vehicle currently in use
231     if(self.owner)
232     {
233         // Colided with world?
234         if(other == world)
235         {
236             // Apply velocity based self damage here
237         }
238         else
239         {
240             if(other.vehicle_flags & VHF_ISVEHICLE)
241             {
242                 //other.velocity += self.velocity * (self.mass / other.mass);
243             }
244             else if(vehicles_crushable(other))
245             {
246                 if(vlen(self.velocity) != 0)
247                     Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_SBCRUSH, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
248             }
249         }
250         return;
251     }
252
253     if(other.classname != "player")
254         return;
255
256     if(other.deadflag != DEAD_NO)
257         return;
258
259     if(other.vehicle != world)
260         return;
261
262     // Remove this when bots know how to use vehicles.
263     if (clienttype(other) != CLIENTTYPE_REAL)
264         return;
265
266     vehicles_enter();
267 }
268
269 void vehicles_enter()
270 {
271    // Remove this when bots know how to use vehicles
272     if (clienttype(other) != CLIENTTYPE_REAL)
273         return;
274
275     if(self.phase > time)
276         return;
277
278     if(teams_matter)
279     if(self.team)
280     if(self.team != other.team)
281         return;
282
283     self.vehicle_ammo1   = 0;
284     self.vehicle_ammo2   = 0;
285     self.vehicle_reload1 = 0;
286     self.vehicle_reload2 = 0;
287     self.vehicle_energy  = 0;
288
289     self.owner          = other;
290     self.switchweapon   = other.switchweapon;
291
292     self.vehicle_hudmodel.viewmodelforclient = self.owner;
293     self.event_damage         = vehicles_damage;
294     self.nextthink            = 0;
295     self.owner.angles         = self.angles;
296     self.owner.takedamage     = DAMAGE_NO;
297     self.owner.solid          = SOLID_NOT;
298     self.owner.movetype       = MOVETYPE_NOCLIP;
299     self.owner.alpha          = -1;
300     self.owner.vehicle        = self;
301     self.owner.event_damage   = SUB_Null;
302     self.owner.view_ofs       = '0 0 0';
303     self.colormap             = self.owner.colormap;
304     if(self.tur_head)
305         self.tur_head.colormap    = self.owner.colormap;
306
307     self.owner.hud            = self.hud;
308     self.owner.PlayerPhysplug = self.PlayerPhysplug;
309
310     self.owner.vehicle_ammo1    = self.vehicle_ammo1;
311     self.owner.vehicle_ammo2    = self.vehicle_ammo2;
312     self.owner.vehicle_reload1  = self.vehicle_reload1;
313     self.owner.vehicle_reload2  = self.vehicle_reload2;
314
315     other.flags &~= FL_ONGROUND;
316     self.flags  &~= FL_ONGROUND;
317
318     self.team                 = self.owner.team;
319     self.flags               -= FL_NOTARGET;
320
321     msg_entity = other;
322     WriteByte (MSG_ONE, SVC_SETVIEWPORT);
323     WriteEntity(MSG_ONE, self.vehicle_viewport);
324
325     WriteByte (MSG_ONE, SVC_SETVIEWANGLES);  // 10 = SVC_SETVIEWANGLES
326     if(self.tur_head)
327     {
328         WriteAngle(MSG_ONE, self.tur_head.angles_x + self.angles_x);    // tilt
329         WriteAngle(MSG_ONE, self.tur_head.angles_y + self.angles_y);    // yaw
330         WriteAngle(MSG_ONE, 0);    // roll
331     }
332     else
333     {
334         WriteByte (MSG_ONE, SVC_SETVIEWANGLES); // 10 = SVC_SETVIEWANGLES
335         WriteAngle(MSG_ONE,  self.angles_x * -1);    // tilt
336         WriteAngle(MSG_ONE,  self.angles_y);    // yaw
337         WriteAngle(MSG_ONE,  0);                // roll
338     }
339
340     vehicles_clearrturn();
341
342     CSQCVehicleSetup(self.owner, self.hud);
343
344     self.vehicle_enter();
345 }
346
347 void vehicles_exit(float eject)
348 {
349         self.flags |= FL_NOTARGET;
350
351     if (self.owner)
352     {
353         msg_entity = self.owner;
354         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
355         WriteEntity( MSG_ONE, self.owner);
356
357         WriteByte (MSG_ONE, SVC_SETVIEWANGLES); // 10 = SVC_SETVIEWANGLES
358         WriteAngle(MSG_ONE, 0);                 // tilt
359         WriteAngle(MSG_ONE, self.angles_y);     // yaw
360         WriteAngle(MSG_ONE, 0);                 // roll
361
362         setsize(self.owner, PL_MIN,PL_MAX);
363
364         self.owner.takedamage     = DAMAGE_AIM;
365         self.owner.solid          = SOLID_SLIDEBOX;
366         self.owner.movetype       = MOVETYPE_WALK;
367         self.owner.effects        &~= EF_NODRAW;
368         self.owner.alpha          = 1;
369         self.owner.PlayerPhysplug = SUB_Null;
370         self.owner.vehicle        = world;
371         self.owner.view_ofs       = PL_VIEW_OFS;
372         self.owner.event_damage   = PlayerDamage;
373         self.owner.hud            = HUD_NORMAL;
374         self.owner.switchweapon   = self.switchweapon;
375     }
376
377     if(self.deadflag == DEAD_NO)
378         self.avelocity          = '0 0 0';
379
380     self.vehicle_hudmodel.viewmodelforclient = self;
381         self.tur_head.nodrawtoclient             = self;
382     vehicles_setreturn();
383
384     self.phase = time + 1;
385
386     if(!teams_matter)
387         self.team = 0;
388
389     self.vehicle_exit(eject);
390     self.owner = world;
391 }
392
393
394 void vehicles_regen(.float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time)
395 {
396     if(self.regen_field < field_max)
397     if(self.timer + rpause < time)
398     {
399         self.regen_field = min(self.regen_field + regen * delta_time, field_max);
400
401         if(self.owner)
402             self.owner.regen_field = self.regen_field / field_max;
403     }
404 }
405
406 void shieldhit_think()
407 {
408     self.alpha -= 0.1;
409     if (self.alpha <= 0)
410     {
411         setmodel(self, "");
412         self.alpha = -1;
413     }
414     else
415     {
416         self.nextthink = time + 0.1;
417     }
418 }
419
420 void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
421 {
422     float ddmg_take;
423
424     self.dmg_time = time;
425
426     if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
427     {
428         if (wasfreed(self.tur_head.enemy) || self.tur_head.enemy == world)
429         {
430             self.tur_head.enemy = spawn();
431             self.tur_head.enemy.effects = EF_LOWPRECISION;
432         }
433
434         setmodel(self.tur_head.enemy, "models/vhshield.md3");
435         setattachment(self.tur_head.enemy, self, "");
436
437         self.tur_head.enemy.colormod    = '1 1 1';
438         self.tur_head.enemy.alpha       = 0.45;
439         self.tur_head.enemy.scale       = 256 / vlen(self.maxs - self.mins);
440         self.tur_head.enemy.angles      = vectoangles(normalize(hitloc - self.origin)) - self.angles;
441         self.tur_head.enemy.think       = shieldhit_think;
442         self.tur_head.enemy.nextthink   = time;
443
444         self.vehicle_shield -= damage;
445         if(self.vehicle_shield < 0)
446         {
447             self.tur_head.enemy.colormod = '2 0 0';
448             ddmg_take = fabs(self.vehicle_shield);
449             self.vehicle_shield         = 0;
450             self.tur_head.enemy.alpha   = 0.75;
451             self.vehicle_health         -= ddmg_take;
452         }
453     }
454     else
455         self.vehicle_health -= damage;
456
457     self.velocity += force; // * (vlen(force) / self.mass);
458
459     if(self.vehicle_health <= 0)
460     {
461         if(self.owner)
462             if(self.vehicle_flags & VHF_DEATHEJECT)
463                 vehicles_exit(VHEF_EJECT);
464             else
465                 vehicles_exit(VHEF_RELESE);
466
467         self.vehicle_die();
468         vehicles_setreturn();
469     }
470 }
471
472 void vehicles_return()
473 {
474     pointparticles(particleeffectnum("teleport"), self.enemy.origin + '0 0 64', '0 0 0', 1);
475
476     self.enemy.think     = vehicles_spawn;
477     self.enemy.nextthink = time;
478
479     remove(self);
480 }
481
482 void vehicles_clearrturn()
483 {
484     entity ret;
485     // Remove "return helper", if any.
486     ret = findchain(classname, "vehicle_return");
487     while(ret)
488     {
489         if(ret.enemy == self)
490         {
491             ret.classname   = "";
492             ret.think       = SUB_Remove;
493             ret.nextthink   = time + 0.1;
494             return;
495         }
496
497         ret = ret.chain;
498     }
499 }
500
501 void vehicles_setreturn()
502 {
503     entity ret;
504
505     vehicles_clearrturn();
506
507     ret = spawn();
508     ret.classname   = "vehicle_return";
509     ret.enemy       = self;
510     ret.think       = vehicles_return;
511     ret.nextthink   = time + self.vehicle_respawntime;
512 }
513
514 float vehicles_customizeentityforclient()
515 {
516     if(self.deadflag  == DEAD_DEAD)
517         return FALSE;
518     else
519         return TRUE;
520 }
521
522 void vehicles_configcheck(string  configname, float check_cvar)
523 {
524     if(check_cvar == 0)
525         localcmd(strcat("exec ", configname, "\n"));
526 }
527
528 void vehicles_reset_colors()
529 {
530     entity e;
531     float _effects, _colormap;
532     vector _glowmod, _colormod;
533
534     if(autocvar_g_nodepthtestplayers)
535         _effects = EF_NODEPTHTEST;
536
537     if(autocvar_g_fullbrightplayers)
538         _effects |= EF_FULLBRIGHT;
539
540     if(self.team)
541         _colormap = 1024 + (self.team - 1) * 17;
542     else
543         _colormap = 1024;
544
545     _glowmod    = '0 0 0';
546     _colormod   = '0 0 0';
547
548     // Find all ents attacked to main model and setup effects, colormod etc.
549     e = findchainentity(tag_entity, self);
550     while(e)
551     {
552         e.effects   = _effects;
553         e.colormod  = _colormod;
554         e.colormap  = _colormap;
555         e.alpha     = 1;
556
557         e = e.chain;
558     }
559
560     self.effects  = _effects;
561     self.colormod = _colormod;
562     self.colormap = _colormap;
563
564     self.alpha          = 1;
565     self.avelocity      = '0 0 0';
566     self.velocity       = '0 0 0';
567
568 }
569
570 float vehicle_initialize(string  net_name,
571                          string  bodymodel,
572                          string  topmodel,
573                          string  hudmodel,
574                          string  toptag,
575                          string  hudtag,
576                          string  viewtag,
577                          float   vhud,
578                          vector min_s,
579                          vector max_s,
580                          float  nodrop,
581                          void()  spawnproc,
582                          float   _respawntime,
583                          float() physproc,
584                          void()  enterproc,
585                          void(float extflag) exitfunc,
586                          void() dieproc,
587                          void() thinkproc )
588 {
589     addstat(STAT_HUD, AS_INT,  hud);
590         addstat(STAT_VEHICLESTAT_HEALTH,  AS_FLOAT, vehicle_health);
591         addstat(STAT_VEHICLESTAT_SHIELD,  AS_FLOAT, vehicle_shield);
592         addstat(STAT_VEHICLESTAT_ENERGY,  AS_FLOAT, vehicle_energy);
593
594         addstat(STAT_VEHICLESTAT_AMMO1,   AS_INT,   vehicle_ammo1);
595         addstat(STAT_VEHICLESTAT_RELOAD1, AS_FLOAT, vehicle_reload1);
596
597         addstat(STAT_VEHICLESTAT_AMMO2,   AS_INT,   vehicle_ammo2);
598         addstat(STAT_VEHICLESTAT_RELOAD2, AS_FLOAT, vehicle_reload2);
599
600     if(bodymodel == "")
601         error("vehicles: missing bodymodel!");
602
603     if(hudmodel == "")
604         error("vehicles: missing hudmodel!");
605
606     if(net_name == "")
607         self.netname = self.classname;
608     else
609         self.netname = net_name;
610
611     if(self.team && !teams_matter)
612         self.team = 0;
613
614     self.vehicle_flags |= VHF_ISVEHICLE;
615
616     setmodel(self, bodymodel);
617
618     self.vehicle_viewport   = spawn();
619     self.vehicle_hudmodel   = spawn();
620     self.tur_head           = spawn();
621     self.tur_head.owner     = self;
622     self.takedamage         = DAMAGE_AIM;
623     self.bot_attack         = TRUE;
624     self.iscreature         = TRUE;
625     self.hud                = vhud;
626
627     self.customizeentityforclient = vehicles_customizeentityforclient;
628     self.vehicle_die         = dieproc;
629     self.vehicle_exit        = exitfunc;
630     self.vehicle_enter       = enterproc;
631     self.PlayerPhysplug      = physproc;
632     self.event_damage        = vehicles_damage;
633     self.touch               = vehicles_touch;
634     self.think               = vehicles_spawn;
635     self.nextthink           = time;
636     self.vehicle_respawntime = _respawntime;
637     self.vehicle_spawn       = spawnproc;
638
639     if(autocvar_g_nodepthtestplayers)
640         self.effects = self.effects | EF_NODEPTHTEST;
641
642     if(autocvar_g_fullbrightplayers)
643         self.effects = self.effects | EF_FULLBRIGHT;
644
645     setmodel(self.vehicle_hudmodel, hudmodel);
646     setmodel(self.vehicle_viewport, "null");
647
648
649     if(topmodel != "")
650     {
651         setmodel(self.tur_head, topmodel);
652         setattachment(self.tur_head, self, toptag);
653         setattachment(self.vehicle_hudmodel, self.tur_head, hudtag);
654         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
655     }
656     else
657     {
658         setattachment(self.tur_head, self, "");
659         setattachment(self.vehicle_hudmodel, self, hudtag);
660         setattachment(self.vehicle_viewport, self.vehicle_hudmodel, viewtag);
661     }
662
663     setsize(self, min_s, max_s);
664     if not (nodrop)
665     {
666         setorigin(self, self.origin);
667         tracebox(self.origin + '0 0 100', min_s, max_s, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
668         setorigin(self, trace_endpos);
669     }
670
671     self.pos1 = self.origin;
672     self.pos2 = self.angles;
673
674     return TRUE;
675 }
676
677
678 void bugmenot()
679 {
680     self.vehicle_exit       = self.vehicle_exit;
681     self.vehicle_enter      = self.vehicle_exit;
682     self.vehicle_die        = self.vehicle_exit;
683     self.vehicle_spawn      = self.vehicle_exit;
684     self.AuxiliaryXhair     = self.AuxiliaryXhair;
685 }