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

Proposed by TiborB
Status: Merged
Merged at revision: 7398
Proposed branch: lp:~widelands-dev/widelands/bug-1380286
Merge into: lp:widelands
Diff against target: 161 lines (+80/-9)
4 files modified
src/scripting/lua_map.cc (+54/-5)
src/scripting/lua_map.h (+3/-1)
test/maps/lua_testsuite.wmf/scripting/flag.lua (+22/-1)
test/maps/lua_testsuite.wmf/scripting/game.lua (+1/-2)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1380286
Reviewer Review Type Date Requested Status
SirVer Approve
TiborB Needs Resubmitting
Review via email: mp+242975@code.launchpad.net

Description of the change

Adding 3 functions for LUA for flag object to list roads, building (if any) and verify whether the flag is connected to any warehouse

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

Can you please add some regression tests as well?

They should go in test/maps/lua_testsuite.wmf/scripting/flag.lua

You can run these tests directly without having to run the whole test suite:

./widelands --scenario=test/maps/lua_testsuite.wmf

I have also spotted a NOCOM comment in the diff - please grep to make sure you have resolved these, unless they contain a codereview question.

Once this is done, I'll have a closer look :)

Revision history for this message
TiborB (tiborb95) wrote :

LUA test done (not pushed up yet) - one issue, though.

It is not trivial to count roads returned as '#field.roads' returns 0, though when accessing a particular road via field.roads.tl works and shows the road is there. So perhaps if convenient, I can add also function field.roads_count returning number of roads. What do you think?

Revision history for this message
GunChleoc (gunchleoc) wrote :

My best guess is that #field.roads doesn't work because you have an associative array rather than an indexed one in your table.

Is there a particular reason you need to count roads in your test? You could test for the 6 directions individually if they're there or not.

Revision history for this message
TiborB (tiborb95) wrote :

Counting of roads can be used to say whether it is an end or "middle" of road. The bug/request was reported by wl-zocker, if he is fine not having such explicit count, I dont mind

I made some changes to the code, as you wanted

Revision history for this message
wl-zocker (wl-zocker) wrote :

I think counting the roads is not necessary. Furthermore, it could be implemented in lua itsself quite easily:
local result = 0
for k,v in pairs(flag.roads) do
   result = result + 1
end
return result
or something along these lines.

The only thing that remains is to use this function in the first tutorial in two or three places (when connecting the quarries). I do not have much time currently, so either one of you does that (there are some comments, I think), or I implement this some day.

Revision history for this message
SirVer (sirver) wrote :

I added a bunch of NOCOM(#codereview) comments in the code. I disagree with the has_warehouse method.

Also you added a bunch of method declarations in lua_bases.h, but did not implement these - I removed them, but that was sloppy. Do you look over the diff when you send something for review?

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

All this is new for me, so it is easily possible I declared something on wrong place or so. As long as it compiles and worked.

Do you really want to reimplement all that is available in C to lua? Lua scripting is used quite a little so I am not sure it is worth the effort.

But all right, I will look at it, and I prefer option 1.

Revision history for this message
SirVer (sirver) wrote :

> Do you really want to reimplement all that is available in C to lua?

No, only what makes sense. Asking a flag if it knows about a warehouse somewhere in its road network makes no sense to me. Option 1) is clearer, but it is also more work.

I would suggest removing get_warehouse from this branch and start a new one with this change.

Revision history for this message
TiborB (tiborb95) wrote :

As we are not in hurry, I will unpropose merge and add that economy staff

Revision history for this message
TiborB (tiborb95) wrote :

I did not unpropose because it would delete all comments and this is what I dont want...

Revision history for this message
GunChleoc (gunchleoc) wrote :

I'd say just do the roads stuff in this branch and merge it.

The warehouse thing is separate code, so it's better to have 2 branches and keep the changes smaller.

Revision history for this message
TiborB (tiborb95) wrote :

