1 #ifndef TURRET_WALKER_H
2 #define TURRET_WALKER_H
4 //#define WALKER_FANCYPATHING
6 #include "walker_weapon.qh"
8 CLASS(WalkerTurret, Turret)
9 /* spawnflags */ ATTRIB(WalkerTurret, spawnflags, int, TUR_FLAG_PLAYER | TUR_FLAG_MOVE);
10 /* mins */ ATTRIB(WalkerTurret, mins, vector, '-70 -70 0');
11 /* maxs */ ATTRIB(WalkerTurret, maxs, vector, '70 70 95');
12 /* modelname */ ATTRIB(WalkerTurret, mdl, string, "walker_body.md3");
13 /* model */ ATTRIB_STRZONE(WalkerTurret, model, string, strcat("models/turrets/", this.mdl));
14 /* head_model */ ATTRIB_STRZONE(WalkerTurret, head_model, string, strcat("models/turrets/", "walker_head_minigun.md3"));
15 /* netname */ ATTRIB(WalkerTurret, netname, string, "walker");
16 /* fullname */ ATTRIB(WalkerTurret, turret_name, string, _("Walker Turret"));
17 ATTRIB(WalkerTurret, m_weapon, Weapon, WEP_WALKER);
18 ENDCLASS(WalkerTurret)
19 REGISTER_TURRET(WALKER, NEW(WalkerTurret));
27 float autocvar_g_turrets_unit_walker_melee_damage;
28 float autocvar_g_turrets_unit_walker_melee_force;
29 float autocvar_g_turrets_unit_walker_melee_range;
30 float autocvar_g_turrets_unit_walker_rocket_damage;
31 float autocvar_g_turrets_unit_walker_rocket_radius;
32 float autocvar_g_turrets_unit_walker_rocket_force;
33 float autocvar_g_turrets_unit_walker_rocket_speed;
34 float autocvar_g_turrets_unit_walker_rocket_range;
35 float autocvar_g_turrets_unit_walker_rocket_range_min;
36 float autocvar_g_turrets_unit_walker_rocket_refire;
37 float autocvar_g_turrets_unit_walker_rocket_turnrate;
38 float autocvar_g_turrets_unit_walker_speed_stop;
39 float autocvar_g_turrets_unit_walker_speed_walk;
40 float autocvar_g_turrets_unit_walker_speed_run;
41 float autocvar_g_turrets_unit_walker_speed_jump;
42 float autocvar_g_turrets_unit_walker_speed_swim;
43 float autocvar_g_turrets_unit_walker_speed_roam;
44 float autocvar_g_turrets_unit_walker_turn;
45 float autocvar_g_turrets_unit_walker_turn_walk;
46 float autocvar_g_turrets_unit_walker_turn_strafe;
47 float autocvar_g_turrets_unit_walker_turn_swim;
48 float autocvar_g_turrets_unit_walker_turn_run;
50 const int ANIM_NO = 0;
51 const int ANIM_TURN = 1;
52 const int ANIM_WALK = 2;
53 const int ANIM_RUN = 3;
54 const int ANIM_STRAFE_L = 4;
55 const int ANIM_STRAFE_R = 5;
56 const int ANIM_JUMP = 6;
57 const int ANIM_LAND = 7;
58 const int ANIM_PAIN = 8;
59 const int ANIM_MELEE = 9;
60 const int ANIM_SWIM = 10;
61 const int ANIM_ROAM = 11;
66 #define WALKER_PATH(this, s, e) pathlib_astar(this, s, e)
68 bool walker_firecheck(entity this)
70 if (this.animflag == ANIM_MELEE)
73 return turret_firecheck(this);
76 void walker_melee_do_dmg(entity this)
81 makevectors(this.angles);
82 where = this.origin + v_forward * 128;
84 e = findradius(where,32);
87 if (turret_validate_target(this, e, this.target_validate_flags))
88 if (e != this && e.owner != this)
89 Damage(e, this, this, (autocvar_g_turrets_unit_walker_melee_damage), DEATH_TURRET_WALK_MELEE.m_id, '0 0 0', v_forward * (autocvar_g_turrets_unit_walker_melee_force));
95 void walker_setnoanim(entity this)
97 turrets_setframe(this, ANIM_NO, false);
98 this.animflag = this.frame;
100 void walker_rocket_explode(entity this)
102 RadiusDamage (this, this.owner, (autocvar_g_turrets_unit_walker_rocket_damage), 0, (autocvar_g_turrets_unit_walker_rocket_radius), this, NULL, (autocvar_g_turrets_unit_walker_rocket_force), DEATH_TURRET_WALK_ROCKET.m_id, NULL);
106 void walker_rocket_damage(entity this, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
108 this.health = this.health - damage;
109 this.velocity = this.velocity + vforce;
111 if (this.health <= 0)
112 W_PrepareExplosionByDamage(this, this.owner, walker_rocket_explode);
115 #define WALKER_ROCKET_MOVE(s) movelib_move_simple((s), newdir, (autocvar_g_turrets_unit_walker_rocket_speed), (autocvar_g_turrets_unit_walker_rocket_turnrate)); UpdateCSQCProjectile(s)
116 void walker_rocket_loop(entity this);
117 void walker_rocket_think(entity this)
124 this.nextthink = time;
126 edist = vlen(this.enemy.origin - this.origin);
128 // Simulate crude guidance
132 this.tur_shotorg = randomvec() * min(edist, 64);
134 this.tur_shotorg = randomvec() * min(edist, 256);
136 this.cnt = time + 0.5;
140 this.tur_shotorg = '0 0 0';
142 if (this.max_health < time)
144 setthink(this, walker_rocket_explode);
145 this.nextthink = time;
149 if (this.shot_dmg != 1337 && random() < 0.01)
151 walker_rocket_loop(this);
155 m_speed = vlen(this.velocity);
157 // Enemy dead? just keep on the current heading then.
158 if (this.enemy == NULL || IS_DEAD(this.enemy))
163 itime = max(edist / m_speed, 1);
164 newdir = steerlib_pull(this, this.enemy.origin + this.tur_shotorg);
167 newdir = normalize(this.velocity);
169 WALKER_ROCKET_MOVE(this);
172 void walker_rocket_loop3(entity this)
174 this.nextthink = time;
176 if (this.max_health < time)
178 setthink(this, walker_rocket_explode);
182 if(vdist(this.origin - this.tur_shotorg, <, 100))
184 setthink(this, walker_rocket_think);
188 vector newdir = steerlib_pull(this, this.tur_shotorg);
189 WALKER_ROCKET_MOVE(this);
191 this.angles = vectoangles(this.velocity);
194 void walker_rocket_loop2(entity this)
196 this.nextthink = time;
198 if (this.max_health < time)
200 setthink(this, walker_rocket_explode);
204 if(vdist(this.origin - this.tur_shotorg, <, 100))
206 this.tur_shotorg = this.origin - '0 0 200';
207 setthink(this, walker_rocket_loop3);
211 vector newdir = steerlib_pull(this, this.tur_shotorg);
212 WALKER_ROCKET_MOVE(this);
215 void walker_rocket_loop(entity this)
217 this.nextthink = time;
218 this.tur_shotorg = this.origin + '0 0 300';
219 setthink(this, walker_rocket_loop2);
220 this.shot_dmg = 1337;
223 void walker_fire_rocket(entity this, vector org)
225 fixedmakevectors(this.angles);
229 entity rocket = new(walker_rocket);
230 setorigin(rocket, org);
232 sound (this, CH_WEAPON_A, SND_HAGAR_FIRE, VOL_BASE, ATTEN_NORM);
233 setsize (rocket, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
236 rocket.bot_dodge = true;
237 rocket.bot_dodgerating = 50;
238 rocket.takedamage = DAMAGE_YES;
239 rocket.damageforcescale = 2;
241 rocket.tur_shotorg = randomvec() * 512;
242 rocket.cnt = time + 1;
243 rocket.enemy = this.enemy;
246 setthink(rocket, walker_rocket_loop);
248 setthink(rocket, walker_rocket_think);
250 rocket.event_damage = walker_rocket_damage;
252 rocket.nextthink = time;
253 rocket.movetype = MOVETYPE_FLY;
254 rocket.velocity = normalize((v_forward + v_up * 0.5) + (randomvec() * 0.2)) * (autocvar_g_turrets_unit_walker_rocket_speed);
255 rocket.angles = vectoangles(rocket.velocity);
256 settouch(rocket, walker_rocket_explode);
257 rocket.flags = FL_PROJECTILE;
258 rocket.solid = SOLID_BBOX;
259 rocket.max_health = time + 9;
260 rocket.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_GUIDED_HEAT;
262 CSQCProjectile(rocket, false, PROJECTILE_ROCKET, false); // no culling, has fly sound
265 .vector enemy_last_loc;
266 .float enemy_last_time;
267 void walker_move_to(entity this, vector _target, float _dist)
269 switch (this.waterlevel)
271 case WATERLEVEL_NONE:
273 this.animflag = ANIM_RUN;
275 this.animflag = ANIM_WALK;
276 case WATERLEVEL_WETFEET:
277 case WATERLEVEL_SWIMMING:
278 if (this.animflag != ANIM_SWIM)
279 this.animflag = ANIM_WALK;
281 this.animflag = ANIM_SWIM;
283 case WATERLEVEL_SUBMERGED:
284 this.animflag = ANIM_SWIM;
287 this.moveto = _target;
288 this.steerto = steerlib_attract2(this, this.moveto, 0.5, 500, 0.95);
292 this.enemy_last_loc = _target;
293 this.enemy_last_time = time;
297 void walker_move_path(entity this)
299 #ifdef WALKER_FANCYPATHING
300 // Are we close enougth to a path node to switch to the next?
301 if(vdist(this.origin - this.pathcurrent.origin, <, 64))
302 if (this.pathcurrent.path_next == NULL)
304 // Path endpoint reached
305 pathlib_deletepath(this.pathcurrent.owner);
306 this.pathcurrent = NULL;
310 if (this.pathgoal.use)
311 this.pathgoal.use(this, NULL, NULL);
313 if (this.pathgoal.enemy)
315 this.pathcurrent = WALKER_PATH(this, this.pathgoal.origin, this.pathgoal.enemy.origin);
316 this.pathgoal = this.pathgoal.enemy;
320 this.pathgoal = NULL;
323 this.pathcurrent = this.pathcurrent.path_next;
325 this.moveto = this.pathcurrent.origin;
326 this.steerto = steerlib_attract2(this, this.moveto,0.5,500,0.95);
327 walker_move_to(this, this.moveto, 0);
330 if(vdist(this.origin - this.pathcurrent.origin, <, 64))
331 this.pathcurrent = this.pathcurrent.enemy;
333 if(!this.pathcurrent)
336 this.moveto = this.pathcurrent.origin;
337 this.steerto = steerlib_attract2(this, this.moveto, 0.5, 500, 0.95);
338 walker_move_to(this, this.moveto, 0);
342 spawnfunc(turret_walker) { if(!turret_initialize(this, TUR_WALKER)) remove(this); }
344 METHOD(WalkerTurret, tr_think, void(WalkerTurret thistur, entity it))
346 fixedmakevectors(it.angles);
348 if (it.spawnflags & TSF_NO_PATHBREAK && it.pathcurrent)
349 walker_move_path(it);
350 else if (it.enemy == NULL)
353 walker_move_path(it);
356 if(it.enemy_last_time != 0)
358 if(vdist(it.origin - it.enemy_last_loc, <, 128) || time - it.enemy_last_time > 10)
359 it.enemy_last_time = 0;
361 walker_move_to(it, it.enemy_last_loc, 0);
365 if(it.animflag != ANIM_NO)
367 traceline(it.origin + '0 0 64', it.origin + '0 0 64' + v_forward * 128, MOVE_NORMAL, it);
369 if(trace_fraction != 1.0)
370 it.tur_head.idletime = -1337;
373 traceline(trace_endpos, trace_endpos - '0 0 256', MOVE_NORMAL, it);
374 if(trace_fraction == 1.0)
375 it.tur_head.idletime = -1337;
378 if(it.tur_head.idletime == -1337)
380 it.moveto = it.origin + randomvec() * 256;
381 it.tur_head.idletime = 0;
384 it.moveto = it.moveto * 0.9 + ((it.origin + v_forward * 500) + randomvec() * 400) * 0.1;
385 it.moveto_z = it.origin_z + 64;
386 walker_move_to(it, it.moveto, 0);
389 if(it.idletime < time)
391 if(random() < 0.5 || !(it.spawnflags & TSL_ROAM))
393 it.idletime = time + 1 + random() * 5;
394 it.moveto = it.origin;
395 it.animflag = ANIM_NO;
399 it.animflag = ANIM_WALK;
400 it.idletime = time + 4 + random() * 2;
401 it.moveto = it.origin + randomvec() * 256;
402 it.tur_head.moveto = it.moveto;
403 it.tur_head.idletime = 0;
411 if (it.tur_dist_enemy < (autocvar_g_turrets_unit_walker_melee_range) && it.animflag != ANIM_MELEE)
415 wish_angle = angleofs(it, it.enemy);
416 if (it.animflag != ANIM_SWIM)
417 if (fabs(wish_angle_y) < 15)
419 it.moveto = it.enemy.origin;
420 it.steerto = steerlib_attract2(it, it.moveto, 0.5, 500, 0.95);
421 it.animflag = ANIM_MELEE;
424 else if (it.tur_head.attack_finished_single[0] < time)
426 if(it.tur_head.shot_volly)
428 it.animflag = ANIM_NO;
430 it.tur_head.shot_volly = it.tur_head.shot_volly -1;
431 if(it.tur_head.shot_volly == 0)
432 it.tur_head.attack_finished_single[0] = time + (autocvar_g_turrets_unit_walker_rocket_refire);
434 it.tur_head.attack_finished_single[0] = time + 0.2;
436 if(it.tur_head.shot_volly > 1)
437 walker_fire_rocket(it, gettaginfo(it, gettagindex(it, "tag_rocket01")));
439 walker_fire_rocket(it, gettaginfo(it, gettagindex(it, "tag_rocket02")));
443 if (it.tur_dist_enemy > (autocvar_g_turrets_unit_walker_rocket_range_min))
444 if (it.tur_dist_enemy < (autocvar_g_turrets_unit_walker_rocket_range))
445 it.tur_head.shot_volly = 4;
450 if (it.animflag != ANIM_MELEE)
451 walker_move_to(it, it.enemy.origin, it.tur_dist_enemy);
457 float turny = 0, turnx = 0;
460 real_angle = vectoangles(it.steerto) - it.angles;
466 movelib_brake_simple(it, (autocvar_g_turrets_unit_walker_speed_stop));
470 turny = (autocvar_g_turrets_unit_walker_turn);
471 movelib_brake_simple(it, (autocvar_g_turrets_unit_walker_speed_stop));
475 turny = (autocvar_g_turrets_unit_walker_turn_walk);
476 movelib_move_simple(it, v_forward, (autocvar_g_turrets_unit_walker_speed_walk), 0.6);
480 turny = (autocvar_g_turrets_unit_walker_turn_run);
481 movelib_move_simple(it, v_forward, (autocvar_g_turrets_unit_walker_speed_run), 0.6);
485 turny = (autocvar_g_turrets_unit_walker_turn_strafe);
486 movelib_move_simple(it, v_right * -1, (autocvar_g_turrets_unit_walker_speed_walk), 0.8);
490 turny = (autocvar_g_turrets_unit_walker_turn_strafe);
491 movelib_move_simple(it, v_right, (autocvar_g_turrets_unit_walker_speed_walk), 0.8);
495 it.velocity += '0 0 1' * (autocvar_g_turrets_unit_walker_speed_jump);
502 if(it.frame != ANIM_PAIN)
503 defer(it, 0.25, walker_setnoanim);
508 if(it.frame != ANIM_MELEE)
510 defer(it, 0.41, walker_setnoanim);
511 defer(it, 0.21, walker_melee_do_dmg);
514 movelib_brake_simple(it, (autocvar_g_turrets_unit_walker_speed_stop));
518 turny = (autocvar_g_turrets_unit_walker_turn_swim);
519 turnx = (autocvar_g_turrets_unit_walker_turn_swim);
521 it.angles_x += bound(-10, shortangle_f(real_angle_x, it.angles_x), 10);
522 movelib_move_simple(it, v_forward, (autocvar_g_turrets_unit_walker_speed_swim), 0.3);
523 vz = it.velocity_z + sin(time * 4) * 8;
527 turny = (autocvar_g_turrets_unit_walker_turn_walk);
528 movelib_move_simple(it, v_forward ,(autocvar_g_turrets_unit_walker_speed_roam), 0.5);
534 turny = bound( turny * -1, shortangle_f(real_angle_y, it.angles_y), turny );
535 it.angles_y += turny;
540 turnx = bound( turnx * -1, shortangle_f(real_angle_x, it.angles_x), turnx );
541 it.angles_x += turnx;
548 if(it.origin != it.oldorigin)
549 it.SendFlags |= TNSF_MOVE;
551 it.oldorigin = it.origin;
552 turrets_setframe(it, it.animflag, false);
554 METHOD(WalkerTurret, tr_death, void(WalkerTurret this, entity it))
556 #ifdef WALKER_FANCYPATHING
558 pathlib_deletepath(it.pathcurrent.owner);
560 it.pathcurrent = NULL;
562 METHOD(WalkerTurret, tr_setup, void(WalkerTurret this, entity it))
568 // Respawn is called & first spawn to, to set team. need to make sure we do not move the initial spawn.
569 if(it.movetype == MOVETYPE_WALK)
572 setorigin(it, it.pos1);
577 it.ammo_flags = TFL_AMMO_BULLETS | TFL_AMMO_RECHARGE | TFL_AMMO_RECIEVE;
578 it.aim_flags = TFL_AIM_LEAD;
579 it.turret_flags |= TUR_FLAG_HITSCAN;
581 it.target_select_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
582 it.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
583 it.iscreature = true;
584 it.teleportable = TELEPORT_NORMAL;
585 it.damagedbycontents = true;
586 it.solid = SOLID_SLIDEBOX;
587 it.takedamage = DAMAGE_AIM;
588 if(it.movetype != MOVETYPE_WALK)
590 setorigin(it, it.origin);
591 tracebox(it.origin + '0 0 128', it.mins, it.maxs, it.origin - '0 0 10000', MOVE_NORMAL, it);
592 setorigin(it, trace_endpos + '0 0 4');
596 it.movetype = MOVETYPE_WALK;
597 it.idle_aim = '0 0 0';
598 it.turret_firecheckfunc = walker_firecheck;
602 e = find(NULL, targetname, it.target);
605 LOG_TRACE("Initital waypoint for walker does NOT exsist, fix your map!\n");
609 if (e.classname != "turret_checkpoint")
610 LOG_TRACE("Warning: not a turrret path\n");
613 #ifdef WALKER_FANCYPATHING
614 it.pathcurrent = WALKER_PATH(it, it.origin, e.origin);
626 #include <common/physics/movelib.qh>
628 void walker_draw(entity this)
632 dt = time - this.move_time;
633 this.move_time = time;
637 fixedmakevectors(this.angles);
638 movelib_groundalign4point(this, 300, 100, 0.25, 45);
639 setorigin(this, this.origin + this.velocity * dt);
640 this.tur_head.angles += dt * this.tur_head.move_avelocity;
641 this.angles_y = this.move_angles_y;
643 if (this.health < 127)
645 te_spark(this.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
648 METHOD(WalkerTurret, tr_setup, void(WalkerTurret this, entity it))
651 it.movetype = MOVETYPE_BOUNCE;
652 it.move_movetype = MOVETYPE_BOUNCE;
653 it.move_origin = it.origin;
655 it.draw = walker_draw;