Index: /experiments/frams/foraminifera/data/scripts/foraminifera.expdef
===================================================================
--- /experiments/frams/foraminifera/data/scripts/foraminifera.expdef	(revision 417)
+++ /experiments/frams/foraminifera/data/scripts/foraminifera.expdef	(revision 418)
@@ -24,10 +24,4 @@
 - added "S" receptor to visualize the difference between diplo and haplo generations
 - signal.value is a MechPart (a reference) so it does not have to be updated after changing the object location
-*/
-
-/* TODO fix
-[ERROR] Population.kill: Invalid index 35 (allowed range is 0..34)
-[ERROR] Population.kill: ... called from reproduce_haploid()  <foraminifera.inc:54>
-[ERROR] Population.kill: ... called from onStep()  <foraminifera.inc:228>
 */
 
@@ -69,6 +63,9 @@
 	pop.othermask = 0x10000;
 
+	//TODO remove unused params
+
 	//radius of the chamber
 	ExpParams.rads = [1.2, 0.6];
+	//ExpParams.rads = [2, 2];
 	//inital genotypes
 	ExpParams.genh = "//0\np:sh=1,sx=" + ExpParams.rads[0] + ",sy=" + ExpParams.rads[0] + ",sz=" + ExpParams.rads[0] + ", rz=3.14159265358979";
@@ -80,5 +77,5 @@
 	ExpParams.feedtrans = 3;
 	ExpParams.creath = -0.99; //just above the bottom
-	ExpParams.foodgen = "//0\np:sh=2,sx=0.1,sy=0.1,sz=0.1\nn:d=T,p=0";
+	ExpParams.foodgen = "//0\np:sh=2,sx=0.1,sy=0.1,sz=0.1\nn:d=T,p=0"; //TODO food size as param
 	ExpParams.autorestart = 0;
 	ExpParams.psize = 10;
@@ -102,5 +99,5 @@
 
 	//grwoth speed in time steps
-	ExpParams.growth_step = 50;
+	ExpParams.growth_step = 50; //50
 
 	ExpState.totaltestedcr = 0;
@@ -308,5 +305,5 @@
 		else
 		{
-			properSize = cr.user2["Va"] >= cr.user1[0]["vamin"];
+			properSize = cr.user2["Va"] >= cr.user1[0]["vamin"]; //TODO gene selection
 		}
 
@@ -333,5 +330,5 @@
 	geno.user1 = cr.user1;
 	geno.user2 = cr.user2;
-	Simulator.print("\"" + cr.name + "\" died...");
+	if (ExpParams.logging == 1) Simulator.print("\"" + cr.name + "\" died...");
 	ExpState.totaltestedcr++;
 }
@@ -422,5 +419,5 @@
 id:growth_step
 name:Growth step
-type:d 10 10000 1000
+type:d -1 10000 1000
 
 prop:
Index: /experiments/frams/foraminifera/data/scripts/foraminifera.inc
===================================================================
--- /experiments/frams/foraminifera/data/scripts/foraminifera.inc	(revision 417)
+++ /experiments/frams/foraminifera/data/scripts/foraminifera.inc	(revision 418)
@@ -4,63 +4,40 @@
 // -------------------------------- step begin --------------------------------
 
-function reproduce_haploid(repro_list)
+function reproduce_haploid(parent, parent2)
 {
-	var len = repro_list.size;
-	if (len % 2 != 0) len = repro_list.size - 1;
+	var number = parent.user2["Va"] / ExpParams.ofnumd + parent2.user2["Va"] / ExpParams.ofnumd;
 
-	for (var i = 0; i < len - 2 ; i = i + 2)
+	for (var j = 0; j < number; j++)
 	{
-		var parent = Populations[0].get(repro_list[i]);
-		var parent2 = Populations[0].get(repro_list[i + 1]);
-
-		var number = parent.user2["Va"] / ExpParams.ofnumd + parent2.user2["Va"] / ExpParams.ofnumd;
-
-		for (var j = 0; j < number; j++)
-		{
-			createOffspring(ExpParams.gend, ExpParams.ofnumd, [parent.user1, parent2.user1], {"Va" : ExpParams.ofnumd, "gen" : 1 , "growth_step" : ExpParams.growth_step, "rsize": ExpParams.rads[1], "vinit": ExpParams.ofnumd}); //TODO genes from both generations in user1
-		}
+		createOffspring(ExpParams.gend, ExpParams.ofnumd, [parent.user1, parent2.user1], {"Va" : ExpParams.ofnumd, "gen" : 1 , "growth_step" : ExpParams.growth_step, "rsize": ExpParams.rads[1], "vinit": ExpParams.ofnumd}); //TODO genes from both generations in user1
 	}
-	killParents(repro_list);
 }
 