alright, I removed warehouse stuff. There are some NOCOM comments to look at

It seems it would be too difficult for me to add new class like LuaEconomy, but debug_economy atribute is working for buildings and flags so you can test like this:
flag_or_building.debug_economy==warehouse.debug_economy

Revision history for this message
TiborB (tiborb95) wrote :

Hey, I am resubmitting this - because it just waits for review...

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

I have pushed a code review. Just split up the tests, and we're good to go.

Revision history for this message
SirVer (sirver) wrote :

I looked at this again right now and went ahead and merged.

I thought it was a good idea to split the tests into two separates, but it had some code duplication that I decided in the end it is better to keep the test together. I just made the tests a bit tighter by directly comparing the immovables instead of just != nil.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/scripting/lua_map.cc'
--- src/scripting/lua_map.cc 2015-02-09 05:57:08 +0000
+++ src/scripting/lua_map.cc 2015-02-12 21:03:16 +0000
@@ -2103,13 +2103,15 @@
2103 MapObject * me = m_get_or_zero(egbase);2103 MapObject * me = m_get_or_zero(egbase);
2104 MapObject * you = other->m_get_or_zero(egbase);2104 MapObject * you = other->m_get_or_zero(egbase);
21052105
2106 // Both objects are destroyed: they are equal2106 // Both objects are destroyed (nullptr) or equal: they are equal
2107 if (me == you) lua_pushboolean(L, true);2107 if (me == you) {
2108 else if (!me || !you) // One of the objects is destroyed: they are distinct2108 lua_pushboolean(L, true);
2109 } else if (me == nullptr || you == nullptr) { // One of the objects is destroyed: they are distinct
2109 lua_pushboolean(L, false);2110 lua_pushboolean(L, false);
2110 else // Compare them2111 } else { // Compare their serial number.
2111 lua_pushboolean2112 lua_pushboolean
2112 (L, other->get(L, egbase)->serial() == get(L, egbase)->serial());2113 (L, other->get(L, egbase)->serial() == get(L, egbase)->serial());
2114 }
21132115
2114 return 1;2116 return 1;
2115}2117}
@@ -2343,6 +2345,8 @@
2343 {nullptr, nullptr},2345 {nullptr, nullptr},
2344};2346};
2345const PropertyType<LuaFlag> LuaFlag::Properties[] = {2347const PropertyType<LuaFlag> LuaFlag::Properties[] = {
2348 PROP_RO(LuaFlag, roads),
2349 PROP_RO(LuaFlag, building),
2346 {nullptr, nullptr, nullptr},2350 {nullptr, nullptr, nullptr},
2347};2351};
23482352
@@ -2352,7 +2356,52 @@
2352 PROPERTIES2356 PROPERTIES
2353 ==========================================================2357 ==========================================================
2354 */2358 */
23552359/* RST
2360 .. attribute:: roads
2361
2362 (RO) Array of roads leading to the flag. Directions
2363 can be tr,r,br,bl,l and tl
2364
2365 :returns: The array of 'direction:road', if any
2366*/
2367int LuaFlag::get_roads(lua_State * L) {
2368
2369 const std::vector<std::string> directions = {"tr", "r", "br", "bl", "l", "tl"};
2370
2371 lua_newtable(L);
2372
2373 EditorGameBase & egbase = get_egbase(L);
2374 Flag * f = get(L, egbase);
2375
2376 for (uint32_t i = 1; i <= 6; i++){
2377 if (f->get_road(i) != nullptr) {
2378 lua_pushstring(L, directions.at(i - 1));
2379 upcasted_map_object_to_lua(L, f->get_road(i));
2380 lua_rawset(L, -3);
2381 }
2382 }
2383 return 1;
2384}
2385
2386/* RST
2387 .. attribute:: building
2388
2389 (RO) building belonging to the flag
2390*/
2391int LuaFlag::get_building(lua_State * L) {
2392
2393 EditorGameBase & egbase = get_egbase(L);
2394 Flag * f = get(L, egbase);
2395
2396 PlayerImmovable * building = f->get_building();
2397 if (!building) {
2398 return 0;
2399 } else {
2400 upcasted_map_object_to_lua(L, building);
2401 }
2402 return 1;
2403
2404}
2356/*2405/*
2357 ==========================================================2406 ==========================================================
2358 LUA METHODS2407 LUA METHODS
23592408
=== modified file 'src/scripting/lua_map.h'
--- src/scripting/lua_map.h 2015-01-31 16:03:59 +0000
+++ src/scripting/lua_map.h 2015-02-12 21:03:16 +0000
@@ -596,13 +596,15 @@
596 /*596 /*
597 * Properties597 * Properties
598 */598 */
599599 int get_roads(lua_State * L);
600 int get_building(lua_State * L);
600 /*601 /*
601 * Lua Methods602 * Lua Methods
602 */603 */
603 int set_wares(lua_State *);604 int set_wares(lua_State *);
604 int get_wares(lua_State *);605 int get_wares(lua_State *);
605606
607
606 /*608 /*
607 * C Methods609 * C Methods
608 */610 */
609611
=== modified file 'test/maps/lua_testsuite.wmf/scripting/flag.lua'
--- test/maps/lua_testsuite.wmf/scripting/flag.lua 2014-07-28 18:30:11 +0000
+++ test/maps/lua_testsuite.wmf/scripting/flag.lua 2015-02-12 21:03:16 +0000
@@ -12,7 +12,10 @@
12 self.f = player1:place_flag(self.field, 1)12 self.f = player1:place_flag(self.field, 1)
13end13end
14function flag_tests:teardown()14function flag_tests:teardown()
15 pcall(self.f.remove, self.f)15 pcall(function() self.f:remove() end)
16 pcall(function() self.f1:remove() end)
17 pcall(function() self.f2:remove() end)
18 pcall(function() self.w:remove() end)
16end19end
1720
18function flag_tests:test_name()21function flag_tests:test_name()
@@ -139,3 +142,21 @@
139 self.f:get_wares{"meat", "balloon"}142 self.f:get_wares{"meat", "balloon"}
140 end)143 end)
141end144end
145
146-- ===================
147-- buildings and roads
148-- ===================
149function flag_tests:roads_and_buildings_test()
150 local field = map:get_field(4,14)
151 self.w = player1:place_building("warehouse", field)
152 self.f1 = self.w.flag
153 local road = player1:place_road(self.f1, "br", "br", "br")
154 self.f2 = self.f1.fields[1].brn.brn.brn.immovable
155
156 assert_equal(self.f2.roads.tl, road)
157 assert_equal(self.f2.roads.tl.start_flag, self.f1)
158 assert_equal(self.f2.roads.tl.end_flag, self.f2)
159 assert_equal(self.f1.building, self.w)
160 assert_nil(self.f2.building)
161 assert_equal(self.f2.debug_economy, self.w.debug_economy)
162end
142163
=== modified file 'test/maps/lua_testsuite.wmf/scripting/game.lua'
--- test/maps/lua_testsuite.wmf/scripting/game.lua 2014-01-12 19:06:22 +0000
+++ test/maps/lua_testsuite.wmf/scripting/game.lua 2015-02-12 21:03:16 +0000
@@ -1,5 +1,5 @@
1-- ===================1-- ===================
2-- Tests for the game 2-- Tests for the game
3-- ===================3-- ===================
44
5test_game = lunit.TestCase("Game functions test")5test_game = lunit.TestCase("Game functions test")
@@ -10,4 +10,3 @@
10function test_game:test_no_editor_class()10function test_game:test_no_editor_class()
11 assert_equal(nil, wl.Editor)11 assert_equal(nil, wl.Editor)
12end12end
13

Subscribers

People subscribed via source and target branches

to status/vote changes: