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_touch(entity this, entity toucher)
108 walker_rocket_explode(this);
111 void walker_rocket_damage(entity this, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector vforce)
113 this.health = this.health - damage;
114 this.velocity = this.velocity + vforce;
116 if (this.health <= 0)
117 W_PrepareExplosionByDamage(this, this.owner, walker_rocket_explode);
120 #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)
121 void walker_rocket_loop(entity this);
122 void walker_rocket_think(entity this)
129 this.nextthink = time;
131 edist = vlen(this.enemy.origin - this.origin);
133 // Simulate crude guidance
137 this.tur_shotorg = randomvec() * min(edist, 64);
139 this.tur_shotorg = randomvec() * min(edist, 256);
141 this.cnt = time + 0.5;
145 this.tur_shotorg = '0 0 0';
147 if (this.max_health < time)
149 setthink(this, walker_rocket_explode);
150 this.nextthink = time;
154 if (this.shot_dmg != 1337 && random() < 0.01)
156 walker_rocket_loop(this);
160 m_speed = vlen(this.velocity);
162 // Enemy dead? just keep on the current heading then.
163 if (this.enemy == NULL || IS_DEAD(this.enemy))
168 itime = max(edist / m_speed, 1);
169 newdir = steerlib_pull(this, this.enemy.origin + this.tur_shotorg);
172 newdir = normalize(this.velocity);
174 WALKER_ROCKET_MOVE(this);
177 void walker_rocket_loop3(entity this)
179 this.nextthink = time;
181 if (this.max_health < time)
183 setthink(this, walker_rocket_explode);
187 if(vdist(this.origin - this.tur_shotorg, <, 100))
189 setthink(this, walker_rocket_think);
193 vector newdir = steerlib_pull(this, this.tur_shotorg);
194 WALKER_ROCKET_MOVE(this);
196 this.angles = vectoangles(this.velocity);
199 void walker_rocket_loop2(entity this)
201 this.nextthink = time;
203 if (this.max_health < time)
205 setthink(this, walker_rocket_explode);
209 if(vdist(this.origin - this.tur_shotorg, <, 100))
211 this.tur_shotorg = this.origin - '0 0 200';
212 setthink(this, walker_rocket_loop3);
216 vector newdir = steerlib_pull(this, this.tur_shotorg);
217 WALKER_ROCKET_MOVE(this);
220 void walker_rocket_loop(entity this)
222 this.nextthink = time;
223 this.tur_shotorg = this.origin + '0 0 300';
224 setthink(this, walker_rocket_loop2);
225 this.shot_dmg = 1337;
228 void walker_fire_rocket(entity this, vector org)
230 fixedmakevectors(this.angles);
234 entity rocket = new(walker_rocket);
235 setorigin(rocket, org);
237 sound (this, CH_WEAPON_A, SND_HAGAR_FIRE, VOL_BASE, ATTEN_NORM);
238 setsize (rocket, '-3 -3 -3', '3 3 3'); // give it some size so it can be shot
241 rocket.bot_dodge = true;
242 rocket.bot_dodgerating = 50;
243 rocket.takedamage = DAMAGE_YES;
244 rocket.damageforcescale = 2;
246 rocket.tur_shotorg = randomvec() * 512;
247 rocket.cnt = time + 1;
248 rocket.enemy = this.enemy;
251 setthink(rocket, walker_rocket_loop);
253 setthink(rocket, walker_rocket_think);
255 rocket.event_damage = walker_rocket_damage;
257 rocket.nextthink = time;
258 set_movetype(rocket, MOVETYPE_FLY);
259 rocket.velocity = normalize((v_forward + v_up * 0.5) + (randomvec() * 0.2)) * (autocvar_g_turrets_unit_walker_rocket_speed);
260 rocket.angles = vectoangles(rocket.velocity);
261 settouch(rocket, walker_rocket_touch);
262 rocket.flags = FL_PROJECTILE;
263 IL_PUSH(g_projectiles, rocket);
264 rocket.solid = SOLID_BBOX;
265 rocket.max_health = time + 9;
266 rocket.missile_flags = MIF_SPLASH | MIF_PROXY | MIF_GUIDED_HEAT;
268 CSQCProjectile(rocket, false, PROJECTILE_ROCKET, false); // no culling, has fly sound
271 .vector enemy_last_loc;
272 .float enemy_last_time;
273 void walker_move_to(entity this, vector _target, float _dist)
275 switch (this.waterlevel)
277 case WATERLEVEL_NONE:
279 this.animflag = ANIM_RUN;
281 this.animflag = ANIM_WALK;
282 case WATERLEVEL_WETFEET:
283 case WATERLEVEL_SWIMMING:
284 if (this.animflag != ANIM_SWIM)
285 this.animflag = ANIM_WALK;
287 this.animflag = ANIM_SWIM;
289 case WATERLEVEL_SUBMERGED:
290 this.animflag = ANIM_SWIM;
293 this.moveto = _target;
294 this.steerto = steerlib_attract2(this, this.moveto, 0.5, 500, 0.95);
298 this.enemy_last_loc = _target;
299 this.enemy_last_time = time;
303 void walker_move_path(entity this)
305 #ifdef WALKER_FANCYPATHING
306 // Are we close enougth to a path node to switch to the next?
307 if(vdist(this.origin - this.pathcurrent.origin, <, 64))
308 if (this.pathcurrent.path_next == NULL)
310 // Path endpoint reached
311 pathlib_deletepath(this.pathcurrent.owner);
312 this.pathcurrent = NULL;
316 if (this.pathgoal.use)
317 this.pathgoal.use(this, NULL, NULL);
319 if (this.pathgoal.enemy)
321 this.pathcurrent = WALKER_PATH(this, this.pathgoal.origin, this.pathgoal.enemy.origin);
322 this.pathgoal = this.pathgoal.enemy;
326 this.pathgoal = NULL;
329 this.pathcurrent = this.pathcurrent.path_next;
331 this.moveto = this.pathcurrent.origin;
332 this.steerto = steerlib_attract2(this, this.moveto,0.5,500,0.95);
333 walker_move_to(this, this.moveto, 0);
336 if(vdist(this.origin - this.pathcurrent.origin, <, 64))
337 this.pathcurrent = this.pathcurrent.enemy;
339 if(!this.pathcurrent)
342 this.moveto = this.pathcurrent.origin;
343 this.steerto = steerlib_attract2(this, this.moveto, 0.5, 500, 0.95);
344 walker_move_to(this, this.moveto, 0);
348 spawnfunc(turret_walker) { if(!turret_initialize(this, TUR_WALKER)) delete(this); }
350 METHOD(WalkerTurret, tr_think, void(WalkerTurret thistur, entity it))
352 fixedmakevectors(it.angles);
354 if (it.spawnflags & TSF_NO_PATHBREAK && it.pathcurrent)
355 walker_move_path(it);
356 else if (it.enemy == NULL)
359 walker_move_path(it);
362 if(it.enemy_last_time != 0)
364 if(vdist(it.origin - it.enemy_last_loc, <, 128) || time - it.enemy_last_time > 10)
365 it.enemy_last_time = 0;
367 walker_move_to(it, it.enemy_last_loc, 0);
371 if(it.animflag != ANIM_NO)
373 traceline(it.origin + '0 0 64', it.origin + '0 0 64' + v_forward * 128, MOVE_NORMAL, it);
375 if(trace_fraction != 1.0)
376 it.tur_head.idletime = -1337;
379 traceline(trace_endpos, trace_endpos - '0 0 256', MOVE_NORMAL, it);
380 if(trace_fraction == 1.0)
381 it.tur_head.idletime = -1337;
384 if(it.tur_head.idletime == -1337)
386 it.moveto = it.origin + randomvec() * 256;
387 it.tur_head.idletime = 0;
390 it.moveto = it.moveto * 0.9 + ((it.origin + v_forward * 500) + randomvec() * 400) * 0.1;
391 it.moveto_z = it.origin_z + 64;
392 walker_move_to(it, it.moveto, 0);
395 if(it.idletime < time)
397 if(random() < 0.5 || !(it.spawnflags & TSL_ROAM))
399 it.idletime = time + 1 + random() * 5;
400 it.moveto = it.origin;
401 it.animflag = ANIM_NO;
405 it.animflag = ANIM_WALK;
406 it.idletime = time + 4 + random() * 2;
407 it.moveto = it.origin + randomvec() * 256;
408 it.tur_head.moveto = it.moveto;
409 it.tur_head.idletime = 0;
417 if (it.tur_dist_enemy < (autocvar_g_turrets_unit_walker_melee_range) && it.animflag != ANIM_MELEE)
421 wish_angle = angleofs(it, it.enemy);
422 if (it.animflag != ANIM_SWIM)
423 if (fabs(wish_angle_y) < 15)
425 it.moveto = it.enemy.origin;
426 it.steerto = steerlib_attract2(it, it.moveto, 0.5, 500, 0.95);
427 it.animflag = ANIM_MELEE;
430 else if (it.tur_head.attack_finished_single[0] < time)
432 if(it.tur_head.shot_volly)
434 it.animflag = ANIM_NO;
436 it.tur_head.shot_volly = it.tur_head.shot_volly -1;
437 if(it.tur_head.shot_volly == 0)
438 it.tur_head.attack_finished_single[0] = time + (autocvar_g_turrets_unit_walker_rocket_refire);
440 it.tur_head.attack_finished_single[0] = time + 0.2;
442 if(it.tur_head.shot_volly > 1)
443 walker_fire_rocket(it, gettaginfo(it, gettagindex(it, "tag_rocket01")));
445 walker_fire_rocket(it, gettaginfo(it, gettagindex(it, "tag_rocket02")));
449 if (it.tur_dist_enemy > (autocvar_g_turrets_unit_walker_rocket_range_min))
450 if (it.tur_dist_enemy < (autocvar_g_turrets_unit_walker_rocket_range))
451 it.tur_head.shot_volly = 4;
456 if (it.animflag != ANIM_MELEE)
457 walker_move_to(it, it.enemy.origin, it.tur_dist_enemy);
463 float turny = 0, turnx = 0;
466 real_angle = vectoangles(it.steerto) - it.angles;
472 movelib_brake_simple(it, (autocvar_g_turrets_unit_walker_speed_stop));
476 turny = (autocvar_g_turrets_unit_walker_turn);
477 movelib_brake_simple(it, (autocvar_g_turrets_unit_walker_speed_stop));
481 turny = (autocvar_g_turrets_unit_walker_turn_walk);
482 movelib_move_simple(it, v_forward, (autocvar_g_turrets_unit_walker_speed_walk), 0.6);
486 turny = (autocvar_g_turrets_unit_walker_turn_run);
487 movelib_move_simple(it, v_forward, (autocvar_g_turrets_unit_walker_speed_run), 0.6);
491 turny = (autocvar_g_turrets_unit_walker_turn_strafe);
492 movelib_move_simple(it, v_right * -1, (autocvar_g_turrets_unit_walker_speed_walk), 0.8);
496 turny = (autocvar_g_turrets_unit_walker_turn_strafe);
497 movelib_move_simple(it, v_right, (autocvar_g_turrets_unit_walker_speed_walk), 0.8);
501 it.velocity += '0 0 1' * (autocvar_g_turrets_unit_walker_speed_jump);
508 if(it.frame != ANIM_PAIN)
509 defer(it, 0.25, walker_setnoanim);
514 if(it.frame != ANIM_MELEE)
516 defer(it, 0.41, walker_setnoanim);
517 defer(it, 0.21, walker_melee_do_dmg);
520 movelib_brake_simple(it, (autocvar_g_turrets_unit_walker_speed_stop));
524 turny = (autocvar_g_turrets_unit_walker_turn_swim);
525 turnx = (autocvar_g_turrets_unit_walker_turn_swim);
527 it.angles_x += bound(-10, shortangle_f(real_angle_x, it.angles_x), 10);
528 movelib_move_simple(it, v_forward, (autocvar_g_turrets_unit_walker_speed_swim), 0.3);
529 vz = it.velocity_z + sin(time * 4) * 8;
533 turny = (autocvar_g_turrets_unit_walker_turn_walk);
534 movelib_move_simple(it, v_forward ,(autocvar_g_turrets_unit_walker_speed_roam), 0.5);
540 turny = bound( turny * -1, shortangle_f(real_angle_y, it.angles_y), turny );
541 it.angles_y += turny;
546 turnx = bound( turnx * -1, shortangle_f(real_angle_x, it.angles_x), turnx );
547 it.angles_x += turnx;
554 if(it.origin != it.oldorigin)
555 it.SendFlags |= TNSF_MOVE;
557 it.oldorigin = it.origin;
558 turrets_setframe(it, it.animflag, false);
560 METHOD(WalkerTurret, tr_death, void(WalkerTurret this, entity it))
562 #ifdef WALKER_FANCYPATHING
564 pathlib_deletepath(it.pathcurrent.owner);
566 it.pathcurrent = NULL;
568 METHOD(WalkerTurret, tr_setup, void(WalkerTurret this, entity it))
574 // Respawn is called & first spawn to, to set team. need to make sure we do not move the initial spawn.
575 if(it.move_movetype == MOVETYPE_WALK)
578 setorigin(it, it.pos1);
583 it.ammo_flags = TFL_AMMO_BULLETS | TFL_AMMO_RECHARGE | TFL_AMMO_RECIEVE;
584 it.aim_flags = TFL_AIM_LEAD;
585 it.turret_flags |= TUR_FLAG_HITSCAN;
587 it.target_select_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
588 it.target_validate_flags = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
589 it.iscreature = true;
590 it.teleportable = TELEPORT_NORMAL;
591 it.damagedbycontents = true;
592 it.solid = SOLID_SLIDEBOX;
593 it.takedamage = DAMAGE_AIM;
594 if(it.move_movetype != MOVETYPE_WALK)
596 setorigin(it, it.origin);
597 tracebox(it.origin + '0 0 128', it.mins, it.maxs, it.origin - '0 0 10000', MOVE_NORMAL, it);
598 setorigin(it, trace_endpos + '0 0 4');
602 set_movetype(it, MOVETYPE_WALK);
603 it.idle_aim = '0 0 0';
604 it.turret_firecheckfunc = walker_firecheck;
608 e = find(NULL, targetname, it.target);
611 LOG_TRACE("Initital waypoint for walker does NOT exsist, fix your map!");
615 if (e.classname != "turret_checkpoint")
616 LOG_TRACE("Warning: not a turrret path");
619 #ifdef WALKER_FANCYPATHING
620 it.pathcurrent = WALKER_PATH(it, it.origin, e.origin);
632 #include <common/physics/movelib.qh>
634 void walker_draw(entity this)
638 dt = time - this.move_time;
639 this.move_time = time;
643 fixedmakevectors(this.angles);
644 movelib_groundalign4point(this, 300, 100, 0.25, 45);
645 setorigin(this, this.origin + this.velocity * dt);
646 this.tur_head.angles += dt * this.tur_head.avelocity;
648 if (this.health < 127)
650 te_spark(this.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
653 METHOD(WalkerTurret, tr_setup, void(WalkerTurret this, entity it))
656 set_movetype(it, MOVETYPE_BOUNCE);
658 it.draw = walker_draw;