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

Update RoomPosition find functions to use Find enum

Open JCGrant opened this issue 3 years ago • 0 comments

This PR updates the find methods to operate similarly to the look methods.

I tried to update this code at screeps-starter-rust:

for source in room.find(find::SOURCES_ACTIVE).iter() {
    let id = source.id();
    creep_targets.insert(name, CreepTarget::Harvest(id));
    break;
}

to use find_closest_by_path but the types kept getting in the way.

You can now write something like this:

let room_position: RoomPosition = creep.pos().into();
let source = room_position.find_closest_by_path(find::SOURCES_ACTIVE);
match source {
    Some(source) => {
        let id = source.id();
        creep_targets.insert(name, CreepTarget::Harvest(id));
    }
    None => {}
}

However, there is a slight quirk which I don't quite understand.

I would ideally like to write something like the following:

creep.pos().find_closest_by_path(SOURCES);

Without needing the intermediate into() call.

Especially because there is another call to creep.pos() above in the same code, which just works.

creep.pos().is_near_to(source.pos())

Do you have an idea why this might be the case?

JCGrant avatar Aug 04 '21 08:08 JCGrant