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