Merge lp:~widelands-dev/widelands/relax-ai-asserts into lp:widelands

Proposed by GunChleoc on 2019-09-06
Status: Merged
Merged at revision: 9200
Proposed branch: lp:~widelands-dev/widelands/relax-ai-asserts
Merge into: lp:widelands
Diff against target: 174 lines (+39/-24)
3 files modified
src/ai/ai_help_structs.cc (+18/-0)
src/ai/ai_help_structs.h (+7/-2)
src/ai/defaultai.cc (+14/-22)
To merge this branch: bzr merge lp:~widelands-dev/widelands/relax-ai-asserts
Reviewer Review Type Date Requested Status
TiborB 2019-09-06 Approve on 2019-09-07
Review via email: mp+372400@code.launchpad.net

Commit message

Some improvements around AI asserts

- Relax AI requirement for hunters
- Be more informative when building is missing the
  "collects_ware_from_map" AI hint

To post a comment you must log in.
9198. By GunChleoc on 2019-09-06

Merged trunk.

9199. By GunChleoc on 2019-09-06

Be more informative when building is missing the "collects_ware_from_map" AI hint.

bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 5404. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/581765188.
Appveyor build 5174. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_relax_ai_asserts-5174.

TiborB (tiborb95) wrote :

code looks OK

review: Approve
GunChleoc (gunchleoc) wrote :

Thanks for the review :)

