Position to Cardinal Direction


#1

I’m trying to tell my ship to move to a specific coordinate that is immediately adjacent to it’s current position and am failing…I know I must be missing something obvious.

Here’s my approach and understanding: I start with ship.position.get_surrounding_cardinals() and get a list of Positions. I loop though the positions to find the one I want (in this case, the shipyard), and then I want to move the ship into the shipyard (Not using naive_navigate because I want to collide if applicable). However, ship.move requires a cardinal direction, not a position.

How do I convert my position into a cardinal direction that I can use with ship.move? I’ve tried Direction.convert() but I get an error indicating my tuple doesn’t have an x variable.

Code that I thought would work is below. I’m using Python.

surround = ship.position.get_surrounding_cardinals()
for sur in surround:
    if game_map[sur].has_structure and game_map[sur].is_occupied:
        return Direction.convert(sur)

I’m open to alternative approaches and forehead slapping corrections to my code. Thanks.


#2

You can use game_map._get_target_direction(ship.position, destination_pos)


#3

Thanks! That works with one modification. The function returns a tuple and I needed to use the first element. Revised code is below.

surround = ship.position.get_surrounding_cardinals()
    for sur in surround:
        if game_map[sur].has_structure and game_map[sur].is_occupied:
            return game_map._get_target_direction(ship.position, sur)[0]