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