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