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

Proposed by Teppo Mäenpää
Status: Merged
Merged at revision: 7357
Proposed branch: lp:~kxq/widelands/bug-1348800-forester_adds_weight
Merge into: lp:widelands
Diff against target: 33 lines (+21/-2)
1 file modified
src/logic/worker.cc (+21/-2)
To merge this branch: bzr merge lp:~kxq/widelands/bug-1348800-forester_adds_weight
Reviewer Review Type Date Requested Status
SirVer Approve
Review via email: mp+246209@code.launchpad.net

Description of the change

Forester now favours the most suitable tree(s).

To post a comment you must log in.
7348. By Teppo Mäenpää

Seemingly, I messed something up just before previous commit. Now this might even compile.

Revision history for this message
SirVer (sirver) wrote :

some nits and some need for explanation.

review: Needs Information
7349. By Teppo Mäenpää

nits..

Revision history for this message
Teppo Mäenpää (kxq) :
Revision history for this message
Teppo Mäenpää (kxq) wrote :

code comments seem to have disappeared.

I put the additional addition to "choice", to ensure that it is always positive and therefore state.ivar2 is always assigned.

7350. By Teppo Mäenpää

Removed the epsilon.

Revision history for this message
SirVer (sirver) wrote :

> code comments seem to have disappeared.

you have to select the diff you want to look at below. Unfortunately, the diff comments are bound to the diff and not listed again. I added one more to the old diff.

review: Needs Fixing
Revision history for this message
Teppo Mäenpää (kxq) wrote :

Would you mind writing it here? For some reason, I cannot see any new comments.

Maybe Launchpad is still updating itself.

7351. By Teppo Mäenpää

Even smaller tuning.

Revision history for this message
Teppo Mäenpää (kxq) wrote :

>you have to select the diff you want to look at below.
...
I added one more to the old diff.

Are you sure you clicked the "save comment" button? I still cannot find the new comment; guessing that it should have propagated through launchpad by now.

Revision history for this message
SirVer (sirver) wrote :

Sure.

> for (auto bsii
use const auto& to avoid reconstructing the tuple. for both loops. Please also add {} for the second for.

> Adding epsilon
I was not concerned about performance - this is not hot inner loop code anyways. I just didn't get it. In fact, I still do not get it - espilon() is 1e-18 or so, why does adding it turn choice into a positive number?

logic_rand_as_double always returns a positive number so it is probably not needed. I just want to understand it.

Revision history for this message
SirVer (sirver) wrote :

In fact I did not save the comment before ... sorry :/

7352. By Teppo Mäenpää

A performance improvement of dozens of percents (in a minuscule loop).

Revision history for this message
Teppo Mäenpää (kxq) wrote :

Now I have solved all the issues I am aware of.

Revision history for this message
Teppo Mäenpää (kxq) wrote :

Ready for a re-review.

Revision history for this message
SirVer (sirver) wrote :

looks good to me. Could you merge?

review: Approve
7353. By Teppo Mäenpää

merged trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/logic/worker.cc'
--- src/logic/worker.cc 2014-12-28 16:45:37 +0000
+++ src/logic/worker.cc 2015-01-16 19:01:32 +0000
@@ -843,8 +843,27 @@
843 }843 }
844844
845 // Randomly pick one of the immovables to be planted.845 // Randomly pick one of the immovables to be planted.
846 const uint32_t idx = game.logic_rand() % best_suited_immovables_index.size();846
847 state.ivar2 = std::get<1>(*std::next(best_suited_immovables_index.begin(), idx));847 // Each candidate is weighted by its probability to grow.
848 double total_weight = 0.0;
849 for (const auto& bsii : best_suited_immovables_index)
850 {
851 double weight = std::get<0>(bsii);
852 total_weight += weight * weight;
853 }
854
855 double choice = logic_rand_as_double(&game) * total_weight;
856
857 for (const auto& bsii : best_suited_immovables_index)
858 {
859 double weight = std::get<0>(bsii);
860 state.ivar2 = std::get<1>(bsii);
861 choice -= weight * weight;
862 if (0 > choice)
863 {
864 break;
865 }
866 }
848867
849 Immovable& newimm =868 Immovable& newimm =
850 game.create_immovable(pos, state.ivar2, state.svar1 == "tribe" ? &descr().tribe() : nullptr);869 game.create_immovable(pos, state.ivar2, state.svar1 == "tribe" ? &descr().tribe() : nullptr);

Subscribers

People subscribed via source and target branches

to status/vote changes: