Index: experiments/frams/foraminifera/data/scripts/foraminifera.inc
===================================================================
--- experiments/frams/foraminifera/data/scripts/foraminifera.inc	(revision 476)
+++ experiments/frams/foraminifera/data/scripts/foraminifera.inc	(revision 479)
@@ -55,10 +55,10 @@
 	if (mode->opt == 0) //initial
 	{
-		mode->cr.data->genes = {"min_repro_energies" : [max_chamber_energ[0][getProperty(0, "min_repro_energ")], max_chamber_energ[1][getProperty(1, "min_repro_energ")]], "hibernation" : mode->species};
-		mode->cr.data->lifeparams = {"max_energy_level" : getProperty(0,"energies0"), "gen" : 0,  "hibernated" : 0, "species" : mode->species, "reproduce" : 0, "dir" : randomDir()};
+		mode->cr.data->genes = {"min_repro_energies" : [energyFromVolume(max_chamber_volume[0][getProperty(0, "min_repro_energ")],0), energyFromVolume(max_chamber_volume[1][getProperty(1, "min_repro_energ")],0)], "hibernation" : mode->species};
+		mode->cr.data->lifeparams = {"max_energy_level" : getProperty(0,"energies0"), "gen" : 0,  "hibernated" : 0, "species" : mode->species, "reproduce" : 0, "dir" : randomDir(), "chamber_growth" : -1, "division_time" : -1};
 	}
 	else if (mode->opt  == 1) //child
 	{
-		mode->cr.data->lifeparams = {"max_energy_level" : getProperty(1 - mode->parent_lifeparams->gen,"energies0"), "gen" : 1 - mode->parent_lifeparams->gen,  "hibernated" : 0, "species" : mode->parent_lifeparams->species, "reproduce" : 0, "dir" : randomDir()};
+		mode->cr.data->lifeparams = {"max_energy_level" : getProperty(1 - mode->parent_lifeparams->gen,"energies0"), "gen" : 1 - mode->parent_lifeparams->gen,  "hibernated" : 0, "species" : mode->parent_lifeparams->species, "reproduce" : 0, "dir" : randomDir(), "chamber_growth" : -1, "division_time" : -1};
 		mode->cr.data->genes = mode->parent_genes;
 	}
@@ -68,4 +68,17 @@
 		mode->cr.data->lifeparams = mode->parent_lifeparams;
 	}
+}
+
+function gametsDivision(parent_energy, energy0)
+{
+	var number = 1;
+	var result = parent_energy;
+	while ((result-ExpParams.divisionCost) >= energy0)
+	{
+		result = (result-ExpParams.divisionCost)/2;
+		number *= 2; 
+	}
+	//Simulator.print("parent: " + parent_energy + " result: " + result + " number " + number);
+	return {"energy" : result, "number" : number};
 }
 
@@ -75,6 +88,7 @@
 	if (clone == 1)
 	{
-		energy0 = getProperty(0,"energies0");
-		number = (( 1 - getProperty(1, "e_repro_cost")) * parent.energy) / energy0;
+		var offspring = gametsDivision(parent.energy,getProperty(0,"energies0"));
+		energy0 = offspring->energy;
+		number = offspring->number;
 		new_genes = parent.data->genes;
 		parent.data->lifeparams->gen = 1 - parent.data->lifeparams->gen; //because of reversal of "gen" in createOffspring function
@@ -83,6 +97,8 @@
 	else
 	{
-		energy0 = getProperty(1,"energies0"); 
-		number = (((1 - getProperty(parent.data->lifeparams->gen, "e_repro_cost")) * parent.energy) + ((1 -(getProperty(parent.data->lifeparams->gen, "e_repro_cost"))) * parent2.energy)) / energy0;
+		var offspring1 = gametsDivision(parent.energy,getProperty(1,"energies0"));
+		var offspring2 = gametsDivision(parent2.energy,getProperty(1,"energies0"));
+		energy0 = (offspring1->energy+offspring2->energy); 
+		number = ExpParams.gametSuccessRate*(offspring1->number+offspring2->number)/2;
 		new_genes = [parent.data->genes, parent2.data->genes];
 		gen = 1 - parent.data->lifeparams->gen;
@@ -152,11 +168,27 @@
 				if (parent1 != null && parent2 != null)
 				{
-					reproduce_haploid(parent1, parent2, 0);
-					print_repro_info(parent1);
-					print_repro_info(parent2);
-					pop.kill(parent1);
-					pop.kill(parent2);
-					parent1 = null;
-					parent2 = null;
+					//when parents are ready for reproduction start gametogenesis
+					if (parent1.data->lifeparams->division_time == -1 && parent2.data->lifeparams->division_time == -1)
+					{
+						var time = int(ExpParams.gametoPeriod/ExpParams.secPerStep);
+						parent1.data->lifeparams->division_time = time;
+						parent2.data->lifeparams->division_time = time;
+						parent1.idleen = 0;
+						parent2.idleen = 0;
+						//Simulator.print("parents "+parent1.uid + " " + parent2.uid + " ready to repro: "+Simulator.stepNumber);
+					}
+					//when gametogenesis is finished fuse gamets 
+					else if (parent1.data->lifeparams->division_time == 0 && parent2.data->lifeparams->division_time == 0)
+					{
+						reproduce_haploid(parent1, parent2, 0);
+						print_repro_info(parent1);
+						print_repro_info(parent2);
+						//Simulator.print("parents "+parent1.uid + " " + parent2.uid + " reproduced: "+Simulator.stepNumber);
+						pop.kill(parent1);
+						pop.kill(parent2);
+						parent1 = null;
+						parent2 = null;
+						
+					}
 				}	
    			} 
