From badc482d63df8eb117f259445c08bd36596d836b Mon Sep 17 00:00:00 2001 From: e-dt Date: Mon, 3 Jan 2022 13:31:25 +1100 Subject: [PATCH] Made pawns move like queens; fixed various bugs making blocking not work; implemented symmetry flags --- src/apr.rs | 42 ++++++++++++++++----------- src/behaviour.rs | 74 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 28 deletions(-) diff --git a/src/apr.rs b/src/apr.rs index db08dbc..bf86394 100644 --- a/src/apr.rs +++ b/src/apr.rs @@ -1,13 +1,3 @@ -//A NOTE TO WHOMSOEVER READS THIS FILE -//EVERYTHING THAT TOUCHES THE BOARD STRUCT IS FUCKED SINCE IT GOES IN ROW-COLUMN AND EVERYTHING ELSE GOES IN COLUMN-ROW -//REFACTORING IS A MUST -//PLEASE LOOK AT ALL USES OF BOARD STRUCT WHEN YOU DO THIS - -//a double note -//I JUST REFACTORED IT -//BUT IM KEEPING THIS COMMENT TIL YOU MOTHERFUCKERS READ IT -//BECAUSE I WANT YOU TO KNOW THE PAIN I HAVE FELT - use ggez::event::EventHandler; use ggez::graphics::{self, Color, DrawMode, Mesh, MeshBuilder}; use ggez::input::mouse::{self, MouseButton}; @@ -62,20 +52,38 @@ pub fn make_grid( impl Apr { pub fn new(_ctx: &mut Context, xlen: u8, ylen: u8) -> GameResult { // Load/create resources such as images here. - let board = board::make_board(xlen, ylen); + let mut board = board::make_board(xlen, ylen); let mut actors: Vec = Vec::new(); let mut behaviours: Vec = vec![behaviour::Behaviour { - moves: vec![behaviour::MovePattern { - movement: (0, -1), - amount: 1, - }], + moves: vec![ + behaviour::MovePattern { + movement: (1, 0), + amount: 255, + negx: true, + negy: false, + }, + behaviour::MovePattern { + movement: (0, 1), + amount: 255, + negx: false, + negy: true, + }, + behaviour::MovePattern { + movement: (1, 1), + amount: 255, + negx: true, + negy: true, + }, + ], }]; //pawn can move ONE forward, ONCE. not really the best test for i in 0..xlen { // white side pawns actors.push(actor::Actor::new(_ctx, "/pawn.png", i, 6, 0)); + board.board[usize::from(i)][6].1 = i; } actors.push(actor::Actor::new(_ctx, "/pawn.png", 4, 4, 0)); //blocking pawn for testing + board.board[4][4].1 = xlen; Ok(Apr { grid: make_grid(_ctx, xlen, ylen, board.board.as_slice())?, @@ -123,8 +131,8 @@ impl EventHandler for Apr { y: prev_y, }; } else { - self.board.board[usize::from(prev_y)][usize::from(prev_x)].1 = 255; - self.board.board[usize::from(y)][usize::from(x)].1 = index as u8; //only 255 actors allowed? should be fine (laughing) (we look back at this in 10 years and die) + self.board.board[usize::from(prev_x)][usize::from(prev_y)].1 = 255; + self.board.board[usize::from(x)][usize::from(y)].1 = index as u8; //only 255 actors allowed? should be fine (laughing) (we look back at this in 10 years and die) //[x][y] or [y][x]?? i think its [y][x] since row-column //update, eveyrthing is fucked actor.posn = actor::ActorPosn::BoardPosn { x, y }; diff --git a/src/behaviour.rs b/src/behaviour.rs index 6ad43db..0152104 100644 --- a/src/behaviour.rs +++ b/src/behaviour.rs @@ -1,27 +1,81 @@ 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 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 - //todo: flags for "on first move only", etc, etc, and corresponding state - //also todo, probably in validate_dest too: implement 'flipped y for enemy' + //todo: flags for "on first move only", etc, etc, and corresponding state + //also todo, probably in validate_dest too: implement 'flipped y for enemy' } pub struct Behaviour { pub moves: Vec, } +pub struct MovePatIt<'a> { + pub iter: std::slice::Iter<'a, MovePattern>, + pub cache: Vec, //MAKE THIS AN ARRAYVEC IF NEED SPEED +} + +impl Iterator for MovePatIt<'_> { + type Item = MovePattern; + fn next(&mut self) -> Option { + 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;