-function reproduce_diploid(repro_list)
+function reproduce_diploid(parent)
 {
-	for (var i = 0; i < repro_list.size; i++)
+	var number = parent.user2["Va"] / ExpParams.ofnumh;
+	for (var j = 0; j < number / 2; j++)
 	{
-		var parent = Populations[0].get(repro_list[i]);
-
-		if (parent.user2["gen"] != 1)
+		var crossed = 0;
+		//crossover
+		if (Math.rnd01 < ExpParams.crossprob)
 		{
-			Simulator.print("gen: " + parent.user2["gen"]);
+			crossover(parent, "vamin");
+			crossed = 1;
 		}
 
-		if (parent.user2["gen"] == 1)
+		for (var k = 0; k < 2; k++)
 		{
-			var number = parent.user2["Va"] / ExpParams.ofnumh;
-			for (var j = 0; j < number / 2; j++)
-			{
-				var crossed = 0;
-				//crossover
-				if (Math.rnd01 < ExpParams.crossprob)
-				{
-					crossover(parent, "vamin");
-					crossed = 1;
-				}
+			createOffspring(ExpParams.genh, ExpParams.ofnumh, {"vamin" : ExpParams.v_min_h, "amin": ExpParams.age_min_h}, {"Va" : ExpParams.ofnumh, "gen" : 0 , "growth_step" : ExpParams.growth_step, "rsize" : ExpParams.rads[0], "vinit": ExpParams.ofnumh});
+		}
 
-				for (var k = 0; k < 2; k++)
-				{
-					createOffspring(ExpParams.genh, ExpParams.ofnumh, {"vamin" : ExpParams.v_min_h, "amin": ExpParams.age_min_h}, {"Va" : ExpParams.ofnumh, "gen" : 0 , "growth_step" : ExpParams.growth_step, "rsize" : ExpParams.rads[0], "vinit": ExpParams.ofnumh});
-				}
-
-				//reverse of crossover for fossilization
-				if (crossed == 1)
-				{
-					crossover(parent, "vamin");
-					crossed = 0;
-				}
-			}
+		//reverse of crossover for fossilization
+		if (crossed == 1)
+		{
+			crossover(parent, "vamin");
+			crossed = 0;
 		}
+			
 	}
-	killParents(repro_list);
 }
 
@@ -72,11 +49,4 @@
 }
 
-function killParents(repro_list)
-{
-	for (var j = 0; j < repro_list.size; j++)
-	{
-		Populations[0].kill(repro_list[j]);
-	}
-}
 
 function createOffspring(geno, energy, new_user1, new_user2)
@@ -164,28 +134,34 @@
 	{
 		reprocounter = 0;
-		var to_repro_h = [];
-		var to_repro_d = [];
-		for (var i = 0; i < Populations[0].size; i++)
+		var parent1 = null;
+		var parent2 = null;
+		var pop = Populations[0];
+		for (var i = pop.size-1; i >= 0; i--)
 		{
-			var cr = Populations[0].get(i);
-			if (cr.signals.size > 0)
-			{
-				if (cr.user2["gen"] == 0)
+   			if (pop[i].signals.size > 0) 
+   			{ 
+				if (pop[i].user2["gen"]==1)
 				{
-					to_repro_h.add(i);
+					reproduce_diploid(pop[i]);
+					pop.kill(i);
 				}
-				if (cr.user2["gen"] == 1)
+      				else if (parent1 == null)
 				{
-					to_repro_d.add(i);
+					parent1 = pop[i];
 				}
-			}
-		}
-		if (to_repro_h.size > 1)
-		{
-			reproduce_haploid(to_repro_h);
-		}
-		if (to_repro_d.size > 0)
-		{
-			reproduce_diploid(to_repro_d);
+				else if (parent2 == null)
+				{
+					parent2 = pop[i];
+				}  
+				else
+				{
+					reproduce_haploid(parent1, parent2);
+					pop.kill(parent1);
+					pop.kill(parent2);
+					parent1 = null;
+					parent2 = null;
+				}
+      		
+   			} 
 		}
 	}
