diff --git a/screeps-game-api/src/objects/impls/construction_site.rs b/screeps-game-api/src/objects/impls/construction_site.rs index 74e666c7..38dc6007 100644 --- a/screeps-game-api/src/objects/impls/construction_site.rs +++ b/screeps-game-api/src/objects/impls/construction_site.rs @@ -15,7 +15,7 @@ simple_accessors! { } impl ConstructionSite { - pub fn owner(&self) -> String { + pub fn owner_name(&self) -> String { (js! { var self = @{self.as_ref()}; if (self.owner) { diff --git a/screeps-game-api/src/objects/impls/creep.rs b/screeps-game-api/src/objects/impls/creep.rs index 6c0da3a9..b866dc40 100644 --- a/screeps-game-api/src/objects/impls/creep.rs +++ b/screeps-game-api/src/objects/impls/creep.rs @@ -24,6 +24,10 @@ impl Creep { js_unwrap!(@{self.as_ref()}.carry[RESOURCE_ENERGY]) } + pub fn owner_name(&self) -> String { + js_unwrap!(@{self.as_ref()}.owner.username) + } + pub fn cancel_order(&self, name: &str) -> ReturnCode { js_unwrap!(@{self.as_ref()}.cancelOrder(@{name})) } @@ -148,6 +152,7 @@ macro_rules! creep_simple_concrete_action { simple_accessors! { Creep; + (id -> id -> String), (carry_capacity -> carryCapacity -> i32), (fatigue -> fatigue -> i32), (hits -> hits -> i32), @@ -158,6 +163,14 @@ simple_accessors! { (ticks_to_live -> ticksToLive -> i32), } +impl PartialEq for Creep { + fn eq(&self, other: &Creep) -> bool{ + self.id() == other.id() + } +} + +impl Eq for Creep {} + creep_simple_generic_action! { (attack(Attackable) -> attack), (dismantle(StructureProperties) -> dismantle), diff --git a/screeps-game-api/src/objects/impls/resource.rs b/screeps-game-api/src/objects/impls/resource.rs index 4adba117..53dd7e6a 100644 --- a/screeps-game-api/src/objects/impls/resource.rs +++ b/screeps-game-api/src/objects/impls/resource.rs @@ -10,4 +10,13 @@ impl Resource { simple_accessors! { Resource; (amount -> amount -> i32), + (id -> id -> String) } + +impl PartialEq for Resource { + fn eq(&self, other: &Resource) -> bool{ + self.id() == other.id() + } +} + +impl Eq for Resource {} diff --git a/screeps-game-api/src/objects/impls/room.rs b/screeps-game-api/src/objects/impls/room.rs index 17f4f4f8..1396b700 100644 --- a/screeps-game-api/src/objects/impls/room.rs +++ b/screeps-game-api/src/objects/impls/room.rs @@ -127,3 +127,11 @@ impl Room { js_unwrap!(@{self.as_ref()}.name) } } + +impl PartialEq for Room { + fn eq(&self, other: &Room) -> bool{ + self.name() == other.name() + } +} + +impl Eq for Room {} diff --git a/screeps-game-api/src/objects/impls/source.rs b/screeps-game-api/src/objects/impls/source.rs index d7d1f850..b464787b 100644 --- a/screeps-game-api/src/objects/impls/source.rs +++ b/screeps-game-api/src/objects/impls/source.rs @@ -7,3 +7,11 @@ simple_accessors! { (id -> id -> String), (ticks_to_regeneration -> ticksToRegeneration -> u32), } + +impl PartialEq for Source { + fn eq(&self, other: &Source) -> bool{ + self.id() == other.id() + } +} + +impl Eq for Source {} diff --git a/screeps-game-api/src/objects/impls/tombstone.rs b/screeps-game-api/src/objects/impls/tombstone.rs index 6cc81ace..317a7fa5 100644 --- a/screeps-game-api/src/objects/impls/tombstone.rs +++ b/screeps-game-api/src/objects/impls/tombstone.rs @@ -5,4 +5,13 @@ simple_accessors! { (creep -> creep -> Creep), (death_time -> deathTime -> u32), (ticks_to_decay -> ticksToDecay -> u32), + (id -> id -> String) } + +impl PartialEq for Tombstone { + fn eq(&self, other: &Tombstone) -> bool{ + self.id() == other.id() + } +} + +impl Eq for Tombstone {} diff --git a/screeps-game-api/src/objects/macros.rs b/screeps-game-api/src/objects/macros.rs index 9360341b..34fc4cc9 100644 --- a/screeps-game-api/src/objects/macros.rs +++ b/screeps-game-api/src/objects/macros.rs @@ -86,3 +86,35 @@ macro_rules! impl_room_object_properties { )* ); } + + +/// Macro for mass implementing `StructureProperties`, `PartialEq` and `Eq` for a type. +/// +/// Macro syntax: +/// impl_structure_properties!{ +/// $struct1, +/// $struct2, +/// ... +/// } +/// +/// This macro accepts a comma-separated list of types on which to implement the unsafe `StructureProperties` trait on +/// a screeps object. +/// From that implementation, the type gets the `id` method which is used to implement `PartialEq` and `Eq`. +/// +/// # Safety +/// The macro assumes that it is implementing the trait to a valid `Reference` +/// (See `reference_wrapper` macro) which will support all `StructureProperties` methods. +/// +macro_rules! impl_structure_properties { + ( $( $struct_name:ty ),+ ) => {$( + unsafe impl StructureProperties for $struct_name {} + + impl PartialEq for $struct_name { + fn eq(&self, other: &$struct_name) -> bool{ + self.id() == other.id() + } + } + + impl Eq for $struct_name {} + )*}; +} diff --git a/screeps-game-api/src/objects/mod.rs b/screeps-game-api/src/objects/mod.rs index c60f4e40..696536b1 100644 --- a/screeps-game-api/src/objects/mod.rs +++ b/screeps-game-api/src/objects/mod.rs @@ -227,7 +227,7 @@ pub unsafe trait OwnedStructureProperties: StructureProperties { fn my(&self) -> bool { js_unwrap!(@{self.as_ref()}.my) } - fn owner(&self) -> Option { + fn owner_name(&self) -> Option { (js! { var self = @{self.as_ref()}; if (self.owner) { @@ -325,27 +325,29 @@ impl_room_object_properties! { Tombstone, } -unsafe impl StructureProperties for OwnedStructure {} -unsafe impl StructureProperties for Structure {} -unsafe impl StructureProperties for StructureContainer {} -unsafe impl StructureProperties for StructureController {} -unsafe impl StructureProperties for StructureExtension {} -unsafe impl StructureProperties for StructureExtractor {} -unsafe impl StructureProperties for StructureKeeperLair {} -unsafe impl StructureProperties for StructureLab {} -unsafe impl StructureProperties for StructureLink {} -unsafe impl StructureProperties for StructureNuker {} -unsafe impl StructureProperties for StructureObserver {} -unsafe impl StructureProperties for StructurePowerBank {} -unsafe impl StructureProperties for StructurePowerSpawn {} -unsafe impl StructureProperties for StructurePortal {} -unsafe impl StructureProperties for StructureRampart {} -unsafe impl StructureProperties for StructureRoad {} -unsafe impl StructureProperties for StructureSpawn {} -unsafe impl StructureProperties for StructureStorage {} -unsafe impl StructureProperties for StructureTerminal {} -unsafe impl StructureProperties for StructureTower {} -unsafe impl StructureProperties for StructureWall {} +impl_structure_properties!{ + OwnedStructure, + Structure, + StructureContainer, + StructureController, + StructureExtension, + StructureExtractor, + StructureKeeperLair, + StructureLab, + StructureLink, + StructureNuker, + StructureObserver, + StructurePowerBank, + StructurePowerSpawn, + StructurePortal, + StructureRampart, + StructureRoad, + StructureSpawn, + StructureStorage, + StructureTerminal, + StructureTower, + StructureWall +} unsafe impl OwnedStructureProperties for OwnedStructure {} unsafe impl OwnedStructureProperties for StructureController {}