@bunnybot merge

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/ai/ai_help_structs.cc'
2--- src/ai/ai_help_structs.cc 2019-09-05 19:57:55 +0000
3+++ src/ai/ai_help_structs.cc 2019-09-06 16:24:16 +0000
4@@ -370,6 +370,24 @@
5 assert(!is(attribute));
6 }
7
8+bool BuildingObserver::has_collected_map_resource() const {
9+ return collected_map_resource != INVALID_INDEX;
10+}
11+void BuildingObserver::set_collected_map_resource(const Widelands::TribeDescr& tribe, const std::string& ware_name) {
12+ if (!ware_name.empty()) {
13+ collected_map_resource = tribe.safe_ware_index(ware_name);
14+ } else {
15+ collected_map_resource = Widelands::INVALID_INDEX;
16+ }
17+}
18+DescriptionIndex BuildingObserver::get_collected_map_resource() const {
19+ if (has_collected_map_resource()) {
20+ return collected_map_resource;
21+ } else {
22+ throw wexception("Building '%s' needs to define the AI hint \"collects_ware_from_map\"", desc->name().c_str());
23+ }
24+}
25+
26 Widelands::AiModeBuildings BuildingObserver::aimode_limit_status() {
27 if (total_count() > cnt_limit_by_aimode) {
28 return Widelands::AiModeBuildings::kLimitExceeded;
29
30=== modified file 'src/ai/ai_help_structs.h'
31--- src/ai/ai_help_structs.h 2019-08-21 17:57:37 +0000
32+++ src/ai/ai_help_structs.h 2019-09-06 16:24:16 +0000
33@@ -449,6 +449,11 @@
34 void set_is(BuildingAttribute);
35 void unset_is(BuildingAttribute);
36
37+ // Building collects a ware from the map
38+ bool has_collected_map_resource() const;
39+ void set_collected_map_resource(const TribeDescr& tribe, const std::string& ware_name);
40+ DescriptionIndex get_collected_map_resource() const;
41+
42 char const* name;
43 Widelands::DescriptionIndex id;
44 Widelands::BuildingDescr const* desc;
45@@ -486,14 +491,13 @@
46 int32_t substitutes_count;
47
48 std::set<DescriptionIndex> production_hints;
49- DescriptionIndex collected_map_resource;
50+
51 bool requires_supporters;
52
53 // information needed for decision on new building construction
54 int16_t initial_preciousness;
55 int16_t max_preciousness;
56 int16_t max_needed_preciousness;
57-
58 int32_t cnt_built;
59 int32_t cnt_under_construction;
60 int32_t cnt_target; // number of buildings as target
61@@ -513,6 +517,7 @@
62 bool build_material_shortage;
63
64 private:
65+ DescriptionIndex collected_map_resource;
66 std::set<BuildingAttribute> is_what;
67 };
68
69
70=== modified file 'src/ai/defaultai.cc'
71--- src/ai/defaultai.cc 2019-09-05 19:57:55 +0000
72+++ src/ai/defaultai.cc 2019-09-06 16:24:16 +0000
73@@ -647,11 +647,7 @@
74 bo.fighting_type = bh.is_fighting_type();
75 bo.mountain_conqueror = bh.is_mountain_conqueror();
76 bo.requires_supporters = bh.requires_supporters();
77- if (!bh.collects_ware_from_map().empty()) {
78- bo.collected_map_resource = tribe_->safe_ware_index(bh.collects_ware_from_map());
79- } else {
80- bo.collected_map_resource = INVALID_INDEX;
81- }
82+ bo.set_collected_map_resource(*tribe_, bh.collects_ware_from_map());
83 if (bo.requires_supporters) {
84 log(" %d: %s strictly requires supporters\n", player_number(), bo.name);
85 }
86@@ -898,7 +894,8 @@
87 assert(count_buildings_with_attribute(BuildingAttribute::kRanger) == 1);
88 assert(count_buildings_with_attribute(BuildingAttribute::kWell) == 1);
89 assert(count_buildings_with_attribute(BuildingAttribute::kLumberjack) == 1);
90- assert(count_buildings_with_attribute(BuildingAttribute::kHunter) == 1);
91+ assert((count_buildings_with_attribute(BuildingAttribute::kHunter) == 1) ||
92+ (count_buildings_with_attribute(BuildingAttribute::kHunter) == 0));
93 assert(count_buildings_with_attribute(BuildingAttribute::kIronMine) >= 1);
94 assert(count_buildings_with_attribute(BuildingAttribute::kFisher) == 1);
95 // If there will be a tribe with more than 3 mines of the same type, just increase the number
96@@ -2707,8 +2704,7 @@
97 prio += bo.primary_priority;
98
99 // keep wells more distant
100- assert(bo.collected_map_resource != INVALID_INDEX);
101- if (bf->collecting_producers_nearby.at(bo.collected_map_resource) > 2) {
102+ if (bf->collecting_producers_nearby.at(bo.get_collected_map_resource()) > 2) {
103 continue;
104 }
105
106@@ -2737,10 +2733,9 @@
107 (bf->trees_nearby - trees_nearby_treshold_) / 10;
108
109 // consider cutters and rangers nearby
110- assert(bo.collected_map_resource != INVALID_INDEX);
111- prio += 2 * bf->supporters_nearby.at(bo.collected_map_resource) *
112+ prio += 2 * bf->supporters_nearby.at(bo.get_collected_map_resource()) *
113 std::abs(management_data.get_military_number_at(25));
114- prio -= bf->collecting_producers_nearby.at(bo.collected_map_resource) *
115+ prio -= bf->collecting_producers_nearby.at(bo.get_collected_map_resource()) *
116 std::abs(management_data.get_military_number_at(36)) * 3;
117
118 } else if (bo.is(BuildingAttribute::kNeedsRocks)) {
119@@ -2772,8 +2767,7 @@
120 }
121
122 // to prevent to many quaries on one spot
123- assert(bo.collected_map_resource != INVALID_INDEX);
124- prio = prio - 50 * bf->collecting_producers_nearby.at(bo.collected_map_resource);
125+ prio = prio - 50 * bf->collecting_producers_nearby.at(bo.get_collected_map_resource());
126
127 } else if (bo.is(BuildingAttribute::kHunter)) {
128
129@@ -2788,11 +2782,10 @@
130 // Overdue priority here
131 prio += bo.primary_priority;
132
133- prio += bf->supporters_nearby.at(bo.collected_map_resource) * 5;
134+ prio += bf->supporters_nearby.at(bo.get_collected_map_resource()) * 5;
135
136- assert(bo.collected_map_resource != INVALID_INDEX);
137 prio += (bf->critters_nearby * 3) - 8 -
138- 5 * bf->collecting_producers_nearby.at(bo.collected_map_resource);
139+ 5 * bf->collecting_producers_nearby.at(bo.get_collected_map_resource());
140
141 } else if (bo.is(BuildingAttribute::kFisher)) { // fisher
142
143@@ -2807,9 +2800,8 @@
144 // Overdue priority here
145 prio += bo.primary_priority;
146
147- assert(bo.collected_map_resource != INVALID_INDEX);
148- prio -= bf->collecting_producers_nearby.at(bo.collected_map_resource) * 20;
149- prio += bf->supporters_nearby.at(bo.collected_map_resource) * 20;
150+ prio -= bf->collecting_producers_nearby.at(bo.get_collected_map_resource()) * 20;
151+ prio += bf->supporters_nearby.at(bo.get_collected_map_resource()) * 20;
152
153 prio += -5 + bf->fish_nearby *
154 (1 + std::abs(management_data.get_military_number_at(63) / 15));
155@@ -3036,7 +3028,7 @@
156 std::abs(management_data.get_military_number_at(102)) / 5;
157 } else {
158 // leave some free space between them
159- prio -= bf->collecting_producers_nearby.at(bo.collected_map_resource) *
160+ prio -= bf->collecting_producers_nearby.at(bo.get_collected_map_resource()) *
161 std::abs(management_data.get_military_number_at(108)) / 5;
162 }
163
164@@ -6067,8 +6059,8 @@
165 for (size_t i = 0; i < bo.ware_outputs.size(); ++i) {
166 ++field.producers_nearby.at(bo.ware_outputs.at(i));
167 }
168- if (bo.collected_map_resource != INVALID_INDEX) {
169- ++field.collecting_producers_nearby.at(bo.collected_map_resource);
170+ if (bo.has_collected_map_resource()) {
171+ ++field.collecting_producers_nearby.at(bo.get_collected_map_resource());
172 }
173
174 if (!bo.production_hints.empty()) {