Example #1
0
def slash(action, state):
    """The Slash action is triggered if a character able to slash steps past an enemy"""

    # Check to make sure the actor is Moving and that they can slash
    if not isinstance(
            action,
            actions.Move) or "Slash" not in action['element']['abilities']:
        return []

    actor = action['element']
    src = state.find(actor)
    dest = action['target']
    # Can only target cells that were next to the actor at the start and end of his movement.
    # By doing it this way, we can handle leaping with the same test.
    targets = grid.neighbors(src) & grid.neighbors(dest)

    results = []
    for target in targets:
        # for every cell in targets, check if an enemy is present
        if target in state and actor['team'] != state[target][
                'team'] and "Health" in state[target]['abilities']:
            # If so, add a Slash Action as a reaction.
            reaction = CreateAction({
                "type": "Slash",
                "element": actor,
                "target": target
            })
            logger.debug("%s triggered %s", action, reaction)
            results.append(reaction)

    return results
Example #2
0
    def get_action(cls, actor, state):
        actor['bomb cooldown'] -= 1
        if actor['bomb cooldown'] >= 0:
            return

        for cell in grid.burst(state.find(actor), 3):
            if cell in state.keys():
                continue

            # Check who we're targeting
            neighbors = [state[c] for c in grid.neighbors(cell) if c in state]
            teams = set(n['team'] for n in neighbors if 'team' in n)
            if actor['team'] in teams:  # Can't hit allies.
                continue
            elif len(teams
                     ) == 0:  # Don't target cells where you won't hit anyone.
                continue
            else:
                return [
                    CreateAction({
                        'type': "ThrowBomb",
                        'element': actor,
                        'target': cell
                    })
                ]
def _redelmeier_routine(p, parent, untried, forbidden):

    # todo use lifo linked list / stack implementation for untried

    if p == 0:
        yield Polyomino(normalize(parent))

    else:

        while untried:

            nbr = untried.pop()

            child = parent | {nbr}

            new_neighbours = list(
                filter(
                    lambda x: x not in parent and x not in forbidden and (
                        (x[1] >= 0 and x[0] >= 0) or x[1] >= 1),
                    neighbors(nbr).difference(untried)))

            forbidden = forbidden.union([nbr])

            yield from _redelmeier_routine(p - 1, child,
                                           untried + new_neighbours, forbidden)
Example #4
0
 def validate(self, state):
     """Validate that the action doesn't violate any rules."""
     source = state.find(self.element)
     if self.target not in grid.neighbors(source):
         return False
     if "team" not in state.get(
             self.target,
         {}) or self.element['team'] == state[self.target]['team']:
         return False
     return True
Example #5
0
def __expand_space(grid, fringe, visited, g, h, parent, curr):
    for n in neighbors(grid, curr):
        if n not in visited:
            if n not in fringe:
                g[n] = float('inf')
                parent[n] = None
            if g[curr] + cost(grid, curr, n) < g[n]:
                g[n] = g[curr] + cost(grid, curr, n)
                parent[n] = curr
                fringe[n] = g[n] + h(n)
Example #6
0
 def validate(self, state):
     if "Move" not in self.element["abilities"]:
         logger.debug("%s does not have the ability to Move")
         return False
     if self.target not in grid.neighbors(state.find(self.element)):
         return False
     if self.target not in grid.VALID_CELLS:
         return False
     if self.target in state:
         logger.debug("%s tried to move to %s, which is occupied by %s",
                      self.element, self.target, state[self.target])
         return False
     return True
Example #7
0
 def get_action(cls, actor, state):
     results = []
     results.append(
         CreateAction({
             'type': "Explode",
             'element': actor,
             'target': state.find(actor)
         }))
     for cell in grid.neighbors(state.find(actor)):
         results.append(
             CreateAction({
                 'type': "BlastWave",
                 'element': actor,
                 'target': cell
             }))
     return results
Example #8
0
    def h_favor_highways_smart(grid, s, goal, *args, **kwargs):
        # Only useful for searching for highways beyond manhattan distance bc we only need to compare manhattan distance
        # d_x = s.coords[0] - goal.coords[0]
        # d_y = s.coords[1] - goal.coords[1]
        m_d = manhattan_distance(s, goal)
        c = h(grid, s, goal, *args, **kwargs)
        for n in neighbors(grid, s):
            if is_horizontal(s, n) or is_vertical(s, n):
                if s.is_highway() and n.is_highway():
                    m_d_n = manhattan_distance(n, goal)
                    if m_d < m_d_n:
                        c = cost(grid, s, n) * m_d
            # In order to take into account searching for highways beyond the manhattan distance,
            # we would need to know the parent of s
            # if s.parent is n:
            #    continue

        return c
Example #9
0
def __expand_space_integrated(grid, o, c_a, c_i, g, anchor, inad, bp, w1, w2, curr):
    for k in o:
        if curr in o[k]:
            o[k].remove(curr)
    for n in neighbors(grid, curr):
        if n not in g:
            g[n] = float('inf')
            bp[n] = None
        g_new = g[curr] + cost(grid, curr, n)
        if g[n] > g[curr] + cost(grid, curr, n):
            g[n] = g_new
            bp[n] = curr
            if n not in c_a:
                o[anchor][n] = key(g, anchor, n, w1)
                if n not in c_i:
                    for i in inad:
                        if key(g, i, n, w1) <= w2*o[anchor][n]:
                            o[i][n] = key(g, i, n, w1)
while not frontier.empty():
    current = frontier.get()
    for next in grid.negihbors(current):
        if next not in visited:
            frontier.put(next)
            visited[next] = True

#modification to keep track of movment
frontier = Queue()
frontier.put(start)
came_from = []
came_from[start] = True

while not frontier.empty():
    current = frontier.get()
    for next in grid.neighbors(current):
        if next not in came_from:
            frontier.put(next)
            came_from[next] = current

#reconstruction of path
current = goal  #here we have goal
path = [current]
while current != start:
    current = came_from[current]
    path.append(current)
path.reverse()

#'eary exit' means to stop expanding the frontier as soon as the goal is found
frontier = Queue()
frontier.put(start)
while not frontier.empty():
	current = frontier.get()
	for next in grid.negihbors(current):
		if next not in visited:
			frontier.put(next)
			visited[next] = True

#modification to keep track of movment
frontier = Queue()
frontier.put(start)
came_from =[]
came_from[start] = True

while not frontier.empty():
	current = frontier.get()
	for next in grid.neighbors(current):
		if next not in came_from:
			frontier.put(next)
			came_from[next] = current

#reconstruction of path
current = goal 	#here we have goal
path = [current]
while current != start:
	current = came_from[current]
	path.append(current)
path.reverse()

#'eary exit' means to stop expanding the frontier as soon as the goal is found
frontier = Queue()
frontier.put(start)
Example #12
0
 def threatened_cells(cls, actor, state):
     return grid.neighbors(state.find(actor))
Example #13
0
 def targets(cls, actor, state):
     results = set()
     for target, element in state.items():
         if 'team' not in element or element['team'] != actor['team']:
             results |= grid.neighbors(target)
     return results
Example #14
0
 def test_neighbors(self):
     n = [s for (i, s) in np.ndenumerate(self.grid) if i is not (1, 1)]
     computed_n = neighbors(self.grid, self.grid[1, 1])
     for s in n:
         assert s in computed_n