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

Proposed by TiborB
Status: Merged
Merged at revision: 7301
Proposed branch: lp:~widelands-dev/widelands/bug-1281823
Merge into: lp:widelands
Diff against target: 412 lines (+73/-41)
17 files modified
src/editor/map_generator.cc (+1/-1)
src/editor/tools/editor_decrease_resources_tool.cc (+2/-2)
src/editor/tools/editor_increase_resources_tool.cc (+2/-2)
src/editor/tools/editor_set_resources_tool.cc (+4/-4)
src/logic/field.h (+4/-4)
src/logic/findnode.cc (+2/-2)
src/logic/map.cc (+2/-2)
src/logic/production_program.cc (+1/-1)
src/logic/worker.cc (+3/-3)
src/map_io/map_resources_packet.cc (+2/-2)
src/map_io/s2map.cc (+1/-1)
src/scripting/lua_map.cc (+18/-1)
src/scripting/lua_map.h (+1/-0)
src/wui/game_debug_ui.cc (+2/-2)
test/maps/lua_testsuite.wmf/scripting/cfield.lua (+7/-8)
test/maps/lua_testsuite.wmf/scripting/efield.lua (+12/-4)
test/maps/lua_testsuite.wmf/scripting/gfield.lua (+9/-2)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1281823
Reviewer Review Type Date Requested Status
SirVer Approve
Review via email: mp+242837@code.launchpad.net

Description of the change

The fix is very trivial - one line was added

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

Could you add a test in test/maps/lua_testsuite.wmf/scripting/efield.lua and make sure that it did not pass before and passes after your fix?

You can run the tests with ./regression_test.py -b ../build/debug/src/widelands -r lua_testsuite

Revision history for this message
TiborB (tiborb95) wrote :

Well, I spent more time on that test then fixing the bug itself. I had even add another function to make the test possible...

Revision history for this message
SirVer (sirver) wrote :

LGTM, a couple of nits. Could you fix them.

Also starting_res_amount is a bit cumbersome. What do you guys think of renaming it to initial_resource_amount throughout the codebase (i.e. also in cpp?).

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

+1 to renaming. Do we want to do that here or in a separate bug? I think doing it here might be a good idea, so we will have it in before we expose it to the Lua interface.

Revision history for this message
TiborB (tiborb95) wrote :

no problem for me to rename

@SirVer - see answer to one of your comments below in diff.

Revision history for this message
SirVer (sirver) wrote :

Do the rename here I'd say.

> I dont understand a meaning of this comment. Why should we care that resource_amount!=starting_resource_amount here. In fact they will probably be equal. Depending on a map used for testing.

I would then suggest that you test one more value and also test the absolute value:

self.f.resource_amount=10
assert_equal(self.f.starting_resource_amount, self.f.resource_amount)
assert_equal(10, self.f.starting_resource_amount)

self.f.resource_amount=20
assert_equal(self.f.starting_resource_amount, self.f.resource_amount)
assert_equal(20, self.f.starting_resource_amount)

Revision history for this message
TiborB (tiborb95) wrote :

initial_res_amount ? - the difference is so small

I will add the test, the suggestion is reasonable :)

Revision history for this message
TiborB (tiborb95) wrote :

@SirVer

so should I remove
 :see also: :attr:`resource`
line? There are more such/similar lines in the lua_map.cc though.

Revision history for this message
SirVer (sirver) wrote :

> so should I remove :see also: :attr:`resource`

I thought it would not work. Could you doublecheck that the current occurences are working in https://wl.widelands.org/docs/wl/ ? If so, then leave it in. If not, please take it out.

Revision history for this message
TiborB (tiborb95) wrote :

It works. It is visible in wiki and link also works

Revision history for this message
TiborB (tiborb95) wrote :

@SirVer - I changed the test as you asked

Revision history for this message
SirVer (sirver) wrote :

great. only the rename from starting_ to initial_ and this is good to go.

Revision history for this message
TiborB (tiborb95) wrote :

renaming done. I did recursive renaming with sed so quite a lot files has been changed.

