Skip to content

Commit

Permalink
Make it not spawn reservers if under attack. More tweaks to hauling. …
Browse files Browse the repository at this point in the history
…Rewrote harvester and remote harvester spawning.
  • Loading branch information
InfinityDevTech committed Jul 20, 2024
1 parent 15cda50 commit 0698607
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 59 deletions.
2 changes: 1 addition & 1 deletion profiler.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/combat/goals/room_reservation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ pub fn attain_reservation(
clear_creeps(goal);
let current_parts = get_claim_parts(goal);

if let Some(remote_room_memory) = memory.remote_rooms.get(target_room) {
if remote_room_memory.under_attack {
//info!("[{}] Room is under attack. Pausing reservation goal...", target_room);
return;
}
}

if goal.accessible_reservation_spots == 0 {
memory.goals.room_reservation.remove(target_room);
return;
Expand Down
4 changes: 4 additions & 0 deletions src/room/cache/tick_cache/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ pub fn haul_containers(cached_room: &mut CachedRoom) {
priority -= 20.0;
}

if cached_room.rcl <= 3 {
priority += 5.0;
}

if cached_room.structures.storage.is_some() && basehauler_count == 0 {
priority += 10000.0;
}
Expand Down
65 changes: 39 additions & 26 deletions src/room/creeps/remote/remote_harvester.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::info;
use rand::{rngs::StdRng, Rng, SeedableRng};
use screeps::{
game, Creep, HasPosition, MaybeHasId, Position, ResourceType, RoomCoordinate,
Expand All @@ -16,7 +17,7 @@ use crate::{
},
};

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
//#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn run_remoteharvester(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap();

Expand All @@ -28,6 +29,12 @@ pub fn run_remoteharvester(creep: &Creep, memory: &mut ScreepsMemory, cache: &mu

if let Some(owning_cache) = cache.rooms.get_mut(&creep_memory.owning_room) {
if !owning_cache.remotes_with_harvester.contains(&remote_room) {
info!(
"[{}] Adding remote room {} to owning room {}",
game::time(),
remote_room,
creep_memory.owning_room
);
owning_cache.remotes_with_harvester.push(remote_room);
}
}
Expand All @@ -38,6 +45,37 @@ pub fn run_remoteharvester(creep: &Creep, memory: &mut ScreepsMemory, cache: &mu
.push(creep.try_id().unwrap());
}

if let Some(remote_room_memory) = memory.remote_rooms.get(&remote_room) {
if remote_room_memory.under_attack {
creep.bsay("🚨", false);

let flee_pos = Position::new(
RoomCoordinate::new(25).unwrap(),
RoomCoordinate::new(25).unwrap(),
creep_memory.owning_room,
);

if creep.pos().get_range_to(flee_pos) > 23 {
let or = creep_memory.owning_room;
creep.better_move_to(
memory,
cache.rooms.get_mut(&creep.room().unwrap().name()).unwrap(),
Position::new(
RoomCoordinate::new(25).unwrap(),
RoomCoordinate::new(25).unwrap(),
or,
),
23,
MoveOptions::default(),
);

return;
}

return;
}
}

if creep.tired() || creep.spawning() || game::cpu::bucket() < 1000 {
creep.bsay("😴", false);
return;
Expand All @@ -48,32 +86,7 @@ pub fn run_remoteharvester(creep: &Creep, memory: &mut ScreepsMemory, cache: &mu

if room_name != remote_room {
if let Some(remote_room_memory) = memory.remote_rooms.get(&remote_room) {
if remote_room_memory.under_attack {
creep.bsay("🚨", false);

let flee_pos = Position::new(
RoomCoordinate::new(25).unwrap(),
RoomCoordinate::new(25).unwrap(),
creep_memory.owning_room,
);

if creep.pos().get_range_to(flee_pos) > 24 {
let or = creep_memory.owning_room;
creep.better_move_to(
memory,
room_cache,
Position::new(
RoomCoordinate::new(25).unwrap(),
RoomCoordinate::new(25).unwrap(),
or,
),
24,
MoveOptions::default(),
);

return;
}
}
}

creep.bsay("🚚", false);
Expand Down
18 changes: 17 additions & 1 deletion src/room/democracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory, cache: &mut Room
// Caches structures and other creep things
cache.create_if_not_exists(&room, memory, None);

// Ensure remote caches are created, just incase a remote harvester
// Is sitting in this room, and the remote hasnt been run yet.
// Should be cheap enough. (hopefully)
if room.my() {
if let Some(room_memory) = memory.rooms.get_mut(&room.name()) {
let remotes = room_memory.remotes.clone();

for remote in remotes {
if let Some(game_room) = game::rooms().get(remote) {
cache.create_if_not_exists(&game_room, memory, Some(room.name()));
}
}
}
}

if room.my() {
info!("[GOVERNMENT] Starting government for room: {}", room.name());

Expand Down Expand Up @@ -252,7 +267,7 @@ pub fn run_crap_planner_code(room: &Room, memory: &mut ScreepsMemory, room_cache
return;
}

if game::cpu::bucket() > 1000 && game::time() % 1000 == 0 {
if game::cpu::bucket() > 1000 && game::time() % 100 == 0 {
let stuffs = get_roads_and_ramparts();

let pos = room_cache.spawn_center.unwrap();
Expand Down Expand Up @@ -304,6 +319,7 @@ pub fn run_crap_planner_code(room: &Room, memory: &mut ScreepsMemory, room_cache

if !memory.rooms.get(&room.name()).unwrap().planned
|| (memory.rooms.get(&room.name()).unwrap().rcl != room.controller().unwrap().level())
|| game::time() % 300 == 0
{
let level = room.controller().unwrap().level();

Expand Down
30 changes: 21 additions & 9 deletions src/room/spawning/creep_sizing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use screeps::{game, Part, ResourceType, Room};
use crate::{
constants::{part_costs, PartsCost},
memory::Role,
room::cache::tick_cache::CachedRoom,
room::cache::{self, tick_cache::CachedRoom},
utils::{self, get_body_cost},
};

/// Returns the parts needed for a miner creep
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn miner_body(room: &Room, cache: &CachedRoom, source_parts_needed: u8) -> (bool, Vec<Part>) {
pub fn miner_body(room: &Room, cache: &CachedRoom, source_parts_needed: u8, force_max: bool) -> (bool, Vec<Part>) {
let mut parts = if cache.rcl < 2 {
vec![Part::Work, Part::Move]
} else {
Expand Down Expand Up @@ -39,12 +39,16 @@ pub fn miner_body(room: &Room, cache: &CachedRoom, source_parts_needed: u8) -> (
let mut current_work_count = 1;
let mut current_cost = utils::get_body_cost(&parts);

let energy_to_use = if miner_count < 2 || base_hauler_count == 0 {
let mut energy_to_use = if miner_count < 2 || base_hauler_count == 0 {
room.energy_available()
} else {
room.energy_capacity_available()
};

if force_max {
energy_to_use = room.energy_capacity_available();
}

while current_cost < energy_to_use {
if current_cost + cost_of_stamp > energy_to_use || current_work_count >= source_parts_needed
{
Expand Down Expand Up @@ -148,7 +152,15 @@ pub fn base_hauler_body(room: &Room, cache: &CachedRoom) -> Vec<Part> {
.unwrap_or(&Vec::new())
.len();

let max_energy = if hauler_count > 0 {
let mut storage_blocked = false;

if let Some(storage) = &cache.structures.storage {
if storage.store().get_used_capacity(Some(ResourceType::Energy)) < 5000 {
storage_blocked = true;
}
}

let max_energy = if hauler_count > 0 && !storage_blocked {
room.energy_capacity_available()
} else {
room.energy_available()
Expand Down Expand Up @@ -278,11 +290,11 @@ pub fn upgrader_body(room: &Room, cache: &CachedRoom) -> Vec<Part> {

let target_work_parts = match level {
1 => 5,
2 => 10,
3 => 25,
4 => 25,
5 => 30,
6 => 40,
2 => 15,
3 => 22,
4 => 30,
5 => 40,
6 => 45,
7 => 50,
8 => 5,
_ => 1,
Expand Down
Loading

0 comments on commit 0698607

Please sign in to comment.