Skip to content

Commit

Permalink
Before test
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Aug 21, 2023
1 parent 7627467 commit 419b816
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 197 deletions.
68 changes: 0 additions & 68 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ js-sys = "0.3"
log = "0.4"
fern = "0.6"
screeps-game-api = "0.15"
wasm-bindgen = "0.2"
wasm-bindgen = {version = "0.2", features=[]}
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
124 changes: 76 additions & 48 deletions javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,85 @@ let wasm_module;

// replace this with the name of your module
const MODULE_NAME = "screeps";
let EXECUTION_PAUSED = false;
let RED_BUTTON = false;

function console_error(...args) {
console.log(...args);
Game.notify(args.join(' '));
console.log(...args);
Game.notify(args.join(" "));
}

global.big_red_button = function (input) {
EXECUTION_PAUSED = true;
console.log("The big red button has been pressed. Are you sure you want to do this?");
console.log("This will suicide EVERY room, and EVERY creep. Are you still sure?");
console.log("If you are sure. Then rerun this command like big_red_button(\"yes\") or big_red_button(\"no\")");

if (input == undefined) {
EXECUTION_PAUSED = true;
return "Suicide? [y/n]"
} else if (input.toLowerCase() == "n" || input.toLowerCase() == "no") {
EXECUTION_PAUSED = false;
return "The bot will live to see another day..."
} else if (input.toLowerCase() == "y" || input.toLowerCase() == "yes") {
RED_BUTTON = true;
return "The bot will suicide on the next tick. Look at what you have done..."
}
};

global.toggle_exec = function () {
EXECUTION_PAUSED = !EXECUTION_PAUSED
return `Successfully toggled execution pause to: ${EXECUTION_PAUSED}`
}

module.exports.loop = function () {
// Replace the Memory object (which gets populated into our global each tick) with an empty
// object, so that accesses to it from within the driver that we can't prevent (such as
// when a creep is spawned) won't trigger an attempt to parse RawMemory. Replace the object
// with one unattached to memory magic - game functions will access the `Memory` object and
// can throw data in here, and it'll go away at the end of tick.

// Because it's in place, RawMemory's string won't be thrown to JSON.parse to deserialize -
// and because that didn't happen, RawMemory._parsed isn't set and won't trigger a
// post-tick serialize.
//delete global.Memory;
//global.Memory = {};
try {
if (wasm_module) {
wasm_module.loop();
} else {
// attempt to load the wasm only if there's enough bucket to do a bunch of work this tick
if (Game.cpu.bucket < 500) {
console.log("Not enough in the CPU bucket, not going to compile - CPU: " + JSON.stringify(Game.cpu));
return;
}

// delect the module from the cache, so we can reload it
if (MODULE_NAME in require.cache) {
delete require.cache[MODULE_NAME];
}
// load the wasm module
wasm_module = require(MODULE_NAME);
if (wasm_module != undefined) {
// load the wasm instance!
wasm_module.initialize_instance();
// run the setup function, which configures logging
wasm_module.setup();
// go ahead and run the loop for its first tick
wasm_module.loop();
} else {
console.log("Wasm module is undefined, is the name correct?")
}
}
} catch (error) {
console_error("Found error: ", error);
if (error.stack) {
console_error("Stack trace: ", error.stack);
}
console_error("Reloading wasm module");
wasm_module = null;
// Replace the Memory object (which gets populated into our global each tick) with an empty
// object, so that accesses to it from within the driver that we can't prevent (such as
// when a creep is spawned) won't trigger an attempt to parse RawMemory. Replace the object
// with one unattached to memory magic - game functions will access the `Memory` object and
// can throw data in here, and it'll go away at the end of tick.

// Because it's in place, RawMemory's string won't be thrown to JSON.parse to deserialize -
// and because that didn't happen, RawMemory._parsed isn't set and won't trigger a
// post-tick serialize.
delete global.Memory;
global.Memory = {};
try {
if (wasm_module) {
if (RED_BUTTON) {
wasm_module.red_button();
}
if (!EXECUTION_PAUSED) {
wasm_module.loop();
}
} else {
// attempt to load the wasm only if there's enough bucket to do a bunch of work this tick
if (Game.cpu.bucket < 500) {
console.log("Not enough in the CPU bucket, not going to compile - CPU: " + JSON.stringify(Game.cpu));
return;
}

// delect the module from the cache, so we can reload it
if (MODULE_NAME in require.cache) {
delete require.cache[MODULE_NAME];
}
// load the wasm module
wasm_module = require(MODULE_NAME);
if (wasm_module != undefined) {
// load the wasm instance!
wasm_module.initialize_instance();
// run the setup function, which configures logging
wasm_module.setup();
} else {
console.log("Wasm module is undefined, is the name correct?");
}
}
}
} catch (error) {
console_error("Found error: ", error);
if (error.stack) {
console_error("Stack trace: ", error.stack);
}
console_error("Reloading wasm module");
wasm_module = null;
}
};
1 change: 1 addition & 0 deletions src/building/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod roads;
47 changes: 47 additions & 0 deletions src/building/roads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use log::info;
use screeps::{Source, StructureSpawn, HasPosition, pathfinder::{SearchOptions, MultiRoomCostResult, self}, RoomName, LocalCostMatrix, game, StructureType, find, StructureProperties, look, ConstructionSite};

pub fn source_to_spawn(source: &Source, spawn: &StructureSpawn) {
let opts = SearchOptions::new(road_callback).max_ops(100000000).plain_cost(2).swamp_cost(5).max_rooms(1);
let path = pathfinder::search(spawn.pos(), source.pos(), 1, Some(opts));
if !path.incomplete() {
info!("Road complete");
for pos in path.path() {
let room = game::rooms().get(pos.room_name()).unwrap();
if room.look_for_at_xy(look::CONSTRUCTION_SITES, pos.x().u8(), pos.y().u8()).is_empty() {
match room.create_construction_site(pos.x().u8(), pos.y().u8(), StructureType::Road, None) {
Ok(_) => {},
Err(e) => {
println!("Error creating construction site: {:?}", e);
}
};
}
}
} else {
info!("Road incomplete?");
}
}

pub fn road_callback(room_name: RoomName) -> MultiRoomCostResult {
let mut matrix = LocalCostMatrix::new();
if let Some(room) = game::rooms().get(room_name) {
for road in room.find(find::STRUCTURES, None).into_iter().filter(|s| s.structure_type() == StructureType::Road) {
matrix.set(road.pos().xy(), 1);
}
let csites = room.find(find::CONSTRUCTION_SITES, None);
for site in csites {
match site.structure_type() {
StructureType::Road => matrix.set(site.pos().xy(), 1),
StructureType::Rampart => matrix.set(site.pos().xy(), 1),
StructureType::Container => matrix.set(site.pos().xy(), 1),
_ => todo!(),
}
}
for creep in room.find(find::CREEPS, None) {
matrix.set(creep.pos().xy(), 1);
}
}

MultiRoomCostResult::CostMatrix(matrix.into())

}
Loading

0 comments on commit 419b816

Please sign in to comment.