← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/bug-1718745-allows-seafaring into lp:widelands

 

GunChleoc has proposed merging lp:~widelands-dev/widelands/bug-1718745-allows-seafaring into lp:widelands.

Commit message:
Split new function cleanup_portspaces() from allows_seafaring(). allows_seafaring() is now const. Exposed 3 new functions to LuaMap:

    - get_allows_seafaring
    - get_number_of_port_spaces
    - set_port_space

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1718745 in widelands: "Please expose "allows_seafaring" in Lua interface"
  https://bugs.launchpad.net/widelands/+bug/1718745

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-1718745-allows-seafaring/+merge/333233
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug-1718745-allows-seafaring into lp:widelands.
=== modified file 'src/editor/ui_menus/main_menu_save_map.cc'
--- src/editor/ui_menus/main_menu_save_map.cc	2017-08-20 17:45:42 +0000
+++ src/editor/ui_menus/main_menu_save_map.cc	2017-11-05 17:26:05 +0000
@@ -279,6 +279,7 @@
 		   g_fs->create_sub_file_system(tmp_name, binary ? FileSystem::ZIP : FileSystem::DIR));
 
 		// Recompute seafaring tag
+		map->cleanup_portspaces();
 		if (map->allows_seafaring()) {
 			map->add_tag("seafaring");
 		} else {

=== modified file 'src/logic/map.cc'
--- src/logic/map.cc	2017-09-13 07:27:00 +0000
+++ src/logic/map.cc	2017-11-05 17:26:05 +0000
@@ -1949,32 +1949,15 @@
 			check_neighbour_heights(n[i], area);
 }
 
