Skip to content

Commit

Permalink
More balancing, weird economic edge cases. Idk, hopefully I can stop …
Browse files Browse the repository at this point in the history
…my rooms from dieing.
  • Loading branch information
InfinityDevTech committed Jul 18, 2024
1 parent 9dc0346 commit 15cda50
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 247 deletions.
2 changes: 1 addition & 1 deletion profiler.json

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions src/combat/rank_room.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use screeps::{find, game, HasId, HasPosition, OwnedStructureProperties, Room, RoomXY, StructureObject};
use screeps::{find, game, HasId, HasPosition, OwnedStructureProperties, Position, Room, RoomXY, StructureObject};

use crate::{
memory::{EnemyPlayer, ScoutedRoom, ScreepsMemory},
room::cache::tick_cache::CachedRoom, utils,
memory::{EnemyPlayer, ScoutedRoom, ScoutedSource, ScreepsMemory}, room::cache::tick_cache::{resources::CachedSource, CachedRoom}, traits::position::PositionExtensions, utils
};

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
Expand Down Expand Up @@ -49,7 +48,16 @@ pub fn scout_room(room: &Room, memory: &mut ScreepsMemory, cached_room: &mut Cac
let sources = if sources.is_empty() {
None
} else {
Some(sources)
let mut cached_sources = Vec::new();
for source in sources {
let pos = Position::new(source.x, source.y, room_name);
cached_sources.push(ScoutedSource {
pos: source,
pos_av: pos.get_accessible_positions_around(1).len() as u8,
});
}

Some(cached_sources)
};

let scouted_room = ScoutedRoom {
Expand Down
8 changes: 6 additions & 2 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ structstruck::strike! {
pub owner: RoomName,

pub creeps: Vec<String>,
pub sources: Vec<ScoutedSource>,

pub under_attack: bool,
}
Expand Down Expand Up @@ -210,7 +211,10 @@ structstruck::strike! {
pub defense_capability: u8,

#[serde(skip_serializing_if = "Option::is_none")]
pub sources: Option<Vec<RoomXY>>,
pub sources: Option<Vec<pub struct ScoutedSource {
pub pos: RoomXY,
pub pos_av: u8,
}>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mineral: Option<ObjectId<Mineral>>,
pub last_scouted: u32,
Expand Down Expand Up @@ -326,7 +330,7 @@ structstruck::strike! {
}
}

//#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
impl ScreepsMemory {
pub fn init_memory() -> Self {
let pre_memory_cpu = game::cpu::get_used();
Expand Down
7 changes: 1 addition & 6 deletions src/movement/move_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub fn lcl_call(room_name: RoomName, from: Position, memory: &ScreepsMemory, mov
matrix
}

//#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn path_call(room_name: RoomName, from: Position, memory: &ScreepsMemory, move_options: MoveOptions) -> MultiRoomCostResult {
let mut matrix = LocalCostMatrix::new();

Expand Down Expand Up @@ -437,11 +437,6 @@ pub fn path_call(room_name: RoomName, from: Position, memory: &ScreepsMemory, mo
for csite in constructions {
let pos = csite.pos();

if !csite.my() {
matrix.set(pos.xy(), 255);
continue;
}

match csite.structure_type() {
StructureType::Container => {},
StructureType::Rampart => {},
Expand Down
4 changes: 4 additions & 0 deletions src/room/cache/tick_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct CachedRoom {
pub idle_haulers: u32,
pub manager: Option<RoomName>,

pub remotes_with_harvester: Vec<RoomName>,

pub remotes: Vec<RoomName>,
pub spawn_center: Option<RoomXY>,
pub storage_center: Option<RoomXY>,
Expand Down Expand Up @@ -103,6 +105,8 @@ impl CachedRoom {
manager: remote_manager,
remotes: Vec::new(),

remotes_with_harvester: Vec::new(),

spawn_center: sp_center,
storage_center: st_center,

Expand Down
10 changes: 9 additions & 1 deletion src/room/cache/tick_cache/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,15 @@ pub fn haul_dropped_resources(cached_room: &mut CachedRoom) {
for resource in &cached_room.resources.dropped_energy {
let amount = resource.amount();

let mut priority = -(amount as f32);

if let Some(storage) = &cached_room.structures.storage {
if storage.store().get_used_capacity(Some(ResourceType::Energy)) < 1000 {
priority -= 999999999999.0;
}
}

cached_room.stats.energy.dropped += amount;
cached_room.hauling.create_order(resource.id().into(), None, Some(resource.resource_type()), Some(resource.amount()), -(amount as f32), HaulingType::NoDistanceCalcPickup);
cached_room.hauling.create_order(resource.id().into(), None, Some(resource.resource_type()), Some(resource.amount()), priority, HaulingType::NoDistanceCalcPickup);
}
}
14 changes: 12 additions & 2 deletions src/room/creeps/combat/bulldozer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::info;
use rand::{rngs::StdRng, Rng, SeedableRng};
use screeps::{find, game, Color, Creep, HasPosition, SharedCreepProperties, StructureObject, StructureProperties, StructureRampart, StructureType};
use screeps::{find, game, structure, Color, Creep, HasPosition, SharedCreepProperties, StructureObject, StructureProperties, StructureRampart, StructureType};

use crate::{
config, memory::{Role, ScreepsMemory}, movement::move_target::MoveOptions, room::cache::tick_cache::RoomCache, traits::{creep::CreepExtensions, intents_tracking::CreepExtensionsTracking}
Expand Down Expand Up @@ -121,9 +121,19 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
}
} else {
let mut structures = creep.room().unwrap().find(find::STRUCTURES, None);
structures.retain(| structure | structure.structure_type() != StructureType::Controller);
structures.retain(| structure | structure.structure_type() != StructureType::Controller || structure.structure_type() != StructureType::Rampart);
structures.sort_by_key(|structure| structure.pos().get_range_to(creep.pos()));

structures.retain(|c| {
for rampart in ramparts.iter() {
if c.pos().is_equal_to(rampart.pos()) {
return false;
}
}

true
});

let structure = structures.first();
if let Some(structure) = structure {
if creep.pos().is_near_to(structure.pos()) {
Expand Down
4 changes: 2 additions & 2 deletions src/room/creeps/local/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ pub fn find_energy(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCa
if let Some((_spawn, spawn_id)) = &room_cache.structures.spawns.clone().into_iter().next() {
if spawn_id
.store()
.get_free_capacity(Some(ResourceType::Energy))
> 0
.get_used_capacity(Some(ResourceType::Energy))
== 0
{
run = false;
}
Expand Down
9 changes: 7 additions & 2 deletions src/room/creeps/local/fast_filler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use screeps::{
game, look, Creep, HasId, HasPosition, MaybeHasId, ObjectId, RawObjectId, ResourceType,
RoomPosition, RoomXY, SharedCreepProperties, StructureContainer, StructureExtension,
game, look, spawn, Creep, HasId, HasPosition, MaybeHasId, ObjectId, RawObjectId, ResourceType, RoomPosition, RoomXY, SharedCreepProperties, StructureContainer, StructureExtension
};

use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -141,6 +140,12 @@ pub fn run_fastfiller(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Roo
pub fn self_renew(creep: &Creep, cache: &mut CachedRoom) {
let spawn = cache.structures.spawns.values().next().unwrap();

// Fix issue where all creeps died and they kept spending the energy on themselves
// Greedy fucks...
if spawn.store().get_free_capacity(Some(ResourceType::Energy)) > 0 {
return;
}

if creep.ticks_to_live() < Some(100)
&& creep.pos().is_near_to(spawn.pos())
&& spawn.spawning().is_none()
Expand Down
5 changes: 5 additions & 0 deletions src/room/creeps/local/harvester.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::info;
use screeps::{
game, Creep, HasHits, HasPosition, MaybeHasId, Part, ResourceType, SharedCreepProperties,
Source, StructureContainer,
Expand All @@ -16,6 +17,7 @@ pub fn run_harvester(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room

if creep_memory.task_id.is_none() {
creep.bsay("kurt kob", true);
info!("Harvester {} has no task id", creep.name());
let _ = creep.ITsuicide();
return;
}
Expand Down Expand Up @@ -69,6 +71,9 @@ pub fn harvest_source(

if let Some(pos) = available_positons.first() {
creep.better_move_to(memory, cache, *pos, 0, MoveOptions::default());
} else {
// If the source is flooded, let the traffic manager figure it out.
creep.better_move_to(memory, cache, source.pos(), 1, MoveOptions::default());
}

None
Expand Down
14 changes: 9 additions & 5 deletions src/room/creeps/local/hauler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ pub fn run_hauler(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCac
if creep_memory.hauling_task.is_none() {
if creep_memory.needs_energy.unwrap_or(false) {
creep.bsay("📋", false);
let room_cache = cache
.rooms
.get_mut(&creep_memory.owning_room)
.unwrap();

cache
.rooms
.get_mut(&creep_memory.owning_room)
.unwrap()
room_cache.idle_haulers += 1;


room_cache
.hauling
.wanting_orders
.push(
Expand All @@ -70,7 +74,7 @@ pub fn run_hauler(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCac
.get_mut(&creep_memory.owning_room)
.unwrap();

//room_cache.idle_haulers += 1;
room_cache.idle_haulers += 1;

room_cache
.hauling
Expand Down
2 changes: 2 additions & 0 deletions src/room/creeps/local/storage_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
utils,
};

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn run_storagesitter(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap();
let room_cache = cache.rooms.get_mut(&creep_memory.owning_room).unwrap();
Expand Down Expand Up @@ -98,6 +99,7 @@ pub fn run_storagesitter(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut
// end nuker stuffs.
}

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn check_pos(creep: &Creep, memory: &mut ScreepsMemory, room_cache: &mut CachedRoom) -> bool {
let wanted_pos = room_cache.storage_center;
let pos = creep.pos();
Expand Down
8 changes: 3 additions & 5 deletions src/room/creeps/local/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,14 @@ pub fn get_energy(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCac
false
}

//#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn sign_controller(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) -> bool {
let room = creep.room().unwrap();
let cache = cache.rooms.get_mut(&room.name()).unwrap();

// E46N38 is a remote of NeonCamoflauge, and I want to sign it ( Totally to not fuck with him ;) ).
if !memory.remote_rooms.contains_key(&room.name()) && !memory.rooms.contains_key(&creep.room().unwrap().name()) {
if room.name() != "E46N38" && room.name() != "E46N37" {
return false;
}
if !memory.remote_rooms.contains_key(&room.name()) && !memory.rooms.contains_key(&creep.room().unwrap().name()) && room.name() != "E46N38" {
return false;
}

if let Some(controller) = cache.structures.controller.as_ref() {
Expand Down
19 changes: 10 additions & 9 deletions src/room/creeps/organizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ pub fn run_creeps(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache
let end_time = game::cpu::get_used();
let cpu_used = end_time - start_time;

if cpu_used > 10.0 && role != Role::Scout && role != Role::Bulldozer {
info!(
" [CREEPS] Suiciding {} due to high CPU usage: {}",
creep.name(),
cpu_used
);

let _ = creep.ITsuicide();
}
// TODO: Make this use an average, im sick of creeps randomly dieing
//if cpu_used > 12.0 && role != Role::Scout && role != Role::Bulldozer && role != Role::Harvester && role != Role::RemoteHarvester {
// info!(
// " [CREEPS] Suiciding {} due to high CPU usage: {}",
// creep.name(),
// cpu_used
// );
//
// let _ = creep.ITsuicide();
//}

if cpu_used > highest_usage {
highest_usage = cpu_used;
Expand Down
8 changes: 7 additions & 1 deletion src/room/creeps/remote/remote_harvester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
memory::{CreepMemory, ScreepsMemory},
movement::move_target::MoveOptions,
room::{
cache::tick_cache::{CachedRoom, RoomCache},
cache::{self, tick_cache::{CachedRoom, RoomCache}},
creeps::local::harvester::{harvest_source, repair_container},
},
traits::{
Expand All @@ -26,6 +26,12 @@ pub fn run_remoteharvester(creep: &Creep, memory: &mut ScreepsMemory, cache: &mu
return;
}

if let Some(owning_cache) = cache.rooms.get_mut(&creep_memory.owning_room) {
if !owning_cache.remotes_with_harvester.contains(&remote_room) {
owning_cache.remotes_with_harvester.push(remote_room);
}
}

if let Some(remote_room) = cache.rooms.get_mut(&remote_room) {
remote_room.resources.sources[creep_memory.task_id.unwrap() as usize]
.creeps
Expand Down
3 changes: 3 additions & 0 deletions src/room/planning/room/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use screeps::{HasId, HasPosition, Position, Room, StructureType};

use crate::{room::cache::tick_cache::CachedRoom, traits::position::PositionExtensions};

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
fn find_pos_most_accessible(
room: &Room,
room_cache: &CachedRoom,
Expand Down Expand Up @@ -42,6 +43,7 @@ fn find_pos_most_accessible(
closest
}

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn plan_containers_and_links(room: &Room, room_cache: &CachedRoom) {
let mut links_placed = 0;

Expand Down Expand Up @@ -138,6 +140,7 @@ pub fn plan_containers_and_links(room: &Room, room_cache: &CachedRoom) {
}
}

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn get_containers() -> Vec<(i8, i8, StructureType)> {
vec![
(-2, -1, StructureType::Container),
Expand Down
8 changes: 6 additions & 2 deletions src/room/planning/room/remotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ pub fn fetch_possible_remotes(
continue;
}

let sroom_memory = memory.scouted_rooms.get(remote_name).unwrap();

let remote = RemoteRoomMemory {
name: *remote_name,
owner: room.name(),

sources: sroom_memory.sources.as_ref().unwrap().to_vec(),

creeps: Vec::new(),
under_attack: false
};
Expand Down Expand Up @@ -143,8 +147,8 @@ pub fn rank_remote_room(
// Go thorugh each source and make a path to it, then average the cost.
for source in scouted.as_ref().unwrap().sources.as_ref().unwrap() {
let position = Position::new(
RoomCoordinate::new(source.x.u8()).unwrap(),
RoomCoordinate::new(source.y.u8()).unwrap(),
RoomCoordinate::new(source.pos.x.u8()).unwrap(),
RoomCoordinate::new(source.pos.y.u8()).unwrap(),
*remote_room,
);
let options = Some(SearchOptions::new(remote_path_call).max_rooms(16).plain_cost(1).swamp_cost(5));
Expand Down
Loading

0 comments on commit 15cda50

Please sign in to comment.