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