diff --git a/Cargo.lock b/Cargo.lock index bc16adf..f2c2bd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "arrayvec" version = "0.7.4" @@ -14,6 +20,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "base64" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bumpalo" version = "3.13.0" @@ -36,12 +57,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "crustacean" version = "0.0.0" dependencies = [ + "base64", + "bincode", "fern", + "flate2", "js-sys", + "lazy_static", "log", "screeps-game-api", "serde", @@ -93,6 +127,16 @@ dependencies = [ "log", ] +[[package]] +name = "flate2" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b9429470923de8e8cbd4d2dc513535400b4b3fef0319fb5c4e1f520a7bef743" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "heck" version = "0.4.1" @@ -114,12 +158,30 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +dependencies = [ + "spin", +] + [[package]] name = "log" version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + [[package]] name = "num-derive" version = "0.4.0" @@ -248,6 +310,12 @@ dependencies = [ "syn", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "structstruck" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index f55e1cb..c371843 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,10 @@ web-sys = { version = "0.3", features = ["console"] } structstruck = "0.4.1" serde_json = "1.0.104" serde = { version = "1.0.183", features = ["derive"] } +bincode = "1.3.3" +lazy_static = { version = "1.4.0", features = ["spin"] } +base64 = "0.21.2" +flate2 = "1.0.26" [dev-dependencies] wasm-bindgen-test = "0.3" diff --git a/LICENSE b/LICENSE deleted file mode 100644 index bae16f1..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 David Ross - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index 481023e..c762351 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,5 @@ -# screeps-starter-rust - -Starter Rust AI for [Screeps: World][screeps], the JavaScript-based MMO game. - -This uses the [`screeps-game-api`] bindings from the [rustyscreeps] organization. - -While it's possible to compile using [`wasm-pack`] directly using the Node.js target, -some modifications are needed to load the output within the Screep environment, so it's -recommended to use [`cargo-screeps`] for building and deploying your code. - -The documentation is currently a bit sparse. API docs which list functions one -can use are located at https://docs.rs/screeps-game-api/. - -Almost all crates on https://crates.io/ are usable (only things which interact with OS -apis are broken). - -Quickstart: +## init ```sh # Install CLI dependency: cargo install cargo-screeps diff --git a/src/lib.rs b/src/lib.rs index 69ee430..2a9ced6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ mod memory; // add wasm_bindgen to any function you would like to expose for call from js #[wasm_bindgen(js_name = setup)] pub fn setup() { - logging::setup_logging(logging::Info); + logging::setup_logging(logging::Debug); } // this enum will represent a creep's lock on a specific target object, storing a js reference @@ -47,6 +47,7 @@ pub fn game_loop() { memory.write_memory(); } else { run_creep(&creep.unwrap(), &mut memory.creeps.get_mut(&name).unwrap()); + memory.write_memory(); } } @@ -69,8 +70,6 @@ pub fn game_loop() { } } } - - memory.write_memory(); info!("Done! cpu: {}", game::cpu::get_used()); } @@ -155,4 +154,4 @@ fn run_creep(creep: &Creep, creepmem: &mut CreepMemory) { } } } -} +} \ No newline at end of file diff --git a/src/memory.rs b/src/memory.rs index 443eb20..1ffa700 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -6,8 +6,6 @@ use serde::{Deserialize, Serialize}; use js_sys::JsString; - - structstruck::strike! { #[strikethrough[derive(Serialize, Deserialize, Debug, Clone)]] pub struct CreepMemory{ @@ -35,11 +33,22 @@ structstruck::strike! { impl Memory { pub fn init_memory() -> Self { let memory_jsstring = screeps::raw_memory::get(); - let memory: Memory = serde_json::from_str(&memory_jsstring.as_string().unwrap()).unwrap(); - return memory; + let memory_string = memory_jsstring.as_string().unwrap(); + if memory_string == "" { + let memory = Memory { + creeps: HashMap::new(), + }; + memory.write_memory(); + memory + } else { + let memory: Memory = serde_json::from_str(&memory_string).unwrap(); + memory + } } pub fn write_memory(&self) { - screeps::raw_memory::set(&JsString::from(serde_json::to_string(&self).unwrap())); + //let serialized = serde_json::to_string(&self).unwrap(); + //et js_serialized = JsString::from(serialized); + //screeps::raw_memory::set(&js_serialized); } }