screeps-game-api icon indicating copy to clipboard operation
screeps-game-api copied to clipboard

Consider adding as_has_store() for OwnedStructure

Open smessmer opened this issue 5 years ago • 2 comments

I use this code to find my structures that can be a delivery destination for energy:

fn structure_has_space_for_more_energy(structure: &OwnedStructure) -> bool {
    if let Some(structure) = structure.as_has_store() {
        structure.store_free_capacity(Some(ResourceType::Energy)) > 0
    } else {
        false
    }
}

fn find_energy_destinations() -> Vec<Structure> {
    let destinations = creep.room()
        .ok_or(...)?
        .find(find::MY_STRUCTURES)
        .iter()
        .filter(|structure| (structure.structure_type() == StructureType::Extension || structure.structure_type() == StructureType::Spawn) && structure_has_space_for_more_energy(structure));
}

But unfortunately, this doesn't work because OwnedStructure does not have a .as_has_store() method. Only Structure does. OwnedStructure does seem to have a .as_structure() method to make it into a Structure, but that one doesn't work on &OwnedStructure. But I don't get a OwnedStructure, filter() only gives me &OwnedStructure. What is the recommended way of getting the free energy capacity of a structure I own?

smessmer avatar Jan 30 '20 01:01 smessmer

.clone().as_structure() worked for me, closing this for now.

But I think it would be a better API if OwnedStructure had as_has_store() (and the other related functions) available without having to cast it to Structure.

smessmer avatar Jan 30 '20 02:01 smessmer

I'll leave it open, just to track the suggestion.

ASalvail avatar Jan 30 '20 12:01 ASalvail

I think we've got equivalent coverage in the bindgen releases with StructureObject::as_has_store, so marking this as fixed by #333

shanemadden avatar Jul 04 '23 04:07 shanemadden