now when a bee mutates its genes get replaced with the ones of the new bee_type

This commit is contained in:
StochasticMouse 2024-10-09 21:28:02 +02:00
parent 2769aa978e
commit 1d3e8470b6

View File

@ -69,6 +69,7 @@ end
local function mutation(type1,type2) local function mutation(type1,type2)
local out_table1 = {} local out_table1 = {}
local out_table2 = {} local out_table2 = {}
local mutation = {false, false}
--check every possible mutation to see if we may obtain it --check every possible mutation to see if we may obtain it
if ( (not (forestry_bees.bee_mutations[type1] == nil)) and (not (forestry_bees.bee_mutations[type1][type2] == nil)) ) then if ( (not (forestry_bees.bee_mutations[type1] == nil)) and (not (forestry_bees.bee_mutations[type1][type2] == nil)) ) then
for out_type, chance in pairs(forestry_bees.bee_mutations[type1][type2]) do for out_type, chance in pairs(forestry_bees.bee_mutations[type1][type2]) do
@ -86,14 +87,16 @@ local function mutation(type1,type2)
local output = final_gene(type1,type2) local output = final_gene(type1,type2)
if next(out_table1) then --if a mutation accurred in gene 1 if next(out_table1) then --if a mutation accurred in gene 1
mutation[1] = true
output[1] = out_table1[math.random(#out_table1)] output[1] = out_table1[math.random(#out_table1)]
end end
if next(out_table2) then --if a mutation accurred in gene 2 if next(out_table2) then --if a mutation accurred in gene 2
mutation[2] = true
output[2] = out_table2[math.random(#out_table2)] output[2] = out_table2[math.random(#out_table2)]
end end
--out_table is always non-empty, now we uniformally --out_table is always non-empty, now we uniformally
--sample from out_table to get the output gene --sample from out_table to get the output gene
return output return output, mutation
end end
local function choose_genes(genes) local function choose_genes(genes)
@ -110,13 +113,27 @@ local function output_genes(mother_genes,father_genes)
local mother_chosen_genes = choose_genes(mother_genes) local mother_chosen_genes = choose_genes(mother_genes)
local father_chosen_genes = choose_genes(father_genes) local father_chosen_genes = choose_genes(father_genes)
local genes = {} local genes = {}
genes["type_gene"], is_mutated = mutation(mother_chosen_genes["type_gene"], father_chosen_genes["type_gene"])
for key,_ in pairs(mother_chosen_genes) do for key,_ in pairs(mother_chosen_genes) do
if key == "type_gene" then --if key == "type_gene" then
genes[key] = mutation(mother_chosen_genes[key], father_chosen_genes[key]) -- genes[key] = mutation(mother_chosen_genes[key], father_chosen_genes[key])
else if not (key == "type_gene") then
genes[key] = final_gene(mother_chosen_genes[key], father_chosen_genes[key]) genes[key] = final_gene(mother_chosen_genes[key], father_chosen_genes[key])
end end
end end
--if a mutation occurred we change the corresponding genes to the base_genes of the mutated bee
if is_mutated[1] then
for key,value in pairs(forestry_bees.return_genes(genes["type_gene"][1])) do
genes[key][1] = value
end
end
if is_mutated[2] then
for key,value in pairs(forestry_bees.return_genes(genes["type_gene"][2])) do
genes[key][2] = value
end
end
return genes return genes
end end