Advertisement
Dimenticare

frog AI

Jul 19th, 2020
2,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function duck_ai_module_shoot_on_timer(interval, offset) {
  2.     static states = {
  3.         hold: undefined,
  4.        
  5.         base: function(states, duck_data) constructor {
  6.             self.states = states;
  7.             self.duck_data = duck_data;
  8.        
  9.             var p = "shoot#";
  10.             var max_ammo_key = p + "max-ammo";
  11.             var interval_key = p + "interval";
  12.        
  13.             self.max_ammo = data[? max_ammo_key];
  14.             self.ammo = self.max_ammo;
  15.             self.interval = data[? interval_key];
  16.        
  17.             self.next_fire_time = current_time + self.interval;
  18.        
  19.             Act = function() {
  20.                 if (ammo <= 0) return states.seek_water;
  21.                 if (interval > current_time) return states.hold;
  22.                 ammo--;
  23.                 duck_data.pawn.DuckCast(DuckElementMasks.FIRE);
  24.                 return states.hold;
  25.             };
  26.         },
  27.        
  28.         seek_water: function(states, duck_data) constructor {
  29.             self.states = states;
  30.             self.duck_data = duck_data;
  31.        
  32.             var p = "shoot#";
  33.             var radius_key = p + "radius";
  34.        
  35.             self.seek_radius = data[? radius_key];
  36.        
  37.             Act = function() {
  38.                 var dest = active_map.GetNearestWater(duck_data.pawn.xx, duck_data.pawn.yy, duck_data.pawn.zz, seek_radius);
  39.            
  40.                 if (dest) {
  41.                     duck_ai_module_navigate_to_point(dest.x, dest.y, dest.z, seek_radius);
  42.                     if (path_exists(duck_data.path)) return states.go_water;
  43.                 }
  44.            
  45.                 var tries = 0;
  46.                 var max_tries = 10;
  47.            
  48.                 do {
  49.                     var tx = random_range(max(duck_data.pawn.xx - seek_radius, 0), min(duck_data.pawn.xx + seek_radius, active_map.xx - 1));
  50.                     var ty = random_range(max(duck_data.pawn.yy - seek_radius, 0), min(duck_data.pawn.yy + seek_radius, active_map.yy - 1));
  51.                     var tz = random_range(max(duck_data.pawn.zz - seek_radius, 0), min(duck_data.pawn.zz + seek_radius, active_map.zz - 1));
  52.                     duck_ai_module_navigate_to_point(tx, ty, tz, seek_radius);
  53.                 } until (((tx != duck_data.pawn.xx || ty != duck_data.pawn.yy || tz != duck_data.pawn.zz) && path_exists(duck_data.path)) || tries >= max_tries);
  54.            
  55.                 if (path_exists(duck_data.path)) return states.go_water;
  56.                 return states.repeat_water;
  57.             };
  58.         },
  59.    
  60.         go_water: function(states, duck_data) constructor {
  61.             self.states = states;
  62.             self.duck_data = duck_data;
  63.        
  64.             Act = function() {
  65.                 if (!path_exists(duck_data.path)) return states.refilling;
  66.                 with (duck_data) duck_ai_module_follow_path();
  67.                 return states.hold;
  68.             };
  69.         },
  70.        
  71.         repeat_water: function(states, duck_data) constructor {
  72.             self.states = states;
  73.             self.duck_data = duck_data;
  74.        
  75.             self.end_time = current_time + 5000;
  76.        
  77.             Act = function() {
  78.                 if (current_time >= end_time) return states.seek_water;
  79.                 return states.hold;
  80.             };
  81.         },
  82.        
  83.         refilling: function(states, duck_data) constructor {
  84.             self.states = states;
  85.             self.duck_data = duck_data;
  86.        
  87.             self.end_time = current_time + 2000;
  88.        
  89.             Act = function() {
  90.                 // todo: play animation
  91.                 if (current_time >= end_time) return states.seek_return;
  92.                 return states.hold;
  93.             };
  94.         },
  95.        
  96.         seek_return: function(states, duck_data) constructor {
  97.             self.states = states;
  98.             self.duck_data = duck_data;
  99.        
  100.             var p = "shoot#";
  101.             var x_key = p + "x";
  102.             var y_key = p + "y";
  103.             var z_key = p + "z";
  104.        
  105.             self.start_x = data[? x_key];
  106.             self.start_y = data[? y_key];
  107.             self.start_z = data[? z_key];
  108.        
  109.             Act = function() {
  110.                 with (duck_data) duck_ai_module_navigate_to_point(start_x, start_y, start_z);
  111.                 if (path_exists(duck_data.path)) return states.repeat_return;
  112.                 return states.go_return;
  113.             };
  114.         },
  115.        
  116.         go_return: function(states, duck_data) constructor {
  117.             self.states = states;
  118.             self.duck_data = duck_data;
  119.        
  120.             Act = function() {
  121.                 if (!path_exists(duck_data.path)) return states.base;
  122.                 with (duck_data) duck_ai_module_follow_path();
  123.                 return states.hold;
  124.             };
  125.         },
  126.        
  127.         repeat_return: function(states, duck_data) constructor {
  128.             self.states = states;
  129.             self.duck_data = duck_data;
  130.        
  131.             self.end_time = current_time + 5000;
  132.        
  133.             Act = function() {
  134.                 if (current_time >= end_time) return states.seek_return;
  135.                 return states.hold;
  136.             };
  137.         },
  138.     };
  139.    
  140.     var ammo_quantity = 3;
  141.     var seek_radius = 5;
  142.     var wait_time = 5000;
  143.    
  144.     var p = "shoot#";
  145.     var states_key = p + "states";
  146.     var state_key = p + "state";
  147.     var x_key = p + "x";
  148.     var y_key = p + "y";
  149.     var z_key = p + "z";
  150.    
  151.     if (!data[? state_key]) {
  152.         data[? states_key] = states;
  153.         data[? state_key] = new states.state_base(states, duck_data);
  154.         data[? x_key] = pawn.xx;
  155.         data[? y_key] = pawn.yy;
  156.         data[? z_key] = pawn.zz;
  157.     }
  158.    
  159.     var result = data[? state_key].Act();
  160.     if (result) data[? state_key] = new result(states, duck_data);
  161. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement