From 1ff9bce3d25549a0d6cd52d8904012da5a60455a Mon Sep 17 00:00:00 2001 From: Alessio Rondelli Date: Mon, 7 Oct 2024 19:52:09 +0200 Subject: [PATCH] mutations added, also fixed textures --- forestry_bees/apiary.lua | 74 ++++++++++++++----------------------- forestry_bees/helper.lua | 71 ++++++++++++++++++++++++++++++++++- forestry_bees/init.lua | 3 +- forestry_bees/mutations.lua | 36 ++++++++++++++++++ forestry_bees/register.lua | 24 +++++++----- 5 files changed, 149 insertions(+), 59 deletions(-) create mode 100644 forestry_bees/mutations.lua diff --git a/forestry_bees/apiary.lua b/forestry_bees/apiary.lua index 01ec7e8..fc83311 100644 --- a/forestry_bees/apiary.lua +++ b/forestry_bees/apiary.lua @@ -93,29 +93,6 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function breed_princess_drone(princess,drone) - local princess_meta = princess:get_meta() - local drone_meta = drone:get_meta() - local queen = forestry_bees.Bee("queen", - princess_meta:get_string("active_gene"), - princess_meta:get_string("inactive_gene")) - local queen_meta = queen:get_meta() - queen_meta:set_string("drone_active_gene",drone_meta:get_string("active_gene")) - queen_meta:set_string("drone_inactive_gene",drone_meta:get_string("inactive_gene")) - return queen -end - -local function apiary_result(queen) - --given a queen calculates the correct princess + drone output - --atm easy just returns the same princess and the same drone - local meta = queen:get_meta() - --the "meta" of the queen is the princess meta and the drone_"meta" is the drone meta - local princess = forestry_bees.Bee("princess",meta:get_string("active_gene"),meta:get_string("inactive_gene")) - local drone = forestry_bees.Bee("drone",meta:get_string("drone_active_gene"),meta:get_string("drone_inactive_gene")) - drone:set_count(2) - return princess, drone -end - local function apiary_node_timer(pos,elapsed) local meta = minetest.get_meta(pos) local src_time = meta:get_float("src_time") or 0 --how much time has passed since src started actioning @@ -153,30 +130,33 @@ local function apiary_node_timer(pos,elapsed) if src_time >= breedtime then --if possible act accordingly to breed or live if can_breed then - local queen = breed_princess_drone(monarchlist[1],dronelist[1]) - local drone_count = dronelist[1]:get_count() - if drone_count >1 then - local final_drone = dronelist[1]:set_count(dronelist[1]:get_count()-1) - else - final_drone = ItemStack() - end - inv:set_stack("monarch",1,queen) - inv:set_stack("drone",1,final_drone) - src_time = src_time - breedtime - else --if actionable but not can_breed we have can_live - local queen = monarchlist[1] - local princess, drone = apiary_result(queen) - - if inv:get_size("dst")-forestry_bees.stacks_in_inv(inv,"dst")>=2 then - inv:add_item("dst",princess) - inv:add_item("dst",drone) - inv:set_stack("monarch",1,ItemStack()) - src_time = src_time - breedtime - update = true - else - minetest.chat_send_all("dst_full") - dst_full = true - end + local queen = forestry_bees.breed_princess_drone(monarchlist[1],dronelist[1]) + local drone_count = dronelist[1]:get_count() + local final_drone + if drone_count == 1 then + final_drone = ItemStack() + else + dronelist[1]:set_count(drone_count - 1) + final_drone = dronelist[1] + end + inv:set_stack("monarch",1,queen) + inv:set_stack("drone",1,final_drone) + src_time = src_time - breedtime + else --if actionable but not can_breed we have can_live + local queen = monarchlist[1] + local princess, dronelistout = forestry_bees.apiary_result(queen) + + if inv:get_size("dst")-forestry_bees.stacks_in_inv(inv,"dst") >= 1 + #dronelistout then + inv:add_item("dst",princess) + for _,drone in pairs(dronelistout) do + inv:add_item("dst",drone) + end + inv:set_stack("monarch",1,ItemStack()) + src_time = src_time - breedtime + update = true + else + dst_full = true + end end end end diff --git a/forestry_bees/helper.lua b/forestry_bees/helper.lua index 498a4fb..65b4155 100644 --- a/forestry_bees/helper.lua +++ b/forestry_bees/helper.lua @@ -18,4 +18,73 @@ function forestry_bees.stacks_in_inv(inv,listname) end end return result -end \ No newline at end of file +end + +function forestry_bees.breed_princess_drone(princess,drone) + local princess_meta = princess:get_meta() + local drone_meta = drone:get_meta() + local queen = forestry_bees.Bee("queen", + princess_meta:get_string("active_gene"), + princess_meta:get_string("inactive_gene")) + local queen_meta = queen:get_meta() + queen_meta:set_string("drone_active_gene",drone_meta:get_string("active_gene")) + queen_meta:set_string("drone_inactive_gene",drone_meta:get_string("inactive_gene")) + return queen +end + +local function final_gene(type1,type2) + local out_table1 = {} + local out_table2 = {} + --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 + local prob1 = math.random() + local prob2 = math.random() + if chance > prob1 then + table.insert(out_table1, out_type) + end + if chance > prob2 then + table.insert(out_table2, out_type) + end + end + end + --if we got no mutations then the eligible out_types are the input_types + output = {} + --randomly choose if the genes are 1,2 or 2,1 + if math.random(2)==2 then + output[1] = type1 + output[2] = type2 + else + output[1] = type2 + output[2] = type1 + end + if next(out_table1) then --if a mutation accurred in gene 1 + output[1] = out_table1[math.random(#out_table1)] + end + if next(out_table2) then --if a mutation accurred in gene 2 + 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 +end + +function forestry_bees.apiary_result(queen) + --given a queen calculates the correct princess + drone output + local meta = queen:get_meta() + local mother_genes = {meta:get_string("active_gene"),meta:get_string("inactive_gene")} + local father_genes = {meta:get_string("drone_active_gene"),meta:get_string("drone_inactive_gene")} + local mother_chosen_gene = mother_genes[math.random(#mother_genes)] + local father_chosen_gene = father_genes[math.random(#father_genes)] + local genes = final_gene(mother_chosen_gene, father_chosen_gene) + local princess = forestry_bees.Bee("princess", genes[1], genes[2]) + local dronelist = {} + for i=1,4 do --in future bees will have fertility and this number should change, so a little bit of futureproofing + mother_chosen_gene = mother_genes[math.random(#mother_genes)] + father_chosen_gene = father_genes[math.random(#father_genes)] + local genes = final_gene(mother_chosen_gene, father_chosen_gene) + local drone = forestry_bees.Bee("drone", genes[1], genes[2]) + table.insert(dronelist, drone) + end + return princess, dronelist +end diff --git a/forestry_bees/init.lua b/forestry_bees/init.lua index e89b7b1..57c868f 100644 --- a/forestry_bees/init.lua +++ b/forestry_bees/init.lua @@ -3,4 +3,5 @@ forestry_bees = {} dofile(minetest.get_modpath("forestry_bees") .. "/helper.lua") dofile(minetest.get_modpath("forestry_bees") .. "/register.lua") -dofile(minetest.get_modpath("forestry_bees") .. "/apiary.lua") \ No newline at end of file +dofile(minetest.get_modpath("forestry_bees") .. "/mutations.lua") +dofile(minetest.get_modpath("forestry_bees") .. "/apiary.lua") diff --git a/forestry_bees/mutations.lua b/forestry_bees/mutations.lua new file mode 100644 index 0000000..8db0d79 --- /dev/null +++ b/forestry_bees/mutations.lua @@ -0,0 +1,36 @@ +forestry_bees.bee_mutations = {} + +local function register_mutation(bee_type_in1,bee_type_in2,bee_type_out,chance) + --bee_type_in are string of the gene ex. "forest" or "meadows" + --bee_type_out is a string of the gene + --chance is the base probability of the mutation + + --the function appends in the table at position "bee_type_in1","bee_type_in2" + --of the matrix forestry_bees.mutations "bee_type_out"=chance + --and does so simmetrically + + --populate table if nil + if forestry_bees.bee_mutations[bee_type_in1] == nil or not next(forestry_bees.bee_mutations[bee_type_in1]) then + forestry_bees.bee_mutations[bee_type_in1] = {} + end + if forestry_bees.bee_mutations[bee_type_in2] == nil or not next(forestry_bees.bee_mutations[bee_type_in2]) then + forestry_bees.bee_mutations[bee_type_in2] = {} + end + if forestry_bees.bee_mutations[bee_type_in1][bee_type_in2] == nil or not next(forestry_bees.bee_mutations[bee_type_in1][bee_type_in2]) then + forestry_bees.bee_mutations[bee_type_in1][bee_type_in2] = {} + end + if forestry_bees.bee_mutations[bee_type_in2][bee_type_in1] == nil or not next(forestry_bees.bee_mutations[bee_type_in2][bee_type_in1]) then + forestry_bees.bee_mutations[bee_type_in2][bee_type_in1] = {} + end + + forestry_bees.bee_mutations[bee_type_in1][bee_type_in2][bee_type_out] = chance + forestry_bees.bee_mutations[bee_type_in2][bee_type_in1][bee_type_out] = chance +end + +register_mutation("forest","meadow","common",0.15) +register_mutation("forest","common","cultivated",0.12) +register_mutation("meadow","common","cultivated",0.12) +register_mutation("common","cultivated","noble",0.10) +register_mutation("common","cultivated","diligent",0.10) + +--print(forestry_bees.mutations["meadows"]["forest"]["common"]) diff --git a/forestry_bees/register.lua b/forestry_bees/register.lua index 02d40d5..87ddda7 100644 --- a/forestry_bees/register.lua +++ b/forestry_bees/register.lua @@ -2,8 +2,8 @@ local function capitalize(s) return s:sub(1,1):upper()..s:sub(2) end -local function makebeetexture(body1,body2,outline,color,ratio) - return body1.."^("..body1.."^[makealpha:0,0,0^[colorize:"..color..":"..ratio..")^"..body2.."^("..outline.."^[colorize:"..color..":"..ratio..")" +local function makebeetexture(body1,body2,outline,color1,color2) + return body1.."^("..body1.."^[makealpha:0,0,0^[colorize:"..color1..":255)^"..body2.."^("..outline.."^[colorize:"..color2..":255)" end local function printbeestats(itemstack, user, pointed_thing) @@ -12,12 +12,12 @@ local function printbeestats(itemstack, user, pointed_thing) minetest.chat_send_player(user:get_player_name(), "Name:"..string.sub(string.match(name, ":.*"),2)..", Active:"..meta:get_string("active_gene")..", Inactive:"..meta:get_string("inactive_gene")) end -local function register_bee(name, color, color_ratio) +local function register_bee(name, color1, color2) minetest.register_craftitem("forestry_bees:"..name.."_princess", { description = capitalize(name).." Princess", inventory_image = makebeetexture("body1.png", "princess.body2.png", - "princess.outline.png",color,color_ratio), + "princess.outline.png",color1,color2), on_use = printbeestats, groups = {bee_princess=1, bee_monarch=1}, }) @@ -25,7 +25,7 @@ local function register_bee(name, color, color_ratio) description = capitalize(name).." Drone", inventory_image = makebeetexture("body1.png", "drone.body2.png", - "drone.outline.png",color,color_ratio), + "drone.outline.png",color1,color2), on_use = printbeestats, groups = {bee_drone=1}, }) @@ -33,7 +33,7 @@ local function register_bee(name, color, color_ratio) description = capitalize(name).." Queen", inventory_image = makebeetexture("body1.png", "queen.body2.png", - "queen.outline.png",color,color_ratio), + "queen.outline.png",color1,color2), on_use = printbeestats, groups = {bee_queen=1, bee_monarch=1}, }) @@ -101,12 +101,16 @@ minetest.register_tool("forestry_bees:scoop", { }, }) -register_bee("forest","#19d0ec","255") -register_bee("meadow","#ef131e","255") -register_bee("common","#b2b2b2","255") +register_bee("forest","#ffdc16","#19d0ec") +register_bee("meadow","#ffdc16","#ef131e") +register_bee("common","#ffdc16","#b2b2b2") +register_bee("cultivated","#ffdc16","#5734ec") +register_bee("noble","#ffdc16","#ec9a19") +register_bee("diligent","#ffdc16","#c219ec") + minetest.register_craftitem("forestry_bees:honey_comb", { description = "Honey Comb", groups = {comb = 1}, inventory_image = "(bee_combs.0.png^[colorize:Yellow:100)^(bee_combs.1.png^[colorize:Yellow:120)" -}) \ No newline at end of file +})