From 7efd7095f9a52eb67f697dd1762906c0f1610a54 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 7 Jan 2024 14:41:25 -0500 Subject: Cache chain generation --- randomizer/generator.gd | 90 +++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/randomizer/generator.gd b/randomizer/generator.gd index 22e0ca5..6c1883b 100644 --- a/randomizer/generator.gd +++ b/randomizer/generator.gd @@ -16,12 +16,15 @@ var _total_generated = 0 var _hint_by_path = {} var _answer_by_path = {} var _painting_puzzles = [] +var _chain_cache = {} var _unique_pool = "" var _answers_by_unique_pool = {} var _needs_reuse = [] var _possible_reuse = [] +var _cur_panel = "" + const kOrangeMiddle = -2 const kWhiteMiddle = -1 const kWhiteTop = 0 @@ -840,6 +843,8 @@ func apply(scene): func _pickRandom(set): + if set.size() == 0: + print("This is a problem: %s" % _cur_panel) var id = _rng.randi_range(0, set.size() - 1) return set[id] @@ -867,6 +872,7 @@ func _clearUniquePool(): func _gSingle(panel_path, puzzle_type): + _cur_panel = panel_path if puzzle_type == kWhiteMiddle: var word_choices = _filterBestPuzzles(_answers_by_type[kWhiteMiddle]) var answer_id = _pickRandom(word_choices) @@ -883,6 +889,7 @@ func _gSingle(panel_path, puzzle_type): func _gDouble(panel1_path, panel2_path, puzzle_type): + _cur_panel = panel1_path if puzzle_type == kWhiteMiddle: var word_choices = _filterBestPuzzles(_answers_by_type[kWhiteMiddle]) var answer_id = _pickRandom(word_choices) @@ -907,6 +914,7 @@ func _gDouble(panel1_path, panel2_path, puzzle_type): func _gQuadruple(panel1_path, panel2_path, panel3_path, panel4_path, puzzle_type): + _cur_panel = panel1_path var pre_choices = _filterBestPuzzles(_answers_by_type[puzzle_type]) var puzzle_choices = [] for word_id in pre_choices: @@ -931,6 +939,7 @@ func _gQuadruple(panel1_path, panel2_path, panel3_path, panel4_path, puzzle_type func _gPaired(panel1_path, panel2_path, puzzle_type): + _cur_panel = panel1_path var needed_type = puzzle_type if puzzle_type == kWhiteMiddle: needed_type = kBlackBottom @@ -950,6 +959,7 @@ func _gPaired(panel1_path, panel2_path, puzzle_type): func _gStack2(panel1_path, puzzle_type1, panel2_path, puzzle_type2): + _cur_panel = panel1_path if puzzle_type1 == kWhiteMiddle or puzzle_type2 == kWhiteMiddle: if puzzle_type1 == kWhiteMiddle: _gSingle(panel2_path, puzzle_type2) @@ -977,40 +987,47 @@ func _gStack2(panel1_path, puzzle_type1, panel2_path, puzzle_type2): func _gChain(panel_path, puzzle_types): - var remaining = [] - puzzle_types.invert() - for ptype in puzzle_types: - if remaining.empty(): - for answer in _answers_by_type[ptype]: - for choice in _puzzle_by_id[answer][1][ptype]: - remaining.append([choice, answer]) - elif ptype == kOrangeMiddle: - var newrem = [] - for puzzle in remaining: - if _generated_puzzles.wanderlust.has(puzzle[0]): - newrem.append(puzzle) - if newrem.empty(): - print("No possible puzzles for panel %s" % panel_path) - return - remaining = newrem - else: - var newrem = [] - for puzzle in remaining: - if _puzzle_by_id[puzzle[0]][1].has(ptype): - for choice in _puzzle_by_id[puzzle[0]][1][ptype]: - newrem.append([choice, puzzle[1]]) - if newrem.empty(): - print("No possible puzzles for panel %s" % panel_path) - return - remaining = newrem + _cur_panel = panel_path var just_answers = [] var hints_by_answer = {} - for puzzle in remaining: - if !just_answers.has(puzzle[1]): - just_answers.append(puzzle[1]) - hints_by_answer[puzzle[1]] = [puzzle[0]] - else: - hints_by_answer[puzzle[1]].append(puzzle[0]) + puzzle_types.invert() + + if _chain_cache.has(puzzle_types): + just_answers = _chain_cache[puzzle_types][0] + hints_by_answer = _chain_cache[puzzle_types][1] + else: + var remaining = [] + for ptype in puzzle_types: + if remaining.empty(): + for answer in _answers_by_type[ptype]: + for choice in _puzzle_by_id[answer][1][ptype]: + remaining.append([choice, answer]) + elif ptype == kOrangeMiddle: + var newrem = [] + for puzzle in remaining: + if _generated_puzzles.wanderlust.has(puzzle[0]): + newrem.append(puzzle) + if newrem.empty(): + print("No possible puzzles for panel %s" % panel_path) + return + remaining = newrem + else: + var newrem = [] + for puzzle in remaining: + if _puzzle_by_id[puzzle[0]][1].has(ptype): + for choice in _puzzle_by_id[puzzle[0]][1][ptype]: + newrem.append([choice, puzzle[1]]) + if newrem.empty(): + print("No possible puzzles for panel %s" % panel_path) + return + remaining = newrem + for puzzle in remaining: + if !just_answers.has(puzzle[1]): + just_answers.append(puzzle[1]) + hints_by_answer[puzzle[1]] = [puzzle[0]] + else: + hints_by_answer[puzzle[1]].append(puzzle[0]) + _chain_cache[puzzle_types] = [just_answers, hints_by_answer] var puzzle_choices = _filterBestPuzzles(just_answers) if puzzle_choices.empty(): print("No possible puzzles for panel %s" % panel_path) @@ -1026,18 +1043,22 @@ func _gChain(panel_path, puzzle_types): func _gCopy(panel1_path, panel2_path): + _cur_panel = panel2_path _writePanel(panel2_path, _hint_by_path[panel1_path], _answer_by_path[panel1_path]) func _gCopyAnswer(panel1_path, panel2_path): + _cur_panel = panel2_path _writePanel(panel2_path, _answer_by_path[panel1_path], _answer_by_path[panel1_path]) func _gCopyHidden(panel1_path, panel2_path): + _cur_panel = panel2_path _writePanel(panel2_path, "?".repeat(_answer_by_path[panel1_path].length()), _answer_by_path[panel1_path]) func _gExactLength(panel_path, puzzle_type, answer_length): + _cur_panel = panel_path var puzzle_choices = _filterBestPuzzles(_answers_by_length[answer_length]) if puzzle_type != kWhiteMiddle: # TODO: Do we even need this? @@ -1055,6 +1076,7 @@ func _gExactLength(panel_path, puzzle_type, answer_length): func _gOrangeNumber(panel_path): + _cur_panel = panel_path var word_id = _pickRandom(_generated_puzzles.wanderlust) var answer = _answer_by_id[word_id] var hint = _applyCipher(answer) @@ -1062,6 +1084,7 @@ func _gOrangeNumber(panel_path): func _gOrangeWord(panel_path): + _cur_panel = panel_path var word_id = _pickRandom(_generated_puzzles.wanderlust) var hint = _answer_by_id[word_id] var answer = _applyCipher(hint) @@ -1069,6 +1092,7 @@ func _gOrangeWord(panel_path): func _gOrangeAddition(panel_path): + _cur_panel = panel_path var puzzle = _pickRandom(_generated_puzzles.addition) var word1 = _answer_by_id[puzzle[0]] var word2 = _answer_by_id[puzzle[1]] @@ -1077,6 +1101,7 @@ func _gOrangeAddition(panel_path): func _gPaintingPuzzle(panel_path, painting_name): + _cur_panel = panel_path var chosen = _pickRandom(_painting_puzzles) _painting_puzzles.erase(chosen) _writePanel(panel_path, "painting", chosen[1]) @@ -1084,6 +1109,7 @@ func _gPaintingPuzzle(panel_path, painting_name): func _gCombo(panel_path, left_type, right_type): + _cur_panel = panel_path var puzzles = _generated_puzzles.combos[left_type][right_type] var chosen = _pickRandom(puzzles) var hint_text -- cgit 1.4.1