Merge lp:~widelands-dev/widelands/bug-1823467-trainingsites-dependencies into lp:widelands

Proposed by GunChleoc
Status: Merged
Merged at revision: 9038
Proposed branch: lp:~widelands-dev/widelands/bug-1823467-trainingsites-dependencies
Merge into: lp:widelands
Diff against target: 302 lines (+92/-45)
5 files modified
data/tribes/scripting/help/building_help.lua (+13/-14)
data/tribes/scripting/help/ware_help.lua (+2/-2)
src/scripting/lua_map.cc (+43/-15)
src/scripting/lua_map.h (+2/-2)
test/maps/lua_testsuite.wmf/scripting/immovables_descriptions.lua (+32/-12)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1823467-trainingsites-dependencies
Reviewer Review Type Date Requested Status
hessenfarmer test and review Approve
Review via email: mp+365633@code.launchpad.net

Commit message

Filter wares' producers and consumers for the tribe in the Lua interface.

Description of the change

Stop Frisian building from showing up in Barbarian help and vice versa.

This could also have been solved by 2 extra "if" statements in Lua, but I decided to go the C++ route to make the code safer against future bugs.

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

Continuous integration builds have changed state:

Travis build 4675. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/516790028.
Appveyor build 4461. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_bug_1823467_trainingsites_dependencies-4461.

Revision history for this message
hessenfarmer (stephan-lutz) wrote :

code looks good.
Have tested the appveyor build. works for the reported bug as weel as for the frisian counterpart.

@bunnybot merge

review: Approve (test and review)
Revision history for this message
GunChleoc (gunchleoc) wrote :

