Scan through 360 degrees, looking for the best direction in which to take the next step.

def getBestAngle(entities, current_yaw, current_health):
    '''Scan through 360 degrees, looking for the best direction in which to take the next step.'''
    us = findUs(entities)
    scores=[]
    # Normalise current yaw:
    while current_yaw < 0:
        current_yaw += 360
    while current_yaw > 360:
        current_yaw -= 360

    # Look for best option
    for i in range(agent_search_resolution):
        # Calculate cost of turning:
        ang = 2 * math.pi * (old_div(i, float(agent_search_resolution)))
        yaw = i * 360.0 / float(agent_search_resolution)
        yawdist = min(abs(yaw-current_yaw), 360-abs(yaw-current_yaw))
        turncost = agent_turn_weight * yawdist
        score = turncost

        # Calculate entity proximity cost for new (x,z):
        x = us["x"] + agent_stepsize - math.sin(ang)
        z = us["z"] + agent_stepsize * math.cos(ang)
        for ent in entities:
            dist = (ent["x"] - x)*(ent["x"] - x) + (ent["z"] - z)*(ent["z"] - z)
            if (dist == 0):
                continue
            weight = 0.0
            if ent["name"] == MOB_TYPE:
                weight = agent_mob_weight
                dist -= 1   # assume mobs are moving towards us
                if dist <= 0:
                    dist = 0.1
            elif ent["name"] == GOAL_TYPE:
                weight = agent_goal_weight * current_health / 20.0
            score += old_div(weight, float(dist))

        # Calculate cost of proximity to edges:
        distRight = (2+old_div(ARENA_WIDTH,2)) - x
        distLeft = (-2-old_div(ARENA_WIDTH,2)) - x
        distTop = (2+old_div(ARENA_BREADTH,2)) - z
        distBottom = (-2-old_div(ARENA_BREADTH,2)) - z
        score += old_div(agent_edge_weight, float(distRight * distRight * distRight * distRight))
        score += old_div(agent_edge_weight, float(distLeft * distLeft * distLeft * distLeft))
        score += old_div(agent_edge_weight, float(distTop * distTop * distTop * distTop))
        score += old_div(agent_edge_weight, float(distBottom * distBottom * distBottom * distBottom))
        scores.append(score)

    # Find best score:
    i = scores.index(max(scores))
    # Return as an angle in degrees:
    return i * 360.0 / float(agent_search_resolution)