BTW: look at the 'Description of the Change' now, funny :) Or terrible overhead I can say :)

Revision history for this message
SirVer (sirver) wrote :

You missed some :). See inline comments.

review: Needs Fixing
Revision history for this message
TiborB (tiborb95) wrote :

alright, next try

Revision history for this message
SirVer (sirver) wrote :

You missed one rename :). I did it for you and merged.

review: Approve
Revision history for this message
SirVer (sirver) wrote :

Spoke to early. The fix is flawed - it also changes initial_res_amount in a game. That leads to wrong results if a scenario changes a fields resource amount. I added a failing test.

review: Needs Fixing
Revision history for this message
TiborB (tiborb95) wrote :

all right to have common design for both, I the new design will be: change the initial_res_amount to res_amount only if initial_res_amount would be lower then new value of res_amount
Agreed?

Revision history for this message
SirVer (sirver) wrote :

No, in the editor it is correct to always change the initial_res_amount too - there is no difference between the too there. And in game you should never touch that. What happens when amount > initial_amount I do not know but there is no reason to change the value if the scripter wants exactly that.

So you will need to check if you are in the editor or in game.

Revision history for this message
TiborB (tiborb95) wrote :

"What happens when amount > initial_amount" - I dont think is it good aproach to allow such situation and in addition, there is no set_initial_res_amount available via LUA - but I will change it, no problem...

Revision history for this message
TiborB (tiborb95) wrote :

@SirVer - I changed it. Test works as well.

Revision history for this message
SirVer (sirver) wrote :

Looks good now. Merged.

> "What happens when amount > initial_amount" - I dont think is it good aproach to allow such situation and in addition, there is no set_initial_res_amount available via LUA - but I will change it, no problem...

