clean up Fusion_Graph and fix get_path
parent
20e24a40e3
commit
2f2c281265
112
Fusion_Graph.py
112
Fusion_Graph.py
|
@ -14,8 +14,8 @@ import struct
|
|||
import sys
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self, vanillaGame, randoSettings = (None,)):
|
||||
|
||||
def __init__(self, vanillaGame, randoSettings=None):
|
||||
self.graph = dict()
|
||||
self.areaConnections = dict()
|
||||
self.areaConnectionOffsets = dict()
|
||||
|
@ -40,9 +40,9 @@ class Game:
|
|||
self.minorItemLocations.clear()
|
||||
self.itemLocations.clear()
|
||||
self.patcher.clear()
|
||||
|
||||
|
||||
# print('DEBUG: Opening ROM to pick stuff')
|
||||
|
||||
|
||||
try:
|
||||
with open(vanillaGame, 'rb') as sourceRom:
|
||||
sourceRom.seek(3967888, 0)
|
||||
|
@ -50,7 +50,7 @@ class Game:
|
|||
sourceDoor = int.from_bytes(sourceRom.read(1), 'little')
|
||||
targetOffset = sourceRom.tell()
|
||||
targetArea = int.from_bytes(sourceRom.read(1), 'little')
|
||||
if sourceArea != 255:
|
||||
while sourceArea != 255:
|
||||
self.areaConnections.update({
|
||||
'S{}-{:02X}'.format(sourceArea, sourceDoor): targetArea })
|
||||
self.areaConnectionOffsets.update({
|
||||
|
@ -67,7 +67,7 @@ class Game:
|
|||
doorData = unpacked[0] - 134217728
|
||||
sourceRom.seek(doorData, 0)
|
||||
doorNumber = 0
|
||||
while True:
|
||||
while True:
|
||||
connectionType = int.from_bytes(sourceRom.read(1), 'little')
|
||||
roomNumber = int.from_bytes(sourceRom.read(1), 'little')
|
||||
sourceRom.seek(4, 1)
|
||||
|
@ -91,20 +91,20 @@ class Game:
|
|||
doorString]
|
||||
doorNumber += 1
|
||||
# print('DEBUG: Parsing seemingly done?')
|
||||
except:
|
||||
except FileNotFoundError:
|
||||
print('Error:', vanillaGame, 'could not be opened.')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def set_setting(self, setting, value):
|
||||
self.settings[setting] = value
|
||||
|
||||
|
||||
|
||||
def get_setting(self, setting):
|
||||
return self.settings[setting]
|
||||
|
||||
|
||||
|
||||
def AddNodeToRoom(self, room, node):
|
||||
nodeList = self.rooms.get(room)
|
||||
nodeList.append(node)
|
||||
|
@ -112,7 +112,7 @@ class Game:
|
|||
self.rooms.update({
|
||||
room: nodeList })
|
||||
|
||||
|
||||
|
||||
def RemoveNodeFromRoom(self, room, node):
|
||||
nodeList = self.rooms.get(room)
|
||||
if node in nodeList:
|
||||
|
@ -121,17 +121,17 @@ class Game:
|
|||
self.rooms.update({
|
||||
room: nodeList })
|
||||
|
||||
|
||||
|
||||
def ClearGraph(self):
|
||||
self.graph.clear()
|
||||
|
||||
|
||||
|
||||
def ConnectAllNodes(self):
|
||||
self.ClearGraph()
|
||||
self.ConnectNodesInRooms()
|
||||
self.ConnectNodesBetweenRooms()
|
||||
|
||||
|
||||
|
||||
def ConnectNodesInRooms(self):
|
||||
for room in self.rooms:
|
||||
for start in self.rooms[room]:
|
||||
|
@ -139,12 +139,12 @@ class Game:
|
|||
if start != end:
|
||||
self.add_edges(start, end)
|
||||
|
||||
|
||||
|
||||
def ConnectNodesBetweenRooms(self):
|
||||
for connection in self.doorConnections.items():
|
||||
if len(connection) == 2:
|
||||
self.add_directed_edge(connection[0], connection[1])
|
||||
|
||||
|
||||
def UpdateDoorConnection(self, source, destination):
|
||||
self.doorConnections.update({
|
||||
source: destination })
|
||||
|
@ -154,7 +154,7 @@ class Game:
|
|||
source: int(destination[1:2]) })
|
||||
return self.areaConnectionOffsets.get(source)
|
||||
|
||||
|
||||
|
||||
def AddConnectedNodesToRoom(self, targetRoom, *nodes):
|
||||
for currentNode in nodes:
|
||||
if targetRoom in self.rooms:
|
||||
|
@ -174,86 +174,82 @@ class Game:
|
|||
else:
|
||||
self.graph[start] = [
|
||||
end]
|
||||
|
||||
|
||||
def add_edges(self, start, *nodes):
|
||||
for end in nodes:
|
||||
self.add_directed_edge(start, end)
|
||||
self.add_directed_edge(end, start)
|
||||
|
||||
|
||||
|
||||
def add_to_majors(self, item):
|
||||
if item not in self.itemLocations:
|
||||
self.itemLocations.append(item)
|
||||
if item not in self.majorItemLocations:
|
||||
self.majorItemLocations.append(item)
|
||||
|
||||
|
||||
|
||||
def add_list_to_majors(self, locations):
|
||||
for item in locations:
|
||||
if item not in self.itemLocations:
|
||||
self.itemLocations.append(item)
|
||||
if item not in self.majorItemLocations:
|
||||
self.majorItemLocations.append(item)
|
||||
|
||||
|
||||
def add_to_minors(self, item):
|
||||
if item not in self.itemLocations:
|
||||
self.itemLocations.append(item)
|
||||
if item not in self.minorItemLocations:
|
||||
self.minorItemLocations.append(item)
|
||||
|
||||
|
||||
def add_list_to_minors(self, locations):
|
||||
for item in locations:
|
||||
if item not in self.itemLocations:
|
||||
self.itemLocations.append(item)
|
||||
if item not in self.minorItemLocations:
|
||||
self.minorItemLocations.append(item)
|
||||
|
||||
|
||||
def get_requirements(self, start, end):
|
||||
checkRequirement = (start, end)
|
||||
return self.requirements.get(checkRequirement)
|
||||
|
||||
def get_path(self, start, end, LimitArea, path, depth = (False, None, 100)):
|
||||
|
||||
def get_path(self, start, end, LimitArea=False, path=None, depth=100):
|
||||
if path == None:
|
||||
self.visited.clear()
|
||||
self.queue.clear()
|
||||
path = list()
|
||||
path = path + [
|
||||
start]
|
||||
path = path + [start]
|
||||
if start not in self.graph:
|
||||
return None
|
||||
if None == end:
|
||||
if start == end:
|
||||
return path
|
||||
# if None(path) >= depth:
|
||||
if path >= depth:
|
||||
if len(path) >= depth:
|
||||
return None
|
||||
for point in None.graph[start]:
|
||||
if point in self.itemLocations and point not in end:
|
||||
continue
|
||||
for point in self.graph[start]:
|
||||
if point in self.itemLocations:
|
||||
if point not in end:
|
||||
continue
|
||||
if point in path:
|
||||
continue
|
||||
if LimitArea:
|
||||
for area in range(0, 7):
|
||||
if 'S{}'.format(area) in start and 'S{}'.format(area) not in point:
|
||||
return None
|
||||
edge = (start, point)
|
||||
self.queue.append(edge)
|
||||
if self.queue:
|
||||
edge = self.queue.pop()
|
||||
if edge not in self.visited:
|
||||
self.visited.append(edge)
|
||||
node = edge[1]
|
||||
pathReqs = self.get_requirements(start, node)
|
||||
if pathReqs == None:
|
||||
newpath = self.get_path(node, end, LimitArea, path, depth)
|
||||
if newpath:
|
||||
path = path + [
|
||||
node]
|
||||
return newpath
|
||||
if pathReqs == True:
|
||||
newpath = self.get_path(node, end, LimitArea, path, depth)
|
||||
if newpath:
|
||||
path = path + [
|
||||
node]
|
||||
return newpath
|
||||
|
||||
|
||||
if 'S{}'.format(area) in start:
|
||||
if 'S{}'.format(area) not in point:
|
||||
return None
|
||||
edge = (start, point)
|
||||
self.queue.append(edge)
|
||||
while self.queue:
|
||||
edge = self.queue.pop()
|
||||
if edge not in self.visited:
|
||||
self.visited.append(edge)
|
||||
node = edge[1]
|
||||
pathReqs = self.get_requirements(start, node)
|
||||
if pathReqs == None:
|
||||
newpath = self.get_path(node, end, LimitArea, path, depth)
|
||||
if newpath:
|
||||
path = path + [node]
|
||||
return newpath
|
||||
elif pathReqs == True:
|
||||
newpath = self.get_path(node, end, LimitArea, path, depth)
|
||||
if newpath:
|
||||
path = path + [node]
|
||||
return newpath
|
||||
|
|
Loading…
Reference in New Issue