screeps-game-api
screeps-game-api copied to clipboard
Consider adding as_has_store() for OwnedStructure
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?
.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.
I'll leave it open, just to track the suggestion.
I think we've got equivalent coverage in the bindgen releases with StructureObject::as_has_store, so marking this as fixed by #333