I understand what you say. If a scripter sets res > initial that could be bad for the engine. But then again, why should initial be changed if somebody changes res? initial is the value at the beginning of the game, it is not the max value. Why should the current value not be higher than the initial value? For fish it could even make logical sense. So I think your implementation is correct - let's see if it makes any trouble.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/editor/map_generator.cc'
2--- src/editor/map_generator.cc 2014-09-20 09:37:47 +0000
3+++ src/editor/map_generator.cc 2014-12-03 20:14:08 +0000
4@@ -138,7 +138,7 @@
5 res_val /= 3;
6 if (editor_change_resource_tool_callback(fc, map_, world, res_idx)) {
7 fc.field->set_resources(res_idx, res_val);
8- fc.field->set_starting_res_amount(res_val);
9+ fc.field->set_initial_res_amount(res_val);
10 }
11 };
12
13
14=== modified file 'src/editor/tools/editor_decrease_resources_tool.cc'
15--- src/editor/tools/editor_decrease_resources_tool.cc 2014-09-20 09:37:47 +0000
16+++ src/editor/tools/editor_decrease_resources_tool.cc 2014-12-03 20:14:08 +0000
17@@ -66,10 +66,10 @@
18 map.overlay_manager().remove_overlay(mr.location(), pic);
19 if (!amount) {
20 mr.location().field->set_resources(0, 0);
21- mr.location().field->set_starting_res_amount(0);
22+ mr.location().field->set_initial_res_amount(0);
23 } else {
24 mr.location().field->set_resources(args.cur_res, amount);
25- mr.location().field->set_starting_res_amount(amount);
26+ mr.location().field->set_initial_res_amount(amount);
27 // set new overlay
28 str = world.get_resource(args.cur_res)->get_editor_pic(amount);
29 pic = g_gr->images().get(str);
30
31=== modified file 'src/editor/tools/editor_increase_resources_tool.cc'
32--- src/editor/tools/editor_increase_resources_tool.cc 2014-09-20 09:37:47 +0000
33+++ src/editor/tools/editor_increase_resources_tool.cc 2014-12-03 20:14:08 +0000
34@@ -121,10 +121,10 @@
35
36 if (!amount) {
37 mr.location().field->set_resources(0, 0);
38- mr.location().field->set_starting_res_amount(0);
39+ mr.location().field->set_initial_res_amount(0);
40 } else {
41 mr.location().field->set_resources(args.cur_res, amount);
42- mr.location().field->set_starting_res_amount(amount);
43+ mr.location().field->set_initial_res_amount(amount);
44 // set new overlay
45 pic = g_gr->images().get
46 (world.get_resource(args.cur_res)->get_editor_pic(amount));
47
48=== modified file 'src/editor/tools/editor_set_resources_tool.cc'
49--- src/editor/tools/editor_set_resources_tool.cc 2014-09-20 09:37:47 +0000
50+++ src/editor/tools/editor_set_resources_tool.cc 2014-12-03 20:14:08 +0000
51@@ -63,10 +63,10 @@
52
53 if (!amount) {
54 mr.location().field->set_resources(0, 0);
55- mr.location().field->set_starting_res_amount(0);
56+ mr.location().field->set_initial_res_amount(0);
57 } else {
58 mr.location().field->set_resources(args.cur_res, amount);
59- mr.location().field->set_starting_res_amount(amount);
60+ mr.location().field->set_initial_res_amount(amount);
61 // set new overlay
62 pic =
63 g_gr->images().get(world.get_resource(args.cur_res)->get_editor_pic(amount));
64@@ -107,10 +107,10 @@
65
66 if (!amount) {
67 mr.location().field->set_resources(0, 0);
68- mr.location().field->set_starting_res_amount(0);
69+ mr.location().field->set_initial_res_amount(0);
70 } else {
71 mr.location().field->set_resources(*it, amount);
72- mr.location().field->set_starting_res_amount(amount);
73+ mr.location().field->set_initial_res_amount(amount);
74 // set new overlay
75 pic = g_gr->images().get(world.get_resource(*it)->get_editor_pic(amount));
76 overlay_manager.register_overlay(mr.location(), pic, 4);
77
78=== modified file 'src/logic/field.h'
79--- src/logic/field.h 2014-09-14 11:31:58 +0000
80+++ src/logic/field.h 2014-12-03 20:14:08 +0000
81@@ -124,7 +124,7 @@
82 OwnerInfoAndSelectionsType owner_info_and_selections;
83
84 ResourceIndex m_resources; ///< Resource type on this field, if any
85- uint8_t m_starting_res_amount; ///< Initial amount of m_resources
86+ uint8_t m_initial_res_amount; ///< Initial amount of m_resources
87 uint8_t m_res_amount; ///< Current amount of m_resources
88
89 Terrains terrains;
90@@ -214,11 +214,11 @@
91 }
92
93 // TODO(unknown): This should take uint8_t
94- void set_starting_res_amount(int32_t const amount) {
95- m_starting_res_amount = amount;
96+ void set_initial_res_amount(int32_t const amount) {
97+ m_initial_res_amount = amount;
98 }
99 // TODO(unknown): This should return uint8_t
100- int32_t get_starting_res_amount() const {return m_starting_res_amount;}
101+ int32_t get_initial_res_amount() const {return m_initial_res_amount;}
102
103 /// \note you must reset this field's + neighbor's brightness when you
104 /// change the height. Map::change_height does this. This function is not
105
106=== modified file 'src/logic/findnode.cc'
107--- src/logic/findnode.cc 2014-11-28 16:40:55 +0000
108+++ src/logic/findnode.cc 2014-12-03 20:14:08 +0000
109@@ -126,7 +126,7 @@
110 if (m_resource != coord.field->get_resources()) {
111 return false;
112 }
113- if (coord.field->get_resources_amount() < coord.field->get_starting_res_amount()) {
114+ if (coord.field->get_resources_amount() < coord.field->get_initial_res_amount()) {
115 return true;
116 }
117 for (Direction dir = FIRST_DIRECTION; dir <= LAST_DIRECTION; ++dir) {
118@@ -134,7 +134,7 @@
119 if
120 (m_resource == neighb.field->get_resources()
121 &&
122- neighb.field->get_resources_amount() < neighb.field->get_starting_res_amount())
123+ neighb.field->get_resources_amount() < neighb.field->get_initial_res_amount())
124 {
125 return true;
126 }
127
128=== modified file 'src/logic/map.cc'
129--- src/logic/map.cc 2014-10-29 06:41:10 +0000
130+++ src/logic/map.cc 2014-12-03 20:14:08 +0000
131@@ -272,10 +272,10 @@
132
133 if (res == -1 || !amount) {
134 f.field->set_resources(0, 0);
135- f.field->set_starting_res_amount(0);
136+ f.field->set_initial_res_amount(0);
137 } else {
138 f.field->set_resources(res, amount);
139- f.field->set_starting_res_amount(amount);
140+ f.field->set_initial_res_amount(amount);
141 }
142
143 }
144
145=== modified file 'src/logic/production_program.cc'
146--- src/logic/production_program.cc 2014-11-22 11:21:12 +0000
147+++ src/logic/production_program.cc 2014-12-03 20:14:08 +0000
148@@ -1254,7 +1254,7 @@
149 uint8_t fres = mr.location().field->get_resources();
150 uint32_t amount = mr.location().field->get_resources_amount();
151 uint32_t start_amount =
152- mr.location().field->get_starting_res_amount();
153+ mr.location().field->get_initial_res_amount();
154
155 if (fres != m_resource) {
156 amount = 0;
157
158=== modified file 'src/logic/worker.cc'
159--- src/logic/worker.cc 2014-11-30 18:49:38 +0000
160+++ src/logic/worker.cc 2014-12-03 20:14:08 +0000
161@@ -235,7 +235,7 @@
162 do {
163 uint8_t fres = mr.location().field->get_resources();
164 uint32_t amount =
165- mr.location().field->get_starting_res_amount() -
166+ mr.location().field->get_initial_res_amount() -
167 mr.location().field->get_resources_amount ();
168
169 // In the future, we might want to support amount = 0 for
170@@ -278,7 +278,7 @@
171 continue;
172
173 uint32_t amount =
174- mr.location().field->get_starting_res_amount() -
175+ mr.location().field->get_initial_res_amount() -
176 mr.location().field->get_resources_amount ();
177
178 pick -= 8 * amount;
179@@ -288,7 +288,7 @@
180 --amount;
181
182 mr.location().field->set_resources
183- (res, mr.location().field->get_starting_res_amount() - amount);
184+ (res, mr.location().field->get_initial_res_amount() - amount);
185 break;
186 }
187 } while (mr.advance(map));
188
189=== modified file 'src/map_io/map_resources_packet.cc'
190--- src/map_io/map_resources_packet.cc 2014-09-20 09:37:47 +0000
191+++ src/map_io/map_resources_packet.cc 2014-12-03 20:14:08 +0000
192@@ -86,7 +86,7 @@
193 if (0xa < set_id)
194 throw "Unknown resource in map file. It is not in world!\n";
195 map[Coords(x, y)].set_resources(set_id, set_amount);
196- map[Coords(x, y)].set_starting_res_amount(set_start_amount);
197+ map[Coords(x, y)].set_initial_res_amount(set_start_amount);
198 }
199 }
200 } else
201@@ -133,7 +133,7 @@
202 const Field & f = map[Coords(x, y)];
203 int32_t res = f.get_resources ();
204 int32_t const amount = f.get_resources_amount ();
205- int32_t const start_amount = f.get_starting_res_amount();
206+ int32_t const start_amount = f.get_initial_res_amount();
207 if (!amount)
208 res = 0;
209 fw.unsigned_8(res);
210
211=== modified file 'src/map_io/s2map.cc'
212--- src/map_io/s2map.cc 2014-10-04 09:40:18 +0000
213+++ src/map_io/s2map.cc 2014-12-03 20:14:08 +0000
214@@ -645,7 +645,7 @@
215 const int32_t real_amount = static_cast<int32_t>
216 (2.86 * static_cast<float>(amount));
217 f->set_resources(nres, real_amount);
218- f->set_starting_res_amount(real_amount);
219+ f->set_initial_res_amount(real_amount);
220 }
221
222
223
224=== modified file 'src/scripting/lua_map.cc'
225--- src/scripting/lua_map.cc 2014-11-30 18:49:38 +0000
226+++ src/scripting/lua_map.cc 2014-12-03 20:14:08 +0000
227@@ -3502,6 +3502,7 @@
228 PROP_RO(LuaField, viewpoint_y),
229 PROP_RW(LuaField, resource),
230 PROP_RW(LuaField, resource_amount),
231+ PROP_RO(LuaField, initial_resource_amount),
232 PROP_RO(LuaField, claimers),
233 PROP_RO(LuaField, owner),
234 {nullptr, nullptr, nullptr},
235@@ -3661,10 +3662,26 @@
236 report_error(L, "Illegal amount: %i, must be >= 0 and <= %i", amount, max_amount);
237
238 field->set_resources(res, amount);
239+ //in editor, reset also initial amount
240+ EditorGameBase & egbase = get_egbase(L);
241+ upcast(Game, game, &egbase);
242+ if (!game) {
243+ field->set_initial_res_amount(amount);
244+ }
245
246 return 0;
247 }
248-
249+/* RST
250+ .. attribute:: initial_resource_amount
251+
252+ (RO) Starting value of resource. It is set be resource_amount
253+
254+ :see also: :attr:`resource`
255+*/
256+int LuaField::get_initial_resource_amount(lua_State * L) {
257+ lua_pushuint32(L, fcoords(L).field->get_initial_res_amount());
258+ return 1;
259+}
260 /* RST
261 .. attribute:: immovable
262
263
264=== modified file 'src/scripting/lua_map.h'
265--- src/scripting/lua_map.h 2014-09-14 11:31:58 +0000
266+++ src/scripting/lua_map.h 2014-12-03 20:14:08 +0000
267@@ -942,6 +942,7 @@
268 int set_resource(lua_State *);
269 int get_resource_amount(lua_State *);
270 int set_resource_amount(lua_State *);
271+ int get_initial_resource_amount(lua_State *);
272 int get_claimers(lua_State *);
273 int get_owner(lua_State *);
274
275
276=== modified file 'src/wui/game_debug_ui.cc'
277--- src/wui/game_debug_ui.cc 2014-11-30 18:49:38 +0000
278+++ src/wui/game_debug_ui.cc 2014-12-03 20:14:08 +0000
279@@ -360,12 +360,12 @@
280 {
281 Widelands::ResourceIndex ridx = m_coords.field->get_resources();
282 int ramount = m_coords.field->get_resources_amount();
283- int startingAmount = m_coords.field->get_starting_res_amount();
284+ int initial_amount = m_coords.field->get_initial_res_amount();
285
286 str += (boost::format("Resource: %s\n")
287 % ibase().egbase().world().get_resource(ridx)->name().c_str()).str();
288
289- str += (boost::format(" Amount: %i/%i\n") % ramount % startingAmount).str();
290+ str += (boost::format(" Amount: %i/%i\n") % ramount % initial_amount).str();
291 }
292
293 m_ui_field.set_text(str.c_str());
294
295=== modified file 'test/maps/lua_testsuite.wmf/scripting/cfield.lua'
296--- test/maps/lua_testsuite.wmf/scripting/cfield.lua 2014-01-12 19:06:22 +0000
297+++ test/maps/lua_testsuite.wmf/scripting/cfield.lua 2014-12-03 20:14:08 +0000
298@@ -1,5 +1,5 @@
299 -- =======================================================================
300--- Field test
301+-- Field test
302 -- =======================================================================
303 field_tests = lunit.TestCase("Field access")
304 function field_tests:test_access()
305@@ -30,8 +30,8 @@
306 assert_error("y is negativ", function() map:get_field(25, -12) end)
307 end
308 function field_tests:test_direct_change_impossible()
309- assert_error("c.x should be read only", function() c.x = 12 end)
310- assert_error("c.y should be read only", function() c.y = 12 end)
311+ assert_error("c.x should be read only", function() c.x = 12 end)
312+ assert_error("c.y should be read only", function() c.y = 12 end)
313 end
314 function field_tests:test_hash()
315 assert_equal("25_40", map:get_field(25,40).__hash)
316@@ -158,7 +158,7 @@
317 end
318
319 -- ==========
320--- Resources
321+-- Resources
322 -- ==========
323 field_resources_tests = lunit.TestCase("Field resources test")
324 function field_resources_tests:setup()
325@@ -172,7 +172,7 @@
326 self.f.terr = self._terr
327 self.f.terd = self._terd
328 self.f.resource = self._res
329- self.f.resource_amount = self._amount
330+ self.f.resource_amount = self._amount
331 end
332
333 function field_resources_tests:test_set_resource_amount()
334@@ -198,7 +198,7 @@
335 end
336
337 -- ==========
338--- Fieldcaps
339+-- Fieldcaps
340 -- ==========
341 field_caps_tests = lunit.TestCase("Field caps tests")
342
343@@ -305,7 +305,7 @@
344 local s2 = p2:place_building("sentry", f, false, true)
345 s2:set_soldiers({0,0,0,0}, 1)
346 self.pis[#self.pis + 1] = f.brn.immovable
347-
348+
349 -- Still owned by first player, second player has same military influence
350 local o = map:get_field(10,10).claimers
351 assert_equal(2, #o)
352@@ -349,4 +349,3 @@
353 assert_equal(0, #f.claimers)
354 assert_equal(player1, map:get_field(15,37).owner)
355 end
356-
357
358=== modified file 'test/maps/lua_testsuite.wmf/scripting/efield.lua'
359--- test/maps/lua_testsuite.wmf/scripting/efield.lua 2014-01-12 19:06:22 +0000
360+++ test/maps/lua_testsuite.wmf/scripting/efield.lua 2014-12-03 20:14:08 +0000
361@@ -1,9 +1,9 @@
362 -- ==================================================
363--- Tests for Fields that are only useful in the Editor
364+-- Tests for Fields that are only useful in the Editor
365 -- ==================================================
366
367 function field_resources_tests:test_default_resource_in_editor()
368- -- The editor doesn't bother to set the default resources on the map.
369+ -- The editor doesn't bother to set the default resources on the map.
370 -- The user might want to overwrite them anyways. It therefore sets
371 -- the resource of all fields to the first in the world and the amount
372 -- to zero.
373@@ -11,5 +11,13 @@
374 assert_equal(0, self.f.resource_amount)
375 end
376
377-
378-
379+function field_resources_tests:test_initial_resource_in_editor()
380+ -- making sure that (set_) resource_amount sets also starting resource in the editor
381+ assert_equal("coal", self.f.resource)
382+ self.f.resource_amount=10
383+ assert_equal(self.f.initial_resource_amount, self.f.resource_amount)
384+ assert_equal(10, self.f.initial_resource_amount)
385+ self.f.resource_amount=20
386+ assert_equal(self.f.initial_resource_amount, self.f.resource_amount)
387+ assert_equal(20, self.f.initial_resource_amount)
388+end
389
390=== modified file 'test/maps/lua_testsuite.wmf/scripting/gfield.lua'
391--- test/maps/lua_testsuite.wmf/scripting/gfield.lua 2014-01-12 19:06:22 +0000
392+++ test/maps/lua_testsuite.wmf/scripting/gfield.lua 2014-12-03 20:14:08 +0000
393@@ -1,5 +1,5 @@
394 -- ==================================================
395--- Tests for Fields that are only useful in the Game
396+-- Tests for Fields that are only useful in the Game
397 -- ==================================================
398
399 function field_resources_tests:test_default_resource_in_game()
400@@ -11,4 +11,11 @@
401
402
403
404-
405+function field_resources_tests:test_initial_resources_in_game()
406+ -- Changing a resource in game should not change the
407+ -- initial_resources.
408+ assert_equal("water", self.f.resource)
409+ assert_equal(5, self.f.initial_resource_amount)
410+ self.f.resource_amount = 1
411+ assert_equal(5, self.f.initial_resource_amount)
412+end

Subscribers

People subscribed via source and target branches

to status/vote changes: