Skip to content

Commit

Permalink
Merge branch 'main' into flat_bindgen_complete
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Dec 20, 2023
2 parents f2d7fc6 + 73420a8 commit 5459167
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
Unreleased
==========

### Breaking:

- A number of functions on `StructureController` now return `Option<u32>` to account for cases
where they may be undefined: `progress`, `progress_total`, `ticks_to_downgrade`, and
`upgrade_blocked`

### Bugfixes:

- An undefined `hits` or `hitsMax` value on an invulnerable wall or certain controllers will no
longer cause a panic when building in dev mode

0.18.0 (2023-11-27)
===================
Expand Down
36 changes: 23 additions & 13 deletions src/objects/impls/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,11 @@ extern "C" {
#[derive(Clone, Debug)]
pub type Structure;

/// Retrieve the current hits of this structure, or `0` if this structure is
/// indestructible, such as a notice area border wall, portal, or room
/// controller.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Structure.hits)
#[wasm_bindgen(method, getter)]
pub fn hits(this: &Structure) -> u32;
#[wasm_bindgen(method, getter = hits)]
fn hits_internal(this: &Structure) -> Option<u32>;

/// Retrieve the maximum hits of this structure, or `0` if this structure is
/// indestructible, such as a notice area border wall, portal, or room
/// controller.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Structure.hitsMax)
#[wasm_bindgen(method, getter = hitsMax)]
pub fn hits_max(this: &Structure) -> u32;
fn hits_max_internal(this: &Structure) -> Option<u32>;

/// Object ID of the structure, which can be used to efficiently fetch a
/// fresh reference to the object on subsequent ticks.
Expand Down Expand Up @@ -66,6 +56,26 @@ extern "C" {
pub fn notify_when_attacked(this: &Structure, val: bool) -> i8;
}

impl Structure {
/// Retrieve the current hits of this structure, or `0` if this structure is
/// indestructible, such as a notice area border wall, portal, or room
/// controller.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Structure.hits)
pub fn hits(&self) -> u32 {
self.hits_internal().unwrap_or(0)
}

/// Retrieve the maximum hits of this structure, or `0` if this structure is
/// indestructible, such as a notice area border wall, portal, or room
/// controller.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Structure.hitsMax)
pub fn hits_max(&self) -> u32 {
self.hits_max_internal().unwrap_or(0)
}
}

impl<T> HasNativeId for T
where
T: AsRef<Structure>,
Expand Down
32 changes: 17 additions & 15 deletions src/objects/impls/structure_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ extern "C" {
#[wasm_bindgen(method, getter)]
pub fn level(this: &StructureController) -> u8;

/// The progress toward upgrading the controller to the next level
/// The progress toward upgrading the controller to the next level, or
/// `None` if the controller is unowned.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.progress)
#[wasm_bindgen(method, getter)]
pub fn progress(this: &StructureController) -> u32;
pub fn progress(this: &StructureController) -> Option<u32>;

/// The total [`StructureController::progress`] needed to upgrade the
/// controller to the next level.
/// controller to the next level, or `None` if the controller is unowned.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.progressTotal)
#[wasm_bindgen(method, getter = progressTotal)]
pub fn progress_total(this: &StructureController) -> u32;
pub fn progress_total(this: &StructureController) -> Option<u32>;

/// Information about the reservation of this controller, if it is currently
/// reserved.
Expand All @@ -57,7 +58,7 @@ extern "C" {
pub fn safe_mode(this: &StructureController) -> Option<u32>;

/// The number of of available safe mode activations, which can be increased
/// by using [`Creep::generate_safe_mode`]
/// by using [`Creep::generate_safe_mode`].
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.safeModeAvailable)
///
Expand All @@ -81,11 +82,12 @@ extern "C" {
pub fn sign(this: &StructureController) -> Option<Sign>;

/// The number of ticks until the level of the controller will be
/// decremented due to a lack of [`Creep::upgrade_controller`] activity.
/// decremented due to a lack of [`Creep::upgrade_controller`] activity, or
/// `None` if the controller is unowned.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.ticksToDowngrade)
#[wasm_bindgen(method, getter = ticksToDowngrade)]
pub fn ticks_to_downgrade(this: &StructureController) -> u32;
pub fn ticks_to_downgrade(this: &StructureController) -> Option<u32>;

/// The number of ticks until the controller can be upgraded, or have safe
/// mode activated, due to [`Creep::attack_controller`].
Expand All @@ -94,27 +96,27 @@ extern "C" {
///
/// [`Creep::attack_controller`]: crate::objects::Creep::attack_controller
#[wasm_bindgen(method, getter = upgradeBlocked)]
pub fn upgrade_blocked(this: &StructureController) -> u32;
pub fn upgrade_blocked(this: &StructureController) -> Option<u32>;

/// Activate safe mode for the room, preventing hostile creep actions in the
/// room for 20,000 ticks
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.activateSafeMode)
#[wasm_bindgen(method, js_name = activateSafeMode)]
fn activate_safe_mode_internal(this: &StructureController) -> i8;

/// Relinquish ownership of the controller and its room.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.unclaim)
#[wasm_bindgen(method, js_name = unclaim)]
fn unclaim_internal(this: &StructureController) -> i8;
}

impl StructureController {
/// Activate safe mode for the room, preventing hostile creep actions in the
/// room for 20,000 ticks
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.activateSafeMode)
pub fn activate_safe_mode(&self) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(self.activate_safe_mode_internal())
}

/// Relinquish ownership of the controller and its room.
///
/// [Screeps documentation](https://docs.screeps.com/api/#StructureController.unclaim)
pub fn unclaim(&self) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(self.unclaim_internal())
}
Expand Down

0 comments on commit 5459167

Please sign in to comment.