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