diff --git a/javascript/main.js b/javascript/main.js index 44e9557..7ba033a 100644 --- a/javascript/main.js +++ b/javascript/main.js @@ -61,6 +61,11 @@ global.suicide_all = function() { } } +global.reset_ivm = function() { + console.log("Resetting IVM..."); + Game.cpu.halt(); +} + function run_loop() { if (ERROR) { // Stops memory leak present in WASM if rust were to error out. @@ -75,10 +80,8 @@ function run_loop() { console.error = console_error try { - - if (!EXECUTION_PAUSED) { - wasm_module.loop(); - } + wasm_module.loop(); + if (RED_BUTTON) { wasm_module.red_button(); global.wipe_memory(); @@ -98,6 +101,7 @@ function run_loop() { let wasm_module; module.exports.loop = function () { + if (EXECUTION_PAUSED) {console.log("Execution is paused, not running..."); return;} // Fixes a memory corruption issue. if (!global.Memory) { global.RawMemory._parsed = {}; global.Memory = {}; global.Memory.rooms = {} diff --git a/screeps.toml b/screeps.toml index f707e24..d4cb238 100644 --- a/screeps.toml +++ b/screeps.toml @@ -21,7 +21,9 @@ prefix = "ptr" # Linux Laptop #destination = "/home/jwjoh/.config/Screeps/scripts/127_0_0_1___21025" # Bot arena -destination = "/home/jwjoh/.config/Screeps/scripts/botarena_screepspl_us___21025" +#destination = "/home/jwjoh/.config/Screeps/scripts/botarena_screepspl_us___21025" +# Botarematch +destination = "/home/jwjoh/.config/Screeps/scripts/screeps_infinity_dev_xyz___21025" branch = "default" [copy.build] diff --git a/src/memory.rs b/src/memory.rs index e7ffaa7..01c4ae0 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,10 +1,10 @@ use std::{cmp, collections::HashMap}; use log::error; -use screeps::{game, look::{self, LookResult}, Creep, HasPosition, ObjectId, Part, RawObjectId, ResourceType, Room, RoomName, Source, StructureLink, Terrain}; +use screeps::{game, look::{self, LookResult}, Creep, HasPosition, ObjectId, Part, RawObjectId, ResourceType, Room, RoomName, Source, StructureContainer, StructureLink, Terrain}; use serde::{Deserialize, Serialize}; -use js_sys::JsString; +use js_sys::{JsString, Object}; use crate::{room::{self, cache::hauling::{HaulingPriority, HaulingType}}, MEMORY_VERSION}; @@ -37,8 +37,8 @@ pub struct CreepMemory{ #[serde(rename = "1")] pub path: Option, // Career - #[serde(rename = "2")] - pub role: Role, + //#[serde(rename = "2")] + //pub role: Role, // Needs Energy? #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "3")] diff --git a/src/room/cache/creeps.rs b/src/room/cache/creeps.rs index dd34b04..36833a7 100644 --- a/src/room/cache/creeps.rs +++ b/src/room/cache/creeps.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use screeps::{find, Creep, Room, SharedCreepProperties}; -use crate::memory::{Role, ScreepsMemory, ALLIES}; +use crate::{memory::{Role, ScreepsMemory, ALLIES}, utils}; #[derive(Debug, Clone)] pub struct CreepCache { @@ -40,10 +40,13 @@ impl CreepCache { let creep_memory = creep_memory.unwrap(); - if let Some(role_vec) = self.creeps_of_role.get_mut(&creep_memory.role) { + let role = utils::name_to_role(&creep.name()); + if role.is_none() { continue; } + + if let Some(role_vec) = self.creeps_of_role.get_mut(&role.unwrap()) { role_vec.push(creep.name()); } else { - self.creeps_of_role.insert(creep_memory.role, vec![creep.name()]); + self.creeps_of_role.insert(role.unwrap(), vec![creep.name()]); } self.creeps.insert(creep.name(), creep); diff --git a/src/room/cache/hauling.rs b/src/room/cache/hauling.rs index c60924f..ed159e5 100644 --- a/src/room/cache/hauling.rs +++ b/src/room/cache/hauling.rs @@ -1,11 +1,13 @@ use std::{cmp::Ordering, collections::HashMap}; use log::info; -use screeps::{game, Creep, HasPosition, Position, RawObjectId, Resource, ResourceType, SharedCreepProperties}; +use screeps::{game, Creep, HasId, HasPosition, Position, RawObjectId, Resource, ResourceType, SharedCreepProperties}; use serde::{Deserialize, Serialize}; use crate::memory::{CreepHaulTask, ScreepsMemory}; +use super::structures::RoomStructureCache; + #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)] pub enum HaulingPriority { Combat = 0, @@ -70,26 +72,18 @@ impl HaulingCache { self.new_orders.insert(id, order); } - pub fn find_new_order(&mut self, creep: &Creep, memory: &mut ScreepsMemory, resource: Option, order_type: Option) -> Option { + pub fn find_new_order(&mut self, creep: &Creep, memory: &mut ScreepsMemory, resource: Option, order_type: Vec) -> Option { let unsorted_orders = self.new_orders.values().collect::>(); let mut orders = unsorted_orders.clone(); - if let Some(order_type) = order_type { - orders.retain(|x| x.haul_type == order_type); - } + orders.retain(|x| order_type.contains(&x.haul_type)); + if let Some(resource_type) = resource { orders.retain(|rsc| rsc.resource == resource_type); } orders.sort_by(|a, b| a.priority.cmp(&b.priority)); - if creep.store().get_used_capacity(Some(ResourceType::Energy)) > 0 { - orders.retain(|x| x.haul_type == HaulingType::Transfer); - info!("Hauling: Deposit or Transfer {}", orders.len()); - } else { - orders.retain(|x| x.haul_type == HaulingType::Withdraw || x.haul_type == HaulingType::Pickup); - } - if let Some(order) = orders.into_iter().next() { let id = order.id; let order = self.new_orders.get_mut(&id).unwrap(); @@ -109,6 +103,23 @@ impl HaulingCache { } None } + + pub fn haul_ruins(&mut self, structures: &RoomStructureCache) { + let ruins = &structures.ruins; + + for ruin in ruins.values() { + if ruin.store().get_used_capacity(Some(ResourceType::Energy)) > 0 { + self.create_order( + ruin.raw_id(), + ResourceType::Energy, + ruin.store().get_used_capacity(Some(ResourceType::Energy)), + HaulingPriority::Energy, + HaulingType::Offer + ); + return; + } + } + } } impl CreepHaulTask { diff --git a/src/room/cache/structures.rs b/src/room/cache/structures.rs index 1015b0a..da8cca7 100644 --- a/src/room/cache/structures.rs +++ b/src/room/cache/structures.rs @@ -1,6 +1,7 @@ use std::{cmp, collections::HashMap}; -use screeps::{find, game, look::{self, LookResult}, ConstructionSite, Creep, HasId, HasPosition, LocalCostMatrix, LocalRoomTerrain, ObjectId, Part, Resource, ResourceType, Room, Source, Structure, StructureContainer, StructureController, StructureExtension, StructureLink, StructureObject, StructureRoad, StructureSpawn, StructureTower, Terrain}; +use log::info; +use screeps::{find, game, look::{self, LookResult}, ConstructionSite, Creep, HasId, HasPosition, LocalCostMatrix, LocalRoomTerrain, ObjectId, OwnedStructureProperties, Part, Resource, ResourceType, Room, Ruin, Source, Structure, StructureContainer, StructureController, StructureExtension, StructureLink, StructureObject, StructureRoad, StructureSpawn, StructureTower, Terrain}; use crate::memory::ScreepsMemory; @@ -9,7 +10,12 @@ use super::hauling::{HaulingCache, HaulingPriority, HaulingType}; #[derive(Debug, Clone)] pub struct CachedSource { pub id: ObjectId, - pub creeps: Vec> + pub creeps: Vec>, + + pub link: Option>, + pub container: Option>, + + pub csites: Vec, } #[derive(Debug, Clone)] @@ -18,6 +24,7 @@ pub struct RoomStructureCache { pub construction_sites: Vec, pub sources: Vec, + pub ruins: HashMap, Ruin>, pub spawns: HashMap, StructureSpawn>, pub extensions: HashMap, StructureExtension>, pub containers: HashMap, StructureContainer>, @@ -38,6 +45,7 @@ impl RoomStructureCache { construction_sites: Vec::new(), sources: Vec::new(), + ruins: HashMap::new(), towers: HashMap::new(), spawns: HashMap::new(), containers: HashMap::new(), @@ -59,6 +67,7 @@ impl RoomStructureCache { cache.refresh_structure_cache(room); cache.refresh_spawn_cache(room); cache.refresh_construction_cache(room); + cache.refresh_ruin_cache(room); cache } @@ -89,6 +98,21 @@ impl RoomStructureCache { let used_capacity = container.store().get_used_capacity(Some(ResourceType::Energy)); let max_capacity = container.store().get_capacity(Some(ResourceType::Energy)); + let mut i = 0; + let mut matching = false; + loop { + if self.sources.len() <= i { break } + if matching { break } + + if let Some(source_container) = self.sources[i].get_container() { + if container.id() == source_container.id() { + matching = true; + } + } + + i += 1; + } + hauling.create_order( container.raw_id(), ResourceType::Energy, @@ -97,7 +121,7 @@ impl RoomStructureCache { HaulingType::Offer ); - if (used_capacity as f32) < (max_capacity as f32 * 0.5) { + if !matching && (used_capacity as f32) <= (max_capacity as f32 * 0.5) { hauling.create_order( container.raw_id(), ResourceType::Energy, @@ -109,8 +133,20 @@ impl RoomStructureCache { } } + pub fn refresh_ruin_cache(&mut self, room: &Room) { + //if game::time() % 100 != 0 { + // return; + //} + + let ruins = room.find(find::RUINS, None).into_iter(); + + for ruin in ruins { + self.ruins.insert(ruin.id(), ruin); + } + } + pub fn refresh_structure_cache(&mut self, room: &Room) { - let structures = room.find(find::MY_STRUCTURES, None).into_iter(); + let structures = room.find(find::STRUCTURES, None).into_iter(); for structure in structures { @@ -118,12 +154,15 @@ impl RoomStructureCache { match structure { StructureObject::StructureTower(tower) => { + if !tower.my() {continue;} self.towers.insert(tower.id(), tower); } StructureObject::StructureExtension(extension) => { + if !extension.my() {continue;} self.extensions.insert(extension.id(), extension); } StructureObject::StructureLink(link) => { + if !link.my() {continue;} self.links.insert(link.id(), link); } StructureObject::StructureRoad(road) => { @@ -146,9 +185,15 @@ impl RoomStructureCache { pub fn refresh_source_cache(&mut self, room: &Room) { let sources = room.find(find::SOURCES, None); for source in sources { + let csites = source.pos().find_in_range(find::CONSTRUCTION_SITES, 1); + let constructed_source = CachedSource { id: source.id(), - creeps: Vec::new() + creeps: Vec::new(), + + link: None, + container: None, + csites, }; self.sources.push(constructed_source); @@ -157,6 +202,31 @@ impl RoomStructureCache { } impl CachedSource { + pub fn get_container(&mut self) -> Option { + if let Some(container_id) = self.container { + return Some(game::get_object_by_id_typed(&container_id).unwrap()); + } + + let source = game::get_object_by_id_typed(&self.id).unwrap(); + let pos = source.pos(); + + let mut find = pos.find_in_range(find::STRUCTURES, 1); + find.retain(|c| { + matches!(c, StructureObject::StructureContainer(_)) + }); + + if !find.is_empty() { + let container = find[0].clone(); + if let StructureObject::StructureContainer(container) = container { + self.container = Some(container.id()); + return Some(container); + } + return None; + } + + None + } + pub fn parts_needed(&self) -> u8 { let source: Source = game::get_object_by_id_typed(&self.id).unwrap(); let max_energy = source.energy_capacity(); diff --git a/src/room/creeps/local/builder.rs b/src/room/creeps/local/builder.rs index 0945e38..dfff4da 100644 --- a/src/room/creeps/local/builder.rs +++ b/src/room/creeps/local/builder.rs @@ -15,7 +15,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach } if needs_energy || creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 { - let _ = creep.say("🌩ī¸", false); + let _ = creep.say("📋", false); find_energy(creep, memory, cache); } else { build(creep, creep_memory, cache) @@ -28,7 +28,7 @@ pub fn build(creep: &Creep, creepmem: &mut CreepMemory, cache: &mut RoomCache) { if !sites.is_empty() { let site = sites.first().unwrap(); if site.pos().get_range_to(creep.pos()) > 1 { - let _ = creep.say("đŸšļ", false); + let _ = creep.say("🚚", false); creep.better_move_to(creepmem, site.pos(), 1); } else { let _ = creep.say("🔨", false); @@ -52,8 +52,8 @@ pub fn find_energy(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCa execute_order(creep, creepmem_mut, task); } else { + let new_order = cache.hauling.find_new_order(creep, memory, Some(ResourceType::Energy), vec![HaulingType::Offer, HaulingType::Pickup]); - let new_order = cache.hauling.find_new_order(creep, memory, Some(ResourceType::Energy), Some(HaulingType::Offer)); if let Some(order) = new_order { execute_order(creep, memory.creeps.get_mut(&creep.name()).unwrap(), &order); } diff --git a/src/room/creeps/local/hauler.rs b/src/room/creeps/local/hauler.rs index 4a43790..f614d44 100644 --- a/src/room/creeps/local/hauler.rs +++ b/src/room/creeps/local/hauler.rs @@ -1,36 +1,53 @@ +use std::vec; + +use log::info; use screeps::{ - game, Creep, HasPosition, ObjectId, Resource, ResourceType, SharedCreepProperties, Structure, StructureObject, StructureStorage + game, Creep, HasPosition, HasStore, ObjectId, Resource, ResourceType, Ruin, SharedCreepProperties, Structure, StructureContainer, StructureObject, StructureStorage }; use wasm_bindgen::JsCast; use crate::{ - memory::{CreepHaulTask, CreepMemory, ScreepsMemory}, room::cache::{hauling::HaulingType, RoomCache}, traits::creep::CreepExtensions + memory::{CreepHaulTask, CreepMemory, ScreepsMemory}, + room::cache::{hauling::HaulingType, RoomCache}, + traits::creep::CreepExtensions, }; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) { - let creep_memory = memory.creeps.get(&creep.name()).unwrap(); - if creep.spawning() || creep.tired() { let _ = creep.say("😴", false); return; } - let order = &creep_memory.hauling_task.clone(); - if let Some(order) = order { + if let Some(order) = &memory.creeps.get(&creep.name()).unwrap().hauling_task.clone() { execute_order(creep, memory.creeps.get_mut(&creep.name()).unwrap(), order); } else { let _ = creep.say("📋", false); - let new_order = cache.hauling.find_new_order(creep, memory, None, None); + + let new_order = if memory.creeps.get(&creep.name()).unwrap().needs_energy.unwrap_or(false) { + cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Pickup, HaulingType::Withdraw, HaulingType::Offer]) + } else { + cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Transfer]) + }; + if let Some(order) = new_order { execute_order(creep, memory.creeps.get_mut(&creep.name()).unwrap(), &order); } + + let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); + + if creep.store().get_free_capacity(None) == 0 { + creep_memory.needs_energy = None; + } + + if creep.store().get_used_capacity(None) == 0 { + creep_memory.needs_energy = Some(true); + } } } pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, order: &CreepHaulTask) { let pickup_target = order.target_id; - let position = order.get_target_position(); if position.is_none() { @@ -38,44 +55,55 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, order: &Cree return; } - let position = position.unwrap(); - - if position.get_range_to(creep.pos()) > 1 { + if position.unwrap().get_range_to(creep.pos()) > 1 { let _ = creep.say("🚚", false); - creep.better_move_to(creep_memory, position, 1); + creep.better_move_to(creep_memory, position.unwrap(), 1); return; } + let _ = creep.say("đŸ“Ļ", false); - let mut success = false; + let target = game::get_object_by_id_erased(&pickup_target); + // || target.as_ref().unwrap().unchecked_ref::().store().get_free_capacity(Some(order.resource)) == 0 + if target.is_none() || creep.store().get_free_capacity(None) == 0 { + creep_memory.hauling_task = None; + return; + } - let _ = creep.say("đŸ“Ļ", false); + let mut success = false; match order.haul_type { HaulingType::Pickup => { - if position.get_range_to(creep.pos()) <= 1 { - let resource: Option = game::get_object_by_id_typed(&ObjectId::from(pickup_target)); - if let Some(resource) = resource { - let _ = creep.pickup(&resource); - success = true; - } - } else { - creep.better_move_to(creep_memory, position, 1); + let resource: Option = game::get_object_by_id_typed(&ObjectId::from(pickup_target)); + if let Some(resource) = resource { + let _ = creep.pickup(&resource); + success = true; + } + } + HaulingType::Withdraw => { + if let Some(target) = target { + let amount = std::cmp::min(creep.store().get_free_capacity(Some(ResourceType::Energy)), order.amount as i32); + let _ = creep.withdraw(target.unchecked_ref::(), order.resource, Some(amount.try_into().unwrap())); } - }, - HaulingType::Withdraw => todo!(), + } HaulingType::Transfer => { - let target = game::get_object_by_id_erased(&pickup_target); if let Some(target) = target { - let _ = creep.transfer(target.unchecked_ref::(), ResourceType::Energy, None); + let _ = creep.transfer( + target.unchecked_ref::(), + ResourceType::Energy, + None, + ); success = true; } - }, + } HaulingType::Offer => { - success = true; + if let Some(target) = target { + let amount = std::cmp::min(creep.store().get_free_capacity(Some(order.resource)), order.amount as i32); + let _ = creep.withdraw(target.unchecked_ref::(), order.resource, Some(amount.try_into().unwrap())); + } } - } + }; if success { creep_memory.hauling_task = None; } -} \ No newline at end of file +} diff --git a/src/room/creeps/local/source_miner.rs b/src/room/creeps/local/source_miner.rs index 8dfa821..a371348 100644 --- a/src/room/creeps/local/source_miner.rs +++ b/src/room/creeps/local/source_miner.rs @@ -1,6 +1,7 @@ use std::str::FromStr; -use screeps::{game, Creep, ErrorCode, HasId, HasPosition, MaybeHasId, Part, ResourceType, RoomName, SharedCreepProperties, Source}; +use log::info; +use screeps::{game, Creep, ErrorCode, HasHits, HasId, HasPosition, MaybeHasId, Part, ResourceType, RoomName, SharedCreepProperties, Source}; use crate::{memory::{CreepMemory, Role, ScreepsMemory}, room::cache::{hauling::{HaulingPriority, HaulingType}, RoomCache}, traits::creep::CreepExtensions}; @@ -66,7 +67,7 @@ fn needs_haul_manually(creep: &Creep, creep_memory: &mut CreepMemory, cache: &Ro fn harvest_source(creep: &Creep, source: Source, memory: &mut CreepMemory) { if !creep.pos().is_near_to(source.pos()) { - let _ = creep.say("âŦ†ī¸", false); + let _ = creep.say("🚚", false); creep.better_move_to(memory, source.pos(), 1); } else { let _ = creep.say("⛏ī¸", false); @@ -96,11 +97,60 @@ fn drop_deposit(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomC return; } + if build_around_source(creep, creep_memory, cache) { return; } + if repair_container(creep, creep_memory, cache) { return; } let _ = creep.say("đŸ“Ļ", false); - let amount = creep.store().get_used_capacity(Some(ResourceType::Energy)); - let _ = creep.drop(ResourceType::Energy, Some(amount)); + if let Some(container) = cache.structures.sources[creep_memory.task_id.unwrap() as usize].get_container() { + if creep.pos().is_near_to(container.pos()) { + let _ = creep.transfer(&container, ResourceType::Energy, None); + } else { + creep.better_move_to(creep_memory, container.pos(), 1); + } + } else { + cache.hauling.create_order( + creep.try_raw_id().unwrap(), + ResourceType::Energy, + creep.store().get_used_capacity(Some(ResourceType::Energy)), + HaulingPriority::Energy, + HaulingType::Pickup + ); + let _ = creep.drop(ResourceType::Energy, None); + } +} + +fn build_around_source(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache) -> bool { + let csites = &cache.structures.sources[creep_memory.task_id.unwrap() as usize].csites; + if csites.is_empty() { return false; } + + let csite = csites.first().unwrap(); - //let mut mutable = cache.hauling.borrow_mut(); - cache.hauling.create_order(creep.try_raw_id().unwrap(), ResourceType::Energy, amount, HaulingPriority::Energy, HaulingType::Pickup); + if creep.pos().is_near_to(csite.pos()) { + let _ = creep.say("🔨", false); + let _ = creep.build(csite); + true + } else { + let _ = creep.say("🚚", false); + creep.better_move_to(creep_memory, csite.pos(), 1); + true + } +} + +fn repair_container(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache) -> bool { + let container = cache.structures.sources[creep_memory.task_id.unwrap() as usize].get_container(); + + if let Some(container) = container { + if (container.hits() as f32) < container.hits_max() as f32 * 0.75 { + if container.pos().get_range_to(creep.pos()) > 1 { + let _ = creep.say("🚚", false); + creep.better_move_to(creep_memory, container.pos(), 1); + return true; + } else { + let _ = creep.say("🔧", false); + let _ = creep.repair(&container); + return true; + } + } +} + false } \ No newline at end of file diff --git a/src/room/creeps/organizer.rs b/src/room/creeps/organizer.rs index c8e2d6d..b75eadc 100644 --- a/src/room/creeps/organizer.rs +++ b/src/room/creeps/organizer.rs @@ -4,7 +4,7 @@ use screeps::{game, Room, SharedCreepProperties}; use crate::{ memory::{Role, ScreepsMemory}, room::cache::RoomCache, - traits::creep::CreepExtensions, + traits::creep::CreepExtensions, utils, }; use super::local; @@ -25,9 +25,13 @@ pub fn run_creeps(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache let creep = creep.unwrap(); - let creep_memory = memory.creeps.get(&creep.name()).unwrap(); + if creep.spawning() { return; } - match creep_memory.role { + let role = utils::name_to_role(&creep.name()); + + if role.is_none() { return; } + + match role.unwrap() { Role::Miner => local::source_miner::run_creep(&creep, memory, cache), Role::Hauler => { cache.hauling.haulers.push(creep.name()); diff --git a/src/room/creeps/recovery.rs b/src/room/creeps/recovery.rs index a50e65c..6d59b74 100644 --- a/src/room/creeps/recovery.rs +++ b/src/room/creeps/recovery.rs @@ -19,7 +19,6 @@ pub fn recover_creeps(memory: &mut ScreepsMemory) { if let Some(role) = role { if role == crate::memory::Role::Hauler { let cmemory = CreepMemory { - role: Role::Hauler, needs_energy: None, task_id: None, link_id: None, @@ -31,7 +30,6 @@ pub fn recover_creeps(memory: &mut ScreepsMemory) { memory.create_creep(room, &creep.name(), cmemory); } else if role == crate::memory::Role::Builder { let cmemory = CreepMemory { - role: Role::Builder, needs_energy: None, task_id: None, link_id: None, diff --git a/src/room/democracy.rs b/src/room/democracy.rs index 0952fee..5c0b4a4 100644 --- a/src/room/democracy.rs +++ b/src/room/democracy.rs @@ -15,6 +15,12 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory) { cache.structures.check_containers(&mut cache.hauling); + // Check for dropped resources, making requests for each + // Run haulers to process the requests generated by structures earlier + cache.resources.haul_dropped_resources(&mut cache.hauling); + cache.hauling.haul_ruins(&cache.structures); + cache.structures.temp(&mut cache.hauling); + // Run creeps and other structures // Does NOT run haulers, as they need to be done last // Reasoning for this decision is below @@ -25,10 +31,6 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory) { // TODO: Get a better spawn implementation let _ = formulate_miner(&room, memory, &mut cache); - // Check for dropped resources, making requests for each - // Run haulers to process the requests generated by structures earlier - cache.resources.haul_dropped_resources(&mut cache.hauling); - cache.structures.temp(&mut cache.hauling); for creep in cache.hauling.haulers.clone().iter() { let creep = game::creeps().get(creep.to_string()).unwrap(); hauler::run_creep(&creep, memory, &mut cache); @@ -41,13 +43,13 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory) { recover_creeps(memory); } - let coords = (27, 17); + let coords = cache.structures.spawns.values().next().unwrap().pos(); let mut things = get_bunker_plan(); let mut viz = RoomVisualExt::new(room.name()); for thing in things.clone().iter() { - let x_offset = thing.0 + coords.0; - let y_offset = thing.1 + coords.1; + let x_offset = thing.0 + coords.x().u8() as i8; + let y_offset = thing.1 + coords.y().u8() as i8; //viz.structure(x_offset.into(), y_offset.into(), thing.2, 0.5); //let _ = room.create_construction_site(x_offset as u8, y_offset as u8, thing.2, None); @@ -83,10 +85,14 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory) { for pos in looked { if let LookResult::Terrain(terrain) = pos.look_result { if Terrain::Plain != terrain { continue; } - let _ = room.create_construction_site(pos.x, pos.y, StructureType::Container, None); + let res = room.create_construction_site(pos.x, pos.y, StructureType::Container, None); + if res.is_ok() { + break; + } } } } + memory.rooms.get_mut(&room.name()).unwrap().planned = true; } } } diff --git a/src/room/planning/creep/miner.rs b/src/room/planning/creep/miner.rs index 3c391e5..bc84862 100644 --- a/src/room/planning/creep/miner.rs +++ b/src/room/planning/creep/miner.rs @@ -70,7 +70,6 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room let spawn_result = spawn.spawn_creep(&body, &name); if spawn_result.is_ok() { let cmemory = CreepMemory { - role: Role::Hauler, needs_energy: None, task_id: None, link_id: None, @@ -104,7 +103,6 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room let spawn_result = spawn.spawn_creep(&body, &name); if spawn_result.is_ok() { let cmemory = CreepMemory { - role: Role::Upgrader, needs_energy: None, task_id: None, link_id: None, @@ -138,7 +136,6 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room let spawn_result = spawn.spawn_creep(&body, &name); if spawn_result.is_ok() { let cmemory = CreepMemory { - role: Role::Builder, needs_energy: None, task_id: None, link_id: None, @@ -174,7 +171,6 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room if spawn_result.is_ok() { let cmemory = CreepMemory { - role: crate::memory::Role::Miner, needs_energy: Some(true), task_id: Some(needed.unwrap().into()), hauling_task: None,