Kill g_antilag_bullets (always assume 1); kill bullet gravity.
[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, autocvar_g_turrets_unit_walker_std_rocket_force, DEATH_TURRET_WALK_ROCKET, world);
55     remove (self);
56 }
57
58 void walker_rocket_damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
59 {
60     self.health = self.health - damage;
61     self.velocity = self.velocity + vforce;
62
63     if (self.health <= 0)
64         W_PrepareExplosionByDamage(self.owner, walker_rocket_explode);
65 }
66
67 #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)
68 void walker_rocket_loop();
69 void walker_rocket_think()
70 {
71     vector newdir;
72     float edist;
73     float itime;
74     float m_speed;
75
76     self.nextthink = time;
77
78     edist = vlen(self.enemy.origin - self.origin);
79
80     // Simulate crude guidance
81     if (self.cnt < time)
82     {
83         if (edist < 1000)
84             self.tur_shotorg = randomvec() * min(edist, 64);
85         else
86             self.tur_shotorg = randomvec() * min(edist, 256);
87
88         self.cnt = time + 0.5;
89     }
90
91     if (edist < 128)
92         self.tur_shotorg = '0 0 0';
93
94     if (self.tur_health < time)
95     {
96         self.think      = walker_rocket_explode;
97         self.nextthink  = time;
98         return;
99     }
100
101     if (self.shot_dmg != 1337 && random() < 0.01)
102     {
103         walker_rocket_loop();
104         return;
105     }
106
107     m_speed = vlen(self.velocity);
108
109     // Enemy dead? just keep on the current heading then.
110     if (self.enemy == world || self.enemy.deadflag != DEAD_NO)
111         self.enemy = world;
112
113     if (self.enemy)
114     {
115         itime = max(edist / m_speed, 1);
116         newdir = steerlib_pull(self.enemy.origin + self.tur_shotorg);
117     }
118     else
119         newdir  = normalize(self.velocity);
120
121     WALKER_ROCKET_MOVE;
122 }
123
124 void walker_rocket_loop3()
125 {
126     vector newdir;
127     self.nextthink = time;
128
129     if (self.tur_health < time)
130     {
131         self.think = walker_rocket_explode;
132         return;
133     }
134
135     if (vlen(self.origin - self.tur_shotorg) < 100 )
136     {
137         self.think = walker_rocket_think;
138         return;
139     }
140
141     newdir = steerlib_pull(self.tur_shotorg);
142     WALKER_ROCKET_MOVE;
143
144     self.angles = vectoangles(self.velocity);
145 }
146
147 void walker_rocket_loop2()
148 {
149     vector newdir;
150
151     self.nextthink = time;
152
153     if (self.tur_health < time)
154     {
155         self.think = walker_rocket_explode;
156         return;
157     }
158
159     if (vlen(self.origin - self.tur_shotorg) < 100 )
160     {
161         self.tur_shotorg = self.origin - '0 0 200';
162         self.think = walker_rocket_loop3;
163         return;
164     }
165
166     newdir = steerlib_pull(self.tur_shotorg);
167     WALKER_ROCKET_MOVE;
168 }
169
170 void walker_rocket_loop()
171 {
172     self.nextthink = time;
173     self.tur_shotorg = self.origin + '0 0 300';
174     self.think = walker_rocket_loop2;
175     self.shot_dmg = 1337;
176 }
177
178 void walker_fire_rocket(vector org)
179 {
180     entity rocket;
181
182     fixedmakevectors(self.angles);
183
184     te_explosion (org);
185
186     rocket = spawn ();
187     setorigin(rocket, org);
188
189     sound (self, CH_WEAPON_A, "weapons/hagar_fire.wav", VOL_BASE, ATTEN_NORM);
190     setsize (rocket, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
191
192     rocket.classname          = "walker_rocket";
193     rocket.owner              = self;
194     rocket.bot_dodge          = TRUE;
195     rocket.bot_dodgerating    = 50;
196     rocket.takedamage         = DAMAGE_YES;
197     rocket.damageforcescale   = 2;
198     rocket.health             = 25;
199     rocket.tur_shotorg        = randomvec() * 512;
200     rocket.cnt                = time + 1;
201     rocket.enemy              = self.enemy;
202
203     if (random() < 0.01)
204         rocket.think          = walker_rocket_loop;
205     else
206         rocket.think          = walker_rocket_think;
207
208     rocket.event_damage       = walker_rocket_damage;
209
210     rocket.nextthink          = time;
211     rocket.movetype           = MOVETYPE_FLY;
212     rocket.velocity           = normalize((v_forward + v_up * 0.5) + (randomvec() * 0.2)) * autocvar_g_turrets_unit_walker_std_rocket_speed;
213     rocket.angles             = vectoangles(rocket.velocity);
214     rocket.touch              = walker_rocket_explode;
215     rocket.flags              = FL_PROJECTILE;
216     rocket.solid              = SOLID_BBOX;
217     rocket.tur_health         = time + 9;
218     rocket.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_GUIDED_HEAT;
219
220     CSQCProjectile(rocket, FALSE, PROJECTILE_ROCKET, FALSE); // no culling, has fly sound
221 }
222
223 .vector enemy_last_loc;
224 .float enemy_last_time;
225 void walker_move_to(vector _target, float _dist)
226 {
227     switch (self.waterlevel)
228     {
229     case WATERLEVEL_NONE:
230         if (_dist > 500)
231             self.animflag = ANIM_RUN;
232         else
233             self.animflag = ANIM_WALK;
234     case WATERLEVEL_WETFEET:
235     case WATERLEVEL_SWIMMING:
236         if (self.animflag != ANIM_SWIM)
237             self.animflag = ANIM_WALK;
238         else
239             self.animflag = ANIM_SWIM;
240         break;
241     case WATERLEVEL_SUBMERGED:
242         self.animflag = ANIM_SWIM;
243     }
244
245     self.moveto = _target;
246     self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
247
248     if(self.enemy)
249     {
250         self.enemy_last_loc = _target;
251         self.enemy_last_time = time;
252     }
253 }
254
255 //#define WALKER_FANCYPATHING
256
257 void walker_move_path()
258 {
259 #ifdef WALKER_FANCYPATHING
260     // Are we close enougth to a path node to switch to the next?
261     if (vlen(self.origin  - self.pathcurrent.origin) < 64)
262         if (self.pathcurrent.path_next == world)
263         {
264             // Path endpoint reached
265             pathlib_deletepath(self.pathcurrent.owner);
266             self.pathcurrent = world;
267
268             if (self.pathgoal)
269             {
270                 if (self.pathgoal.use)
271                     self.pathgoal.use();
272
273                 if (self.pathgoal.enemy)
274                 {
275                     self.pathcurrent = WALKER_PATH(self.pathgoal.origin,self.pathgoal.enemy.origin);
276                     self.pathgoal = self.pathgoal.enemy;
277                 }
278             }
279             else
280                 self.pathgoal = world;
281         }
282         else
283             self.pathcurrent = self.pathcurrent.path_next;
284
285     self.moveto = self.pathcurrent.origin;
286     self.steerto = steerlib_attract2(self.moveto,0.5,500,0.95);
287     walker_move_to(self.moveto, 0);
288
289 #else
290     if (vlen(self.origin - self.pathcurrent.origin) < 64)
291         self.pathcurrent = self.pathcurrent.enemy;
292
293     if(!self.pathcurrent)
294         return;
295
296     self.moveto = self.pathcurrent.origin;
297     self.steerto = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
298     walker_move_to(self.moveto, 0);
299 #endif
300 }
301
302 .float idletime;
303 void walker_postthink()
304 {
305     fixedmakevectors(self.angles);
306
307     if (self.spawnflags & TSF_NO_PATHBREAK && self.pathcurrent)
308         walker_move_path();
309     else if (self.enemy == world)
310     {
311         if(self.pathcurrent)
312             walker_move_path();
313         else
314         {
315             if(self.enemy_last_time != 0)
316             {
317                 if(vlen(self.origin - self.enemy_last_loc) < 128 || time - self.enemy_last_time > 10)
318                     self.enemy_last_time = 0;
319                 else
320                     walker_move_to(self.enemy_last_loc, 0);
321             }
322             else
323             {
324                 if(self.animflag != ANIM_NO)
325                 {
326                     traceline(self.origin + '0 0 64', self.origin + '0 0 64' + v_forward * 128, MOVE_NORMAL, self);
327
328                     if(trace_fraction != 1.0)
329                         self.tur_head.idletime = -1337;
330                     else
331                     {
332                         traceline(trace_endpos, trace_endpos - '0 0 256', MOVE_NORMAL, self);
333                         if(trace_fraction == 1.0)
334                             self.tur_head.idletime = -1337;
335                     }
336
337                     if(self.tur_head.idletime == -1337)
338                     {
339                         self.moveto = self.origin + randomvec() * 256;
340                         self.tur_head.idletime = 0;
341                     }
342
343                     self.moveto = self.moveto * 0.9 + ((self.origin + v_forward * 500) + randomvec() * 400) * 0.1;
344                     self.moveto_z = self.origin_z + 64;
345                     walker_move_to(self.moveto, 0);
346                 }
347
348                 if(self.idletime < time)
349                 {
350                     if(random() < 0.5 || !(self.spawnflags & TSL_ROAM))
351                     {
352                         self.idletime = time + 1 + random() * 5;
353                         self.moveto = self.origin;
354                         self.animflag = ANIM_NO;
355                     }
356                     else
357                     {
358                         self.animflag = ANIM_WALK;
359                         self.idletime = time + 4 + random() * 2;
360                         self.moveto = self.origin + randomvec() * 256;
361                         self.tur_head.moveto = self.moveto;
362                         self.tur_head.idletime = 0;
363                     }
364                 }
365             }
366         }
367     }
368     else
369     {
370         if (self.tur_dist_enemy < autocvar_g_turrets_unit_walker_std_meele_range && self.animflag != ANIM_MEELE)
371         {
372             vector wish_angle;
373
374             wish_angle = angleofs(self, self.enemy);
375             if (self.animflag != ANIM_SWIM)
376             if (fabs(wish_angle_y) < 15)
377             {
378                 self.moveto   = self.enemy.origin;
379                 self.steerto  = steerlib_attract2(self.moveto, 0.5, 500, 0.95);
380                 self.animflag = ANIM_MEELE;
381             }
382         }
383         else if (self.tur_head.attack_finished_single < time)
384         {
385             if(self.tur_head.shot_volly)
386             {
387                 self.animflag = ANIM_NO;
388
389                 self.tur_head.shot_volly = self.tur_head.shot_volly -1;
390                 if(self.tur_head.shot_volly == 0)
391                     self.tur_head.attack_finished_single = time + autocvar_g_turrets_unit_walker_std_rocket_refire;
392                 else
393                     self.tur_head.attack_finished_single = time + 0.2;
394
395                 if(self.tur_head.shot_volly > 1)
396                     walker_fire_rocket(gettaginfo(self, gettagindex(self, "tag_rocket01")));
397                 else
398                     walker_fire_rocket(gettaginfo(self, gettagindex(self, "tag_rocket02")));
399             }
400             else
401             {
402                 if (self.tur_dist_enemy > autocvar_g_turrets_unit_walker_std_rockets_range_min)
403                 if (self.tur_dist_enemy < autocvar_g_turrets_unit_walker_std_rockets_range)
404                     self.tur_head.shot_volly = 4;
405             }
406         }
407         else
408         {
409             if (self.animflag != ANIM_MEELE)
410                 walker_move_to(self.enemy.origin, self.tur_dist_enemy);
411         }
412     }
413
414     //if(self.animflag != ANIM_NO)
415     {
416         vector real_angle;
417         float turny = 0, turnx = 0;
418         float  vz;
419
420         real_angle = vectoangles(self.steerto) - self.angles;
421         vz         = self.velocity_z;
422
423         switch (self.animflag)
424         {
425             case ANIM_NO:
426                 movelib_beak_simple(autocvar_g_turrets_unit_walker_speed_stop);
427                 break;
428
429             case ANIM_TURN:
430                 turny = autocvar_g_turrets_unit_walker_turn;
431                 movelib_beak_simple(autocvar_g_turrets_unit_walker_speed_stop);
432                 break;
433
434             case ANIM_WALK:
435                 turny = autocvar_g_turrets_unit_walker_turn_walk;
436                 movelib_move_simple(v_forward, autocvar_g_turrets_unit_walker_speed_walk, 0.6);
437                 break;
438
439             case ANIM_RUN:
440                 turny = autocvar_g_turrets_unit_walker_turn_run;
441                 movelib_move_simple(v_forward, autocvar_g_turrets_unit_walker_speed_run, 0.6);
442                 break;
443
444             case ANIM_STRAFE_L:
445                 turny = autocvar_g_turrets_unit_walker_turn_strafe;
446                 movelib_move_simple(v_right * -1, autocvar_g_turrets_unit_walker_speed_walk, 0.8);
447                 break;
448
449             case ANIM_STRAFE_R:
450                 turny = autocvar_g_turrets_unit_walker_turn_strafe;
451                 movelib_move_simple(v_right, autocvar_g_turrets_unit_walker_speed_walk, 0.8);
452                 break;
453
454             case ANIM_JUMP:
455                 self.velocity += '0 0 1' * autocvar_g_turrets_unit_walker_speed_jump;
456                 break;
457
458             case ANIM_LAND:
459                 break;
460
461             case ANIM_PAIN:
462                 if(self.frame != ANIM_PAIN)
463                     defer(0.25, walker_setnoanim);
464
465                 break;
466
467             case ANIM_MEELE:
468                 if(self.frame != ANIM_MEELE)
469                 {
470                     defer(0.41, walker_setnoanim);
471                     defer(0.21, walker_meele_do_dmg);
472                 }
473
474                 movelib_beak_simple(autocvar_g_turrets_unit_walker_speed_stop);
475                 break;
476
477             case ANIM_SWIM:
478                 turny = autocvar_g_turrets_unit_walker_turn_swim;
479                 turnx = autocvar_g_turrets_unit_walker_turn_swim;
480
481                 self.angles_x += bound(-10, shortangle_f(real_angle_x, self.angles_x), 10);
482                 movelib_move_simple(v_forward, autocvar_g_turrets_unit_walker_speed_swim, 0.3);
483                 vz = self.velocity_z + sin(time * 4) * 8;
484                 break;
485
486             case ANIM_ROAM:
487                 turny = autocvar_g_turrets_unit_walker_turn_walk;
488                 movelib_move_simple(v_forward ,autocvar_g_turrets_unit_walker_speed_roam, 0.5);
489                 break;
490         }
491
492         if(turny)
493         {
494             turny = bound( turny * -1, shortangle_f(real_angle_y, self.angles_y), turny );
495             self.angles_y += turny;
496         }
497
498         if(turnx)
499         {
500             turnx = bound( turnx * -1, shortangle_f(real_angle_x, self.angles_x), turnx );
501             self.angles_x += turnx;
502         }
503
504         self.velocity_z = vz;
505     }
506
507
508     if(self.origin != self.oldorigin)
509         self.SendFlags |= TNSF_MOVE;
510
511     self.oldorigin = self.origin;
512     turrets_setframe(self.animflag, FALSE);
513 }
514
515 void walker_attack()
516 {
517     sound (self, CH_WEAPON_A, "weapons/uzi_fire.wav", VOL_BASE, ATTEN_NORM);
518     fireBallisticBullet (self.tur_shotorg, self.tur_shotdir_updated, self.shot_spread, self.shot_speed, 5, self.shot_dmg, self.shot_force, DEATH_TURRET_WALK_GUN, 0, autocvar_g_balance_uzi_bulletconstant);
519     endFireBallisticBullet();
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 }