-/*
-===========
-Map::allows_seafaring()
-
-This function checks if there are two ports that are reachable
-for each other - then the map is seafaring.
-=============
-*/
-bool Map::allows_seafaring() {
-	Map::PortSpacesSet port_spaces = get_port_spaces();
-	std::vector<Coords> portdocks;
+bool Map::allows_seafaring() const {
 	std::set<Coords> swim_coords;
 
-	for (const Coords& c : port_spaces) {
+	for (const Coords& c : get_port_spaces()) {
 		std::queue<Coords> q_positions;
 		std::set<Coords> visited_positions;
 		FCoords fc = get_fcoords(c);
-		portdocks = find_portdock(fc);
-
-		/* remove the port space if it is not longer valid port space */
-		if ((fc.field->get_caps() & BUILDCAPS_SIZEMASK) != BUILDCAPS_BIG || portdocks.empty()) {
-			set_port_space(c, false);
-			continue;
-		}
-
-		for (const Coords& portdock : portdocks) {
+
+		for (const Coords& portdock : find_portdock(fc)) {
 			visited_positions.insert(portdock);
 			q_positions.push(portdock);
 		}
@@ -2003,6 +1986,16 @@
 	return false;
 }
 
+void Map::cleanup_portspaces() {
+	for (const Coords& c : get_port_spaces()) {
+		const FCoords fc = get_fcoords(c);
+		if ((fc.field->get_caps() & BUILDCAPS_SIZEMASK) != BUILDCAPS_BIG || find_portdock(fc).empty()) {
+			set_port_space(c, false);
+			continue;
+		}
+	}
+}
+
 bool Map::has_artifacts() {
 	for (MapIndex i = 0; i < max_index(); ++i) {
 		if (upcast(Immovable, immovable, fields_[i].get_immovable())) {

=== modified file 'src/logic/map.h'
--- src/logic/map.h	2017-09-01 15:45:59 +0000
+++ src/logic/map.h	2017-11-05 17:26:05 +0000
@@ -451,7 +451,11 @@
 		return port_spaces_;
 	}
 	std::vector<Coords> find_portdock(const Widelands::Coords& c) const;
-	bool allows_seafaring();
+
+	/// Check whether there are at least 2 port spaces that can be reached from each other by water
+	bool allows_seafaring() const;
+	/// Remove all port spaces that are not valid (Buildcap < big or not enough space for a portdock)
+	void cleanup_portspaces();
 
 	/// Checks whether there are any artifacts on the map
 	bool has_artifacts();

=== modified file 'src/scripting/lua_map.cc'
--- src/scripting/lua_map.cc	2017-10-04 00:27:47 +0000
+++ src/scripting/lua_map.cc	2017-11-05 17:26:05 +0000
@@ -1138,7 +1138,7 @@
 
 .. class:: Map
 
-   Access to the map and it's objects. You cannot instantiate this directly,
+   Access to the map and its objects. You cannot instantiate this directly,
    instead access it via :attr:`wl.Game.map`.
 */
 const char LuaMap::className[] = "Map";
@@ -1146,9 +1146,12 @@
    METHOD(LuaMap, place_immovable),
    METHOD(LuaMap, get_field),
    METHOD(LuaMap, recalculate),
+   METHOD(LuaMap, set_port_space),
    {nullptr, nullptr},
 };
 const PropertyType<LuaMap> LuaMap::Properties[] = {
+   PROP_RO(LuaMap, allows_seafaring),
+   PROP_RO(LuaMap, number_of_port_spaces),
    PROP_RO(LuaMap, width),
    PROP_RO(LuaMap, height),
    PROP_RO(LuaMap, player_slots),
@@ -1167,6 +1170,31 @@
  ==========================================================
  */
 /* RST
+   .. attribute:: allows_seafaring
+
+      (RO) Whether the map currently allows seafaring. This will calculate a path between port spaces,
+		so it's more accurate but less efficient than :any:`number_of_port_spaces`.
+
+		:returns: True if there are at least two port spaces that can be reached from each other.
+*/
+int LuaMap::get_allows_seafaring(lua_State* L) {
+	lua_pushboolean(L, get_egbase(L).map().allows_seafaring());
+	return 1;
+}
+/* RST
+   .. attribute:: number_of_port_spaces
+
+      (RO) The amount of port spaces on the map. If this is >= 2, one can assume that the map
+      allows seafaring. This is checked very quickly and is more efficient than :any:`allows_seafaring`,
+      but it won't detect whether the port spaces can be reached from each other, so it's less accurate.
+
+      :returns: An integer with the number of port spaces.
+*/
+int LuaMap::get_number_of_port_spaces(lua_State* L) {
+	lua_pushuint32(L, get_egbase(L).map().get_port_spaces().size());
+	return 1;
+}
+/* RST
    .. attribute:: width
 
       (RO) The width of the map in fields.
@@ -1297,6 +1325,26 @@
 	return 0;
 }
 
+/* RST
+   .. method:: set_port_space(x, y, allowed)
+
+      Sets whether a port space is allowed at the coordinates (x, y).
+
+      :arg x: The x coordinate of the port space to set/unset.
+      :type x: :class:`int`
+      :arg y: The y coordinate of the port space to set/unset.
+      :type y: :class:`int`
+      :arg allowed: Whether building a port will be allowed here.
+      :type allowed: :class:`boolean`
+*/
+int LuaMap::set_port_space(lua_State* L) {
+	const int x = luaL_checkint32(L, 2);
+	const int y = luaL_checkint32(L, 3);
+	const bool allowed = luaL_checkboolean(L, 4);
+	get_egbase(L).mutable_map()->set_port_space(Widelands::Coords(x, y), allowed);
+	return 0;
+}
+
 /*
  ==========================================================
  C METHODS

=== modified file 'src/scripting/lua_map.h'
--- src/scripting/lua_map.h	2017-09-22 19:54:27 +0000
+++ src/scripting/lua_map.h	2017-11-05 17:26:05 +0000
@@ -86,6 +86,8 @@
 	/*
 	 * Properties
 	 */
+	int get_allows_seafaring(lua_State*);
+	int get_number_of_port_spaces(lua_State*);
 	int get_width(lua_State*);
 	int get_height(lua_State*);
 	int get_player_slots(lua_State*);
@@ -96,6 +98,7 @@
 	int place_immovable(lua_State*);
 	int get_field(lua_State*);
 	int recalculate(lua_State*);
+	int set_port_space(lua_State*);
 
 	/*
 	 * C methods

=== added directory 'test/maps/two_ponds.wmf'
=== added directory 'test/maps/two_ponds.wmf/binary'
=== added file 'test/maps/two_ponds.wmf/binary/heights'
Binary files test/maps/two_ponds.wmf/binary/heights	1970-01-01 00:00:00 +0000 and test/maps/two_ponds.wmf/binary/heights	2017-11-05 17:26:05 +0000 differ
=== added file 'test/maps/two_ponds.wmf/binary/mapobjects'
Binary files test/maps/two_ponds.wmf/binary/mapobjects	1970-01-01 00:00:00 +0000 and test/maps/two_ponds.wmf/binary/mapobjects	2017-11-05 17:26:05 +0000 differ
=== added file 'test/maps/two_ponds.wmf/binary/resource'
Binary files test/maps/two_ponds.wmf/binary/resource	1970-01-01 00:00:00 +0000 and test/maps/two_ponds.wmf/binary/resource	2017-11-05 17:26:05 +0000 differ
=== added file 'test/maps/two_ponds.wmf/binary/terrain'
Binary files test/maps/two_ponds.wmf/binary/terrain	1970-01-01 00:00:00 +0000 and test/maps/two_ponds.wmf/binary/terrain	2017-11-05 17:26:05 +0000 differ
=== added file 'test/maps/two_ponds.wmf/elemental'
--- test/maps/two_ponds.wmf/elemental	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/elemental	2017-11-05 17:26:05 +0000
@@ -0,0 +1,12 @@
+# Automatically created by Widelands bzr8477[trunk] (Debug)
+
+[global]
+packet_version="1"
+map_w="64"
+map_h="64"
+nr_players="1"
+name="Two Ponds"
+author="Unknown"
+descr="Test map with 2 bodies of water, with 1 port space each."
+hint=
+tags=

=== added file 'test/maps/two_ponds.wmf/minimap.png'
Binary files test/maps/two_ponds.wmf/minimap.png	1970-01-01 00:00:00 +0000 and test/maps/two_ponds.wmf/minimap.png	2017-11-05 17:26:05 +0000 differ
=== added file 'test/maps/two_ponds.wmf/objective'
--- test/maps/two_ponds.wmf/objective	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/objective	2017-11-05 17:26:05 +0000
@@ -0,0 +1,4 @@
+# Automatically created by Widelands bzr8477[trunk] (Debug)
+
+[global]
+packet_version="2"

=== added file 'test/maps/two_ponds.wmf/player_names'
--- test/maps/two_ponds.wmf/player_names	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/player_names	2017-11-05 17:26:05 +0000
@@ -0,0 +1,10 @@
+# Automatically created by Widelands bzr8477[trunk] (Debug)
+
+[global]
+packet_version="2"
+
+[player_1]
+name="Cluicheadair 1"
+tribe="barbarians"
+ai=
+closeable="false"

=== added file 'test/maps/two_ponds.wmf/player_position'
--- test/maps/two_ponds.wmf/player_position	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/player_position	2017-11-05 17:26:05 +0000
@@ -0,0 +1,5 @@
+# Automatically created by Widelands bzr8477[trunk] (Debug)
+
+[global]
+packet_version="2"
+player_1="63 59"

=== added file 'test/maps/two_ponds.wmf/port_spaces'
--- test/maps/two_ponds.wmf/port_spaces	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/port_spaces	2017-11-05 17:26:05 +0000
@@ -0,0 +1,9 @@
+# Automatically created by Widelands bzr8477[trunk] (Debug)
+
+[global]
+packet_version="1"
+number_of_port_spaces="2"
+
+[port_spaces]
+0="5 2"
+1="12 24"

=== added directory 'test/maps/two_ponds.wmf/scripting'
=== added file 'test/maps/two_ponds.wmf/scripting/init.lua'
--- test/maps/two_ponds.wmf/scripting/init.lua	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/scripting/init.lua	2017-11-05 17:26:05 +0000
@@ -0,0 +1,5 @@
+include "scripting/coroutine.lua"
+include "scripting/lunit.lua"
+
+game = wl.Game()
+map = game.map

=== added file 'test/maps/two_ponds.wmf/scripting/test_seafaring.lua'
--- test/maps/two_ponds.wmf/scripting/test_seafaring.lua	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/scripting/test_seafaring.lua	2017-11-05 17:26:05 +0000
@@ -0,0 +1,17 @@
+run(function()
+   -- The map in its initial state has 2 unconnected port spaces, so it should not allow seafaring
+   assert_equal(2, map.number_of_port_spaces)
+   assert_equal(false, map.allows_seafaring)
+
+   -- Now add a connecting port space - we should have seafaring then
+   map:set_port_space(0, 2, true)
+   assert_equal(3, map.number_of_port_spaces)
+   assert_equal(true, map.allows_seafaring)
+
+   map:set_port_space(0, 2, false)
+   assert_equal(2, map.number_of_port_spaces)
+   assert_equal(false, map.allows_seafaring)
+
+   print("# All Tests passed.")
+   wl.ui.MapView():close()
+end)

=== added file 'test/maps/two_ponds.wmf/version'
--- test/maps/two_ponds.wmf/version	1970-01-01 00:00:00 +0000
+++ test/maps/two_ponds.wmf/version	2017-11-05 17:26:05 +0000
@@ -0,0 +1,11 @@
+# Automatically created by Widelands bzr8477[trunk] (Debug)
+
+[global]
+map_source_url=
+map_release=
+map_creator_version="bzr8477[trunk]"
+map_version_major="0"
+map_version_minor="1"
+map_version_timestamp="1509900753"
+packet_version="1"
+packet_compatibility="1"


Follow ups