From 6b46fd4f44ca0f9f9aa0915d3669c35ddd8b1f63 Mon Sep 17 00:00:00 2001 From: Infinity Dev <47366052+InfinityDevTech@users.noreply.github.com> Date: Wed, 22 May 2024 15:28:24 -0400 Subject: [PATCH] Sync --- Cargo.lock | 302 +++++++++++++++++- Cargo.toml | 1 + screeps.toml | 4 +- src/lib.rs | 17 +- src/memory.rs | 56 ++-- src/movement/creep.rs | 4 +- src/room/creeps/local/builder.rs | 8 +- src/room/creeps/local/hauler.rs | 36 ++- src/room/creeps/local/source_miner.rs | 79 +++-- src/room/creeps/local/upgrader.rs | 12 +- src/room/creeps/organizer.rs | 23 +- src/room/democracy.rs | 4 +- src/room/hauling.rs | 26 +- src/room/mod.rs | 2 +- .../{structure_cache.rs => object_cache.rs} | 28 +- src/room/planning/creep/miner.rs | 38 +-- src/room/planning/room/mod.rs | 2 +- src/room/tower.rs | 2 +- src/traits/creep.rs | 16 +- src/traits/room.rs | 2 +- 20 files changed, 504 insertions(+), 158 deletions(-) rename src/room/{structure_cache.rs => object_cache.rs} (71%) diff --git a/Cargo.lock b/Cargo.lock index 59dad6b..7e7fabf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,12 @@ version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" +[[package]] +name = "cc" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" + [[package]] name = "cfg-if" version = "1.0.0" @@ -53,6 +59,7 @@ dependencies = [ "fern", "js-sys", "log", + "qcell", "regex", "screeps-game-api", "serde", @@ -95,6 +102,15 @@ dependencies = [ "syn", ] +[[package]] +name = "exclusion-set" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3708c48ed7245587498d116a41566942d6d9943f5b3207fbf522e2bd0b72d0" +dependencies = [ + "loom", +] + [[package]] name = "fern" version = "0.6.2" @@ -104,6 +120,19 @@ dependencies = [ "log", ] +[[package]] +name = "generator" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc16584ff22b460a382b7feec54b23d2908d858152e5739a120b949293bd74e" +dependencies = [ + "cc", + "libc", + "log", + "rustversion", + "windows", +] + [[package]] name = "heck" version = "0.4.1" @@ -125,18 +154,62 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + [[package]] name = "log" version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +[[package]] +name = "loom" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-derive" version = "0.4.0" @@ -163,6 +236,18 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + [[package]] name = "proc-macro2" version = "1.0.82" @@ -172,6 +257,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "qcell" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f6c04aa3dea4dab485f6d87449ba94d5664c388c0f1fe2b07c4891175513345" +dependencies = [ + "exclusion-set", + "once_cell", +] + [[package]] name = "quote" version = "1.0.36" @@ -189,8 +284,17 @@ checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.6", + "regex-syntax 0.8.3", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -201,15 +305,27 @@ checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.3", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + [[package]] name = "ryu" version = "1.0.15" @@ -294,6 +410,21 @@ dependencies = [ "syn", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "structstruck" version = "0.4.1" @@ -317,12 +448,89 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "venial" version = "0.5.0" @@ -432,3 +640,91 @@ dependencies = [ "js-sys", "wasm-bindgen", ] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 903b2b4..552a55b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ serde_json = "1.0.104" serde = { version = "1.0.183", features = ["derive"] } regex = "1.9.4" console_error_panic_hook = "0.1.7" +qcell = "0.5.4" [dev-dependencies] wasm-bindgen-test = "0.3" diff --git a/screeps.toml b/screeps.toml index 03925d9..e5fcdba 100644 --- a/screeps.toml +++ b/screeps.toml @@ -17,9 +17,9 @@ prefix = "ptr" [copy] # Windows Desktop -destination = "C:\\Users\\jwjoh\\AppData\\Local\\Screeps\\scripts\\127_0_0_1___21025" +#destination = "C:\\Users\\jwjoh\\AppData\\Local\\Screeps\\scripts\\127_0_0_1___21025" # Linux Laptop -#destination = "/home/jwjoh/.config/Screeps/scripts/127_0_0_1___21025" +destination = "/home/jwjoh/.config/Screeps/scripts/127_0_0_1___21025" # Bot arena #destination = "C:\\Users\\jwjoh\\AppData\\Local\\Screeps\\scripts\\botarena_screepspl_us___21025" diff --git a/src/lib.rs b/src/lib.rs index 6c4cd34..27b0f9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,17 +32,11 @@ pub fn game_loop() { if game::time() % 10 == 0 { for room in game::rooms().values() { if let Some(controller) = room.controller() { - if controller.my() && !memory.rooms.contains_key(&room.name_str()) { + if controller.my() && !memory.rooms.contains_key(&room.name()) { plan_room(&room, &mut memory); } } } - - for creep in memory.clone().creeps.keys() { - if game::creeps().get(creep.clone()).is_none() { - memory.creeps.remove(&creep.clone()); - } - } } if just_reset() { @@ -57,8 +51,13 @@ pub fn game_loop() { } } - for room in memory.clone().rooms.values() { - room::democracy::start_government(game::rooms().get(RoomName::from_str(&room.name).unwrap()).unwrap(), &mut memory); + for room in game::rooms().keys() { + let game_room = game::rooms().get(room).unwrap(); + let room_memory = memory.rooms.get(&game_room.name()); + + if room_memory.is_none() && game_room.my() { plan_room(&game_room, &mut memory); } + + room::democracy::start_government(game::rooms().get(room).unwrap(), &mut memory); } // Bot is finished, write the stats and local copy of memory. diff --git a/src/memory.rs b/src/memory.rs index b7a187e..462e21a 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,6 +1,7 @@ use std::{cmp, collections::HashMap}; use log::error; +use qcell::QCell; use screeps::{game, ObjectId, RawObjectId, Resource, ResourceType, RoomName, Source, Structure, StructureLink}; use serde::{Deserialize, Serialize}; @@ -25,25 +26,31 @@ structstruck::strike! { #[strikethrough[derive(Serialize, Deserialize, Debug, Clone)]] pub struct CreepMemory{ // Owning room - pub o_r: String, + #[serde(rename = "0")] + pub owning_room: String, // Path #[serde(skip_serializing_if = "Option::is_none")] - pub p: Option, + #[serde(rename = "1")] + pub path: Option, // Career - pub r: Role, + #[serde(rename = "2")] + pub role: Role, // Needs Energy? #[serde(skip_serializing_if = "Option::is_none")] - pub n_e: Option, + #[serde(rename = "3")] + pub needs_energy: Option, // This is miner specific, the ID of the link next to it // If this is empty, then the miner is not linked to a link and it will drop resources on the ground // If it is, but the link isnt next to it, the miner will clear the link id. If it is, the miner will deposit resources into the link #[serde(skip_serializing_if = "Option::is_none")] - pub l_id: Option, + #[serde(rename = "4")] + pub link_id: Option, // This is a pointer that changes based on the role of the creep // Hauler - A reference to the ID of the current haul orders // Miner - A reference to the source in the vec of sources #[serde(skip_serializing_if = "Option::is_none")] - pub t_id: Option, + #[serde(rename = "5")] + pub task_id: Option, } } @@ -62,12 +69,19 @@ pub struct RoomMemory{ }>, pub haul_orders: HashMap, + #[serde(rename = "5")] pub haul_type: HaulType, + #[serde(rename = "6")] pub amount: u32, }>, #[serde(skip_serializing_if = "Option::is_none")] @@ -82,7 +96,7 @@ structstruck::strike! { #[strikethrough[derive(Serialize, Deserialize, Debug, Clone)]] pub struct ScreepsMemory { pub mem_version: u8, - pub rooms: HashMap, + pub rooms: HashMap, pub creeps: HashMap } } @@ -92,11 +106,13 @@ impl ScreepsMemory { let memory_jsstring = screeps::raw_memory::get(); let memory_string = memory_jsstring.as_string().unwrap(); if memory_string.is_empty() { + let mut memory = ScreepsMemory { mem_version: MEMORY_VERSION, rooms: HashMap::new(), creeps: HashMap::new(), }; + memory.write_memory(); memory } else { @@ -125,33 +141,19 @@ impl ScreepsMemory { screeps::raw_memory::set(&js_serialized); } - pub fn create_creep(&mut self, room_name: &str, creep_name: &str, object: &CreepMemory) { - self.creeps.insert(creep_name.to_string(), object.clone()); + pub fn create_creep(&mut self, room_name: &str, creep_name: &str, object: CreepMemory) { + self.creeps.insert(creep_name.to_string(), object); - let room = self.get_room_mut(&RoomName::new(room_name).unwrap()); + let room = self.rooms.get_mut(&RoomName::new(room_name).unwrap()).unwrap(); room.creeps.push(creep_name.to_string()); } - pub fn create_room(&mut self, name: &RoomName, object: &RoomMemory) { + pub fn create_room(&mut self, name: &RoomName, object: RoomMemory) { self.rooms.insert( - name.to_string(), - object.clone() + *name, + object ); } - - pub fn get_room_mut(&mut self, name: &RoomName) -> &mut RoomMemory { - self.rooms.get_mut(&name.to_string()).expect("Failure to resolve room in memory.") - } - pub fn get_creep_mut(&mut self, name: &str) -> &mut CreepMemory { - self.creeps.get_mut(name).expect("Failure to resolve creep in memory.") - } - - pub fn get_room(&self, name: &RoomName) -> RoomMemory { - self.rooms.get(&name.to_string()).expect("Failure to resolve room in memory.").clone() - } - pub fn get_creep(&self, name: &str) -> CreepMemory { - self.creeps.get(name).expect("Failure to resolve in memory.").clone() - } } impl ScoutedSource { diff --git a/src/movement/creep.rs b/src/movement/creep.rs index c76147b..d547639 100644 --- a/src/movement/creep.rs +++ b/src/movement/creep.rs @@ -9,14 +9,14 @@ pub fn move_to(creep_name: &String, creep_memory: &mut CreepMemory, target: Posi let creep = game::creeps().get(creep_name.to_string()).unwrap(); match &creep_memory.movement { Some(path) => { - move_by_path(creep_name.to_string(), path.clone(), creep_memory) + move_by_path(creep_name.to_string(), path, creep_memory) } None => { let target = MoveTarget { pos: target, range: 1, }.find_path_to(creep.pos()); - creep_memory.movement = Some(target.clone()); + creep_memory.movement = Some(target); move_by_path(creep_name.to_string(), target, creep_memory); } diff --git a/src/room/creeps/local/builder.rs b/src/room/creeps/local/builder.rs index c97a0f1..259ef3f 100644 --- a/src/room/creeps/local/builder.rs +++ b/src/room/creeps/local/builder.rs @@ -4,8 +4,8 @@ use screeps::{find, Creep, HasPosition, ResourceType, SharedCreepProperties}; use crate::{memory::{CreepMemory, ScreepsMemory}, traits::creep::CreepExtensions}; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) { - let creep_memory = memory.get_creep_mut(creep.name().as_str()); - let needs_energy = creep_memory.n_e.unwrap_or(false); + let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); + let needs_energy = creep_memory.needs_energy.unwrap_or(false); if needs_energy || creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 { find_energy(creep, creep_memory) @@ -17,7 +17,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) { pub fn build(creep: &Creep, creepmem: &mut CreepMemory) { let closest_site = creep.pos().find_closest_by_range(find::CONSTRUCTION_SITES); if let Some(site) = closest_site { - if creep.pos().is_near_to(site.clone().pos()) { + if creep.pos().is_near_to(site.pos()) { let _ = creep.build(&site); } else { creep.better_move_to(creepmem, site.pos(), 1) @@ -30,7 +30,7 @@ pub fn find_energy(creep: &Creep, creepmem: &mut CreepMemory) { .pos() .find_closest_by_range(find::DROPPED_RESOURCES); if let Some(energy) = closest_energy { - if creep.pos().is_near_to(energy.clone().pos()) { + if creep.pos().is_near_to(energy.pos()) { let _ = creep.pickup(&energy); } else { creep.better_move_to(creepmem, energy.pos(), 1) diff --git a/src/room/creeps/local/hauler.rs b/src/room/creeps/local/hauler.rs index 112bae7..3127ad6 100644 --- a/src/room/creeps/local/hauler.rs +++ b/src/room/creeps/local/hauler.rs @@ -1,30 +1,50 @@ use std::{cmp::min, str::FromStr}; use screeps::{ - find, game, Creep, HasPosition, ObjectId, Resource, ResourceType, RoomName, SharedCreepProperties, Structure, StructureObject, Transferable + find, game, Creep, HasPosition, ObjectId, Resource, ResourceType, Room, RoomName, SharedCreepProperties, Structure, StructureObject, Transferable }; use crate::{ - memory::{CreepMemory, ScreepsMemory}, room, traits::creep::CreepExtensions + memory::{CreepMemory, HaulOrder, ScreepsMemory}, room, traits::creep::CreepExtensions }; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) { - let creep_memory = memory.get_creep_mut(&creep.name()); + let creep_memory = memory.creeps.get(&creep.name()).unwrap(); - let order = creep_memory.t_id; + let order = creep_memory.task_id; if let Some(order_id) = order { execute_order(creep, memory, order_id) } else { - let room_memory = memory.get_room_mut(&RoomName::from_str(&creep_memory.o_r).unwrap()); - let new_order = room_memory.find_haul_order(creep, memory); + let new_order = find_haul_order(creep, memory); if let Some(order) = new_order { execute_order(creep, memory, order.id); } } } +pub fn find_haul_order(creep: &Creep, memory: &mut ScreepsMemory) -> Option { + let creep_memory = memory.creeps.get(&creep.name()).unwrap(); + let room_memory = memory.rooms.get_mut(&RoomName::from_str(&creep_memory.owning_room).unwrap()).unwrap(); + + let order_list = room_memory.haul_orders.clone(); + let mut orders = order_list.values().collect::>(); + orders.sort_by(|a, b| a.priority.cmp(&b.priority)); + + let unresponded_orders = orders.into_iter().filter(|&order| order.responder.is_none()); + + if let Some(order) = unresponded_orders.into_iter().next() { + let order = room_memory.haul_orders.get_mut(&order.id).unwrap(); + order.add_responder(creep); + let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); + creep_memory.task_id = Some(order.id); + Some(order.clone()) + } else { + None + } +} + pub fn execute_order(creep: &Creep, memory: &mut ScreepsMemory, order_id: u128) { - let room_memory = memory.get_room_mut(&RoomName::from_str(&memory.get_creep(&creep.name()).o_r).unwrap()); + let room_memory = memory.rooms.get_mut(&RoomName::from_str(&memory.creeps.get(&creep.name()).unwrap().owning_room).unwrap()).unwrap(); let order = room_memory.get_haul_order(order_id).unwrap(); let pickup_target = order.target_id; @@ -32,7 +52,7 @@ pub fn execute_order(creep: &Creep, memory: &mut ScreepsMemory, order_id: u128) let position = order.get_target_position(); if position.get_range_to(creep.pos()) > 1 { - let creep_memory = memory.get_creep_mut(&creep.name()); + let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); creep.better_move_to(creep_memory, position, 1); return; } diff --git a/src/room/creeps/local/source_miner.rs b/src/room/creeps/local/source_miner.rs index 1152b33..c720d0d 100644 --- a/src/room/creeps/local/source_miner.rs +++ b/src/room/creeps/local/source_miner.rs @@ -1,8 +1,9 @@ use std::str::FromStr; -use screeps::{game, Creep, HasPosition, Part, ResourceType, RoomName, SharedCreepProperties, Source}; +use log::info; +use screeps::{game, Creep, ErrorCode, HasId, HasPosition, MaybeHasId, Part, ResourceType, RoomName, SharedCreepProperties, Source}; -use crate::{memory::{CreepMemory, RoomMemory, ScreepsMemory}, room::structure_cache::RoomStructureCache, traits::creep::CreepExtensions}; +use crate::{memory::{CreepMemory, HaulOrder, RoomMemory, ScreepsMemory}, room::{hauling::HaulPriorities, object_cache::RoomStructureCache}, traits::{creep::CreepExtensions, room::RoomExtensions}}; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, structures: &RoomStructureCache) { @@ -12,37 +13,57 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, structures: &RoomStr return; } - let cloned_memory = memory.clone(); - let creep_memory = memory.get_creep_mut(&creep.name()); - let room_memory = cloned_memory.get_room(&RoomName::from_str(&creep_memory.o_r).unwrap()); + let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); + let room_memory = memory.rooms.get(&RoomName::from_str(&creep_memory.owning_room).unwrap()).unwrap(); - if creep_memory.t_id.is_none() { + if creep_memory.task_id.is_none() { let _ = creep.say("kurt kob", true); let _ = creep.suicide(); } - let pointer_index = creep_memory.t_id.unwrap() as usize; + let pointer_index = creep_memory.task_id.unwrap() as usize; let scouted_source = &room_memory.sources[pointer_index]; let source = game::get_object_by_id_typed(&scouted_source.id).unwrap(); - if creep_memory.n_e.unwrap_or(false) { + if creep_memory.needs_energy.unwrap_or(false) { harvest_source(creep, source, creep_memory); if creep.store().get_used_capacity(Some(ResourceType::Energy)) >= creep.store().get_capacity(Some(ResourceType::Energy)) { - creep_memory.n_e = None; + creep_memory.needs_energy = None; } } else { - if !link_deposit(creep, creep_memory, &room_memory, structures) { - drop_deposit(creep, creep_memory, structures); + if !link_deposit(creep, creep_memory, room_memory, structures) { + let room_mem = memory.rooms.get_mut(&RoomName::from_str(&creep_memory.owning_room).unwrap()).unwrap(); + drop_deposit(creep, room_mem, creep_memory, structures); } if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 { - creep_memory.n_e = Some(true); + creep_memory.needs_energy = Some(true); } } } +fn needs_haul_manually(creep: &Creep, creep_memory: &mut CreepMemory, room_memory: &mut RoomMemory, structures: &RoomStructureCache) -> bool { + let spawn = structures.spawns.values().next().unwrap(); + let spawn = game::get_object_by_id_typed(&spawn.id()).unwrap(); + + let room = game::rooms().get(RoomName::from_str(&creep_memory.owning_room).unwrap()).unwrap(); + + let haulers = room.creeps_of_role(room_memory, crate::memory::Role::Hauler); + if haulers.is_empty() { + let _ = creep.say("🚚", true); + // info!("{:#?}", creep.transfer(&spawn, ResourceType::Energy, Some(creep.store().get_used_capacity(Some(ResourceType::Energy)))).err().unwrap()); + if creep.transfer(&spawn, ResourceType::Energy, Some(creep.store().get_used_capacity(Some(ResourceType::Energy)))) == Err(ErrorCode::NotInRange) { + creep.better_move_to(creep_memory, spawn.pos(), 1); + return true; + } else { + return false; + } + } + return false; +} + fn harvest_source(creep: &Creep, source: Source, memory: &mut CreepMemory) { if !creep.pos().is_near_to(source.pos()) { creep.better_move_to(memory, source.pos(), 1); @@ -52,7 +73,7 @@ fn harvest_source(creep: &Creep, source: Source, memory: &mut CreepMemory) { } fn link_deposit(creep: &Creep, creep_memory: &CreepMemory, room_memory: &RoomMemory, structures: &RoomStructureCache) -> bool { - let link_pos = creep_memory.l_id; + let link_pos = creep_memory.link_id; if let Some(links) = &room_memory.links { let link_id = links.get(link_pos.unwrap() as usize).unwrap(); @@ -67,26 +88,30 @@ fn link_deposit(creep: &Creep, creep_memory: &CreepMemory, room_memory: &RoomMem false } -fn drop_deposit(creep: &Creep, creep_memory: &mut CreepMemory, structures: &RoomStructureCache) { - //let spawn = structures.spawns.iter().next().unwrap().1; - let spawn = structures.controller.clone().unwrap(); +fn drop_deposit(creep: &Creep, room_memory: &mut RoomMemory, creep_memory: &mut CreepMemory, structures: &RoomStructureCache) { - //info!("Euclidean: {} - Chebeshev: {}", creep.pos().distance_to(spawn.pos()), creep.pos().get_range_to(spawn.pos())); - if creep.pos().get_range_to(spawn.pos()) <= 2 { - //let _ = creep.transfer(spawn, ResourceType::Energy, None); - let _ = creep.upgrade_controller(&spawn); - } else { - creep.better_move_to(creep_memory, spawn.pos(), 2); + if needs_haul_manually(creep, creep_memory, room_memory, structures) { + return; } + + let amount = creep.store().get_used_capacity(Some(ResourceType::Energy)); + let dropped = creep.drop(ResourceType::Energy, Some(amount)); + + room_memory.create_haul_order(HaulPriorities::Normal, + creep.try_id().unwrap().into(), + ResourceType::Energy, + amount, + crate::room::hauling::HaulType::Pickup); } fn handle_death(creep: &Creep, memory: &mut ScreepsMemory) { - let creep_memory = memory.clone().get_creep(&creep.name()); + let CreepMemory {task_id, owning_room, ..} = memory.creeps.get(&creep.name()).unwrap().clone(); + + let room_memory = memory.rooms.get_mut(&RoomName::from_str(&owning_room).unwrap()).unwrap(); - memory.get_room(&RoomName::from_str(&creep_memory.o_r).unwrap()).creeps.retain(|x| x != &creep.name()); + room_memory.creeps.retain(|x| x != &creep.name()); memory.creeps.remove(&creep.name()); - let room_memory = memory.get_room_mut(&RoomName::from_str(&creep_memory.o_r).unwrap()); - room_memory.sources[creep_memory.t_id.unwrap() as usize].assigned_creeps -= 1; - room_memory.sources[creep_memory.t_id.unwrap() as usize].work_parts -= creep.parts_of_type(Part::Work) as u8; + room_memory.sources[task_id.unwrap() as usize].assigned_creeps -= 1; + room_memory.sources[task_id.unwrap() as usize].work_parts -= creep.parts_of_type(Part::Work) as u8; } \ No newline at end of file diff --git a/src/room/creeps/local/upgrader.rs b/src/room/creeps/local/upgrader.rs index 6d6fa66..8a2daae 100644 --- a/src/room/creeps/local/upgrader.rs +++ b/src/room/creeps/local/upgrader.rs @@ -1,11 +1,11 @@ use screeps::{find, Creep, ErrorCode, HasPosition, ResourceType, SharedCreepProperties}; -use crate::{memory::ScreepsMemory, room::structure_cache::RoomStructureCache, traits::creep::CreepExtensions}; +use crate::{memory::ScreepsMemory, room::object_cache::RoomStructureCache, traits::creep::CreepExtensions}; pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, _structure_cache: &RoomStructureCache) { - let creep_memory = memory.get_creep_mut(creep.name().as_str()); + let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); - let needs_energy = creep_memory.n_e.unwrap_or(false); + let needs_energy = creep_memory.needs_energy.unwrap_or(false); let controller = creep.room().unwrap().controller().unwrap(); if needs_energy { @@ -14,10 +14,10 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, _structure_cache: &R .find_closest_by_range(find::DROPPED_RESOURCES); if let Some(energy) = closest_energy { - if creep.pos().is_near_to(energy.clone().pos()) { + if creep.pos().is_near_to(energy.pos()) { let _ = creep.pickup(&energy); if creep.store().get_free_capacity(Some(ResourceType::Energy)) == 0 { - creep_memory.n_e = None; + creep_memory.needs_energy = None; } } else { creep.better_move_to(creep_memory, energy.pos(), 1); @@ -30,7 +30,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, _structure_cache: &R creep.better_move_to(creep_memory, controller.pos(), 2); } Err(ErrorCode::NotEnough) => { - creep_memory.n_e = Some(true); + creep_memory.needs_energy = Some(true); } _ => {} } diff --git a/src/room/creeps/organizer.rs b/src/room/creeps/organizer.rs index d83ed25..5b1a00b 100644 --- a/src/room/creeps/organizer.rs +++ b/src/room/creeps/organizer.rs @@ -1,34 +1,25 @@ use log::info; -use screeps::{game, Room}; +use screeps::{game, Room, SharedCreepProperties}; -use crate::{memory::{Role, ScreepsMemory}, room::structure_cache::RoomStructureCache, traits::creep::CreepExtensions}; +use crate::{memory::{Role, ScreepsMemory}, room::object_cache::RoomStructureCache, traits::creep::CreepExtensions}; use super::local; pub fn run_creeps(room: &Room, memory: &mut ScreepsMemory, structure_cache: &RoomStructureCache) { info!(" [CREEPS] Running creeps in room: {}", room.name()); - let creeps = memory.get_room(&room.name()).creeps.clone(); - for creep_name in creeps { - let creep = game::creeps().get(creep_name.clone()); + let creeps = memory.rooms.get(&room.name()).unwrap().creeps.clone(); - if creep.is_none() { - memory.creeps.remove(&creep_name); - memory - .get_room(&room.name()) - .creeps - .retain(|x| x != &creep_name); - continue; - } - let creep = creep.unwrap(); - let creep_memory = memory.get_creep(&creep_name); + for creep_name in creeps { + let creep = game::creeps().get(creep_name.to_string()).unwrap(); + let creep_memory = memory.creeps.get(&creep.name()).unwrap(); if creep.spawning() || creep.tired() { let _ = creep.say("😴", false); continue; } - match creep_memory.r { + match creep_memory.role { Role::Miner => local::source_miner::run_creep(&creep, memory, structure_cache), Role::Hauler => local::hauler::run_creep(&creep, memory), Role::Upgrader => local::upgrader::run_creep(&creep, memory, structure_cache), diff --git a/src/room/democracy.rs b/src/room/democracy.rs index 9c7aa9a..0d76c9c 100644 --- a/src/room/democracy.rs +++ b/src/room/democracy.rs @@ -1,13 +1,13 @@ use log::info; use screeps::Room; -use crate::{memory::ScreepsMemory, room::{creeps::organizer, structure_cache::RoomStructureCache}}; +use crate::{memory::ScreepsMemory, room::{creeps::organizer, object_cache::RoomStructureCache}}; use super::{planning::creep::miner::formulate_miner, tower}; pub fn start_government(room: Room, memory: &mut ScreepsMemory) { info!("[GOVERNMENT] Starting government for room: {}", room.name()); - let structure_cache = RoomStructureCache::new_from_room(&room); + let structure_cache = RoomStructureCache::new_from_room(&room, memory); let spawn = structure_cache.spawns.iter().next(); diff --git a/src/room/hauling.rs b/src/room/hauling.rs index e54bf0c..119f130 100644 --- a/src/room/hauling.rs +++ b/src/room/hauling.rs @@ -42,6 +42,15 @@ impl RoomMemory { } pub fn create_haul_order(&mut self, priority: HaulPriorities, target_id: RawObjectId, resource: ResourceType, amount: u32, haul_type: HaulType) { + let t = self.clone(); + let mut existing_orders = t.haul_orders.values().filter(|order| order.target_id == target_id && order.haul_type == haul_type); + + if existing_orders.clone().count() > 0 { + let order = existing_orders.next().unwrap(); + + let new_amount = order.amount + amount; + self.haul_orders.get_mut(&order.id).unwrap().amount = new_amount; + } let id = self.create_unique_id(); let order = HaulOrder { @@ -57,23 +66,6 @@ impl RoomMemory { self.haul_orders.insert(id, order); } - pub fn find_haul_order(&mut self, creep: &Creep, memory: &mut ScreepsMemory) -> Option<&HaulOrder> { - let mut orders = self.haul_orders.values().collect::>(); - orders.sort_by(|a, b| a.priority.cmp(&b.priority)); - - let unresponded_order = orders.into_iter().find(|&order| order.responder.is_none()); - - if let Some(order) = unresponded_order { - let order = self.get_haul_order_mut(order.id).unwrap(); - order.add_responder(creep); - let creep_memory = memory.get_creep_mut(&creep.name()); - creep_memory.t_id = Some(order.id); - Some(order) - } else { - None - } - } - pub fn get_haul_order(&self, order_id: u128) -> Option<&HaulOrder> { self.haul_orders.get(&order_id) } diff --git a/src/room/mod.rs b/src/room/mod.rs index 4a27d4c..45d3b69 100644 --- a/src/room/mod.rs +++ b/src/room/mod.rs @@ -3,5 +3,5 @@ pub mod building; pub mod creeps; pub mod tower; pub mod planning; -pub mod structure_cache; +pub mod object_cache; pub mod hauling; \ No newline at end of file diff --git a/src/room/structure_cache.rs b/src/room/object_cache.rs similarity index 71% rename from src/room/structure_cache.rs rename to src/room/object_cache.rs index b6dcbb3..10bf9d3 100644 --- a/src/room/structure_cache.rs +++ b/src/room/object_cache.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; -use screeps::{find, HasId, ObjectId, Resource, Room, Source, StructureController, StructureExtension, StructureLink, StructureObject, StructureSpawn, StructureTower}; +use screeps::{find, game, Creep, HasId, ObjectId, Resource, Room, SharedCreepProperties, Source, StructureController, StructureExtension, StructureLink, StructureObject, StructureSpawn, StructureTower}; + +use crate::memory::ScreepsMemory; #[derive(Debug, Clone)] pub struct RoomStructureCache { @@ -12,11 +14,13 @@ pub struct RoomStructureCache { pub controller: Option, pub links: HashMap, StructureLink>, - pub towers: HashMap, StructureTower> + pub towers: HashMap, StructureTower>, + + pub creeps: HashMap, } impl RoomStructureCache { - pub fn new_from_room(room: &Room) -> RoomStructureCache { + pub fn new_from_room(room: &Room, memory: &mut ScreepsMemory) -> RoomStructureCache { let mut cache = RoomStructureCache { sources: HashMap::new(), towers: HashMap::new(), @@ -26,7 +30,9 @@ impl RoomStructureCache { controller: None, links: HashMap::new(), - extensions: HashMap::new() + extensions: HashMap::new(), + + creeps: HashMap::new(), }; if let Some(controller) = room.controller() { @@ -37,6 +43,7 @@ impl RoomStructureCache { cache.refresh_structure_cache(room); cache.refresh_spawn_cache(room); cache.refresh_dropped_resources(room); + cache.get_creep_cache(room, memory); cache } @@ -80,4 +87,17 @@ impl RoomStructureCache { self.sources.insert(source.id(), source); } } + + pub fn get_creep_cache(&mut self, room: &Room, memory: &mut ScreepsMemory) { + let creeps = &mut memory.rooms.get_mut(&room.name()).unwrap().creeps; + + for creep_name in creeps.clone().iter() { + let in_game_creep = game::creeps().get(creep_name.to_string()); + if let Some(creep) = in_game_creep { + self.creeps.insert(creep.name(), creep); + } else { + creeps.retain(|x| x != creep_name); + } + } + } } \ No newline at end of file diff --git a/src/room/planning/creep/miner.rs b/src/room/planning/creep/miner.rs index 2f13e94..27c73ec 100644 --- a/src/room/planning/creep/miner.rs +++ b/src/room/planning/creep/miner.rs @@ -9,27 +9,27 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, spawn: Structure let mut cost = 0; let mut parts = Vec::new(); - let room_memory = memory.get_room_mut(&room.name()); + let room_memory = memory.rooms.get_mut(&room.name()).unwrap(); - let needed = room.get_target_for_miner(&room_memory.clone()); + let needed = room.get_target_for_miner(&room_memory); if needed.is_none() { - let body = [Part::Move, Part::Work, Part::Carry]; - let role_name = role_to_name(Role::Upgrader); + let body = [Part::Carry, Part::Work, Part::Carry]; + let role_name = role_to_name(Role::Hauler); let name = format!("{}-{}-{}", role_name, memory.creeps.len() + 1, room.name()); let spawn_result = spawn.spawn_creep(&body, &name); if spawn_result.is_ok() { let cmemory = CreepMemory { - r: Role::Upgrader, - n_e: Some(true), - t_id: None, - l_id: None, - o_r: room.name().to_string(), - p: None, + role: Role::Hauler, + needs_energy: None, + task_id: None, + link_id: None, + owning_room: room.name().to_string(), + path: None, }; - memory.create_creep(&room.name_str(), &name, &cmemory); + memory.create_creep(&room.name_str(), &name, cmemory); } return false; } @@ -60,16 +60,16 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, spawn: Structure info!(" [SPANWER] Spawned a new miner!"); let cmemory = CreepMemory { - r: crate::memory::Role::Miner, - n_e: Some(true), - t_id: Some(needed.unwrap().into()), - l_id: None, - o_r: room.name().to_string(), - p: None, + role: crate::memory::Role::Miner, + needs_energy: Some(true), + task_id: Some(needed.unwrap().into()), + link_id: None, + owning_room: room.name().to_string(), + path: None, }; - memory.create_creep(&room.name_str(), &name, &cmemory); - let room_memory = memory.get_room_mut(&room.name()); + memory.create_creep(&room.name_str(), &name, cmemory); + let room_memory = memory.rooms.get_mut(&room.name()).unwrap(); room_memory.creeps_manufactured += 1; room_memory.sources.get_mut(needed.unwrap() as usize).unwrap().work_parts += parts.len() as u8 - 3; diff --git a/src/room/planning/room/mod.rs b/src/room/planning/room/mod.rs index 19e0097..6929272 100644 --- a/src/room/planning/room/mod.rs +++ b/src/room/planning/room/mod.rs @@ -29,6 +29,6 @@ pub fn plan_room(room: &Room, memory: &mut ScreepsMemory) -> bool { room_memory.sources = sources; - memory.create_room(&room.name(), &room_memory); + memory.create_room(&room.name(), room_memory); true } \ No newline at end of file diff --git a/src/room/tower.rs b/src/room/tower.rs index e852724..069f7eb 100644 --- a/src/room/tower.rs +++ b/src/room/tower.rs @@ -1,6 +1,6 @@ use screeps::{Room, find}; -use super::structure_cache::RoomStructureCache; +use super::object_cache::RoomStructureCache; pub fn run_towers(room: &Room, structure_cache: &RoomStructureCache) { let towers = structure_cache.towers.values(); diff --git a/src/traits/creep.rs b/src/traits/creep.rs index b27f214..19cb12b 100644 --- a/src/traits/creep.rs +++ b/src/traits/creep.rs @@ -35,7 +35,7 @@ impl CreepExtensions for screeps::Creep { }) .collect::>(); if serialized_vec.is_empty() { - memory.p = None; + memory.path = None; return; } let step_dir = num_to_dir(serialized_vec[0]); @@ -49,10 +49,10 @@ impl CreepExtensions for screeps::Creep { .collect::>() .join(""); if serialized_vec.is_empty() { - memory.p = None; + memory.path = None; return; } else { - memory.p = Some(serialized_path); + memory.path = Some(serialized_path); } let mut points = vec![]; @@ -65,7 +65,7 @@ impl CreepExtensions for screeps::Creep { } } Err(e) => { - memory.p = None; + memory.path = None; info!("Creep move failed, {:?}", e) } }; @@ -77,9 +77,9 @@ impl CreepExtensions for screeps::Creep { return; } - match creep_memory.clone().p { + match &creep_memory.path { Some(path) => { - self.better_move_by_path(path.clone(), creep_memory); + self.better_move_by_path(path.to_string(), creep_memory); } None => { let target = MoveTarget { @@ -88,9 +88,9 @@ impl CreepExtensions for screeps::Creep { } .find_path_to(creep.pos()); - creep_memory.p = Some(target.clone()); + creep_memory.path = Some(target.clone()); - self.better_move_by_path(target.clone(), creep_memory); + self.better_move_by_path(target, creep_memory); } } } diff --git a/src/traits/room.rs b/src/traits/room.rs index dcc2029..a607801 100644 --- a/src/traits/room.rs +++ b/src/traits/room.rs @@ -56,7 +56,7 @@ impl RoomExtensions for screeps::Room { fn creeps_of_role(&self, room_memory: &RoomMemory, desired_role: Role) -> Vec { room_memory.creeps.iter().filter_map(|creep_name| { - let creep_role = utils::name_to_role(&creep_name.clone()); + let creep_role = utils::name_to_role(&creep_name); if creep_role == desired_role { Some(creep_name.clone())