rust/lib-hwengine-future/src/ai/mod.rs
author unC0Rr
Tue, 31 Dec 2024 15:18:18 +0100
branchtransitional_engine
changeset 16082 85d7d6b71087
parent 16078 db18f1a30b0c
permissions -rw-r--r--
Add some more work on rust ai
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16069
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
     1
mod action;
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
     2
pub mod ammo;
16082
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
     3
mod attack_tests;
16069
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
     4
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
     5
use crate::GameField;
16069
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
     6
use action::*;
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
     7
use integral_geometry::Point;
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
     8
use std::collections::HashMap;
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
     9
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    10
pub struct Target {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    11
    point: Point,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    12
    health: i32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    13
    radius: u32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    14
    density: f32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    15
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    16
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    17
pub struct Hedgehog {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    18
    pub(crate) x: f32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    19
    pub(crate) y: f32,
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    20
    pub(crate) ammo: [u32; ammo::AmmoType::Count as usize],
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    21
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    22
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    23
pub struct AI<'a> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    24
    game_field: &'a GameField,
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    25
    ammo: [u32; ammo::AmmoType::Count as usize],
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    26
    targets: Vec<Target>,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    27
    team: Vec<Hedgehog>,
16069
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
    28
    planned_actions: Option<Actions>,
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    29
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    30
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    31
#[derive(Clone)]
16082
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    32
pub(crate) struct Waypoint {
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    33
    x: f32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    34
    y: f32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    35
    ticks: usize,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    36
    damage: usize,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    37
    previous_point: Option<(usize, Action)>,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    38
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    39
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    40
#[derive(Default)]
16082
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    41
pub(crate) struct Waypoints {
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    42
    key_points: Vec<Waypoint>,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    43
    points: HashMap<Point, Waypoint>,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    44
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    45
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    46
impl Waypoints {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    47
    fn add_keypoint(&mut self, waypoint: Waypoint) {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    48
        let [x, y] = [waypoint.x, waypoint.y].map(|i| i as i32);
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    49
        let point = Point::new(x, y);
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    50
        self.key_points.push(waypoint.clone());
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    51
        self.points.insert(point, waypoint);
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    52
    }
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    53
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    54
16082
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    55
impl IntoIterator for Waypoints {
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    56
    type Item = Waypoint;
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    57
    type IntoIter = std::collections::hash_map::IntoValues<Point, Waypoint>;
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    58
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    59
    fn into_iter(self) -> Self::IntoIter {
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    60
        self.points.into_values()
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    61
    }
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    62
}
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    63
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    64
impl<'a> AI<'a> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    65
    pub fn new(game_field: &'a GameField) -> AI<'a> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    66
        Self {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    67
            game_field,
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    68
            ammo: [0; ammo::AmmoType::Count as usize],
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    69
            targets: vec![],
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    70
            team: vec![],
16069
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
    71
            planned_actions: None,
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    72
        }
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    73
    }
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    74
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    75
    pub fn set_available_ammo(&mut self, ammo: [u32; ammo::AmmoType::Count as usize]) {
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    76
        self.ammo = ammo;
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    77
    }
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    78
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    79
    pub fn get_team_mut(&mut self) -> &mut Vec<Hedgehog> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    80
        &mut self.team
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    81
    }
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    82
16082
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    83
    pub fn walk(&self, hedgehog: &Hedgehog) {
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    84
        let mut stack = Vec::<usize>::new();
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    85
        let mut waypoints = Waypoints::default();
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    86
16078
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16069
diff changeset
    87
        waypoints.add_keypoint(Waypoint {
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    88
            x: hedgehog.x,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    89
            y: hedgehog.y,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    90
            ticks: 0,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    91
            damage: 0,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    92
            previous_point: None,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    93
        });
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    94
16082
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    95
        while let Some(waypoint) = stack.pop() {
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    96
            // find other positions
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    97
        }
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    98
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
    99
        for Waypoint { x, y, .. } in waypoints {
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   100
            self.analyze_position_attacks(x, y);
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   101
        }
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   102
    }
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   103
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   104
    fn analyze_position_attacks(&self, x: f32, y: f32) {
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   105
        self.ammo
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   106
            .iter()
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   107
            .enumerate()
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   108
            .filter(|&(_, &count)| count > 0u32)
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   109
            .for_each(|(a, &count)| {
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   110
                let a = ammo::AmmoType::try_from(a).expect("What are you iterating over?");
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   111
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   112
                a.analyze_attacks(self.game_field, &self.targets, x, y)
85d7d6b71087 Add some more work on rust ai
unC0Rr
parents: 16078
diff changeset
   113
            });
16068
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
   114
    }
16069
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
   115
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
   116
    pub fn have_plan(&self) -> bool {
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
   117
        self.planned_actions.is_some()
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
   118
    }
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16068
diff changeset
   119
}