Skip to content

Commit

Permalink
Tweaking hauling more and more. Funni
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Jun 4, 2024
1 parent da3fb2d commit 632f1db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/room/cache/tick_cache/hauling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct HaulingCache {
pub current_id_index: u32,

pub haulers: Vec<String>,

iterator_salt: u32,
}

impl HaulingCache {
Expand All @@ -52,6 +54,8 @@ impl HaulingCache {
new_orders: HashMap::new(),
current_id_index: game::time(),
haulers: Vec::new(),

iterator_salt: 0,
}
}

Expand Down Expand Up @@ -86,7 +90,8 @@ impl HaulingCache {

orders.sort_by(|a, b| b.priority.partial_cmp(&a.priority).unwrap());

let mut seedable = StdRng::seed_from_u64(game::time().into());
let mut seedable = StdRng::seed_from_u64((game::time() + self.iterator_salt).into());
self.iterator_salt += 1;

if let Some(order) = orders.clone().into_iter().next() {
let id = order.id;
Expand Down
29 changes: 21 additions & 8 deletions src/room/cache/tick_cache/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct RoomStructureCache {
pub all_structures: Vec<StructureObject>,
pub construction_sites: Vec<ConstructionSite>,

pub needs_repair: Vec<StructureObject>,

pub sources: Vec<CachedSource>,
pub ruins: HashMap<ObjectId<Ruin>, Ruin>,
pub spawns: HashMap<ObjectId<StructureSpawn>, StructureSpawn>,
Expand Down Expand Up @@ -58,6 +60,7 @@ impl RoomStructureCache {
let mut cache = RoomStructureCache {
all_structures: Vec::new(),
construction_sites: Vec::new(),
needs_repair: Vec::new(),

sources: Vec::new(),
ruins: HashMap::new(),
Expand Down Expand Up @@ -147,20 +150,20 @@ impl RoomStructureCache {
let max_capacity = container.store().get_capacity(Some(ResourceType::Energy));

let mut i = 0;
let mut matching = false;
let mut pull = true;
let mut is_source_container = false;
let mut is_controller_container = true;

loop {
if self.sources.len() <= i {
break;
}
if matching {
if is_source_container {
break;
}

if let Some(source_container) = self.sources[i].get_container() {
if container.id() == source_container.id() {
matching = true;
is_source_container = true;
}
}

Expand All @@ -171,12 +174,12 @@ impl RoomStructureCache {
if controller.container.is_some() {
let controller_container = controller.container.as_ref().unwrap();
if container.id() == controller_container.id() {
pull = false;
is_controller_container = false;
}
}
}

if pull && used_capacity > 0 {
if is_controller_container && used_capacity > 0 {
if container
.pos()
.get_range_to(self.spawns.values().next().unwrap().pos())
Expand All @@ -199,7 +202,11 @@ impl RoomStructureCache {
priority,
HaulingType::Offer,
);
} else {

} else if !is_controller_container {
// Caused an issue where a hauler would grab from the container
// Container would go lower than 50 percent, than the hauler
// Would stick it back in said container :)
let priority = scale_haul_priority(
container.store().get_capacity(Some(ResourceType::Energy)),
container.store().get_used_capacity(Some(ResourceType::Energy)),
Expand All @@ -219,7 +226,7 @@ impl RoomStructureCache {
}
}

if !matching && (used_capacity as f32) <= (max_capacity as f32 * 0.5) {
if !is_source_container && (used_capacity as f32) <= (max_capacity as f32 * 0.5) {
if container
.pos()
.get_range_to(self.spawns.values().next().unwrap().pos())
Expand Down Expand Up @@ -283,6 +290,12 @@ impl RoomStructureCache {
for structure in structures {
self.all_structures.push(structure.clone());

if let Some(repairable) = structure.as_repairable() {
if repairable.hits() < repairable.hits_max() {
self.needs_repair.push(structure.clone());
}
}

match structure {
StructureObject::StructureTower(tower) => {
if !tower.my() {
Expand Down
8 changes: 8 additions & 0 deletions src/room/creeps/local/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ pub fn build(creep: &Creep, creepmem: &mut CreepMemory, cache: &mut RoomCache) {
}

}
} else if let Some(repairable) = cache.structures.needs_repair.first() {
if repairable.pos().get_range_to(creep.pos()) > 1 {
let _ = creep.say("🚚", false);
creep.better_move_to(creepmem, cache, repairable.pos(), 1);
} else {
let _ = creep.say("🔨", false);
let _ = creep.repair(repairable.as_repairable().unwrap());
}
}

if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 {
Expand Down
9 changes: 7 additions & 2 deletions src/room/planning/creep/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,13 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room
let max_work_parts_needed = cache.structures.sources[needed.unwrap() as usize].parts_needed();

for _ in 0..cmp::min(max_work_parts_makeable, (max_work_parts_needed + 2).into()) {
parts.push(Part::Work);
cost += 100;
if parts.len() % 4 == 0 {
parts.push(Part::Move);
cost += 50;
} else {
parts.push(Part::Work);
cost += 100;
}
}

let name_prefix = role_to_name(Role::Miner);
Expand Down
6 changes: 3 additions & 3 deletions src/room/visuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub fn run_full_visuals(room: &Room, memory: &mut ScreepsMemory, cache: &mut Roo
pub fn visualise_spawn_progess(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
for spawn in cache.structures.spawns.values() {
if let Some(spawning) = spawn.spawning() {
let progress = (spawning.remaining_time() as f32 / spawning.need_time() as f32);
let progress = (spawning.remaining_time() as f32 / spawning.need_time() as f32) * 100.0;

room.visual().text(
spawn.pos().x().u8() as f32,
spawn.pos().y().u8() as f32 + 0.25,
format!("{}%", progress.round() as u32),
format!("{:.1}", progress),
Default::default(),
);
}
Expand All @@ -31,7 +31,7 @@ pub fn visualise_controller_progress(room: &Room, memory: &mut ScreepsMemory, ca
room.visual().text(
controller.pos().x().u8() as f32,
controller.pos().y().u8() as f32 - 1.0,
format!("{}%", progress.round() as u32),
format!("{:.2}%", progress),
Default::default(),
);

Expand Down

0 comments on commit 632f1db

Please sign in to comment.