Skip to content
Browse files

Networks now check for state changes and redraw. The algorithm gets r…

…eally slow with ~50 network connections.
  • Loading branch information...
dfloer committed Nov 8, 2018
1 parent bd4100f commit 9d72e6775f483573079466d5f27f51e53cff9a03
Showing with 40 additions and 8 deletions.
  1. +40 −8
@@ -474,10 +474,21 @@ def __init__(self):
Dictionary will be of the form {Hexagon(q, r, s): {"type": network type}, ...}
Possible values for network type are "energy" and "control".
""" = {Hexagon(0, 0, 0): {"type": "start"}} = {Hexagon(0, 0, 0): {"type": "start", "powered": True}}

def update_powered(self):
Update all nodes in the network as to whether they're powered or not.
powered = self.find_all_connected(Hexagon(0, 0, 0))
for n in
if n in powered:[n]["powered"] = True
else:[n]["powered"] = False

def find_connected(self, cell_source, cell_destination):
print(cell_source, cell_destination)
Checks to see if the source cell is connected to a destination cell.
Uses A*. Dijkstra’s may be better with multiple sources.
@@ -489,16 +500,12 @@ def find_connected(self, cell_source, cell_destination):
q = []
heappush(q, [cell_source, 0])
print("qs", q)
visited = {}
total_cost = {}
visited[cell_source] = None
total_cost[cell_source] = 0

while len(q) != 0:
print("q", q)
print("v", visited)
print("tc", total_cost)
current, _ = heappop(q)
if current == cell_destination:
return visited
@@ -512,6 +519,28 @@ def find_connected(self, cell_source, cell_destination):
visited[next_cell] = current
return None

def find_all_connected(self, start_cell):
Finds all all the cells connected to the current cell.
start_cell (Hexagon): cell we want to determine connectivity from.
visited (list): cells that we have visited already and don't need to be checked again.
List of cells that are connected.
return self.find_all_connected_inner(start_cell, set())

def find_all_connected_inner(self, start_cell, visited):
if start_cell in visited:
return visited
neighbours = [hex_math.hex_neighbor(start_cell, x) for x in range(6) if
hex_math.hex_neighbor(start_cell, x) in]
for n in neighbours:
if n not in visited:
visited.union(self.find_all_connected_inner(n, visited))
return visited

def __len__(self):
return len(

@@ -527,6 +556,7 @@ def draw_network(self):
Handles drawing of the network.
for k, h in
if h["type"] == "start":
@@ -545,7 +575,8 @@ def draw_network(self):
self.network_batch.add(sprite, z=-k.r - 10, name=f"{k.q}_{k.r}_{k.s}_6")
except Exception:
self.network_batch.add(sprite, z=-k.r - 10, name=f"{k.q}_{k.r}_{k.s}_6")
for idx, n in enumerate(neighbours):
if n["type"] in (h["type"], "start"):
@@ -556,7 +587,8 @@ def draw_network(self):
self.network_batch.add(sprite, z=-k.r, name=f"{k.q}_{k.r}_{k.s}_{idx}")
except Exception:
pass # This sprite must already exist, so we skip it.
self.network_batch.add(sprite, z=-k.r, name=f"{k.q}_{k.r}_{k.s}_{idx}")

def plop_network(self, cell, network_type="energy"):

0 comments on commit 9d72e67

Please sign in to comment.
You can’t perform that action at this time.