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