rust/lib-hwengine-future/src/ai/mod.rs
branchtransitional_engine
changeset 16082 85d7d6b71087
parent 16078 db18f1a30b0c
equal deleted inserted replaced
16081:6633961698ad 16082:85d7d6b71087
     1 mod action;
     1 mod action;
     2 pub mod ammo;
     2 pub mod ammo;
       
     3 mod attack_tests;
     3 
     4 
     4 use crate::GameField;
     5 use crate::GameField;
     5 use action::*;
     6 use action::*;
     6 use integral_geometry::Point;
     7 use integral_geometry::Point;
     7 use std::collections::HashMap;
     8 use std::collections::HashMap;
    15 
    16 
    16 pub struct Hedgehog {
    17 pub struct Hedgehog {
    17     pub(crate) x: f32,
    18     pub(crate) x: f32,
    18     pub(crate) y: f32,
    19     pub(crate) y: f32,
    19     pub(crate) ammo: [u32; ammo::AmmoType::Count as usize],
    20     pub(crate) ammo: [u32; ammo::AmmoType::Count as usize],
    20 
       
    21 }
    21 }
    22 
    22 
    23 pub struct AI<'a> {
    23 pub struct AI<'a> {
    24     game_field: &'a GameField,
    24     game_field: &'a GameField,
    25     ammo: [u32; ammo::AmmoType::Count as usize],
    25     ammo: [u32; ammo::AmmoType::Count as usize],
    27     team: Vec<Hedgehog>,
    27     team: Vec<Hedgehog>,
    28     planned_actions: Option<Actions>,
    28     planned_actions: Option<Actions>,
    29 }
    29 }
    30 
    30 
    31 #[derive(Clone)]
    31 #[derive(Clone)]
    32 struct Waypoint {
    32 pub(crate) struct Waypoint {
    33     x: f32,
    33     x: f32,
    34     y: f32,
    34     y: f32,
    35     ticks: usize,
    35     ticks: usize,
    36     damage: usize,
    36     damage: usize,
    37     previous_point: Option<(usize, Action)>,
    37     previous_point: Option<(usize, Action)>,
    38 }
    38 }
    39 
    39 
    40 #[derive(Default)]
    40 #[derive(Default)]
    41 pub struct Waypoints {
    41 pub(crate) struct Waypoints {
    42     key_points: Vec<Waypoint>,
    42     key_points: Vec<Waypoint>,
    43     points: HashMap<Point, Waypoint>,
    43     points: HashMap<Point, Waypoint>,
    44 }
    44 }
    45 
    45 
    46 impl Waypoints {
    46 impl Waypoints {
    47     fn add_keypoint(&mut self, waypoint: Waypoint) {
    47     fn add_keypoint(&mut self, waypoint: Waypoint) {
    48         let [x, y] = [waypoint.x, waypoint.y].map(|i| i as i32);
    48         let [x, y] = [waypoint.x, waypoint.y].map(|i| i as i32);
    49         let point = Point::new(x, y);
    49         let point = Point::new(x, y);
    50         self.key_points.push(waypoint.clone());
    50         self.key_points.push(waypoint.clone());
    51         self.points.insert(point, waypoint);
    51         self.points.insert(point, waypoint);
       
    52     }
       
    53 }
       
    54 
       
    55 impl IntoIterator for Waypoints {
       
    56     type Item = Waypoint;
       
    57     type IntoIter = std::collections::hash_map::IntoValues<Point, Waypoint>;
       
    58 
       
    59     fn into_iter(self) -> Self::IntoIter {
       
    60         self.points.into_values()
    52     }
    61     }
    53 }
    62 }
    54 
    63 
    55 impl<'a> AI<'a> {
    64 impl<'a> AI<'a> {
    56     pub fn new(game_field: &'a GameField) -> AI<'a> {
    65     pub fn new(game_field: &'a GameField) -> AI<'a> {
    69 
    78 
    70     pub fn get_team_mut(&mut self) -> &mut Vec<Hedgehog> {
    79     pub fn get_team_mut(&mut self) -> &mut Vec<Hedgehog> {
    71         &mut self.team
    80         &mut self.team
    72     }
    81     }
    73 
    82 
    74     pub fn walk(hedgehog: &Hedgehog) {
    83     pub fn walk(&self, hedgehog: &Hedgehog) {
    75         let mut stack = Vec::<usize>::new();
    84         let mut stack = Vec::<usize>::new();
    76         let mut waypoints = Waypoints::default();
    85         let mut waypoints = Waypoints::default();
    77 
    86 
    78         waypoints.add_keypoint(Waypoint {
    87         waypoints.add_keypoint(Waypoint {
    79             x: hedgehog.x,
    88             x: hedgehog.x,
    81             ticks: 0,
    90             ticks: 0,
    82             damage: 0,
    91             damage: 0,
    83             previous_point: None,
    92             previous_point: None,
    84         });
    93         });
    85 
    94 
    86         while let Some(wp) = stack.pop() {}
    95         while let Some(waypoint) = stack.pop() {
       
    96             // find other positions
       
    97         }
       
    98 
       
    99         for Waypoint { x, y, .. } in waypoints {
       
   100             self.analyze_position_attacks(x, y);
       
   101         }
       
   102     }
       
   103 
       
   104     fn analyze_position_attacks(&self, x: f32, y: f32) {
       
   105         self.ammo
       
   106             .iter()
       
   107             .enumerate()
       
   108             .filter(|&(_, &count)| count > 0u32)
       
   109             .for_each(|(a, &count)| {
       
   110                 let a = ammo::AmmoType::try_from(a).expect("What are you iterating over?");
       
   111 
       
   112                 a.analyze_attacks(self.game_field, &self.targets, x, y)
       
   113             });
    87     }
   114     }
    88 
   115 
    89     pub fn have_plan(&self) -> bool {
   116     pub fn have_plan(&self) -> bool {
    90         self.planned_actions.is_some()
   117         self.planned_actions.is_some()
    91     }
   118     }