@ -1,12 +1,11 @@
use crate ::board ;
#[ derive(Clone, Copy) ]
pub struct MovePattern {
pub movement : ( i8 , i8 ) ,
pub amount : u8 ,
/*
pub negx : bool , //some form of symmetry that is expressed by negating x value. whatever comes out of that is what this is
pub negy : bool
* / //commented out because they complicate implementation
pub negy : bool , //commented out because they complicate implementation
//todo: flags for "on first move only", etc, etc, and corresponding state
//also todo, probably in validate_dest too: implement 'flipped y for enemy'
@ -16,12 +15,67 @@ pub struct Behaviour {
pub moves : Vec < MovePattern > ,
}
pub struct MovePatIt < 'a > {
pub iter : std ::slice ::Iter < 'a , MovePattern > ,
pub cache : Vec < MovePattern > , //MAKE THIS AN ARRAYVEC IF NEED SPEED
}
impl Iterator for MovePatIt < '_ > {
type Item = MovePattern ;
fn next ( & mut self ) -> Option < Self ::Item > {
match self . cache . pop ( ) {
Some ( x ) = > return Some ( x ) ,
None = > ( ) ,
}
let k = self . iter . next ( ) ? ;
let ( xm , ym ) = k . movement ;
if k . negx & & k . negy {
self . cache . push ( MovePattern {
movement : ( - xm , - ym ) ,
amount : k . amount ,
negx : false ,
negy : false ,
} ) ;
}
if k . negx {
self . cache . push ( MovePattern {
movement : ( - xm , ym ) ,
amount : k . amount ,
negx : false ,
negy : false ,
} ) ;
}
if k . negy {
self . cache . push ( MovePattern {
movement : ( xm , - ym ) ,
amount : k . amount ,
negx : false ,
negy : false ,
} ) ;
}
return Some ( * k ) ;
}
} //if naive sucks
impl Behaviour {
// pub fn new(m: Vec<(i8, i8)>, d: Vec<(i8, i8)>) -> Behaviour {
// Behaviour { moves: m, dirs: d }
// }
//We don't need a `new' until it's not literally just an alias for the name of the struct.
pub fn iter ( & self ) -> MovePatIt {
MovePatIt {
iter : self . moves . iter ( ) ,
cache : vec ! [ ] ,
}
}
pub fn validate_dest (
& self ,
origin : ( u8 , u8 ) ,
@ -33,8 +87,9 @@ impl Behaviour {
//just write down all you WANT to optimise as a todo.
//will we get around to it?
//who knows
'moves : for moveset in self . moves . iter ( ) {
'moves : for moveset in self . iter ( ) {
let mut posn : ( i16 , i16 ) = ( origin . 0. into ( ) , origin . 1. into ( ) ) ;
let orig = posn ;
let mut fuel = moveset . amount ;
let dest_from : ( i16 , i16 ) = ( destination . 0. into ( ) , destination . 1. into ( ) ) ;
while posn . 0 > = 0
@ -42,23 +97,20 @@ impl Behaviour {
& & posn . 1 > = 0
& & posn . 1 < board . ylen . into ( )
{
//println!("{:?}, {:?}", posn, dest_from);
if posn = = dest_from {
return true ; //hey hey! we got there
}
if board . board [ posn . 1 as usize ] [ posn . 0 as usize ] . 1 ! = 255 {
if posn ! = orig & & board . board [ posn . 0 as usize ] [ posn . 1 as usize ] . 1 ! = 255 {
//`as' is safe because we guarantee it's above 0 in the while.
//don't do captures yet
//lol
//mvp
//lol
continue 'moves ; //abort this line of movement
}
if fuel = = 0 {
//println!("Augh!!");
continue 'moves ; //gone all i can
}
fuel - = 1 ;