Skip to content

Commit

Permalink
fix: wrap artefact in namedtuple (#1498)
Browse files Browse the repository at this point in the history
* fix: wrap artefact in namedtuple

* declare Artefact
  • Loading branch information
dionizh committed Mar 30, 2021
1 parent b82a05c commit c5a1a6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions aimmo-game-worker/simulation/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def __init__(self, artefact):
print("MoveTowardsAction got an invalid parameter. Is it empty?")
return

if len(artefact.get("path", [])) < 2:
if len(artefact.path) < 2:
return # not a valid path

# the first cell in the path is the starting cell
avatar_location = artefact["path"][0].location
next_location = artefact["path"][1].location
avatar_location = artefact.path[0].location
next_location = artefact.path[1].location

# calculate direction
x = next_location.x - avatar_location.x
Expand Down
15 changes: 12 additions & 3 deletions aimmo-game-worker/simulation/world_map.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections import defaultdict
from collections import defaultdict, namedtuple
from enum import Enum

from .avatar_state import create_avatar_state
Expand All @@ -25,6 +25,9 @@ def __str__(self):
return self.value


Artefact = namedtuple("Artefact", ["type", "location", "path"])


class Cell(object):

"""
Expand Down Expand Up @@ -176,15 +179,21 @@ def scan_nearby(self, avatar_location, radius=SCAN_RADIUS) -> List[dict]:
nearby = defaultdict(list)
for art_cell in artefact_cells:
path = astar(self, self.cells.get(avatar_location), art_cell)
# only add to the list when there's a path
if path:
nearby[len(path)].append((art_cell, path))

# sort them by distance (the length of path) and take the nearest first
nearest = []
for distance in sorted(nearby.keys()):
for art_cell, path in nearby[distance]:
art_cell.interactable["path"] = path
nearest.append(art_cell.interactable)
# use namedtuple so fields accessible by attribute lookup
artefact = Artefact(
type=art_cell.interactable["type"],
location=art_cell.location,
path=path,
)
nearest.append(artefact)
if len(nearest) > SCAN_LIMIT:
break

Expand Down

0 comments on commit c5a1a6c

Please sign in to comment.