Thanks for the review and testing :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/tribes/scripting/help/building_help.lua'
2--- data/tribes/scripting/help/building_help.lua 2018-09-26 07:08:39 +0000
3+++ data/tribes/scripting/help/building_help.lua 2019-04-07 07:50:32 +0000
4@@ -116,14 +116,15 @@
5 -- Creates a dependencies line for any number of weapons.
6 --
7 -- :arg weapons: an array of weapon names
8+-- :arg tribename: the name of the tribe for filtering the buildings
9 -- :returns: a list weapons images with the producing and receiving building
10 --
11-function dependencies_training_weapons(weapons)
12+function dependencies_training_weapons(weapons, tribename)
13 local result = "";
14 local producers = {};
15 for count, weaponname in pairs(weapons) do
16 local weapon_description = wl.Game():get_ware_description(weaponname)
17- for i, producer in ipairs(weapon_description.producers) do
18+ for i, producer in ipairs(weapon_description:producers(tribename)) do
19 if (producers[producer.name] == nil) then
20 producers[producer.name] = {}
21 end
22@@ -270,13 +271,11 @@
23
24 for i, ware_description in ipairs(building_description.inputs) do
25 hasinput = true
26- for j, producer in ipairs(ware_description.producers) do
27- if (tribe:has_building(producer.name)) then
28- result = result .. dependencies(
29- {producer, ware_description},
30- _"%1$s from: %2$s":bformat(ware_description.descname, producer.descname)
31- )
32- end
33+ for j, producer in ipairs(ware_description:producers(tribe.name)) do
34+ result = result .. dependencies(
35+ {producer, ware_description},
36+ _"%1$s from: %2$s":bformat(ware_description.descname, producer.descname)
37+ )
38 end
39 end
40 if (hasinput) then
41@@ -341,7 +340,7 @@
42 end
43
44 -- Normal buildings
45- for j, consumer in ipairs(ware_description.consumers) do
46+ for j, consumer in ipairs(ware_description:consumers(tribe.name)) do
47 if (tribe:has_building(consumer.name)) then
48 outgoing = outgoing .. dependencies({ware_description, consumer}, consumer.descname)
49 end
50@@ -388,7 +387,7 @@
51 building_description.icon_name,
52 "tribes/workers/" .. tribe.name .. "/soldier/health_level" .. (building_description.max_health + 1) ..".png"})
53 result = result .. dependencies_training_food(building_description.food_health)
54- result = result .. dependencies_training_weapons(building_description.weapons_health)
55+ result = result .. dependencies_training_weapons(building_description.weapons_health, tribe.name)
56 end
57 if (building_description.max_attack and building_description.min_attack) then
58 result = result .. h2(_"Attack Training")
59@@ -401,7 +400,7 @@
60 building_description.icon_name,
61 "tribes/workers/" .. tribe.name .. "/soldier/attack_level" .. (building_description.max_attack + 1) ..".png"})
62 result = result .. dependencies_training_food(building_description.food_attack)
63- result = result .. dependencies_training_weapons(building_description.weapons_attack)
64+ result = result .. dependencies_training_weapons(building_description.weapons_attack, tribe.name)
65 end
66 if (building_description.max_defense and building_description.min_defense) then
67 result = result .. h2(_"Defense Training")
68@@ -414,7 +413,7 @@
69 building_description.icon_name,
70 "tribes/workers/" .. tribe.name .. "/soldier/defense_level" .. (building_description.max_defense + 1) ..".png"})
71 result = result .. dependencies_training_food(building_description.food_defense)
72- result = result .. dependencies_training_weapons(building_description.weapons_defense)
73+ result = result .. dependencies_training_weapons(building_description.weapons_defense, tribe.name)
74 end
75 if (building_description.max_evade and building_description.min_evade) then
76 result = result .. h2(_"Evade Training")
77@@ -427,7 +426,7 @@
78 building_description.icon_name,
79 "tribes/workers/" .. tribe.name .. "/soldier/evade_level" .. (building_description.max_evade + 1) ..".png"})
80 result = result .. dependencies_training_food(building_description.food_evade)
81- result = result .. dependencies_training_weapons(building_description.weapons_evade)
82+ result = result .. dependencies_training_weapons(building_description.weapons_evade, tribe.name)
83 end
84 return result
85 end
86
87=== modified file 'data/tribes/scripting/help/ware_help.lua'
88--- data/tribes/scripting/help/ware_help.lua 2018-09-11 16:54:54 +0000
89+++ data/tribes/scripting/help/ware_help.lua 2019-04-07 07:50:32 +0000
90@@ -48,7 +48,7 @@
91 --
92 function ware_help_producers_string(tribe, ware_description)
93 local result = ""
94- for i, building in ipairs(ware_description.producers) do
95+ for i, building in ipairs(ware_description:producers(tribe.name)) do
96 if (tribe:has_building(building.name)) then
97 -- TRANSLATORS: Ware Encyclopedia: A building producing a ware
98 result = result .. h2(_"Producer")
99@@ -130,7 +130,7 @@
100 local consumers_string = ""
101 local consumers_amount = 0
102
103- for i, building in ipairs(ware_description.consumers) do
104+ for i, building in ipairs(ware_description:consumers(tribe.name)) do
105 if (tribe:has_building(building.name)) then
106 consumers_string = consumers_string .. dependencies({ware_description, building}, building.descname)
107 consumers_amount = consumers_amount + 1
108
109=== modified file 'src/scripting/lua_map.cc'
110--- src/scripting/lua_map.cc 2019-03-24 13:13:34 +0000
111+++ src/scripting/lua_map.cc 2019-04-07 07:50:32 +0000
112@@ -680,6 +680,14 @@
113 return result;
114 }
115
116+const Widelands::TribeDescr& get_tribe_descr(lua_State* L, const std::string& tribename) {
117+ if (!Widelands::tribe_exists(tribename)) {
118+ report_error(L, "Tribe '%s' does not exist", tribename.c_str());
119+ }
120+ const Tribes& tribes = get_egbase(L).tribes();
121+ return *tribes.get_tribe_descr(tribes.tribe_index(tribename));
122+}
123+
124 } // namespace
125
126 /*
127@@ -3046,12 +3054,12 @@
128 */
129 const char LuaWareDescription::className[] = "WareDescription";
130 const MethodType<LuaWareDescription> LuaWareDescription::Methods[] = {
131+ METHOD(LuaWareDescription, consumers),
132 METHOD(LuaWareDescription, is_construction_material),
133+ METHOD(LuaWareDescription, producers),
134 {nullptr, nullptr},
135 };
136 const PropertyType<LuaWareDescription> LuaWareDescription::Properties[] = {
137- PROP_RO(LuaWareDescription, consumers),
138- PROP_RO(LuaWareDescription, producers),
139 {nullptr, nullptr, nullptr},
140 };
141
142@@ -3075,25 +3083,35 @@
143 */
144
145 /* RST
146- .. attribute:: consumers
147+ .. method:: consumers(tribename)
148+
149+ :arg tribename: the name of the tribe that this ware gets checked for
150+ :type tribename: :class:`string`
151
152 (RO) An array with :class:`LuaBuildingDescription` with buildings that
153 need this ware for their production.
154 */
155-int LuaWareDescription::get_consumers(lua_State* L) {
156+int LuaWareDescription::consumers(lua_State* L) {
157+ if (lua_gettop(L) != 2) {
158+ report_error(L, "Takes only one argument.");
159+ }
160+ const Widelands::TribeDescr& tribe = get_tribe_descr(L, luaL_checkstring(L, 2));
161+
162 lua_newtable(L);
163 int index = 1;
164 for (const DescriptionIndex& building_index : get()->consumers()) {
165- lua_pushint32(L, index++);
166- upcasted_map_object_descr_to_lua(
167- L, get_egbase(L).tribes().get_building_descr(building_index));
168- lua_rawset(L, -3);
169+ if (tribe.has_building(building_index)) {
170+ lua_pushint32(L, index++);
171+ upcasted_map_object_descr_to_lua(
172+ L, get_egbase(L).tribes().get_building_descr(building_index));
173+ lua_rawset(L, -3);
174+ }
175 }
176 return 1;
177 }
178
179 /* RST
180- .. method:: is_construction_material
181+ .. method:: is_construction_material(tribename)
182
183 :arg tribename: the name of the tribe that this ware gets checked for
184 :type tribename: :class:`string`
185@@ -3114,19 +3132,29 @@
186 }
187
188 /* RST
189- .. attribute:: producers
190+ .. method:: producers(tribename)
191+
192+ :arg tribename: the name of the tribe that this ware gets checked for
193+ :type tribename: :class:`string`
194
195 (RO) An array with :class:`LuaBuildingDescription` with buildings that
196 can procude this ware.
197 */
198-int LuaWareDescription::get_producers(lua_State* L) {
199+int LuaWareDescription::producers(lua_State* L) {
200+ if (lua_gettop(L) != 2) {
201+ report_error(L, "Takes only one argument.");
202+ }
203+ const Widelands::TribeDescr& tribe = get_tribe_descr(L, luaL_checkstring(L, 2));
204+
205 lua_newtable(L);
206 int index = 1;
207 for (const DescriptionIndex& building_index : get()->producers()) {
208- lua_pushint32(L, index++);
209- upcasted_map_object_descr_to_lua(
210- L, get_egbase(L).tribes().get_building_descr(building_index));
211- lua_rawset(L, -3);
212+ if (tribe.has_building(building_index)) {
213+ lua_pushint32(L, index++);
214+ upcasted_map_object_descr_to_lua(
215+ L, get_egbase(L).tribes().get_building_descr(building_index));
216+ lua_rawset(L, -3);
217+ }
218 }
219 return 1;
220 }
221
222=== modified file 'src/scripting/lua_map.h'
223--- src/scripting/lua_map.h 2019-03-09 11:20:45 +0000
224+++ src/scripting/lua_map.h 2019-04-07 07:50:32 +0000
225@@ -564,13 +564,13 @@
226 /*
227 * Properties
228 */
229- int get_consumers(lua_State*);
230- int get_producers(lua_State*);
231
232 /*
233 * Lua methods
234 */
235+ int consumers(lua_State*);
236 int is_construction_material(lua_State*);
237+ int producers(lua_State*);
238
239 /*
240 * C methods
241
242=== modified file 'test/maps/lua_testsuite.wmf/scripting/immovables_descriptions.lua'
243--- test/maps/lua_testsuite.wmf/scripting/immovables_descriptions.lua 2018-11-12 08:46:18 +0000
244+++ test/maps/lua_testsuite.wmf/scripting/immovables_descriptions.lua 2019-04-07 07:50:32 +0000
245@@ -472,13 +472,23 @@
246 end
247
248 local ware_description = egbase:get_ware_description("coal")
249- assert_equal(true, find_building("barbarians_lime_kiln", ware_description.consumers))
250- assert_equal(true, find_building("empire_smelting_works", ware_description.consumers))
251- assert_equal(true, find_building("atlanteans_smelting_works", ware_description.consumers))
252- assert_equal(true, find_building("barbarians_warmill", ware_description.consumers))
253- assert_equal(true, find_building("barbarians_ax_workshop", ware_description.consumers))
254- assert_equal(true, find_building("barbarians_helmsmithy", ware_description.consumers))
255- assert_equal(false, find_building("atlanteans_crystalmine", ware_description.producers))
256+ assert_equal(true, find_building("barbarians_lime_kiln", ware_description:consumers("barbarians")))
257+ assert_equal(true, find_building("empire_smelting_works", ware_description:consumers("empire")))
258+ assert_equal(true, find_building("atlanteans_smelting_works", ware_description:consumers("atlanteans")))
259+ assert_equal(true, find_building("barbarians_warmill", ware_description:consumers("barbarians")))
260+ assert_equal(true, find_building("barbarians_ax_workshop", ware_description:consumers("barbarians")))
261+ assert_equal(true, find_building("barbarians_helmsmithy", ware_description:consumers("barbarians")))
262+ -- Building does not consume this
263+ assert_equal(false, find_building("barbarians_helmsmithy", ware_description:consumers("atlanteans")))
264+ -- Wrong tribe
265+ assert_equal(false, find_building("atlanteans_crystalmine", ware_description:consumers("atlanteans")))
266+
267+ -- Test when multiple tribes use the same ware
268+ ware_description = egbase:get_ware_description("helmet")
269+ assert_equal(true, find_building("barbarians_trainingcamp", ware_description:consumers("barbarians")))
270+ assert_equal(true, find_building("frisians_training_camp", ware_description:consumers("frisians")))
271+ assert_equal(1, #ware_description:consumers("barbarians"))
272+ assert_equal(1, #ware_description:consumers("frisians"))
273 end
274
275 function test_descr:test_icon_name()
276@@ -497,11 +507,21 @@
277 end
278
279 local ware_description = egbase:get_ware_description("coal")
280- assert_equal(true, find_building("empire_charcoal_kiln", ware_description.producers))
281- assert_equal(true, find_building("barbarians_coalmine_deeper", ware_description.producers))
282- assert_equal(true, find_building("barbarians_coalmine_deep", ware_description.producers))
283- assert_equal(true, find_building("atlanteans_coalmine", ware_description.producers))
284- assert_equal(false, find_building("atlanteans_crystalmine", ware_description.producers))
285+ assert_equal(true, find_building("empire_charcoal_kiln", ware_description:producers("empire")))
286+ assert_equal(true, find_building("barbarians_coalmine_deeper", ware_description:producers("barbarians")))
287+ assert_equal(true, find_building("barbarians_coalmine_deep", ware_description:producers("barbarians")))
288+ assert_equal(true, find_building("atlanteans_coalmine", ware_description:producers("atlanteans")))
289+ -- Building does not produce this
290+ assert_equal(false, find_building("atlanteans_crystalmine", ware_description:producers("atlanteans")))
291+ -- Wrong tribe
292+ assert_equal(false, find_building("atlanteans_coalmine", ware_description:producers("barbarians")))
293+
294+ -- Test when multiple tribes use the same ware
295+ ware_description = egbase:get_ware_description("helmet")
296+ assert_equal(true, find_building("barbarians_helmsmithy", ware_description:producers("barbarians")))
297+ assert_equal(true, find_building("frisians_armor_smithy_small", ware_description:producers("frisians")))
298+ assert_equal(1, #ware_description:producers("barbarians"))
299+ assert_equal(1, #ware_description:producers("frisians"))
300 end
301
302 function test_descr:is_construction_material()

Subscribers

People subscribed via source and target branches

to status/vote changes: