From 2542c04a9732aa59c01b8a463c17d8bf5938d1be Mon Sep 17 00:00:00 2001 From: InfinityDevTech <47366052+InfinityDevTech@users.noreply.github.com> Date: Tue, 21 May 2024 21:34:01 -0400 Subject: [PATCH] Id breh --- src/lib.rs | 3 + src/memory.rs | 11 ++-- src/room/creeps/local/hauler.rs | 97 +++++++++++++++----------------- src/room/hauling.rs | 54 +++++++++++++++--- src/room/planning/creep/miner.rs | 2 +- src/room/planning/room/mod.rs | 4 +- src/utils.rs | 22 +++++++- 7 files changed, 127 insertions(+), 66 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4281622..6c4cd34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,11 +66,14 @@ pub fn game_loop() { // This is done like this because its basically MemHack for you JS people. memory.write_memory(); + let heap = game::cpu::get_heap_statistics(); + info!("[STATS] Statistics are as follows: "); info!(" GCL {}. Next: {} / {}", game::gcl::level(), game::gcl::progress(), game::gcl::progress_total()); info!(" CPU Usage:"); info!(" Total: {}", game::cpu::get_used()); info!(" Bucket: {}", game::cpu::bucket()); + info!(" Heap: {:.1}/{:.1}", (heap.total_heap_size() / 1000000), (heap.heap_size_limit() / 1000000)); } #[wasm_bindgen(js_name = red_button)] diff --git a/src/memory.rs b/src/memory.rs index 5ef1003..b7a187e 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,12 +1,12 @@ use std::{cmp, collections::HashMap}; use log::error; -use screeps::{game, ObjectId, Resource, ResourceType, RoomName, Source, Structure, StructureLink}; +use screeps::{game, ObjectId, RawObjectId, Resource, ResourceType, RoomName, Source, Structure, StructureLink}; use serde::{Deserialize, Serialize}; use js_sys::JsString; -use crate::{room::hauling::HaulPriorities, MEMORY_VERSION}; +use crate::{room::hauling::{HaulPriorities, HaulType}, MEMORY_VERSION}; #[derive(Clone, Serialize, Deserialize, Debug, PartialEq)] pub enum Role { @@ -43,7 +43,7 @@ pub struct CreepMemory{ // Hauler - A reference to the ID of the current haul orders // Miner - A reference to the source in the vec of sources #[serde(skip_serializing_if = "Option::is_none")] - pub t_id: Option, + pub t_id: Option, } } @@ -61,12 +61,13 @@ pub struct RoomMemory{ pub work_parts: u8, }>, - pub haul_orders: Vec, + pub target_id: RawObjectId, pub target_type: ResourceType, pub responder: Option, + pub haul_type: HaulType, pub amount: u32, }>, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/room/creeps/local/hauler.rs b/src/room/creeps/local/hauler.rs index aa35e8b..112bae7 100644 --- a/src/room/creeps/local/hauler.rs +++ b/src/room/creeps/local/hauler.rs @@ -1,70 +1,65 @@ -use std::cmp::min; +use std::{cmp::min, str::FromStr}; use screeps::{ - find, game, Creep, HasPosition, ResourceType, SharedCreepProperties, StructureObject, + find, game, Creep, HasPosition, ObjectId, Resource, ResourceType, RoomName, SharedCreepProperties, Structure, StructureObject, Transferable }; use crate::{ - memory::{CreepMemory, ScreepsMemory}, - traits::creep::CreepExtensions, + memory::{CreepMemory, ScreepsMemory}, room, traits::creep::CreepExtensions }; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) { let creep_memory = memory.get_creep_mut(&creep.name()); - let _needs_energy = creep_memory.n_e.unwrap_or(false); - if creep.store().get_free_capacity(Some(ResourceType::Energy)) > 0 { - get_energy(creep, creep_memory); + let order = creep_memory.t_id; + if let Some(order_id) = order { + execute_order(creep, memory, order_id) } else { - haul_energy(creep, memory); - } -} - -pub fn get_energy(creep: &Creep, creep_memory: &mut CreepMemory) { - let closest_energy = creep.pos().find_closest_by_range(find::DROPPED_RESOURCES); - if let Some(energy) = closest_energy { - if creep.pos().is_near_to(energy.clone().pos()) { - let _ = creep.pickup(&energy); - } else { - creep.better_move_to(creep_memory, energy.pos(), 1) + let room_memory = memory.get_room_mut(&RoomName::from_str(&creep_memory.o_r).unwrap()); + let new_order = room_memory.find_haul_order(creep, memory); + if let Some(order) = new_order { + execute_order(creep, memory, order.id); } } } -pub fn haul_energy(creep: &Creep, screeps_memory: &mut ScreepsMemory) { - let room_memory = screeps_memory.get_room(&creep.room().unwrap().name()); - let creep_memory = screeps_memory.get_creep_mut(&creep.name()); +pub fn execute_order(creep: &Creep, memory: &mut ScreepsMemory, order_id: u128) { + let room_memory = memory.get_room_mut(&RoomName::from_str(&memory.get_creep(&creep.name()).o_r).unwrap()); + + let order = room_memory.get_haul_order(order_id).unwrap(); + let pickup_target = order.target_id; + + let position = order.get_target_position(); + + if position.get_range_to(creep.pos()) > 1 { + let creep_memory = memory.get_creep_mut(&creep.name()); + creep.better_move_to(creep_memory, position, 1); + return; + } - //let task_id = &room_memory.haul_orders[creep_memory.t_id.unwrap() as usize]; - let task_id = &room_memory.haul_orders[0]; - let target_structure = game::get_object_by_id_typed(&task_id.target_id).unwrap(); - let structure_object = StructureObject::from(target_structure); + match order.haul_type { + crate::room::hauling::HaulType::Pickup => { + let target: Option = game::get_object_by_id_typed(&ObjectId::from(pickup_target)); - if let Some(structure) = structure_object.as_transferable() { - if structure_object - .as_has_store() - .unwrap() - .store() - .get_free_capacity(Some(ResourceType::Energy)) - > 0 - { - if creep.pos().is_near_to(structure.pos()) { - let _ = creep.transfer( - structure, - ResourceType::Energy, - Some(min( - creep.store().get_used_capacity(Some(ResourceType::Energy)), - structure_object - .as_has_store() - .unwrap() - .store() - .get_free_capacity(Some(ResourceType::Energy)) - as u32, - )), - ); - } else { - creep.better_move_to(creep_memory, structure.pos(), 1); + if target.is_none() { + room_memory.destroy_haul_order(order_id); + return; } - } + + let _ = creep.pickup(&target.unwrap()); + }, + crate::room::hauling::HaulType::Deposit => { + //let target = game::get_object_by_id_erased(&pickup_target); + + //if target.is_none() { + // room_memory.destroy_haul_order(order_id); + // return; + //} + + //let transferrable: &dyn Transferable = target.unwrap().as_ref(); + + //let _ = creep.transfer(target.unwrap().as_ref(), order.target_type, Some(min(creep.store().get_used_capacity(Some(order.target_type)), 32))); + }, + crate::room::hauling::HaulType::Withdraw => todo!(), } -} +} \ No newline at end of file diff --git a/src/room/hauling.rs b/src/room/hauling.rs index 142929e..e54bf0c 100644 --- a/src/room/hauling.rs +++ b/src/room/hauling.rs @@ -1,20 +1,34 @@ -use screeps::{Creep, HasId, ResourceType, SharedCreepProperties, Structure}; +use screeps::{game, Creep, HasId, HasPosition, ObjectId, Position, RawObjectId, ResourceType, SharedCreepProperties, Structure}; use serde::{Deserialize, Serialize}; -use crate::memory::{HaulOrder, RoomMemory}; +use crate::memory::{CreepMemory, HaulOrder, RoomMemory, ScreepsMemory}; -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)] pub enum HaulPriorities { Combat = 0, Spawning = 1, Defensive = 2, Normal = 3, + Storage = 4, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)] +pub enum HaulType { + Pickup = 0, + Withdraw = 1, + Deposit = 2, } impl HaulOrder { pub fn add_responder(&mut self, creep: &Creep) { self.responder = Some(creep.name()); } + + pub fn get_target_position(&self) -> Position { + let target = game::get_object_by_id_erased(&self.target_id).unwrap(); + + target.pos() + } } impl RoomMemory { @@ -24,21 +38,47 @@ impl RoomMemory { id } pub fn destroy_haul_order(&mut self, order_id: u128) { - self.haul_orders.retain(|x| x.id != order_id); + self.haul_orders.remove(&order_id); } - pub fn create_haul_order(&mut self, priority: HaulPriorities, target: Structure, resource: ResourceType, amount: u32) { + pub fn create_haul_order(&mut self, priority: HaulPriorities, target_id: RawObjectId, resource: ResourceType, amount: u32, haul_type: HaulType) { let id = self.create_unique_id(); let order = HaulOrder { id, priority, - target_id: target.id(), + target_id, target_type: resource, responder: None, + haul_type, amount, }; - self.haul_orders.push(order); + self.haul_orders.insert(id, order); + } + + pub fn find_haul_order(&mut self, creep: &Creep, memory: &mut ScreepsMemory) -> Option<&HaulOrder> { + let mut orders = self.haul_orders.values().collect::>(); + orders.sort_by(|a, b| a.priority.cmp(&b.priority)); + + let unresponded_order = orders.into_iter().find(|&order| order.responder.is_none()); + + if let Some(order) = unresponded_order { + let order = self.get_haul_order_mut(order.id).unwrap(); + order.add_responder(creep); + let creep_memory = memory.get_creep_mut(&creep.name()); + creep_memory.t_id = Some(order.id); + Some(order) + } else { + None + } + } + + pub fn get_haul_order(&self, order_id: u128) -> Option<&HaulOrder> { + self.haul_orders.get(&order_id) + } + + pub fn get_haul_order_mut(&mut self, order_id: u128) -> Option<&mut HaulOrder> { + self.haul_orders.get_mut(&order_id) } } \ No newline at end of file diff --git a/src/room/planning/creep/miner.rs b/src/room/planning/creep/miner.rs index 4b3bbe2..2f13e94 100644 --- a/src/room/planning/creep/miner.rs +++ b/src/room/planning/creep/miner.rs @@ -62,7 +62,7 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, spawn: Structure let cmemory = CreepMemory { r: crate::memory::Role::Miner, n_e: Some(true), - t_id: Some(needed.unwrap()), + t_id: Some(needed.unwrap().into()), l_id: None, o_r: room.name().to_string(), p: None, diff --git a/src/room/planning/room/mod.rs b/src/room/planning/room/mod.rs index cbc152f..19e0097 100644 --- a/src/room/planning/room/mod.rs +++ b/src/room/planning/room/mod.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use log::info; use screeps::{game, Room}; @@ -18,7 +20,7 @@ pub fn plan_room(room: &Room, memory: &mut ScreepsMemory) -> bool { id: 0, creeps: Vec::new(), sources: Vec::new(), - haul_orders: Vec::new(), + haul_orders: HashMap::new(), links: None, creeps_manufactured: 0 }; diff --git a/src/utils.rs b/src/utils.rs index 980abf7..c24b423 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,24 @@ -use crate::memory::Role; +use screeps::{Room, RoomName}; + +use crate::{memory::Role, traits::room::RoomExtensions}; + +pub fn room_to_number(room: &Room) -> u32 { + let parts = room.split_name(); + + let w_e = match parts.0.as_str() { + "W" => 1, + "E" => 2, + _ => 0, + }; + + let n_s = match parts.2.as_str() { + "N" => 1, + "S" => 2, + _ => 0, + }; + + return w_e + parts.1 + n_s + parts.3; +} pub fn role_to_name(role: Role) -> String { let data = match role {