def cardPath(gameMap, startLocation, cards): '''Return a list of MovePoints containing every step resulting from applying cards at startLocation. Ignores all robots on the map but does take into account walls, conveyor belts and gears. gameMap -- the current state of the game map. startLocation -- the starting BoardLocation of the move. cards -- list of robot Card moves, to be applied in order. ''' #if we can't move, we end up where we started points = [MovePoint(startLocation)] for card in cards: #move robot endPoint = move(gameMap, startLocation, card.move) if endPoint.dead: points.append(endPoint) return points if endPoint.location != startLocation: startLocation = endPoint.location points.append(MovePoint(startLocation)) sq = gameMap.getSquare(startLocation.mapPosition) #conveyor belt - may cause a 1/4 turn. speed = 1 while sq.conveyor is not None and speed <= sq.conveyor.speed: endPoint = move(gameMap, startLocation.mapPosition, sq.conveyor.direction) locMove = BoardLocation(endPoint.location.mapPosition, startLocation.direction) sq = gameMap.getSquare(endPoint.location.mapPosition) if sq.conveyor is not None: dirEnter = moveDirection(startLocation.mapPosition, endPoint.location.mapPosition) if dirEnter is None: dirEnter = startLocation.direction locMove = locMove.rotate(DIRS[sq.conveyor.direction] - DIRS[dirEnter]) startLocation = locMove points.append(MovePoint(startLocation)) speed += 1 #gears if sq.type == "ROTATE_CLOCKWISE": startLocation = startLocation.rotate(1) elif sq.type == "ROTATE_COUNTERCLOCKWISE": startLocation = startLocation.rotate(-1) else: assert sq.type in TYPE points.append(MovePoint(startLocation)) #damage damage = calcLaserDamage(gameMap, startLocation) if damage != 0: points[-1].damage = damage return points
def _moveByDirection(gameMap, position, direction): '''Move a unit one square in the given direction. Ignores all robots on the map but does take into account walls, conveyor belts and gears. Returns the final location of the move. ''' #watch for wall in this direction sideExit = SIDE[_sideMoveOut[DIRS[direction]]] sideEnter = SIDE[_sideMoveIn [DIRS[direction]]] location = BoardLocation(position, direction) #can we exit this square? sq = gameMap.getSquare(position) if (sq.walls & sideExit) != 0: return MovePoint(location) moveTo = location.move(1) #did we go off the board? if ( moveTo.mapPosition[0] < 0 or gameMap.width <= moveTo.mapPosition[0] or moveTo.mapPosition[1] < 0 or gameMap.height <= moveTo.mapPosition[1] or gameMap.getSquare(moveTo.mapPosition).type == "PIT" ): return MovePoint(location, dead=True) #can we enter the new square? sq = gameMap.getSquare(moveTo.mapPosition) if (sq.walls & sideEnter) != 0: return MovePoint(location) return MovePoint(moveTo)