]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/tturrets/units/unit_walker.qc
a438229cf5df53d264e077bebec549fa0c4970fc
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / tturrets / units / unit_walker.qc
1 #define ANIM_NO         0
2 #define ANIM_TURN       1
3 #define ANIM_WALK       2
4 #define ANIM_RUN        3
5 #define ANIM_STRAFE_L   4
6 #define ANIM_STRAFE_R   5
7 #define ANIM_JUMP       6
8 #define ANIM_LAND       7
9 #define ANIM_PAIN       8
10 #define ANIM_MEELE      9
11 #define ANIM_SWIM       10
12 #define ANIM_ROAM       11
13 .float animflag;
14
15 #define WALKER_MIN '-70 -70 0'
16 #define WALKER_MAX '70 70 95'
17
18 #define WALKER_PATH(s,e) pathlib_astar(s,e)
19
20 float walker_firecheck()
21 {
22     if (self.animflag == ANIM_MEELE)
23         return 0;
24
25     return turret_stdproc_firecheck();
26 }
27
28 void walker_meele_do_dmg()
29 {
30     vector where;
31     entity e;
32     
33     makevectors(self.angles);
34     where = self.origin + v_forward * 128;
35
36     e = findradius(where,32);
37     while (e) 
38     {
39         if (turret_validate_target(self,e,self.target_validate_flags))
40             if (e != self && e.owner != self)
41                 Damage(e, self, self, autocvar_g_turrets_unit_walker_std_meele_dmg ,DEATH_TURRET_WALKER_MEELE,'0 0 0', v_forward * autocvar_g_turrets_unit_walker_std_meele_force);
42
43         e = e.chain;
44     }
45 }
46
47 void walker_setnoanim()
48 {
49     turrets_setframe(ANIM_NO, FALSE);
50     self.animflag = self.frame;
51 }
52 void walker_rocket_explode()
53 {
54     if (self.event_damage != SUB_Null)
55     {
56         self.event_damage = SUB_Null;
57         self.think = walker_rocket_explode;
58         self.nextthink = time;
59         return;
60     }
61
62     RadiusDamage (self, self.owner, autocvar_g_turrets_unit_walker_std_rocket_dmg, 0, autocvar_g_turrets_unit_walker_std_rocket_radius, world,autocvar_g_turrets_unit_walker_std_rocket_force, DEATH_TURRET_WALKER_ROCKET, world);
63
64     remove (self);
65 }
66
67 void walker_rocket_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
68 {
69     self.health = self.health - damage;
70     self.velocity = self.velocity + vforce;
71     if (self.health <= 0)
72         walker_rocket_explode();
73 }
74
75 #define WALKER_ROCKET_MOVE movelib_move_simple(newdir, autocvar_g_turrets_unit_walker_std_rocket_speed, autocvar_g_turrets_unit_walker_std_rocket_turnrate); UpdateCSQCProjectile(self)
76 void walker_rocket_loop();
77 void walker_rocket_think()
78 {
79     vector newdir;
80     float edist;
81     float itime;
82     float m_speed;
83
84     self.nextthink = time;
85
86     edist = vlen(self.enemy.origin - self.origin);
87
88     // Simulate crude guidance
89     if (self.cnt < time)
90     {
91         if (edist < 1000)
92             self.tur_shotorg = randomvec() * min(edist, 64);
93         else
94             self.tur_shotorg = randomvec() * min(edist, 256);
95
96         self.cnt = time + 0.5;
97     }
98
99     if (edist < 256)
100         self.tur_shotorg = '0 0 0';
101
102     if (self.tur_health < time)
103     {
104         self.think      = walker_rocket_explode;
105         self.nextthink  = time;
106         return;
107     }
108
109     if (self.shot_dmg != 1337 && random() < 0.01)
110     {
111         walker_rocket_loop();
112         return;
113     }
114
115     m_speed = vlen(self.velocity);
116
117     // Enemy dead? just keep on the current heading then.
118     if (self.enemy == world || self.enemy.deadflag != DEAD_NO)
119         self.enemy = world;
120
121     if (self.enemy)
122     {
123         itime = max(edist / m_speed, 1);
124         newdir = steerlib_pull(self.enemy.origin + self.tur_shotorg);
125     }
126     else
127         newdir  = normalize(self.velocity);
128
129     WALKER_ROCKET_MOVE;
130 }
131
132 void walker_rocket_loop3()
133 {
134     vector newdir;
135     self.nextthink = time;
136
137     if (self.tur_health < time)
138     {
139         self.think = walker_rocket_explode;
140         return;
141     }
142
143     if (vlen(self.origin - self.tur_shotorg) < 100 )
144     {
145         self.think = walker_rocket_think;
146         return;
147     }
148
149     newdir = steerlib_pull(self.tur_shotorg);
150     WALKER_ROCKET_MOVE;
151
152     self.angles = vectoangles(self.velocity);
153 }
154
155 void walker_rocket_loop2()
156 {
157     vector newdir;
158
159     self.nextthink = time;
160
161     if (self.tur_health < time)
162     {
163         self.think = walker_rocket_explode;
164         return;
165     }
166
167     if (vlen(self.origin - self.tur_shotorg) < 100 )
168     {
169         self.tur_shotorg = self.origin - '0 0 200';
170         self.think = walker_rocket_loop3;
171         return;
172     }
173
174     newdir = steerlib_pull(self.tur_shotorg);
175     WALKER_ROCKET_MOVE;
176 }
177
178 void walker_rocket_loop()
179 {
180     self.nextthink = time;
181     self.tur_shotorg = self.origin + '0 0 300';
182     self.think = walker_rocket_loop2;
183     self.shot_dmg = 1337;
184 }
185
186 void walker_fire_rocket(vector org)
187 {
188     entity rocket;
189  
190     fixedmakevectors(self.angles);
191
192     te_explosion (org);
193
194     rocket = spawn ();
195     setorigin(rocket, org);
196
197     sound (self, CH_WEAPON_A, "weapons/hagar_fire.wav", VOL_BASE, ATTN_NORM);
198     setsize (rocket, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
199
200     rocket.classname          = "walker_rocket";
201     rocket.owner              = self;
202     rocket.bot_dodge          = TRUE;
203     rocket.bot_dodgerating    = 50;
204     rocket.takedamage         = DAMAGE_YES;
205     rocket.damageforcescale   = 2;
206     rocket.health             = 25;
207     rocket.tur_shotorg        = randomvec() * 512;
208     rocket.cnt                = time + 1;
209     rocket.enemy              = self.enemy;
210
211     if (random() < 0.01)
212         rocket.think          = walker_rocket_loop;
213     else
214         rocket.think          = walker_rocket_think;
215
216     rocket.event_damage       = walker_rocket_damage;
217
218     rocket.nextthink          = time;
219     rocket.movetype           = MOVETYPE_FLY;
220     rocket.velocity           = normalize((v_forward + v_up * 0.5) + (randomvec() * 0.2)) * autocvar_g_turrets_unit_walker_std_rocket_speed;
221     rocket.angles             = vectoangles(rocket.velocity);
222     rocket.touch              = walker_rocket_explode;
223     rocket.flags              = FL_PROJECTILE;
224     rocket.solid              = SOLID_BBOX;
225     rocket.tur_health         = time + 9;
226
227     CSQCProjectile(rocket, FALSE, PROJECTILE_ROCKET, FALSE); // no culling, has fly sound
228 }
229
230 .vector enemy_last_loc;
231 .float enemy_last_time;
232 void walker_move_to(vector _target, float _dist)
233 {
234     switch (self.waterlevel)
235     {
236     case WATERLEVEL_NONE:
237         if (_dist > 500)
238             self.animflag = ANIM_RUN;
239         else
240             self.animflag = ANIM_WALK;
241     case WATERLEVEL_WETFEET:
242     case WATERLEVEL_SWIMMING:
243         if (self.animflag != ANIM_SWIM)
244             self.animflag = ANIM_WALK;
245         else
246             self.animflag = ANIM_SWIM;
247         break;
248     case WATERLEVEL_SUBMERGED:
249         self.animflag = ANIM_SWIM;
250     }
251
252     self.moveto = _target;
253     self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
254
255     if(self.enemy)
256     {
257         self.enemy_last_loc = _target;
258         self.enemy_last_time = time;        
259     }
260 }
261
262 //#define WALKER_FANCYPATHING
263
264 void walker_move_path()
265 {
266 #ifdef WALKER_FANCYPATHING
267     // Are we close enougth to a path node to switch to the next?
268     if (vlen(self.origin  - self.pathcurrent.origin) < 64)
269         if (self.pathcurrent.path_next == world)
270         {
271             // Path endpoint reached
272             pathlib_deletepath(self.pathcurrent.owner);
273             self.pathcurrent = world;
274
275             if (self.pathgoal)
276             {
277                 if (self.pathgoal.use)
278                     self.pathgoal.use();
279
280                 if (self.pathgoal.enemy)
281                 {
282                     self.pathcurrent = WALKER_PATH(self.pathgoal.origin,self.pathgoal.enemy.origin);
283                     self.pathgoal = self.pathgoal.enemy;
284                 }
285             }
286             else
287                 self.pathgoal = world;
288         }
289         else
290             self.pathcurrent = self.pathcurrent.path_next;
291
292     self.moveto = self.pathcurrent.origin;
293     self.steerto = steerlib_attract2(self.moveto,0.5,500,0.95);
294     walker_move_to(self.moveto, 0);
295
296 #else
297     if (vlen(self.origin - self.pathcurrent.origin) < 64)    
298         self.pathcurrent = self.pathcurrent.enemy;
299     
300     if(!self.pathcurrent)
301         return;
302     
303     self.moveto = self.pathcurrent.origin;
304     self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
305     walker_move_to(self.moveto, 0);
306 #endif
307 }
308
309 .float idletime;
310
311
312
313 void walker_postthink()
314 {
315     fixedmakevectors(self.angles);
316
317     if (self.spawnflags & TSF_NO_PATHBREAK && self.pathcurrent)
318         walker_move_path();
319     else if (self.enemy == world)
320     {
321         if(self.pathcurrent)
322             walker_move_path();
323         else
324         {
325             if(self.enemy_last_time != 0)
326             {
327                 if(vlen(self.origin - self.enemy_last_loc) < 128 || time - self.enemy_last_time > 10)
328                     self.enemy_last_time = 0;
329                 else                
330                     walker_move_to(self.enemy_last_loc, 0);
331             }
332             else
333             {        
334                 if(self.animflag != ANIM_NO)
335                 {                    
336                     traceline(self.origin + '0 0 64', self.origin + '0 0 64' + v_forward * 128, MOVE_NORMAL, self);
337                     
338                     if(trace_fraction != 1.0)
339                         self.tur_head.idletime = -1337;
340                     else
341                     {
342                         traceline(trace_endpos, trace_endpos - '0 0 256', MOVE_NORMAL, self);            
343                         if(trace_fraction == 1.0)
344                             self.tur_head.idletime = -1337;
345                     }
346                     
347                     if(self.tur_head.idletime == -1337)
348                     {
349                         self.moveto = self.origin + randomvec() * 256;        
350                         self.tur_head.idletime = 0;
351                     }
352
353                     self.moveto = self.moveto * 0.9 + ((self.origin + v_forward * 500) + randomvec() * 400) * 0.1;
354                     self.moveto_z = self.origin_z + 64;            
355                     walker_move_to(self.moveto, 0);
356                 }         
357                 
358                 if(self.idletime < time)
359                 {
360                     if(random() < 0.5 || !(self.spawnflags & TSL_ROAM))
361                     {
362                         self.idletime = time + 1 + random() * 5;
363                         self.moveto = self.origin;
364                         self.animflag = ANIM_NO;
365                     }
366                     else
367                     {
368                         self.animflag = ANIM_WALK;
369                         self.idletime = time + 4 + random() * 2;
370                         self.moveto = self.origin + randomvec() * 256;        
371                         self.tur_head.moveto = self.moveto;
372                         self.tur_head.idletime = 0;
373                     }
374                 }
375             }            
376         }
377     }
378     else
379     {
380         if (self.tur_dist_enemy < autocvar_g_turrets_unit_walker_std_meele_range && self.animflag != ANIM_MEELE)
381         {
382             vector wish_angle;
383             
384             wish_angle = angleofs(self, self.enemy);    
385             if (self.animflag != ANIM_SWIM)
386             if (fabs(wish_angle_y) < 15)
387             {
388                 self.moveto   = self.enemy.origin;
389                 self.steerto  = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
390                 self.animflag = ANIM_MEELE;
391             }
392         }
393         else if (self.tur_head.attack_finished_single < time)
394         {            
395             if(self.tur_head.shot_volly)
396             {
397                 self.animflag = ANIM_NO;
398                 
399                 self.tur_head.shot_volly = self.tur_head.shot_volly -1;
400                 if(self.tur_head.shot_volly == 0)
401                     self.tur_head.attack_finished_single = time + autocvar_g_turrets_unit_walker_std_rocket_refire;
402                 else
403                     self.tur_head.attack_finished_single = time + 0.2;
404                 
405                 if(self.tur_head.shot_volly > 1)
406                     walker_fire_rocket(gettaginfo(self, gettagindex(self, "tag_rocket01")));
407                 else
408                     walker_fire_rocket(gettaginfo(self, gettagindex(self, "tag_rocket02")));
409             }
410             else
411             {
412                 if (self.tur_dist_enemy > autocvar_g_turrets_unit_walker_std_rockets_range_min)        
413                 if (self.tur_dist_enemy < autocvar_g_turrets_unit_walker_std_rockets_range)
414                     self.tur_head.shot_volly = 4;
415             }
416         }
417         else
418         {            
419             if (self.animflag != ANIM_MEELE)
420                 walker_move_to(self.enemy.origin, self.tur_dist_enemy);
421         }
422     }
423
424     //if(self.animflag != ANIM_NO)
425     {
426         vector real_angle;
427         float turny, turnx;
428         float  vz;
429
430         real_angle = vectoangles(self.steerto) - self.angles;
431         vz         = self.velocity_z;
432             
433         switch (self.animflag)
434         {
435             case ANIM_NO:
436                 movelib_beak_simple(autocvar_g_turrets_unit_walker_speed_stop);
437                 break;
438
439             case ANIM_TURN:
440                 turny = autocvar_g_turrets_unit_walker_turn;
441                 movelib_beak_simple(autocvar_g_turrets_unit_walker_speed_stop);
442                 break;
443
444             case ANIM_WALK:
445                 turny = autocvar_g_turrets_unit_walker_turn_walk;
446                 movelib_move_simple(v_forward, autocvar_g_turrets_unit_walker_speed_walk, 0.6);
447                 break;
448
449             case ANIM_RUN:
450                 turny = autocvar_g_turrets_unit_walker_turn_run;
451                 movelib_move_simple(v_forward, autocvar_g_turrets_unit_walker_speed_run, 0.6);
452                 break;
453
454             case ANIM_STRAFE_L:
455                 turny = autocvar_g_turrets_unit_walker_turn_strafe;
456                 movelib_move_simple(v_right * -1, autocvar_g_turrets_unit_walker_speed_walk, 0.8);
457                 break;
458
459             case ANIM_STRAFE_R:
460                 turny = autocvar_g_turrets_unit_walker_turn_strafe;
461                 movelib_move_simple(v_right, autocvar_g_turrets_unit_walker_speed_walk, 0.8);
462                 break;
463
464             case ANIM_JUMP:
465                 self.velocity += '0 0 1' * autocvar_g_turrets_unit_walker_speed_jump;
466                 break;
467
468             case ANIM_LAND:
469                 break;
470
471             case ANIM_PAIN:
472                 if(self.frame != ANIM_PAIN)
473                     defer(0.25, walker_setnoanim);
474                 
475                 break;
476
477             case ANIM_MEELE:
478                 if(self.frame != ANIM_MEELE)
479                 {
480                     defer(0.41, walker_setnoanim);
481                     defer(0.21, walker_meele_do_dmg);
482                 }
483
484                 movelib_beak_simple(autocvar_g_turrets_unit_walker_speed_stop);
485                 break;
486
487             case ANIM_SWIM:
488                 turny = autocvar_g_turrets_unit_walker_turn_swim;
489                 turnx = autocvar_g_turrets_unit_walker_turn_swim;
490                 
491                 self.angles_x += bound(-10, shortangle_f(real_angle_x, self.angles_x), 10);
492                 movelib_move_simple(v_forward, autocvar_g_turrets_unit_walker_speed_swim, 0.3);
493                 vz = self.velocity_z + sin(time * 4) * 8;
494                 break;
495
496             case ANIM_ROAM:
497                 turny = autocvar_g_turrets_unit_walker_turn_walk;
498                 movelib_move_simple(v_forward ,autocvar_g_turrets_unit_walker_speed_roam, 0.5);
499                 break;
500         }
501             
502         if(turny)
503         {        
504             turny = bound( turny * -1, shortangle_f(real_angle_y, self.angles_y), turny );
505             self.angles_y += turny;
506         }
507
508         if(turnx)
509         {        
510             turnx = bound( turnx * -1, shortangle_f(real_angle_x, self.angles_x), turnx );
511             self.angles_x += turnx;
512         }
513
514         self.velocity_z = vz;        
515     }
516
517     
518     if(self.origin != self.oldorigin)
519         self.SendFlags |= TNSF_MOVE;
520     
521     self.oldorigin = self.origin;
522     turrets_setframe(self.animflag, FALSE);
523
524 }
525
526 void walker_attack()
527 {
528     sound (self, CH_WEAPON_A, "weapons/uzi_fire.wav", VOL_BASE, ATTN_NORM);
529     fireBallisticBullet (self.tur_shotorg, self.tur_shotdir_updated, self.shot_spread, self.shot_speed, 5, self.shot_dmg, 0, self.shot_force, DEATH_TURRET_WALKER_GUN, 0, 1, autocvar_g_balance_uzi_bulletconstant);
530     endFireBallisticBullet();
531     pointparticles(particleeffectnum("laser_muzzleflash"), self.tur_shotorg, self.tur_shotdir_updated * 1000, 1);
532 }
533
534
535 void walker_respawnhook()
536 {
537     entity e;
538
539     // Respawn is called & first spawn to, to set team. need to make sure we do not move the initial spawn.
540     if(self.movetype  != MOVETYPE_WALK)
541                 return;
542                 
543     setorigin(self, self.pos1);
544     self.angles = self.pos2;
545     
546     if (self.target != "")
547     {
548         e = find(world,targetname,self.target);
549         if (!e)
550         {
551             dprint("Warning! initital waypoint for Walker does NOT exsist!\n");
552             self.target = "";
553         }
554
555         if (e.classname != "turret_checkpoint")
556             dprint("Warning: not a turrret path\n");
557         else
558         {
559             self.pathcurrent = WALKER_PATH(self.origin,e.origin);
560             self.pathgoal = e;
561         }
562     }
563 }
564
565 void walker_diehook()
566 {
567 #ifdef self.pathcurrent
568     if (self.pathcurrent)
569         pathlib_deletepath(self.pathcurrent.owner);
570 #endif
571
572     self.pathcurrent = world;
573 }
574
575 void turret_walker_dinit()
576 {
577
578     entity e;
579
580     if (self.netname == "")      self.netname     = "Walker Turret";
581
582     self.ammo_flags = TFL_AMMO_BULLETS | TFL_AMMO_RECHARGE | TFL_AMMO_RECIVE;
583     self.turrcaps_flags = TFL_TURRCAPS_PLAYERKILL | TFL_TURRCAPS_MOVE ;
584     self.aim_flags = TFL_AIM_LEAD;
585
586     if (autocvar_g_antilag_bullets)
587         self.turrcaps_flags |= TFL_TURRCAPS_HITSCAN;
588     else
589         self.aim_flags      |= TFL_AIM_SHOTTIMECOMPENSATE;
590
591
592     self.turret_respawnhook = walker_respawnhook;
593     self.turret_diehook = walker_diehook;
594
595     self.ticrate = 0.05;
596     if (turret_stdproc_init("walker_std", "models/turrets/walker_body.md3", "models/turrets/walker_head_minigun.md3", TID_WALKER) == 0)
597     {
598         remove(self);
599         return;
600     }
601
602     self.damage_flags |= TFL_DMG_DEATH_NOGIBS;
603     self.target_select_flags   = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
604     self.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMTS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
605
606     self.iscreature = TRUE;
607     self.movetype   = MOVETYPE_WALK;
608     self.solid      = SOLID_SLIDEBOX;
609     self.takedamage = DAMAGE_AIM;
610
611
612     setsize(self, WALKER_MIN, WALKER_MAX);
613
614     setorigin(self, self.origin);
615     tracebox(self.origin + '0 0 128', self.mins,self.maxs,self.origin - '0 0 10000', MOVE_NORMAL, self);
616     setorigin(self, trace_endpos + '0 0 4');
617     self.pos1 = self.origin;
618     self.pos2 = self.angles;
619     
620     self.idle_aim = '0 0 0';
621     self.turret_firecheckfunc = walker_firecheck;
622     self.turret_firefunc      = walker_attack;
623     self.turret_postthink     = walker_postthink;
624
625     if (self.target != "")
626     {
627         e = find(world, targetname, self.target);
628         if (!e)
629         {
630             dprint("Initital waypoint for walker does NOT exsist, fix your map!\n");
631             self.target = "";
632         }
633
634         if (e.classname != "turret_checkpoint")
635             dprint("Warning: not a turrret path\n");
636         else
637         {
638 #ifdef WALKER_FANCYPATHING
639             self.pathcurrent = WALKER_PATH(self.origin, e.origin);
640             self.pathgoal = e;
641 #else
642             self.pathcurrent = e;
643 #endif
644         }
645     }
646 }
647
648
649 void spawnfunc_turret_walker()
650 {
651     g_turrets_common_precash();
652
653     precache_model ("models/turrets/walker_head_minigun.md3");
654     precache_model ("models/turrets/walker_body.md3");
655     precache_model ( "models/turrets/rocket.md3");
656     precache_sound ( "weapons/rocket_impact.wav" );
657
658     self.think = turret_walker_dinit;
659     self.nextthink = time + 0.5;
660 }