Merge lp:~widelands-dev/widelands/bug-1518223 into lp:widelands

Proposed by TiborB
Status: Merged
Merged at revision: 7639
Proposed branch: lp:~widelands-dev/widelands/bug-1518223
Merge into: lp:widelands
Diff against target: 75 lines (+38/-14)
1 file modified
src/logic/productionsite.cc (+38/-14)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1518223
Reviewer Review Type Date Requested Status
GunChleoc Approve
Review via email: mp+278249@code.launchpad.net

Description of the change

I found that has_workers function in productionsite.cc is badly broken. It is used only by AI to find out if current building to be upgraded has enough experienced workers for upgraded building. It used to return almost random results before...

To post a comment you must log in.
Revision history for this message
GunChleoc (gunchleoc) wrote :

Added some diff comments.

Revision history for this message
TiborB (tiborb95) wrote :

I am not able to fix one of issues, see comment in diff

Revision history for this message
GunChleoc (gunchleoc) wrote :

I see your point - see my diff comment.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/logic/productionsite.cc'
2--- src/logic/productionsite.cc 2015-11-11 09:53:54 +0000
3+++ src/logic/productionsite.cc 2015-11-23 18:34:18 +0000
4@@ -284,33 +284,57 @@
5 }
6
7 /**
8- * Detect if the workers are experienced enough for an upgrade
9+ * Detect if the workers are experienced enough for an target building
10+ * Buildable workers are skipped, but upgraded ones (required be target site) are tested
11 * @param idx Index of the enhancement
12 */
13 bool ProductionSite::has_workers(DescriptionIndex targetSite, Game & /* game */)
14 {
15 // bld holds the description of the building we want to have
16 if (upcast(ProductionSiteDescr const, bld, owner().tribe().get_building_descr(targetSite))) {
17- // if he has workers
18+
19 if (bld->nr_working_positions()) {
20- DescriptionIndex need = bld->working_positions()[0].first;
21- for (unsigned int i = 0; i < descr().nr_working_positions(); ++i) {
22- if (!working_positions()[i].worker) {
23- return false; // no one is in this house
24- } else {
25- DescriptionIndex have = working_positions()[i].worker->descr().worker_index();
26- if (owner().tribe().get_worker_descr(have)->can_act_as(need)) {
27- return true; // he found a lead worker
28+
29+ // Iterating over workers positions in target building
30+ for (const auto& wp : bld->working_positions()) {
31+
32+ // If worker for this position is buildable, just skip him
33+ if (owner().tribe().get_worker_descr(wp.first)->is_buildable()){
34+ continue;
35+ }
36+
37+ // This position needs promoted worker, so trying to find out if there is such worker
38+ // currently available in this site
39+ const DescriptionIndex needed_worker = wp.first;
40+ bool worker_available = false;
41+ for (unsigned int i = 0; i < descr().nr_working_positions(); ++i) {
42+ const Worker* cw = working_positions()[i].worker;
43+ if (cw) {
44+ DescriptionIndex current_worker = cw->descr().worker_index();
45+ if (owner().tribe().get_worker_descr(current_worker)->can_act_as(needed_worker)) {
46+ worker_available = true; // We found a worker for the position
47+ break;
48+ }
49 }
50 }
51+ if (!worker_available) {
52+ // We dont have needed workers in the site :(
53+ return false;
54+ }
55+
56 }
57- return false;
58+
59+ //if we are here, all needs are satisfied
60+ return true;
61+
62+ } else {
63+ throw wexception("Building, index: %d, needs no workers!\n", targetSite);
64 }
65- return true;
66- } else return true;
67+ } else {
68+ throw wexception("No such building, index: %d\n", targetSite);
69+ }
70 }
71
72-
73 WaresQueue & ProductionSite::waresqueue(DescriptionIndex const wi) {
74 for (WaresQueue * ip_queue : m_input_queues) {
75 if (ip_queue->get_ware() == wi) {

Subscribers

People subscribed via source and target branches

to status/vote changes: