rust/lib-hwengine-future/src/ai/mod.rs
author unC0Rr
Thu, 19 Dec 2024 14:18:55 +0100
branchtransitional_engine
changeset 16052 6633961698ad
parent 16049 db18f1a30b0c
permissions -rw-r--r--
Make wider range of numbers representable with FPNum on the cost of smaller precision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16040
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
     1
mod action;
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
     2
pub mod ammo;
16040
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
     3
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
     4
use crate::GameField;
16040
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
     5
use action::*;
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
     6
use integral_geometry::Point;
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
     7
use std::collections::HashMap;
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
     8
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
     9
pub struct Target {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    10
    point: Point,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    11
    health: i32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    12
    radius: u32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    13
    density: f32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    14
}
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    15
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    16
pub struct Hedgehog {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    17
    pub(crate) x: f32,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    18
    pub(crate) y: f32,
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    19
    pub(crate) ammo: [u32; ammo::AmmoType::Count as usize],
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    20
16039
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,
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    25
    ammo: [u32; ammo::AmmoType::Count as usize],
16039
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>,
16040
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    28
    planned_actions: Option<Actions>,
16039
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)]
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    32
struct Waypoint {
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)]
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    41
pub struct Waypoints {
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
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    55
impl<'a> AI<'a> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    56
    pub fn new(game_field: &'a GameField) -> AI<'a> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    57
        Self {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    58
            game_field,
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    59
            ammo: [0; ammo::AmmoType::Count as usize],
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    60
            targets: vec![],
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    61
            team: vec![],
16040
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    62
            planned_actions: None,
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    63
        }
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    64
    }
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    65
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    66
    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: 16040
diff changeset
    67
        self.ammo = ammo;
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    68
    }
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    69
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    70
    pub fn get_team_mut(&mut self) -> &mut Vec<Hedgehog> {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    71
        &mut self.team
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
    pub fn walk(hedgehog: &Hedgehog) {
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    75
        let mut stack = Vec::<usize>::new();
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    76
        let mut waypoints = Waypoints::default();
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    77
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    78
        waypoints.add_keypoint(Waypoint {
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    79
            x: hedgehog.x,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    80
            y: hedgehog.y,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    81
            ticks: 0,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    82
            damage: 0,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    83
            previous_point: None,
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    84
        });
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    85
16049
db18f1a30b0c Implement passing of available ammo to rust AI
unC0Rr
parents: 16040
diff changeset
    86
        while let Some(wp) = stack.pop() {}
16039
a236360669cc Start on AI implementation in rust
unC0Rr
parents:
diff changeset
    87
    }
16040
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    88
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    89
    pub fn have_plan(&self) -> bool {
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    90
        self.planned_actions.is_some()
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    91
    }
6c5b3c576fc6 Add some progress on rust AI
unC0Rr
parents: 16039
diff changeset
    92
}