Skip to content

Commit

Permalink
t
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Aug 11, 2023
1 parent 3362c18 commit 7627467
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 47 deletions.
68 changes: 68 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

18 changes: 1 addition & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -69,8 +70,6 @@ pub fn game_loop() {
}
}
}

memory.write_memory();
info!("Done! cpu: {}", game::cpu::get_used());
}

Expand Down Expand Up @@ -155,4 +154,4 @@ fn run_creep(creep: &Creep, creepmem: &mut CreepMemory) {
}
}
}
}
}
19 changes: 14 additions & 5 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use serde::{Deserialize, Serialize};

use js_sys::JsString;



structstruck::strike! {
#[strikethrough[derive(Serialize, Deserialize, Debug, Clone)]]
pub struct CreepMemory{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7627467

Please sign in to comment.