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
1=== modified file 'src/scripting/lua_map.cc'
2--- src/scripting/lua_map.cc 2015-02-09 05:57:08 +0000
3+++ src/scripting/lua_map.cc 2015-02-12 21:03:16 +0000
4@@ -2103,13 +2103,15 @@
5 MapObject * me = m_get_or_zero(egbase);
6 MapObject * you = other->m_get_or_zero(egbase);
7
8- // Both objects are destroyed: they are equal
9- if (me == you) lua_pushboolean(L, true);
10- else if (!me || !you) // One of the objects is destroyed: they are distinct
11+ // Both objects are destroyed (nullptr) or equal: they are equal
12+ if (me == you) {
13+ lua_pushboolean(L, true);
14+ } else if (me == nullptr || you == nullptr) { // One of the objects is destroyed: they are distinct
15 lua_pushboolean(L, false);
16- else // Compare them
17+ } else { // Compare their serial number.
18 lua_pushboolean
19 (L, other->get(L, egbase)->serial() == get(L, egbase)->serial());
20+ }
21
22 return 1;
23 }
24@@ -2343,6 +2345,8 @@
25 {nullptr, nullptr},
26 };
27 const PropertyType<LuaFlag> LuaFlag::Properties[] = {
28+ PROP_RO(LuaFlag, roads),
29+ PROP_RO(LuaFlag, building),
30 {nullptr, nullptr, nullptr},
31 };
32
33@@ -2352,7 +2356,52 @@
34 PROPERTIES
35 ==========================================================
36 */
37-
38+/* RST
39+ .. attribute:: roads
40+
41+ (RO) Array of roads leading to the flag. Directions
42+ can be tr,r,br,bl,l and tl
43+
44+ :returns: The array of 'direction:road', if any
45+*/
46+int LuaFlag::get_roads(lua_State * L) {
47+
48+ const std::vector<std::string> directions = {"tr", "r", "br", "bl", "l", "tl"};
49+
50+ lua_newtable(L);
51+
52+ EditorGameBase & egbase = get_egbase(L);
53+ Flag * f = get(L, egbase);
54+
55+ for (uint32_t i = 1; i <= 6; i++){
56+ if (f->get_road(i) != nullptr) {
57+ lua_pushstring(L, directions.at(i - 1));
58+ upcasted_map_object_to_lua(L, f->get_road(i));
59+ lua_rawset(L, -3);
60+ }
61+ }
62+ return 1;
63+}
64+
65+/* RST
66+ .. attribute:: building
67+
68+ (RO) building belonging to the flag
69+*/
70+int LuaFlag::get_building(lua_State * L) {
71+
72+ EditorGameBase & egbase = get_egbase(L);
73+ Flag * f = get(L, egbase);
74+
75+ PlayerImmovable * building = f->get_building();
76+ if (!building) {
77+ return 0;
78+ } else {
79+ upcasted_map_object_to_lua(L, building);
80+ }
81+ return 1;
82+
83+}
84 /*
85 ==========================================================
86 LUA METHODS
87
88=== modified file 'src/scripting/lua_map.h'
89--- src/scripting/lua_map.h 2015-01-31 16:03:59 +0000
90+++ src/scripting/lua_map.h 2015-02-12 21:03:16 +0000
91@@ -596,13 +596,15 @@
92 /*
93 * Properties
94 */
95-
96+ int get_roads(lua_State * L);
97+ int get_building(lua_State * L);
98 /*
99 * Lua Methods
100 */
101 int set_wares(lua_State *);
102 int get_wares(lua_State *);
103
104+
105 /*
106 * C Methods
107 */
108
109=== modified file 'test/maps/lua_testsuite.wmf/scripting/flag.lua'
110--- test/maps/lua_testsuite.wmf/scripting/flag.lua 2014-07-28 18:30:11 +0000
111+++ test/maps/lua_testsuite.wmf/scripting/flag.lua 2015-02-12 21:03:16 +0000
112@@ -12,7 +12,10 @@
113 self.f = player1:place_flag(self.field, 1)
114 end
115 function flag_tests:teardown()
116- pcall(self.f.remove, self.f)
117+ pcall(function() self.f:remove() end)
118+ pcall(function() self.f1:remove() end)
119+ pcall(function() self.f2:remove() end)
120+ pcall(function() self.w:remove() end)
121 end
122
123 function flag_tests:test_name()
124@@ -139,3 +142,21 @@
125 self.f:get_wares{"meat", "balloon"}
126 end)
127 end
128+
129+-- ===================
130+-- buildings and roads
131+-- ===================
132+function flag_tests:roads_and_buildings_test()
133+ local field = map:get_field(4,14)
134+ self.w = player1:place_building("warehouse", field)
135+ self.f1 = self.w.flag
136+ local road = player1:place_road(self.f1, "br", "br", "br")
137+ self.f2 = self.f1.fields[1].brn.brn.brn.immovable
138+
139+ assert_equal(self.f2.roads.tl, road)
140+ assert_equal(self.f2.roads.tl.start_flag, self.f1)
141+ assert_equal(self.f2.roads.tl.end_flag, self.f2)
142+ assert_equal(self.f1.building, self.w)
143+ assert_nil(self.f2.building)
144+ assert_equal(self.f2.debug_economy, self.w.debug_economy)
145+end
146
147=== modified file 'test/maps/lua_testsuite.wmf/scripting/game.lua'
148--- test/maps/lua_testsuite.wmf/scripting/game.lua 2014-01-12 19:06:22 +0000
149+++ test/maps/lua_testsuite.wmf/scripting/game.lua 2015-02-12 21:03:16 +0000
150@@ -1,5 +1,5 @@
151 -- ===================
152--- Tests for the game
153+-- Tests for the game
154 -- ===================
155
156 test_game = lunit.TestCase("Game functions test")
157@@ -10,4 +10,3 @@
158 function test_game:test_no_editor_class()
159 assert_equal(nil, wl.Editor)
160 end
161-

Subscribers

People subscribed via source and target branches

to status/vote changes: