diff --git a/src/movement/creep.rs b/src/movement/creep.rs new file mode 100644 index 0000000..f44e8fb --- /dev/null +++ b/src/movement/creep.rs @@ -0,0 +1,88 @@ +use log::info; +use screeps::{game, Direction, HasPosition, Position}; + +use crate::memory::{Movement, CreepMemory}; + +use super::move_target::MoveTarget; + +pub fn move_to(creep_name: &String, creep_memory: &mut CreepMemory, target: Position) { + let creep = game::creeps().get(creep_name.to_string()).unwrap(); + match &creep_memory.movement { + Some(path) => { + move_by_path(creep_name.to_string(), path.clone(), creep_memory) + } + None => { + let target = MoveTarget { + pos: target, + range: 1, + }.find_path_to(creep.pos()); + creep_memory.movement = Some(target.clone()); + move_by_path(creep_name.to_string(), target, creep_memory); + } + + } +} + +pub fn move_by_path(creep_name: String, path: Movement, memory: &mut CreepMemory) { + let creep = game::creeps().get(creep_name).unwrap(); + + if creep.fatigue() > 0 { + return; + } + let serialized_path = path.path; + let serialized_vec = serialized_path.split("").filter(|x| x != &"").map(|x| x.parse::().unwrap()).collect::>(); + let step_dir = num_to_dir(serialized_vec[0]); + + match creep.move_direction(step_dir) { + Ok(_) => {}, + Err(e) => info!("Creep move failed, {:?}", e), + }; + + let serialized_vec = serialized_vec[1..].to_vec(); + let serialized_path = serialized_vec.iter().map(|x| x.to_string()).collect::>().join(""); + if serialized_vec.is_empty() { + memory.set_movement(None); + } else { + memory.set_movement(Some(Movement { + dest: path.dest, + path: serialized_path, + room: path.room, + })); + } + + let mut points = vec![]; + let mut cursor = (creep.pos().x().u8() as f32, creep.pos().y().u8() as f32); + for step in serialized_vec { + let dir = num_to_dir(step); + let (x, y) = dir_to_coords(dir, cursor.0, cursor.1); + points.push((x, y)); + cursor = (x, y); + } +} + +pub fn num_to_dir(num: u8) -> Direction { + match num { + 1 => Direction::Top, + 2 => Direction::TopRight, + 3 => Direction::Right, + 4 => Direction::BottomRight, + 5 => Direction::Bottom, + 6 => Direction::BottomLeft, + 7 => Direction::Left, + 8 => Direction::TopLeft, + _ => Direction::Top, + } +} + +pub fn dir_to_coords(dir: Direction, x: f32, y: f32) -> (f32, f32) { + match dir { + Direction::Top => (x, y - 1_f32), + Direction::TopRight => (x + 1_f32, y - 1_f32), + Direction::Right => (x + 1_f32, y), + Direction::BottomRight => (x + 1_f32, y + 1_f32), + Direction::Bottom => (x, y + 1_f32), + Direction::BottomLeft => (x - 1_f32, y + 1_f32), + Direction::Left => (x - 1_f32, y), + Direction::TopLeft => (x - 1_f32, y - 1_f32), + } +} diff --git a/src/room/local.rs b/src/room/local.rs new file mode 100644 index 0000000..ff4f0a7 --- /dev/null +++ b/src/room/local.rs @@ -0,0 +1,16 @@ +use screeps::{Room, find, HasPosition}; + +use crate::memory::RoomMemory; + +pub fn run_rom(room: Room, _roommem: RoomMemory) { + let controller = room.controller().unwrap(); + match controller.sign() { + Some(sign) => { + if sign.text() != "Ferris FTW!" { + if let Some(_creep) = controller.pos().find_closest_by_range(find::MY_CREEPS) { + } + } + }, + None => todo!(), + } +} \ No newline at end of file