From 1d3e8470b6b5d2e367b2f2a64bc30abb4c5b73da Mon Sep 17 00:00:00 2001 From: StochasticMouse Date: Wed, 9 Oct 2024 21:28:02 +0200 Subject: [PATCH] now when a bee mutates its genes get replaced with the ones of the new bee_type --- forestry_bees/helper.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/forestry_bees/helper.lua b/forestry_bees/helper.lua index 1889c05..dcb1717 100644 --- a/forestry_bees/helper.lua +++ b/forestry_bees/helper.lua @@ -69,6 +69,7 @@ end local function mutation(type1,type2) local out_table1 = {} local out_table2 = {} + local mutation = {false, false} --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 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) if next(out_table1) then --if a mutation accurred in gene 1 + mutation[1] = true output[1] = out_table1[math.random(#out_table1)] end if next(out_table2) then --if a mutation accurred in gene 2 + mutation[2] = true output[2] = out_table2[math.random(#out_table2)] end --out_table is always non-empty, now we uniformally --sample from out_table to get the output gene - return output + return output, mutation end 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 father_chosen_genes = choose_genes(father_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 - if key == "type_gene" then - genes[key] = mutation(mother_chosen_genes[key], father_chosen_genes[key]) - else + --if key == "type_gene" then + -- genes[key] = mutation(mother_chosen_genes[key], father_chosen_genes[key]) + if not (key == "type_gene") then genes[key] = final_gene(mother_chosen_genes[key], father_chosen_genes[key]) 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 end