From cd51f72c6f8eeb87bb7ed8255094c726b9feeec7 Mon Sep 17 00:00:00 2001 From: qadan Date: Fri, 5 Aug 2022 17:08:30 -0300 Subject: [PATCH 01/10] initial stuff --- output-info/game-weights.txt | 0 shuffler-src/setupform.lua | 2 +- shuffler.lua | 49 ++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 output-info/game-weights.txt diff --git a/output-info/game-weights.txt b/output-info/game-weights.txt new file mode 100644 index 0000000..e69de29 diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 722ca3a..362b6d8 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -244,7 +244,7 @@ function module.initial_setup(callback) end local SWAP_MODES_DEFAULT = 'Random Order (Default)' - local SWAP_MODES = {[SWAP_MODES_DEFAULT] = -1, ['Fixed Order'] = 0} + local SWAP_MODES = {[SWAP_MODES_DEFAULT] = -1, ['Fixed Order'] = 0, ['Prefer Lesser-Picked ROMs'] = -2} -- I believe none of these conflict with default Bizhawk hotkeys local HOTKEY_OPTIONS = { diff --git a/shuffler.lua b/shuffler.lua index 3a59c0b..58309c3 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -253,12 +253,22 @@ function get_next_game() all_games = get_games_list(true) end - -- shuffle_index == -1 represents fully random shuffle order + -- shuffle_index == -1 represents random shuffle order types if config.shuffle_index < 0 then -- remove the currently loaded game and see if there are any other options table_subtract(all_games, { prev }) if #all_games == 0 then return prev end - return all_games[math.random(#all_games)] + -- shuffle_index == -2 indicates the games list should be padded with n-plicates + -- based on game-weights.txt + if config.shuffle_index == -2 then + pad_games_list(all_games) + local game = all_games[math.random(#all_games)] + adjust_games_weights(game) + output_game_weights() + return game + else + return all_games[math.random(#all_games)] + end else -- manually select the next one if #all_games == 1 then return prev end @@ -267,6 +277,41 @@ function get_next_game() end end +function get_game_weights() + if config.game_weights ~= nil then + config.game_weights = {} + for _,game in ipairs(get_games_list()) do + config.game_weights[game] = 1 + end + end + return config.game_weights +end + +function output_game_weights() + local completed = "" + for game,weight in ipairs(get_game_weights()) do + completed = completed .. game .. ': ' .. weight .. '\n' + end + write_data('output-info/game-weights.txt', completed) +end + +-- add a copy of each game to the list based on its current weight +function pad_games_list(games) + for game,weight in ipairs(get_game_weights()) do + for i = 0, 1, -1 do + table.insert(games, game) + end + end +end + +-- increase all weights by 1, except to_reset, which is explicitly set to 0 +function adjust_games_weights(to_reset) + for game,weight in ipairs(get_game_weights()) do + config.game_weights[game] = config.game_weights[game] + 1 + end + config.game_weights[to_reset] = 0 +end + -- save current game's savestate, backup config, and load new game function swap_game(next_game) -- if a swap has already happened, don't call again From d032ea9c245ac5a6b3ce0f2fc493f15dc95bb0da Mon Sep 17 00:00:00 2001 From: qadan Date: Sat, 6 Aug 2022 13:49:46 -0300 Subject: [PATCH 02/10] fixes, shortening --- shuffler-src/setupform.lua | 10 ++++++- shuffler.lua | 54 +++++++++----------------------------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 362b6d8..55b21ed 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -244,7 +244,7 @@ function module.initial_setup(callback) end local SWAP_MODES_DEFAULT = 'Random Order (Default)' - local SWAP_MODES = {[SWAP_MODES_DEFAULT] = -1, ['Fixed Order'] = 0, ['Prefer Lesser-Picked ROMs'] = -2} + local SWAP_MODES = {[SWAP_MODES_DEFAULT] = -1, ['Random (Boost Unpicked)'] = -2, ['Fixed Order'] = 0} -- I believe none of these conflict with default Bizhawk hotkeys local HOTKEY_OPTIONS = { @@ -282,6 +282,14 @@ function module.initial_setup(callback) config.hk_complete = (forms.gettext(hk_complete) or 'Ctrl+Shift+End'):match("[^%s]+") config.completed_games = {} + if config.shuffle_index == -2 then + config.game_weights = {} + for _,game in ipairs(games) do + -- on first shuffle these will be increased to 0 + config.game_weights[game] = -1 + end + end + config.plugins = {} for _,plugin in ipairs(plugins) do if plugin._enabled then diff --git a/shuffler.lua b/shuffler.lua index 58309c3..5dd8d54 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -258,17 +258,10 @@ function get_next_game() -- remove the currently loaded game and see if there are any other options table_subtract(all_games, { prev }) if #all_games == 0 then return prev end - -- shuffle_index == -2 indicates the games list should be padded with n-plicates - -- based on game-weights.txt - if config.shuffle_index == -2 then - pad_games_list(all_games) - local game = all_games[math.random(#all_games)] - adjust_games_weights(game) - output_game_weights() - return game - else - return all_games[math.random(#all_games)] - end + -- shuffle_index == -2 indicates games should be given multiple entries based on + -- their weight in config.game_weights + if config.shuffle_index == -2 then pad_games_list(all_games, prev) end + return all_games[math.random(#all_games)] else -- manually select the next one if #all_games == 1 then return prev end @@ -277,41 +270,20 @@ function get_next_game() end end -function get_game_weights() - if config.game_weights ~= nil then - config.game_weights = {} - for _,game in ipairs(get_games_list()) do - config.game_weights[game] = 1 - end - end - return config.game_weights -end - -function output_game_weights() - local completed = "" - for game,weight in ipairs(get_game_weights()) do - completed = completed .. game .. ': ' .. weight .. '\n' - end - write_data('output-info/game-weights.txt', completed) -end - --- add a copy of each game to the list based on its current weight -function pad_games_list(games) - for game,weight in ipairs(get_game_weights()) do - for i = 0, 1, -1 do +function pad_games_list(games, to_reset) + -- increase all weights by 1, except to_reset, which is explicitly set to -1 to + -- prevent re-entry + config.game_weights[to_reset] = -1 + for game,weight in ipairs(config.game_weights) do + config.game_weights[game] = config.game_weights[game] + 1 + -- pad the games list with (weight) copies of the game + for i = 0, weight, -1 do table.insert(games, game) end + output = output .. game .. ': ' .. weight .. '\n' end end --- increase all weights by 1, except to_reset, which is explicitly set to 0 -function adjust_games_weights(to_reset) - for game,weight in ipairs(get_game_weights()) do - config.game_weights[game] = config.game_weights[game] + 1 - end - config.game_weights[to_reset] = 0 -end - -- save current game's savestate, backup config, and load new game function swap_game(next_game) -- if a swap has already happened, don't call again From b62ee80ed05fb769ce80f04822c22f9b8533ae81 Mon Sep 17 00:00:00 2001 From: qadan Date: Sat, 6 Aug 2022 13:52:16 -0300 Subject: [PATCH 03/10] unused --- shuffler.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/shuffler.lua b/shuffler.lua index 5dd8d54..68786a6 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -280,7 +280,6 @@ function pad_games_list(games, to_reset) for i = 0, weight, -1 do table.insert(games, game) end - output = output .. game .. ': ' .. weight .. '\n' end end From 7a25026150de30254c50b67b5ede29874e938719 Mon Sep 17 00:00:00 2001 From: qadan Date: Sat, 6 Aug 2022 14:04:54 -0300 Subject: [PATCH 04/10] more fixes --- shuffler-src/setupform.lua | 3 +-- shuffler.lua | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 55b21ed..5760fed 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -285,8 +285,7 @@ function module.initial_setup(callback) if config.shuffle_index == -2 then config.game_weights = {} for _,game in ipairs(games) do - -- on first shuffle these will be increased to 0 - config.game_weights[game] = -1 + config.game_weights[game] = 0 end end diff --git a/shuffler.lua b/shuffler.lua index 68786a6..39bcfd8 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -275,11 +275,11 @@ function pad_games_list(games, to_reset) -- prevent re-entry config.game_weights[to_reset] = -1 for game,weight in ipairs(config.game_weights) do - config.game_weights[game] = config.game_weights[game] + 1 -- pad the games list with (weight) copies of the game for i = 0, weight, -1 do table.insert(games, game) end + config.game_weights[game] = config.game_weights[game] + 1 end end From 0db4853a0a0a8b4afd5013e29a1d15b0fca7e1d1 Mon Sep 17 00:00:00 2001 From: qadan Date: Sat, 6 Aug 2022 14:11:03 -0300 Subject: [PATCH 05/10] and another --- shuffler.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/shuffler.lua b/shuffler.lua index 39bcfd8..23fdc09 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -271,9 +271,8 @@ function get_next_game() end function pad_games_list(games, to_reset) - -- increase all weights by 1, except to_reset, which is explicitly set to -1 to - -- prevent re-entry - config.game_weights[to_reset] = -1 + -- to_reset is explicitly set to 0 to prevent re-entry + config.game_weights[to_reset] = 0 for game,weight in ipairs(config.game_weights) do -- pad the games list with (weight) copies of the game for i = 0, weight, -1 do From c8421bb1afb91f4b2a1f0f1c669924fbd9805dc7 Mon Sep 17 00:00:00 2001 From: qadan Date: Sun, 7 Aug 2022 01:21:31 -0300 Subject: [PATCH 06/10] fixing the edges --- shuffler.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/shuffler.lua b/shuffler.lua index 23fdc09..45ea9ad 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -260,7 +260,10 @@ function get_next_game() if #all_games == 0 then return prev end -- shuffle_index == -2 indicates games should be given multiple entries based on -- their weight in config.game_weights - if config.shuffle_index == -2 then pad_games_list(all_games, prev) end + if config.shuffle_index == -2 then + config.game_weights[prev] = 0 + pad_games_list(all_games) + end return all_games[math.random(#all_games)] else -- manually select the next one @@ -270,12 +273,12 @@ function get_next_game() end end -function pad_games_list(games, to_reset) - -- to_reset is explicitly set to 0 to prevent re-entry - config.game_weights[to_reset] = 0 - for game,weight in ipairs(config.game_weights) do +function pad_games_list(games) + for _,game in ipairs(games) do + -- accounting for new games + if config.game_weights[game] ~= nil then config.game_weights[game] = 0 end -- pad the games list with (weight) copies of the game - for i = 0, weight, -1 do + for i = 0, config.game_weights[game] do table.insert(games, game) end config.game_weights[game] = config.game_weights[game] + 1 @@ -408,6 +411,10 @@ end function mark_complete() -- mark the game as complete in the config file rather than moving files around table.insert(config.completed_games, config.current_game) + -- purge it from game_weights if necessary + if config.game_weights ~= nil then + table.remove(config.game_weights, config.current_game) + end log_message(config.current_game .. ' marked complete') for _,plugin in ipairs(plugins) do if plugin.on_complete ~= nil then From d0edbe1708e329a3879d4e344c2f45238070a8a7 Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 8 Aug 2022 13:32:40 -0300 Subject: [PATCH 07/10] accurate thing --- output-info/game-weights.txt | 0 shuffler.lua | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 output-info/game-weights.txt diff --git a/output-info/game-weights.txt b/output-info/game-weights.txt deleted file mode 100644 index e69de29..0000000 diff --git a/shuffler.lua b/shuffler.lua index 45ea9ad..42eee68 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -253,7 +253,7 @@ function get_next_game() all_games = get_games_list(true) end - -- shuffle_index == -1 represents random shuffle order types + -- shuffle_index < 0 represents random shuffle order types if config.shuffle_index < 0 then -- remove the currently loaded game and see if there are any other options table_subtract(all_games, { prev }) From 3e43dbd2b65b3a85c1857a3c6e5a50c388223dbc Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 22 Aug 2022 20:52:16 -0300 Subject: [PATCH 08/10] actually get games list --- shuffler-src/setupform.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 5760fed..1759156 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -284,7 +284,7 @@ function module.initial_setup(callback) if config.shuffle_index == -2 then config.game_weights = {} - for _,game in ipairs(games) do + for _,game in ipairs(get_games_list(true)) do config.game_weights[game] = 0 end end From df7a4b269d9b44813770628dacb4ca282e185766 Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 22 Aug 2022 20:52:33 -0300 Subject: [PATCH 09/10] tested now --- shuffler.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/shuffler.lua b/shuffler.lua index 42eee68..607f61f 100644 --- a/shuffler.lua +++ b/shuffler.lua @@ -261,7 +261,7 @@ function get_next_game() -- shuffle_index == -2 indicates games should be given multiple entries based on -- their weight in config.game_weights if config.shuffle_index == -2 then - config.game_weights[prev] = 0 + if prev ~= nil then config.game_weights[prev] = 0 end pad_games_list(all_games) end return all_games[math.random(#all_games)] @@ -274,12 +274,21 @@ function get_next_game() end function pad_games_list(games) - for _,game in ipairs(games) do + -- copy the games list so we're not iterating over an ever-expanding list + local initial_games = {} + for _,game in pairs(games) do + table.insert(initial_games, game) + end + -- actual padding + for _,game in pairs(initial_games) do -- accounting for new games - if config.game_weights[game] ~= nil then config.game_weights[game] = 0 end - -- pad the games list with (weight) copies of the game - for i = 0, config.game_weights[game] do - table.insert(games, game) + if config.game_weights[game] ~= nil then + -- pad the games list with (weight) copies of the game + for i = 0, config.game_weights[game] do + table.insert(games, game) + end + else + config.game_weights[game] = 0 end config.game_weights[game] = config.game_weights[game] + 1 end From 789a020e254417d704e43d71f8e236796be44951 Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 22 Aug 2022 21:28:08 -0300 Subject: [PATCH 10/10] some cleanup --- shuffler-src/setupform.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shuffler-src/setupform.lua b/shuffler-src/setupform.lua index 1759156..898ba2e 100644 --- a/shuffler-src/setupform.lua +++ b/shuffler-src/setupform.lua @@ -284,7 +284,7 @@ function module.initial_setup(callback) if config.shuffle_index == -2 then config.game_weights = {} - for _,game in ipairs(get_games_list(true)) do + for _,game in pairs(get_games_list()) do config.game_weights[game] = 0 end end