Skip to content

Commit

Permalink
Bump haulers to 12
Browse files Browse the repository at this point in the history
Fix hauler state machine??
Made SourceMiners drop if container full.
  • Loading branch information
InfinityDevTech committed May 30, 2024
1 parent 65c03c8 commit 0837d04
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 48 deletions.
31 changes: 13 additions & 18 deletions src/room/creeps/local/hauler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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; }
Expand All @@ -82,10 +89,6 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut
let _ = creep.withdraw(target.unchecked_ref::<StructureStorage>(), 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);
Expand All @@ -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);
Expand All @@ -109,10 +108,6 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut
let _ = creep.withdraw(target.unchecked_ref::<StructureStorage>(), order.resource, Some(amount.try_into().unwrap()));
success = true;
}

if creep.store().get_free_capacity(None) >= 0 {
creep_memory.needs_energy = None;
}
}
};

Expand Down
106 changes: 77 additions & 29 deletions src/room/creeps/local/source_miner.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();

Expand All @@ -24,15 +35,17 @@ 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);
//}
}
} 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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -79,26 +96,50 @@ 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;
}
}
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);
Expand All @@ -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();

Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion src/room/planning/creep/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0837d04

Please sign in to comment.