From 83bccf8fa68aa77c6b878a2b001c054e2f0af5fd Mon Sep 17 00:00:00 2001 From: Shane Madden Date: Mon, 27 Nov 2023 19:00:38 -0700 Subject: [PATCH 1/2] Change tower damage constant types for easier use (#469) --- CHANGELOG.md | 5 +++++ src/constants/numbers.rs | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46f3c646..3db5dcd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Unreleased ========== +### Breaking: + +- Change `TOWER_OPTIMAL_RANGE` and `TOWER_FALLOFF_RANGE` types to `u8` and `TOWER_FALLOFF` type + to `f64` + ### Bugfixes: - Implement `JsCollectionIntoValue` for `Direction`, making the `JsHashMap` returned by diff --git a/src/constants/numbers.rs b/src/constants/numbers.rs index 1d0c6b8a..5429a8f6 100644 --- a/src/constants/numbers.rs +++ b/src/constants/numbers.rs @@ -398,10 +398,10 @@ pub const TOWER_POWER_HEAL: u32 = 400; pub const TOWER_POWER_REPAIR: u32 = 800; /// Tower actions at a range beyond this distance suffer falloff penalties - see /// [`TOWER_FALLOFF`]. -pub const TOWER_OPTIMAL_RANGE: u32 = 5; +pub const TOWER_OPTIMAL_RANGE: u8 = 5; /// Tower actions at a range greater than or equal to this distance suffer the /// maxium falloff penalties - see [`TOWER_FALLOFF`]. -pub const TOWER_FALLOFF_RANGE: u32 = 20; +pub const TOWER_FALLOFF_RANGE: u8 = 20; /// Maximum percentage reduction in healing, repair, and attack effectiveness /// for towers due to range. /// @@ -415,7 +415,7 @@ pub const TOWER_FALLOFF_RANGE: u32 = 20; /// ``` /// /// [source]: https://github.com/screeps/engine/blob/f02d16a44a00c35615ae227fc72a3c9a07a6a39a/src/processor/intents/towers/attack.js#L38 -pub const TOWER_FALLOFF: f32 = 0.75; +pub const TOWER_FALLOFF: f64 = 0.75; /// Initial hits for observer structures; consider using the /// [`StructureType::initial_hits`] function. From c96cff26486ff5209e12f68549982dc8c94b53d8 Mon Sep 17 00:00:00 2001 From: Shane Madden Date: Mon, 27 Nov 2023 19:14:05 -0700 Subject: [PATCH 2/2] `Option` returns for out of map room status/terrain (#467) --- CHANGELOG.md | 6 ++++++ src/game/map.rs | 25 +++++++------------------ src/objects/impls/room_terrain.rs | 8 ++++---- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3db5dcd6..aecad2c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Unreleased - Change `TOWER_OPTIMAL_RANGE` and `TOWER_FALLOFF_RANGE` types to `u8` and `TOWER_FALLOFF` type to `f64` +- Changed `RoomTerrain::new` and `game::map::get_room_terrain` return type to + `Option`, returning `None` when the specified room is outside the server's + map +- Changed `game::map::get_room_status` return type to `Option`, returning + `None` instead of the previous behavior of returning an artificial 'normal' status for rooms + outside the server's map ### Bugfixes: diff --git a/src/game/map.rs b/src/game/map.rs index 6d0a8aa4..167b0870 100644 --- a/src/game/map.rs +++ b/src/game/map.rs @@ -33,8 +33,8 @@ extern "C" { #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getRoomLinearDistance)] fn get_room_linear_distance(room_1: &JsString, room_2: &JsString, continuous: bool) -> u32; - #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getRoomTerrain)] - fn get_room_terrain(room_name: &JsString) -> RoomTerrain; + #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getRoomTerrain, catch)] + fn get_room_terrain(room_name: &JsString) -> Result; #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getWorldSize)] fn get_world_size() -> u32; @@ -70,10 +70,10 @@ pub fn get_room_linear_distance(from_room: RoomName, to_room: RoomName, continuo /// vision in. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.map.getRoomTerrain) -pub fn get_room_terrain(room_name: RoomName) -> RoomTerrain { +pub fn get_room_terrain(room_name: RoomName) -> Option { let name = room_name.into(); - Map::get_room_terrain(&name) + Map::get_room_terrain(&name).ok() } /// Get the size of the world map. @@ -95,6 +95,7 @@ extern "C" { pub fn timestamp(this: &JsRoomStatusResult) -> Option; } +#[derive(Clone, Debug)] pub struct RoomStatusResult { status: RoomStatus, timestamp: Option, @@ -110,15 +111,6 @@ impl RoomStatusResult { } } -impl Default for RoomStatusResult { - fn default() -> Self { - RoomStatusResult { - status: RoomStatus::Normal, - timestamp: None, - } - } -} - impl From for RoomStatusResult { fn from(val: JsRoomStatusResult) -> Self { RoomStatusResult { @@ -141,13 +133,10 @@ pub enum RoomStatus { /// area or currently inaccessible. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.map.getRoomStatus) -pub fn get_room_status(room_name: RoomName) -> RoomStatusResult { +pub fn get_room_status(room_name: RoomName) -> Option { let name = room_name.into(); - Map::get_room_status(&name) - .ok() - .map(RoomStatusResult::from) - .unwrap_or_default() + Map::get_room_status(&name).ok().map(RoomStatusResult::from) } #[wasm_bindgen] diff --git a/src/objects/impls/room_terrain.rs b/src/objects/impls/room_terrain.rs index ed37062f..4dfe77e6 100644 --- a/src/objects/impls/room_terrain.rs +++ b/src/objects/impls/room_terrain.rs @@ -15,8 +15,8 @@ extern "C" { #[wasm_bindgen(js_namespace = Room, js_name = Terrain)] pub type RoomTerrain; - #[wasm_bindgen(constructor, js_namespace = Room, js_class = Terrain)] - fn new_internal(room_name: &JsString) -> RoomTerrain; + #[wasm_bindgen(constructor, js_namespace = Room, js_class = Terrain, catch)] + fn new_internal(room_name: &JsString) -> Result; /// Get the type of terrain at given coordinates. /// @@ -38,10 +38,10 @@ impl RoomTerrain { /// of the room. /// /// [Screeps documentation](https://docs.screeps.com/api/#Room.Terrain.constructor) - pub fn new(room_name: RoomName) -> RoomTerrain { + pub fn new(room_name: RoomName) -> Option { let name = room_name.into(); - Self::new_internal(&name) + Self::new_internal(&name).ok() } /// Get a copy of the underlying Uint8Array with the data about the room's