Name: KenirasRandomRecipes
Description: Randomizes recipes for different ratios in every game.
License: MIT
Version: 1.6.5
Latest release: 2020-04-13
Tested-With-Factorio-Version: 0.18
Mod Portal Link
Code: Select all
require("config")
require("functions")
local crc32 = require("crc32") -- <== new require, use the crc32 library above or some other
rand_seed = settings.startup["keniras-random-recipes-random-seed"].value
rand_all = settings.startup["keniras-random-recipes-randomize-all"].value
rand_products = settings.startup["keniras-random-recipes-products"].value
local function reseed (recipeName) -- <== new function
    local seed = crc32.Hash(recipeName .. tostring(rand_seed)) -- generate new seed based on recipe name and rand_seed
    math.randomseed(seed)
end
-- ...bulk of code here... (can remove the math.randomseed initialization stuff here since we're reseeding for each recipe anyway)
if listed == true then
   for _, recipe in pairs(item_recipes) do
      if recipe then
         reseed(item.name) -- <== new function call here
         local ingredient_one = false
         -- etc...
Code: Select all
   {
    type = "string-setting",
    name = "keniras-random-recipes-blacklist-extra",
    setting_type = "startup",
    default_value = "enrichment-process,uranium-process,-barrel,2uc-recipe,y-ac-uc2,y_uc2",
    order = "e",
   },
------------------------------
--extra blacklist
local function split_string(sep, csv)
   local sep, fields = sep or ",", {}
   local pattern = string.format("([^%s]+)", sep)
   csv:gsub(pattern, function(c) fields[#fields+1] = c end)
   return fields
end
recipe_blacklist_extra = split_string(settings.startup["keniras-random-recipes-blacklist-extra"].value, ",")
	-- check all items set as "listed" so far if they're on the blacklist
	if listed == true and blacklist_enabled == true then
		for _, item_from_blacklist in pairs(recipe_blacklist_extra) do
			if (item.name ~= nil and string.find(item.name,item_from_blacklist)) then
				listed = false
			end
		end
	end
Code: Select all
function split_string(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end


Oh, right. Forgot about that. Yea, it wouldn't work since you can't get the seed on startup, only once you choose a map.Kenira wrote:That's not possible because you need the random seed at the very beginning when booting up the game, before you even get to the main menu and could load a map. Same for any other places where you think you could get a random number from, factorio is made to be deterministic. Plus i like it better so that with a given seed the random numbers will always be the same, even independant of other mods installed.

Code: Select all
	-- if an item has normal and expensive recipes, randomize both.
	if item.normal and item.expensive then
		item_recipes = {	item.normal,
							item.expensive,
							}
	else
		item_recipes = {	item,
							}
	endCode: Select all
	-- if an item has normal and expensive recipes, randomize both.
	if item.normal and item.expensive then
		item_recipes = {	item.normal,
							item.expensive,
							}
	else
		item_recipes = {	item.normal,
							}
	end