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