diff --git a/src/room/creeps/local/hauler.rs b/src/room/creeps/local/hauler.rs index 1bc96f7..8913d83 100644 --- a/src/room/creeps/local/hauler.rs +++ b/src/room/creeps/local/hauler.rs @@ -21,15 +21,26 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach if let Some(order) = &memory.creeps.get(&creep.name()).unwrap().hauling_task.clone() { execute_order(creep, memory.creeps.get_mut(&creep.name()).unwrap(), cache, order); } else { - let _ = creep.say("📋", false); - let new_order = if memory.creeps.get(&creep.name()).unwrap().needs_energy.unwrap_or(false) { + if creep.store().get_free_capacity(None) == 0 { + memory.creeps.get_mut(&creep.name()).unwrap().needs_energy = None; + } + + let _ = creep.say("NEED", false); + cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Pickup, HaulingType::Withdraw, HaulingType::Offer]) } else { + if creep.store().get_used_capacity(None) == 0 { + memory.creeps.get_mut(&creep.name()).unwrap().needs_energy = Some(true); + } + + let _ = creep.say("DUMP", false); + cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Transfer]) }; if let Some(order) = new_order { + let _ = creep.say("EXEC", false); execute_order(creep, memory.creeps.get_mut(&creep.name()).unwrap(), cache, &order); } } @@ -69,10 +80,6 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut let _ = creep.pickup(&resource); success = true; } - - if creep.store().get_free_capacity(None) >= 0 { - creep_memory.needs_energy = None; - } } HaulingType::Withdraw => { if creep_full { return; } @@ -82,10 +89,6 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut let _ = creep.withdraw(target.unchecked_ref::(), order.resource, Some(amount.try_into().unwrap())); success = true; } - - if creep.store().get_free_capacity(None) >= 0 { - creep_memory.needs_energy = None; - } } HaulingType::Transfer => { let _ = creep.say("TFER", false); @@ -97,10 +100,6 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut ); success = true; } - - if creep.store().get_used_capacity(None) == 0 { - creep_memory.needs_energy = Some(true); - } } HaulingType::Offer => { let _ = creep.say("OFFR", false); @@ -109,10 +108,6 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut let _ = creep.withdraw(target.unchecked_ref::(), order.resource, Some(amount.try_into().unwrap())); success = true; } - - if creep.store().get_free_capacity(None) >= 0 { - creep_memory.needs_energy = None; - } } }; diff --git a/src/room/creeps/local/source_miner.rs b/src/room/creeps/local/source_miner.rs index fe82fc8..1260cf4 100644 --- a/src/room/creeps/local/source_miner.rs +++ b/src/room/creeps/local/source_miner.rs @@ -1,9 +1,18 @@ -use screeps::{game, Creep, ErrorCode, HasHits, HasPosition, MaybeHasId, ResourceType, SharedCreepProperties, Source}; - -use crate::{memory::{CreepMemory, Role, ScreepsMemory}, room::cache::{hauling::{HaulingPriority, HaulingType}, RoomCache}, traits::creep::CreepExtensions}; +use screeps::{ + game, Creep, ErrorCode, HasHits, HasPosition, MaybeHasId, ResourceType, SharedCreepProperties, + Source, +}; + +use crate::{ + memory::{CreepMemory, Role, ScreepsMemory}, + room::cache::{ + hauling::{HaulingPriority, HaulingType}, + RoomCache, + }, + traits::creep::CreepExtensions, +}; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) { - let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); if creep_memory.task_id.is_none() { @@ -12,7 +21,9 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach } let pointer_index = creep_memory.task_id.unwrap() as usize; - cache.structures.sources[pointer_index].creeps.push(creep.try_id().unwrap()); + cache.structures.sources[pointer_index] + .creeps + .push(creep.try_id().unwrap()); let scouted_source = &cache.structures.sources[pointer_index]; let source = game::get_object_by_id_typed(&scouted_source.id).unwrap(); @@ -24,7 +35,9 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach if creep_memory.needs_energy.unwrap_or(false) { harvest_source(creep, source, creep_memory, cache); - if creep.store().get_used_capacity(Some(ResourceType::Energy)) >= creep.store().get_capacity(Some(ResourceType::Energy)) { + if creep.store().get_used_capacity(Some(ResourceType::Energy)) + >= creep.store().get_capacity(Some(ResourceType::Energy)) + { creep_memory.needs_energy = None; //if !link_deposit(creep, creep_memory, cache) { // drop_deposit(creep, creep_memory, cache); @@ -32,7 +45,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach } } else { if !link_deposit(creep, creep_memory, cache) { - drop_deposit(creep, creep_memory, cache); + deposit_energy(creep, creep_memory, cache); } if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 { @@ -42,7 +55,11 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach } } -fn needs_haul_manually(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache) -> bool { +fn needs_haul_manually( + creep: &Creep, + creep_memory: &mut CreepMemory, + cache: &mut RoomCache, +) -> bool { let count = if let Some(creeps) = cache.creeps.creeps_of_role.get(&Role::Hauler) { creeps.len() } else { @@ -79,7 +96,11 @@ fn link_deposit(creep: &Creep, creep_memory: &mut CreepMemory, cache: &RoomCache if creep.pos().is_near_to(link.pos()) { let _ = creep.say("🔗", false); - let _ = creep.transfer(link, ResourceType::Energy, Some(creep.store().get_used_capacity(Some(ResourceType::Energy)))); + let _ = creep.transfer( + link, + ResourceType::Energy, + Some(creep.store().get_used_capacity(Some(ResourceType::Energy))), + ); } else { return false; } @@ -87,18 +108,38 @@ fn link_deposit(creep: &Creep, creep_memory: &mut CreepMemory, cache: &RoomCache false } -fn drop_deposit(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache) { - +fn deposit_energy(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache) { if needs_haul_manually(creep, creep_memory, cache) { return; } - if build_around_source(creep, creep_memory, cache) { return; } - if repair_container(creep, creep_memory, cache) { return; } + if build_around_source(creep, creep_memory, cache) { + return; + } + if repair_container(creep, creep_memory, cache) { + return; + } let _ = creep.say("📦", false); - if let Some(container) = cache.structures.sources[creep_memory.task_id.unwrap() as usize].get_container() { - if creep.pos().is_near_to(container.pos()) { + if let Some(container) = + cache.structures.sources[creep_memory.task_id.unwrap() as usize].get_container() + { + if container + .store() + .get_free_capacity(Some(ResourceType::Energy)) + == 0 + { + let amount = creep.store().get_used_capacity(Some(ResourceType::Energy)); + + let _ = creep.drop(ResourceType::Energy, Some(amount)); + cache.hauling.create_order( + creep.try_raw_id().unwrap(), + ResourceType::Energy, + creep.store().get_used_capacity(Some(ResourceType::Energy)), + HaulingPriority::Energy, + HaulingType::Pickup, + ); + } else if creep.pos().is_near_to(container.pos()) { let _ = creep.transfer(&container, ResourceType::Energy, None); } else { creep.better_move_to(creep_memory, cache, container.pos(), 1); @@ -109,15 +150,21 @@ fn drop_deposit(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomC ResourceType::Energy, creep.store().get_used_capacity(Some(ResourceType::Energy)), HaulingPriority::Energy, - HaulingType::Pickup + HaulingType::Pickup, ); let _ = creep.drop(ResourceType::Energy, None); } } -fn build_around_source(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache) -> bool { +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; } + if csites.is_empty() { + return false; + } let csite = csites.first().unwrap(); @@ -133,20 +180,21 @@ fn build_around_source(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mu } 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(); + 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, cache, container.pos(), 1); - return true; - } else { - let _ = creep.say("🔧", false); - let _ = creep.repair(&container); - return true; + if container.pos().get_range_to(creep.pos()) > 1 { + let _ = creep.say("🚚", false); + creep.better_move_to(creep_memory, cache, 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/planning/creep/miner.rs b/src/room/planning/creep/miner.rs index bc84862..c549db0 100644 --- a/src/room/planning/creep/miner.rs +++ b/src/room/planning/creep/miner.rs @@ -50,7 +50,7 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room .unwrap_or(&vec![]) .len(); - if hauler_count < 8 { + if hauler_count < 12 { let mut body = Vec::new(); let cost = 100; let max = room.energy_capacity_available();