Why get_unsafe_moves gives me wrong direction?


#1

Hi all. New to Halite, trying to use the get_unsafe_moves function.

I’m writing a part of code to bring my ship to shipyard:

        directions_to_shipyard = game_map.get_unsafe_moves(ship.position, me.shipyard.position)
        logging.info("SHIP {} from {} to {}".format(ship.id,(ship.position.x,ship.position.y),(me.shipyard.position.x,me.shipyard.position.y)))
        logging.info("SHIP {}'s RETURN ROUTE: {}".format(ship.id,str(directions_to_shipyard)))

But it keeps giving me the wrong direction. Here’s an example log:

INFO:root:SHIP 3 from (14, 24) to (12, 24)
INFO:root:SHIP 3’s RETURN ROUTE: [(1, 0)]

It should be [(-1,0)]. But it gives me (1,0) which makes my ship further away from shipyard…

I checked the source code of get_unsafe_moves. Nothing weird there. What might be causing this?


#2

I had a problem where the distance calculation in get_unsafe_moves was inadvertently normalizing itself when it shouldn’t be normalized in the Python 3 starter kit. Could this be a problem for you as well?


#3

Looking closely into the source code of game_map.py and positional.py, I found that the get_unsafe_move is kinda weird.

It calculates distance by subtracting two points. Looking at positional.py, we know that by default, this coordinate would be normalized.

So lets say we want to move from (14,24) to (12,24). The distance would be (46,0), which caused the get_unsafe_moves function to think they can go faster by choosing the inverted direction.

I proposed a solution by a pull request in their github repo. If anyone else using Python3 and this function, feel free to use this one-line fix: