Skip to content

Add conversion from StructureObject to StructureType, add StructureType::is_obstacle #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Unreleased
==========

### Additions:

- Add `StructureObject::structure_type` function to return the associated `StructureType`
- Implement `OBSTACLE_OBJECT_TYPES` constant for structures by adding `StructureType::is_obstacle`
function

0.23.0 (2025-04-09)
===================

Expand Down
2 changes: 1 addition & 1 deletion src/constants/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::types::{ResourceType, StructureType};

// LOOK_* defined in `look.rs`

// OBSTACLE_OBJECT_TYPES not yet implemented
// OBSTACLE_OBJECT_TYPES defined in `types.rs`

// BODYPART_COST defined in `small_enums.rs`

Expand Down
32 changes: 32 additions & 0 deletions src/constants/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,38 @@ impl StructureType {
};
Some(hits)
}

/// Translates the `OBSTACLE_OBJECT_TYPES` constant, returning true for
/// structure types that are obstacles
#[inline]
pub const fn is_obstacle(self) -> bool {
use self::StructureType::*;

match self {
Spawn => true,
Extension => true,
Road => false,
Wall => true,
Rampart => false,
KeeperLair => false,
Portal => false,
Controller => true,
Link => true,
Storage => true,
Tower => true,
Observer => true,
PowerBank => true,
PowerSpawn => true,
Extractor => false,
Lab => true,
Terminal => true,
Container => false,
Nuker => true,
Factory => true,
InvaderCore => true,
_ => false,
}
}
}

/// Translates `SUBSCRIPTION_TOKEN` and `INTERSHARD_RESOURCES` constants.
Expand Down
36 changes: 35 additions & 1 deletion src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ pub mod action_error_codes;
use enum_dispatch::enum_dispatch;
use wasm_bindgen::{JsCast, JsValue};

use crate::{objects::*, prelude::*, ResourceType, RESOURCES_ALL};
use crate::{
constants::{ResourceType, StructureType, RESOURCES_ALL},
objects::*,
prelude::*,
};

#[enum_dispatch(Attackable)]
pub enum AttackableObject {
Expand Down Expand Up @@ -346,6 +350,36 @@ pub enum StructureObject {
StructureWall,
}

impl StructureObject {
pub fn structure_type(&self) -> StructureType {
use StructureObject::*;

match self {
StructureContainer(_) => StructureType::Container,
StructureController(_) => StructureType::Controller,
StructureExtension(_) => StructureType::Extension,
StructureExtractor(_) => StructureType::Extractor,
StructureFactory(_) => StructureType::Factory,
StructureInvaderCore(_) => StructureType::InvaderCore,
StructureKeeperLair(_) => StructureType::KeeperLair,
StructureLab(_) => StructureType::Lab,
StructureLink(_) => StructureType::Link,
StructureNuker(_) => StructureType::Nuker,
StructureObserver(_) => StructureType::Observer,
StructurePortal(_) => StructureType::Portal,
StructurePowerBank(_) => StructureType::PowerBank,
StructurePowerSpawn(_) => StructureType::PowerSpawn,
StructureRampart(_) => StructureType::Rampart,
StructureRoad(_) => StructureType::Road,
StructureSpawn(_) => StructureType::Spawn,
StructureStorage(_) => StructureType::Storage,
StructureTerminal(_) => StructureType::Terminal,
StructureTower(_) => StructureType::Tower,
StructureWall(_) => StructureType::Wall,
}
}
}

#[enum_dispatch(Transferable)]
pub enum TransferableObject {
StructureExtension,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@

// #![warn(clippy::missing_const_for_fn)]

// temporary - TODO remove and fix these if it's made permanent in the current form
#![allow(clippy::uninlined_format_args)]
// disable deprecation warnings - TODO need to figure out how to get wasm_bindgen's new thread_local
// attribute working
#![allow(deprecated)]
Expand Down