forestry/forestry_bees/helper.lua

102 lines
3.8 KiB
Lua
Raw Normal View History

2024-10-07 14:31:20 +02:00
function forestry_bees.Bee(bee_type,active_gene,inactive_gene)
local itemstack = ItemStack({name = forestry_bees.bee_name(bee_type,active_gene)})
itemstack:get_meta():set_string("active_gene",active_gene)
itemstack:get_meta():set_string("inactive_gene",inactive_gene)
return itemstack
end
function forestry_bees.calculate_drop(bee_type) --later we will take as input also the bee_prod_speed and the item_modifier (for frames)
local possible_drops = forestry_bees.bee_drops[bee_type] --this gets us a table
local out_table = {}
for drop,chance in pairs(possible_drops) do
if math.random() < chance then
table.insert(out_table,ItemStack(drop))
end
end
return out_table
end
2024-10-07 14:31:20 +02:00
function forestry_bees.bee_name(bee_type,active_gene)
return "forestry_bees:"..active_gene.."_"..bee_type
end
function forestry_bees.stacks_in_inv(inv,listname)
local stack_list = inv:get_list(listname)
local result = 0
for i = 1,#stack_list do
if not stack_list[i]:is_empty() then
result = result + 1
end
end
return result
2024-10-07 19:52:09 +02:00
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