Skip to content

Commit

Permalink
Merge branch 'main' into remove_math
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Nov 28, 2023
2 parents 75fbb87 + 5a80f57 commit b672b72
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 36 deletions.
34 changes: 33 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
Unreleased
==========

- Fix incorrect setter name on `visualize_path_style` causing the setting to not work
### Breaking:

- 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<RoomTerrain>`, returning `None` when the specified room is outside the server's
map
- Changed `game::map::get_room_status` return type to `Option<RoomStatusResult>`, returning
`None` instead of the previous behavior of returning an artificial 'normal' status for rooms
outside the server's map
- Remove `constants::math::control_points_for_gcl` and `power_for_gpl` - moved to new
`screeps-game-utils` crate

### Bugfixes:

- Implement `JsCollectionIntoValue` for `Direction`, making the `JsHashMap` returned by
`game::map::describe_exits` able to be used
- Handle object return properly from `RoomTerrain::get_raw_buffer_to_array` when built in dev mode

0.16.1 (2023-10-11)
===================

### Additions:

- Add new geometry helper functions to `Direction`: `is_diagonal`, `is_orthogonal`, `multi_rot`,
`rot_cw`, and `rot_ccw`
- Add `checked_add` and `saturating_add` functions to `RoomCoordinate` and `RoomXY`, as well as
`checked_add_direction` and `saturating_add_direction` to `RoomXY`

### Bugfixes:

- Fix incorrect setter name on `visualize_path_style` causing the setting to not work
- `OwnedStructure`, `OwnedStructureObject`, and `OwnedStructureProperties`'s `my` method now
correctly handles the value being undefined.
- This fixes a panic on checking `my` for unowned controllers. (again)

0.16.0 (2023-09-14)
===================

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "screeps-game-api"
version = "0.16.0"
version = "0.16.1"
authors = ["David Ross <[email protected]>"]
documentation = "https://docs.rs/screeps-game-api/"
edition = "2021"
Expand Down Expand Up @@ -38,8 +38,8 @@ num-traits = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_repr = "0.1"
serde-wasm-bindgen = "0.6"
wasm-bindgen = "0.2"
serde-wasm-bindgen = "0.5"

[dev-dependencies]
bincode = "1.3"
Expand Down
6 changes: 3 additions & 3 deletions src/constants/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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.
Expand Down
86 changes: 86 additions & 0 deletions src/constants/small_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{borrow::Cow, fmt};
use enum_iterator::Sequence;
use js_sys::JsString;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use serde::{
de::{Error as _, Unexpected},
Deserialize, Serialize,
Expand Down Expand Up @@ -115,6 +116,91 @@ pub enum Direction {
TopLeft = 8,
}

impl Direction {
/// Whether the direction is orthogonal.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.is_orthogonal(), true);
/// assert_eq!(TopRight.is_orthogonal(), false);
/// ```
pub fn is_orthogonal(self) -> bool {
use Direction::*;

matches!(self, Top | Right | Bottom | Left)
}

/// Whether the direction is diagonal.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.is_diagonal(), false);
/// assert_eq!(TopRight.is_diagonal(), true);
/// ```
pub fn is_diagonal(self) -> bool {
!self.is_orthogonal()
}

/// Rotate the direction by a specified number of steps clockwise if
/// positive or counter-clockwise if negative.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.multi_rot(1), TopRight);
/// assert_eq!(Top.multi_rot(2), Right);
/// assert_eq!(Top.multi_rot(-1), TopLeft);
/// assert_eq!(Top.multi_rot(-2), Left);
/// assert_eq!(Top.multi_rot(64), Top);
/// ```
pub fn multi_rot(self, times: i8) -> Self {
let raw_dir = ((self as u8) - 1).wrapping_add_signed(times) % 8 + 1;
// unwrap should be optimized away, as the integer we ended up with
// is always a valid value
Self::from_u8(raw_dir).unwrap()
}

/// Rotate the direction clockwise by one step.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.rot_cw(), TopRight);
/// ```
pub fn rot_cw(self) -> Self {
self.multi_rot(1)
}

/// Rotate the direction counter-clockwise by one step.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.rot_ccw(), TopLeft);
/// ```
pub fn rot_ccw(self) -> Self {
self.multi_rot(-1)
}
}

impl JsCollectionIntoValue for Direction {
fn into_value(self) -> JsValue {
(self as u8).into()
}
}

impl From<Direction> for (i32, i32) {
/// Returns the change in (x, y) when moving in each direction.
#[inline]
Expand Down
25 changes: 7 additions & 18 deletions src/game/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomTerrain, JsValue>;

#[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getWorldSize)]
fn get_world_size() -> u32;
Expand Down Expand Up @@ -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<RoomTerrain> {
let name = room_name.into();

Map::get_room_terrain(&name)
Map::get_room_terrain(&name).ok()
}

/// Get the size of the world map.
Expand All @@ -95,6 +95,7 @@ extern "C" {
pub fn timestamp(this: &JsRoomStatusResult) -> Option<f64>;
}

#[derive(Clone, Debug)]
pub struct RoomStatusResult {
status: RoomStatus,
timestamp: Option<f64>,
Expand All @@ -110,15 +111,6 @@ impl RoomStatusResult {
}
}

impl Default for RoomStatusResult {
fn default() -> Self {
RoomStatusResult {
status: RoomStatus::Normal,
timestamp: None,
}
}
}

impl From<JsRoomStatusResult> for RoomStatusResult {
fn from(val: JsRoomStatusResult) -> Self {
RoomStatusResult {
Expand All @@ -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<RoomStatusResult> {
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]
Expand Down
Loading

0 comments on commit b672b72

Please sign in to comment.