Jump to content

Recommended Posts

I'm sorry for that this is not so much about mods but I think it is easier to get the answer here.

My question is:

How does the game check if the ingredients fulfill the recipe of a dish while the essential ingredients also have the food value that the recipe requires or excludes?

For example, when I cook one turkey dinner with 3 drumsticks and 1 berries, how does the game check the ingredients?

A)    It consumes 2 drumsticks first, and then checks if there are any meat value left.

B)    It checks them separately. Checking if there are 2 or more drumstcks, then it checks if there are 1.25 or more meat value in total.

 

Any reply would be appreciated.

Spoiler

local function GetIngredientValues(prefablist)
    local prefabs = {}
    local tags = {}
    for k,v in pairs(prefablist) do
        local name = aliases[v] or v
        prefabs[name] = (prefabs[name] or 0) + 1
        local data = ingredients[name]
        if data ~= nil then
            for kk, vv in pairs(data.tags) do
                tags[kk] = (tags[kk] or 0) + vv
            end
        end
    end
    return { tags = tags, names = prefabs }
end

function GetCandidateRecipes(cooker, ingdata)
	local recipes = cookerrecipes[cooker] or {}
	local candidates = {}

	--find all potentially valid recipes
	for k,v in pairs(recipes) do
		if v.test(cooker, ingdata.names, ingdata.tags) then
			table.insert(candidates, v)
		end
	end

	table.sort( candidates, function(a,b) return (a.priority or 0) > (b.priority or 0) end )
	if #candidates > 0 then
		--find the set of highest priority recipes
		local top_candidates = {}
		local idx = 1
		local val = candidates[1].priority or 0

		for k,v in ipairs(candidates) do
			if k > 1 and (v.priority or 0) < val then
				break
			end
			table.insert(top_candidates, v)
		end
		return top_candidates
	end

	return candidates
end

local function CalculateRecipe(cooker, names)
	local ingdata = GetIngredientValues(names)
	local candidates = GetCandidateRecipes(cooker, ingdata)

	table.sort( candidates, function(a,b) return (a.weight or 1) > (b.weight or 1) end )
	local total = 0
	for k,v in pairs(candidates) do
		total = total + (v.weight or 1)
	end

	local val = math.random()*total
	local idx = 1
	while idx <= #candidates do
		val = val - candidates[idx].weight
		if val <= 0 then
			return candidates[idx].name, candidates[idx].cooktime or 1
		end

		idx = idx+1
	end
end

 


butterflymuffin =
	{
		test = function(cooker, names, tags) return (names.butterflywings or names.moonbutterflywings) and not tags.meat and tags.veggie and tags.veggie >= 0.5 end,
		priority = 1,
		weight = 1,
		foodtype = FOODTYPE.VEGGIE,
		health = TUNING.HEALING_MED,
		hunger = TUNING.CALORIES_LARGE,
		perishtime = TUNING.PERISH_SLOW,
		sanity = TUNING.SANITY_TINY,
		cooktime = 2,
        floater = {"small", 0.05, 0.7},
	},

 

These are all the function that calculate the resulting food.

First, the game calculates the ingredient values with GetIngredientValues. So you have 4 carrots in there, it will return vegetable = 4.

The it gets all possible recipes that can be cooked with these ingredient values with the function GetCandidateRecipes and saves them in a table.

It does that by running the test function that is defined for each prepared food, I added an example of the butterflymuffin.
You can see, it checks if there is enough veggie in there, no meat and if there is food in there that is butterfly or moonbutterflywings.

Then it sorts the table by the priority that is defined for the prepared food.

Then it calculates the recipe by running CalculateRecipe.

There it chooses at random one of the top candidates for the recipe if there are more as 1 with the same priority. These foods can have different probabilities depending on their chance, defined as weight.

Hope that clears it up!

  • Like 2
  • Health 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...