summary refs log tree commit diff stats
path: root/randomizer/generator.gd
diff options
context:
space:
mode:
Diffstat (limited to 'randomizer/generator.gd')
-rw-r--r--randomizer/generator.gd90
1 files 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
16var _hint_by_path = {} 16var _hint_by_path = {}
17var _answer_by_path = {} 17var _answer_by_path = {}
18var _painting_puzzles = [] 18var _painting_puzzles = []
19var _chain_cache = {}
19 20
20var _unique_pool = "" 21var _unique_pool = ""
21var _answers_by_unique_pool = {} 22var _answers_by_unique_pool = {}
22var _needs_reuse = [] 23var _needs_reuse = []
23var _possible_reuse = [] 24var _possible_reuse = []
24 25
26var _cur_panel = ""
27
25const kOrangeMiddle = -2 28const kOrangeMiddle = -2
26const kWhiteMiddle = -1 29const kWhiteMiddle = -1
27const kWhiteTop = 0 30const kWhiteTop = 0
@@ -840,6 +843,8 @@ func apply(scene):
840 843
841 844
842func _pickRandom(set): 845func _pickRandom(set):
846 if set.size() == 0:
847 print("This is a problem: %s" % _cur_panel)
843 var id = _rng.randi_range(0, set.size() - 1) 848 var id = _rng.randi_range(0, set.size() - 1)
844 return set[id] 849 return set[id]
845 850
@@ -867,6 +872,7 @@ func _clearUniquePool():
867 872
868 873
869func _gSingle(panel_path, puzzle_type): 874func _gSingle(panel_path, puzzle_type):
875 _cur_panel = panel_path
870 if puzzle_type == kWhiteMiddle: 876 if puzzle_type == kWhiteMiddle:
871 var word_choices = _filterBestPuzzles(_answers_by_type[kWhiteMiddle]) 877 var word_choices = _filterBestPuzzles(_answers_by_type[kWhiteMiddle])
872 var answer_id = _pickRandom(word_choices) 878 var answer_id = _pickRandom(word_choices)
@@ -883,6 +889,7 @@ func _gSingle(panel_path, puzzle_type):
883 889
884 890
885func _gDouble(panel1_path, panel2_path, puzzle_type): 891func _gDouble(panel1_path, panel2_path, puzzle_type):
892 _cur_panel = panel1_path
886 if puzzle_type == kWhiteMiddle: 893 if puzzle_type == kWhiteMiddle:
887 var word_choices = _filterBestPuzzles(_answers_by_type[kWhiteMiddle]) 894 var word_choices = _filterBestPuzzles(_answers_by_type[kWhiteMiddle])
888 var answer_id = _pickRandom(word_choices) 895 var answer_id = _pickRandom(word_choices)
@@ -907,6 +914,7 @@ func _gDouble(panel1_path, panel2_path, puzzle_type):
907 914
908 915
909func _gQuadruple(panel1_path, panel2_path, panel3_path, panel4_path, puzzle_type): 916func _gQuadruple(panel1_path, panel2_path, panel3_path, panel4_path, puzzle_type):
917 _cur_panel = panel1_path
910 var pre_choices = _filterBestPuzzles(_answers_by_type[puzzle_type]) 918 var pre_choices = _filterBestPuzzles(_answers_by_type[puzzle_type])
911 var puzzle_choices = [] 919 var puzzle_choices = []
912 for word_id in pre_choices: 920 for word_id in pre_choices:
@@ -931,6 +939,7 @@ func _gQuadruple(panel1_path, panel2_path, panel3_path, panel4_path, puzzle_type
931 939
932 940
933func _gPaired(panel1_path, panel2_path, puzzle_type): 941func _gPaired(panel1_path, panel2_path, puzzle_type):
942 _cur_panel = panel1_path
934 var needed_type = puzzle_type 943 var needed_type = puzzle_type
935 if puzzle_type == kWhiteMiddle: 944 if puzzle_type == kWhiteMiddle:
936 needed_type = kBlackBottom 945 needed_type = kBlackBottom
@@ -950,6 +959,7 @@ func _gPaired(panel1_path, panel2_path, puzzle_type):
950 959
951 960
952func _gStack2(panel1_path, puzzle_type1, panel2_path, puzzle_type2): 961func _gStack2(panel1_path, puzzle_type1, panel2_path, puzzle_type2):
962 _cur_panel = panel1_path
953 if puzzle_type1 == kWhiteMiddle or puzzle_type2 == kWhiteMiddle: 963 if puzzle_type1 == kWhiteMiddle or puzzle_type2 == kWhiteMiddle:
954 if puzzle_type1 == kWhiteMiddle: 964 if puzzle_type1 == kWhiteMiddle:
955 _gSingle(panel2_path, puzzle_type2) 965 _gSingle(panel2_path, puzzle_type2)
@@ -977,40 +987,47 @@ func _gStack2(panel1_path, puzzle_type1, panel2_path, puzzle_type2):
977 987
978 988
979func _gChain(panel_path, puzzle_types): 989func _gChain(panel_path, puzzle_types):
980 var remaining = [] 990 _cur_panel = panel_path
981 puzzle_types.invert()
982 for ptype in puzzle_types:
983 if remaining.empty():
984 for answer in _answers_by_type[ptype]:
985 for choice in _puzzle_by_id[answer][1][ptype]:
986 remaining.append([choice, answer])
987 elif ptype == kOrangeMiddle:
988 var newrem = []
989 for puzzle in remaining:
990 if _generated_puzzles.wanderlust.has(puzzle[0]):
991 newrem.append(puzzle)
992 if newrem.empty():
993 print("No possible puzzles for panel %s" % panel_path)
994 return
995 remaining = newrem
996 else:
997 var newrem = []
998 for puzzle in remaining:
999 if _puzzle_by_id[puzzle[0]][1].has(ptype):
1000 for choice in _puzzle_by_id[puzzle[0]][1][ptype]:
1001 newrem.append([choice, puzzle[1]])
1002 if newrem.empty():
1003 print("No possible puzzles for panel %s" % panel_path)
1004 return
1005 remaining = newrem
1006 var just_answers = [] 991 var just_answers = []
1007 var hints_by_answer = {} 992 var hints_by_answer = {}
1008 for puzzle in remaining: 993 puzzle_types.invert()
1009 if !just_answers.has(puzzle[1]): 994
1010 just_answers.append(puzzle[1]) 995 if _chain_cache.has(puzzle_types):
1011 hints_by_answer[puzzle[1]] = [puzzle[0]] 996 just_answers = _chain_cache[puzzle_types][0]
1012 else: 997 hints_by_answer = _chain_cache[puzzle_types][1]
1013 hints_by_answer[puzzle[1]].append(puzzle[0]) 998 else:
999 var remaining = []
1000 for ptype in puzzle_types:
1001 if remaining.empty():
1002 for answer in _answers_by_type[ptype]:
1003 for choice in _puzzle_by_id[answer][1][ptype]:
1004 remaining.append([choice, answer])
1005 elif ptype == kOrangeMiddle:
1006 var newrem = []
1007 for puzzle in remaining:
1008 if _generated_puzzles.wanderlust.has(puzzle[0]):
1009 newrem.append(puzzle)
1010 if newrem.empty():
1011 print("No possible puzzles for panel %s" % panel_path)
1012 return
1013 remaining = newrem
1014 else:
1015 var newrem = []
1016 for puzzle in remaining:
1017 if _puzzle_by_id[puzzle[0]][1].has(ptype):
1018 for choice in _puzzle_by_id[puzzle[0]][1][ptype]:
1019 newrem.append([choice, puzzle[1]])
1020 if newrem.empty():
1021 print("No possible puzzles for panel %s" % panel_path)
1022 return
1023 remaining = newrem
1024 for puzzle in remaining:
1025 if !just_answers.has(puzzle[1]):
1026 just_answers.append(puzzle[1])
1027 hints_by_answer[puzzle[1]] = [puzzle[0]]
1028 else:
1029 hints_by_answer[puzzle[1]].append(puzzle[0])
1030 _chain_cache[puzzle_types] = [just_answers, hints_by_answer]
1014 var puzzle_choices = _filterBestPuzzles(just_answers) 1031 var puzzle_choices = _filterBestPuzzles(just_answers)
1015 if puzzle_choices.empty(): 1032 if puzzle_choices.empty():
1016 print("No possible puzzles for panel %s" % panel_path) 1033 print("No possible puzzles for panel %s" % panel_path)
@@ -1026,18 +1043,22 @@ func _gChain(panel_path, puzzle_types):
1026 1043
1027 1044
1028func _gCopy(panel1_path, panel2_path): 1045func _gCopy(panel1_path, panel2_path):
1046 _cur_panel = panel2_path
1029 _writePanel(panel2_path, _hint_by_path[panel1_path], _answer_by_path[panel1_path]) 1047 _writePanel(panel2_path, _hint_by_path[panel1_path], _answer_by_path[panel1_path])
1030 1048
1031 1049
1032func _gCopyAnswer(panel1_path, panel2_path): 1050func _gCopyAnswer(panel1_path, panel2_path):
1051 _cur_panel = panel2_path
1033 _writePanel(panel2_path, _answer_by_path[panel1_path], _answer_by_path[panel1_path]) 1052 _writePanel(panel2_path, _answer_by_path[panel1_path], _answer_by_path[panel1_path])
1034 1053
1035 1054
1036func _gCopyHidden(panel1_path, panel2_path): 1055func _gCopyHidden(panel1_path, panel2_path):
1056 _cur_panel = panel2_path
1037 _writePanel(panel2_path, "?".repeat(_answer_by_path[panel1_path].length()), _answer_by_path[panel1_path]) 1057 _writePanel(panel2_path, "?".repeat(_answer_by_path[panel1_path].length()), _answer_by_path[panel1_path])
1038 1058
1039 1059
1040func _gExactLength(panel_path, puzzle_type, answer_length): 1060func _gExactLength(panel_path, puzzle_type, answer_length):
1061 _cur_panel = panel_path
1041 var puzzle_choices = _filterBestPuzzles(_answers_by_length[answer_length]) 1062 var puzzle_choices = _filterBestPuzzles(_answers_by_length[answer_length])
1042 if puzzle_type != kWhiteMiddle: 1063 if puzzle_type != kWhiteMiddle:
1043 # TODO: Do we even need this? 1064 # TODO: Do we even need this?
@@ -1055,6 +1076,7 @@ func _gExactLength(panel_path, puzzle_type, answer_length):
1055 1076
1056 1077
1057func _gOrangeNumber(panel_path): 1078func _gOrangeNumber(panel_path):
1079 _cur_panel = panel_path
1058 var word_id = _pickRandom(_generated_puzzles.wanderlust) 1080 var word_id = _pickRandom(_generated_puzzles.wanderlust)
1059 var answer = _answer_by_id[word_id] 1081 var answer = _answer_by_id[word_id]
1060 var hint = _applyCipher(answer) 1082 var hint = _applyCipher(answer)
@@ -1062,6 +1084,7 @@ func _gOrangeNumber(panel_path):
1062 1084
1063 1085
1064func _gOrangeWord(panel_path): 1086func _gOrangeWord(panel_path):
1087 _cur_panel = panel_path
1065 var word_id = _pickRandom(_generated_puzzles.wanderlust) 1088 var word_id = _pickRandom(_generated_puzzles.wanderlust)
1066 var hint = _answer_by_id[word_id] 1089 var hint = _answer_by_id[word_id]
1067 var answer = _applyCipher(hint) 1090 var answer = _applyCipher(hint)
@@ -1069,6 +1092,7 @@ func _gOrangeWord(panel_path):
1069 1092
1070 1093
1071func _gOrangeAddition(panel_path): 1094func _gOrangeAddition(panel_path):
1095 _cur_panel = panel_path
1072 var puzzle = _pickRandom(_generated_puzzles.addition) 1096 var puzzle = _pickRandom(_generated_puzzles.addition)
1073 var word1 = _answer_by_id[puzzle[0]] 1097 var word1 = _answer_by_id[puzzle[0]]
1074 var word2 = _answer_by_id[puzzle[1]] 1098 var word2 = _answer_by_id[puzzle[1]]
@@ -1077,6 +1101,7 @@ func _gOrangeAddition(panel_path):
1077 1101
1078 1102
1079func _gPaintingPuzzle(panel_path, painting_name): 1103func _gPaintingPuzzle(panel_path, painting_name):
1104 _cur_panel = panel_path
1080 var chosen = _pickRandom(_painting_puzzles) 1105 var chosen = _pickRandom(_painting_puzzles)
1081 _painting_puzzles.erase(chosen) 1106 _painting_puzzles.erase(chosen)
1082 _writePanel(panel_path, "painting", chosen[1]) 1107 _writePanel(panel_path, "painting", chosen[1])
@@ -1084,6 +1109,7 @@ func _gPaintingPuzzle(panel_path, painting_name):
1084 1109
1085 1110
1086func _gCombo(panel_path, left_type, right_type): 1111func _gCombo(panel_path, left_type, right_type):
1112 _cur_panel = panel_path
1087 var puzzles = _generated_puzzles.combos[left_type][right_type] 1113 var puzzles = _generated_puzzles.combos[left_type][right_type]
1088 var chosen = _pickRandom(puzzles) 1114 var chosen = _pickRandom(puzzles)
1089 var hint_text 1115 var hint_text