You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.0 KiB

import json
from PyQt6.QtCore import QObject, pyqtSignal, QRunnable
from db import db
from routes.game_world import GameWorld
from routes.pokemon_game_desc import PokemonGameDesc
from routes.requirements import determine_must_visit_locations
from routes.route_planner import RoutePlanner
class SolveRouteWorkerSignals(QObject):
finished = pyqtSignal(list)
class SolveRouteWorker(QRunnable):
def __init__(self, initial_data: PokemonGameDesc):
super().__init__()
self.signals = SolveRouteWorkerSignals()
self.initial_data = initial_data
def run(self):
try:
stage_results = []
current_conditions = {
'badges': set(),
'conditions': frozenset()
}
for i, stage in enumerate(self.initial_data.stages):
if i == 0:
world = stage.create_world()
else:
prev_result = stage_results[-1]
current_conditions['conditions'] = prev_result.conditions
#current_conditions['badges'] = prev_result.conditions.intersection(all_badges_set) # if badges are a subset of conditions
stage.start = prev_result.path[-1] # end of last path is start of this stage
world = stage.create_world(previous_conditions=current_conditions)
# First route calculation
planner = RoutePlanner(world)
initial_plan = planner.astar_search()
# Load plan.json and determine if more visits are needed
with open("./temp/plan.json", 'r', encoding='utf-8') as f:
plan_json = json.load(f)
needed_locations = determine_must_visit_locations(plan_json, db, self.initial_data.game_name)
missing_locations = needed_locations - set(initial_plan.path)
# compare those to all the ones we visit in the generated route
node_list = list(world.graph.nodes())
# add missing ones to the has_visited list and re-gen the problem, then run it again
nodes_to_visit = []
for loc in missing_locations:
if loc in node_list:
nodes_to_visit.append(loc)
print("Additional Nodes to Visit:", nodes_to_visit)
result = None
if nodes_to_visit:
stage.must_visit = stage.must_visit.union(nodes_to_visit)
re_visisted = stage.create_world()
planner = RoutePlanner(re_visisted)
distances = planner.compute_shortest_path(re_visisted.graph, stage.must_visit)
result = planner.astar_search()
else:
result = initial_plan
if result:
print(result.path)
stage_results.append(result)
self.signals.finished.emit(stage_results)
except Exception as e:
print(f"Error: {e}")