← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~kxq/widelands/bug-1348800-forester_adds_weight into lp:widelands

 

Teppo Mäenpää has proposed merging lp:~kxq/widelands/bug-1348800-forester_adds_weight into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1348800 in widelands: "Parameters for terrain affinity need tweaking"
  https://bugs.launchpad.net/widelands/+bug/1348800

For more details, see:
https://code.launchpad.net/~kxq/widelands/bug-1348800-forester_adds_weight/+merge/246209

Forester now favours the most suitable tree(s).


-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~kxq/widelands/bug-1348800-forester_adds_weight into lp:widelands.
=== modified file 'src/logic/worker.cc'
--- src/logic/worker.cc	2014-12-28 16:45:37 +0000
+++ src/logic/worker.cc	2015-01-12 18:29:14 +0000
@@ -843,8 +843,24 @@
 	}
 
 	// Randomly pick one of the immovables to be planted.
-	const uint32_t idx = game.logic_rand() % best_suited_immovables_index.size();
-	state.ivar2 = std::get<1>(*std::next(best_suited_immovables_index.begin(), idx));
+
+	// Each candidate is weighted by its probability to grow.
+	double total_weight = 0.0;
+	for (auto bsii : best_suited_immovables_index)
+	{
+		double weight = std::get<0>(bsii)
+		total_weight += weight * weight;
+	}
+
+	double choice = logic_rand_as_double(&game) * total_weight;
+
+	for (auto bsii : best_suited_immovables_index)
+	if (0 < choice)
+	{
+		double weight = std::get<0>(bsii)
+		state.ivar2 = std::get<1>(bsii);
+		choice -= weight * weight;
+	}
 
 	Immovable& newimm =
 	   game.create_immovable(pos, state.ivar2, state.svar1 == "tribe" ? &descr().tribe() : nullptr);