# HG changeset patch # User unC0Rr # Date 1735654698 -3600 # Node ID 85d7d6b710872d86097653abaa40b5e39fd4bcd4 # Parent 6633961698add338e1943bb0abe9c6c9d78bf070 Add some more work on rust ai diff -r 6633961698ad -r 85d7d6b71087 hedgewars/uAI2.pas --- a/hedgewars/uAI2.pas Thu Dec 19 14:18:55 2024 +0100 +++ b/hedgewars/uAI2.pas Tue Dec 31 15:18:18 2024 +0100 @@ -6,7 +6,7 @@ procedure initModule; implementation -uses uLandUtils, uFloat, uVariables, uTypes; +uses uLandUtils, uFloat, uVariables, uTypes, uAmmos; {$linklib hwengine_future} @@ -43,7 +43,7 @@ for itAmmo:= Low(TAmmoType) to High(TAmmoType) do ammoCounts[itAmmo]:= HHHasAmmo(CurrentTeam^.Hedgehogs[itHedgehog], itAmmo); - ai_add_team_hedgehog(ai, hwFloat2float(Gear^.X), hwFloat2float(Gear^.Y), ammoCounts) + ai_add_team_hedgehog(ai, hwFloat2float(Gear^.X), hwFloat2float(Gear^.Y), @ammoCounts) end; itHedgehog:= Succ(itHedgehog) mod CurrentTeam^.HedgehogsNumber; until (itHedgehog = currHedgehogIndex); diff -r 6633961698ad -r 85d7d6b71087 rust/lib-hwengine-future/src/ai/ammo.rs --- a/rust/lib-hwengine-future/src/ai/ammo.rs Thu Dec 19 14:18:55 2024 +0100 +++ b/rust/lib-hwengine-future/src/ai/ammo.rs Tue Dec 31 15:18:18 2024 +0100 @@ -63,3 +63,15 @@ Sentry, // 60 Count, } + +impl TryFrom for AmmoType { + type Error = &'static str; + + fn try_from(value: usize) -> Result { + if value < Self::Count as usize { + Ok(unsafe { std::mem::transmute(value) }) + } else { + Err("Invalid ammo type") + } + } +} diff -r 6633961698ad -r 85d7d6b71087 rust/lib-hwengine-future/src/ai/attack_tests.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/lib-hwengine-future/src/ai/attack_tests.rs Tue Dec 31 15:18:18 2024 +0100 @@ -0,0 +1,20 @@ +use crate::ai::ammo::AmmoType; +use crate::ai::Target; +use crate::GameField; + +fn analyze_grenade(game_field: &GameField, targets: &[Target], my_x: f32, my_y: f32) {} + +impl AmmoType { + pub(crate) fn analyze_attacks( + &self, + game_field: &GameField, + targets: &[Target], + my_x: f32, + my_y: f32, + ) { + match self { + AmmoType::Grenade => analyze_grenade(game_field, targets, my_x, my_y), + _ => {} + } + } +} diff -r 6633961698ad -r 85d7d6b71087 rust/lib-hwengine-future/src/ai/mod.rs --- a/rust/lib-hwengine-future/src/ai/mod.rs Thu Dec 19 14:18:55 2024 +0100 +++ b/rust/lib-hwengine-future/src/ai/mod.rs Tue Dec 31 15:18:18 2024 +0100 @@ -1,5 +1,6 @@ mod action; pub mod ammo; +mod attack_tests; use crate::GameField; use action::*; @@ -17,7 +18,6 @@ pub(crate) x: f32, pub(crate) y: f32, pub(crate) ammo: [u32; ammo::AmmoType::Count as usize], - } pub struct AI<'a> { @@ -29,7 +29,7 @@ } #[derive(Clone)] -struct Waypoint { +pub(crate) struct Waypoint { x: f32, y: f32, ticks: usize, @@ -38,7 +38,7 @@ } #[derive(Default)] -pub struct Waypoints { +pub(crate) struct Waypoints { key_points: Vec, points: HashMap, } @@ -52,6 +52,15 @@ } } +impl IntoIterator for Waypoints { + type Item = Waypoint; + type IntoIter = std::collections::hash_map::IntoValues; + + fn into_iter(self) -> Self::IntoIter { + self.points.into_values() + } +} + impl<'a> AI<'a> { pub fn new(game_field: &'a GameField) -> AI<'a> { Self { @@ -71,7 +80,7 @@ &mut self.team } - pub fn walk(hedgehog: &Hedgehog) { + pub fn walk(&self, hedgehog: &Hedgehog) { let mut stack = Vec::::new(); let mut waypoints = Waypoints::default(); @@ -83,7 +92,25 @@ previous_point: None, }); - while let Some(wp) = stack.pop() {} + while let Some(waypoint) = stack.pop() { + // find other positions + } + + for Waypoint { x, y, .. } in waypoints { + self.analyze_position_attacks(x, y); + } + } + + fn analyze_position_attacks(&self, x: f32, y: f32) { + self.ammo + .iter() + .enumerate() + .filter(|&(_, &count)| count > 0u32) + .for_each(|(a, &count)| { + let a = ammo::AmmoType::try_from(a).expect("What are you iterating over?"); + + a.analyze_attacks(self.game_field, &self.targets, x, y) + }); } pub fn have_plan(&self) -> bool { diff -r 6633961698ad -r 85d7d6b71087 rust/lib-hwengine-future/src/lib.rs --- a/rust/lib-hwengine-future/src/lib.rs Thu Dec 19 14:18:55 2024 +0100 +++ b/rust/lib-hwengine-future/src/lib.rs Tue Dec 31 15:18:18 2024 +0100 @@ -11,8 +11,8 @@ use lfprng::LaggedFibonacciPRNG; use mapgen::{theme::Theme, MapGenerator}; use std::fs; +use std::ptr::slice_from_raw_parts; use std::{ffi::CStr, path::Path}; -use std::ptr::slice_from_raw_parts; #[repr(C)] pub struct GameField { @@ -257,11 +257,21 @@ } #[no_mangle] -pub unsafe extern "C" fn ai_add_team_hedgehog(ai: &mut AI, x: f32, y: f32, ammo_counts: *const u32) { - let ammo_counts = &*slice_from_raw_parts(ammo_counts, crate::ai::ammo::AmmoType::Count as usize); +pub unsafe extern "C" fn ai_add_team_hedgehog( + ai: &mut AI, + x: f32, + y: f32, + ammo_counts: *const u32, +) { + let ammo_counts = + &*slice_from_raw_parts(ammo_counts, crate::ai::ammo::AmmoType::Count as usize); let ammo_counts = std::array::from_fn(|i| ammo_counts[i].clone()); - ai.get_team_mut().push(Hedgehog { x, y, ammo: ammo_counts }); + ai.get_team_mut().push(Hedgehog { + x, + y, + ammo: ammo_counts, + }); } #[no_mangle]