diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-01-07 14:41:25 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-01-07 14:41:25 -0500 |
commit | 7efd7095f9a52eb67f697dd1762906c0f1610a54 (patch) | |
tree | 0d52adc5fb3709c7d4998b0b7f0eb46dcbbaacb4 /randomizer/generator.gd | |
parent | 7682d34a0e1193b3d0c3dc40a59326f83392c9f8 (diff) | |
download | lingo-randomizer-7efd7095f9a52eb67f697dd1762906c0f1610a54.tar.gz lingo-randomizer-7efd7095f9a52eb67f697dd1762906c0f1610a54.tar.bz2 lingo-randomizer-7efd7095f9a52eb67f697dd1762906c0f1610a54.zip |
Cache chain generation
Diffstat (limited to 'randomizer/generator.gd')
-rw-r--r-- | randomizer/generator.gd | 90 |
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 | |||
16 | var _hint_by_path = {} | 16 | var _hint_by_path = {} |
17 | var _answer_by_path = {} | 17 | var _answer_by_path = {} |
18 | var _painting_puzzles = [] | 18 | var _painting_puzzles = [] |
19 | var _chain_cache = {} | ||
19 | 20 | ||
20 | var _unique_pool = "" | 21 | var _unique_pool = "" |
21 | var _answers_by_unique_pool = {} | 22 | var _answers_by_unique_pool = {} |
22 | var _needs_reuse = [] | 23 | var _needs_reuse = [] |
23 | var _possible_reuse = [] | 24 | var _possible_reuse = [] |
24 | 25 | ||
26 | var _cur_panel = "" | ||
27 | |||
25 | const kOrangeMiddle = -2 | 28 | const kOrangeMiddle = -2 |
26 | const kWhiteMiddle = -1 | 29 | const kWhiteMiddle = -1 |
27 | const kWhiteTop = 0 | 30 | const kWhiteTop = 0 |
@@ -840,6 +843,8 @@ func apply(scene): | |||
840 | 843 | ||
841 | 844 | ||
842 | func _pickRandom(set): | 845 | func _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 | ||
869 | func _gSingle(panel_path, puzzle_type): | 874 | func _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 | ||
885 | func _gDouble(panel1_path, panel2_path, puzzle_type): | 891 | func _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 | ||
909 | func _gQuadruple(panel1_path, panel2_path, panel3_path, panel4_path, puzzle_type): | 916 | func _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 | ||
933 | func _gPaired(panel1_path, panel2_path, puzzle_type): | 941 | func _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 | ||
952 | func _gStack2(panel1_path, puzzle_type1, panel2_path, puzzle_type2): | 961 | func _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 | ||
979 | func _gChain(panel_path, puzzle_types): | 989 | func _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 | ||
1028 | func _gCopy(panel1_path, panel2_path): | 1045 | func _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 | ||
1032 | func _gCopyAnswer(panel1_path, panel2_path): | 1050 | func _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 | ||
1036 | func _gCopyHidden(panel1_path, panel2_path): | 1055 | func _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 | ||
1040 | func _gExactLength(panel_path, puzzle_type, answer_length): | 1060 | func _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 | ||
1057 | func _gOrangeNumber(panel_path): | 1078 | func _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 | ||
1064 | func _gOrangeWord(panel_path): | 1086 | func _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 | ||
1071 | func _gOrangeAddition(panel_path): | 1094 | func _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 | ||
1079 | func _gPaintingPuzzle(panel_path, painting_name): | 1103 | func _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 | ||
1086 | func _gCombo(panel_path, left_type, right_type): | 1111 | func _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 |