<commit_before>"""
Functions for doing geometry calculations in the various types of coordinates
shared between client and server.
"""

import heapq
import math

from src.shared.config import CHUNK_SIZE
from src.shared.exceptions import NoPathToTargetError
from src.shared.logconfig import newLogger

log = newLogger(__name__)

# Costs used by pathfinding code.
# Measure distances in unit coordinates.
ORTHOGONAL_COST = CHUNK_SIZE
DIAGONAL_COST   = int(ORTHOGONAL_COST * math.sqrt(2))

def findPath(gameState, srcPos, destPos):
    """
    Compute and return a path from srcPos to destPos, avoiding any obstacles.

    The returned path will be a list of waypoints such that a unit at srcPos
    could travel by straight line to each of the waypoints in order and thus
    get to destPos without hitting any obstacles.
    """

    log.debug("Searching for path from {} to {}".format(srcPos, destPos))

    chunkWidth, chunkHeight = gameState.sizeInChunks

    # Value larger than any actual distance.
    FAR_FAR_AWAY = ORTHOGONAL_COST**2 * chunkWidth * chunkHeight

    srcChunk  = unitToChunk(srcPos)
    destChunk = unitToChunk(destPos)
    srcCX,  srcCY  = srcChunk
    destCX, destCY = destChunk

    # If the source and dest points are in the same chunk, there's no point
    # doing a chunk-based search to find a path, because the result will be
    # trivial. Just go straight to the dest.
    if srcChunk == destChunk:
        return [destPos]

    # This list actually serves 2 purposes. First, it keeps track of which
    # chunks have been visited already. Second, for those that have been
    # visited, it tracks which chunk came before it in the shortest path from
    # the srcChunk to it.
    parents = [[None for y in range(chunkHeight)]
               for x in range(chunkWidth)]

    # Set to True for a node once we know we've found a shortest path to it, so
    # that we don't keep checking new paths to that node.
    nodeFinalized = [[False for y in range(chunkHeight)]
                      for x in range(chunkWidth)]
<commit_msg>Add bounds checking to starting point of path finding. Fixes #23.<commit_after>"""
Functions for doing geometry calculations in the various types of coordinates
shared between client and server.
"""

import heapq
import math

from src.shared.config import CHUNK_SIZE
from src.shared.exceptions import NoPathToTargetError
from src.shared.logconfig import newLogger

log = newLogger(__name__)

# Costs used by pathfinding code.
# Measure distances in unit coordinates.
ORTHOGONAL_COST = CHUNK_SIZE
DIAGONAL_COST   = int(ORTHOGONAL_COST * math.sqrt(2))

def findPath(gameState, srcPos, destPos):
    """
    Compute and return a path from srcPos to destPos, avoiding any obstacles.

    The returned path will be a list of waypoints such that a unit at srcPos
    could travel by straight line to each of the waypoints in order and thus
    get to destPos without hitting any obstacles.
    """

    log.debug("Searching for path from {} to {}".format(srcPos, destPos))

    chunkWidth, chunkHeight = gameState.sizeInChunks

    # Value larger than any actual distance.
    FAR_FAR_AWAY = ORTHOGONAL_COST**2 * chunkWidth * chunkHeight

    srcChunk  = unitToChunk(srcPos)
    destChunk = unitToChunk(destPos)
    srcCX,  srcCY  = srcChunk
    destCX, destCY = destChunk

    # Make sure we're starting within the world.
    if not (0 <= srcCX < chunkWidth and 0 <= srcCY < chunkHeight):
        raise NoPathToTargetError("Starting point {} outside the word."
                                  .format(srcPos))

    # If the source and dest points are in the same chunk, there's no point
    # doing a chunk-based search to find a path, because the result will be
    # trivial. Just go straight to the dest.
    if srcChunk == destChunk:
        return [destPos]

    # This list actually serves 2 purposes. First, it keeps track of which
    # chunks have been visited already. Second, for those that have been
    # visited, it tracks which chunk came before it in the shortest path from
    # the srcChunk to it.
    parents = [[None for y in range(chunkHeight)]
               for x in range(chunkWidth)]

    # Set to True for a node once we know we've found a shortest path to it, so
    # that we don't keep checking new paths to that node.
    nodeFinalized = [[False for y in range(chunkHeight)]
                      for x in range(chunkWidth)]

{'fraction_non_alphanumeric': 0.0627833182230281, 'fraction_numerical': 0.0022665457842248413, 'mean_word_length': 3.1869070208728654, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2722257', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1207, 'n_words': 549}