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