← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/fleet_update into lp:widelands

 

TiborB has proposed merging lp:~widelands-dev/widelands/fleet_update into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1509220 in widelands: "Atlanteans campaign keeps counting 0 ships"
  https://bugs.launchpad.net/widelands/+bug/1509220

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/fleet_update/+merge/276457

Small redesign when fleet update is triggered. I partially undo previous changes (too frequent updates and some behaviour was disabled by mistake). Fleet is now updated also after port removal.

This fixes also linked bug.

Modification of regression test to test also situation when a ship is put on shore - mimics building of new ship.

Modification of one campaing lua file - just simplification of code.

Regression tests run OK.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/fleet_update into lp:widelands.
=== modified file 'campaigns/atl01.wmf/scripting/mission_thread.lua'
--- campaigns/atl01.wmf/scripting/mission_thread.lua	2014-10-31 11:36:29 +0000
+++ campaigns/atl01.wmf/scripting/mission_thread.lua	2015-11-02 20:20:50 +0000
@@ -169,30 +169,8 @@
 
 function check_for_ships()
    -- Check if the ships are done, then the mission ends successfully
-   local lake_fields = Set:new()
-   for idx,f in ipairs(map:get_field(75,80):region(12)) do
-      if f:has_caps("swimmable") then
-         lake_fields:add(f)
-      end
-   end
-
-   while true do
-      local nships = 0
-      -- Count the ships
-      for f in lake_fields:items() do
-         local bobs = f.bobs
-         if #bobs then
-            for idx, b in ipairs(bobs) do
-               if b.descr.type_name == "ship" then
-                  nships = nships + 1
-               end
-            end
-         end
-      end
-      if nships >= 3 then
-         break
-      end
-      sleep(8234)
+   while #p1:get_ships() < 3 do
+        sleep(8234)
    end
 
    -- Success

=== modified file 'src/economy/fleet.cc'
--- src/economy/fleet.cc	2015-10-24 15:42:37 +0000
+++ src/economy/fleet.cc	2015-11-02 20:20:50 +0000
@@ -578,8 +578,13 @@
 			Economy::check_split(m_ports[0]->base_flag(), port->base_flag());
 	}
 
-	if (m_ships.empty() && m_ports.empty())
+	if (m_ships.empty() && m_ports.empty()) {
 		remove(egbase);
+	} else if (upcast(Game, game, &egbase)) {
+		// Some ship perhaps losses its destination now, so new destination must be appointed (if any)
+		molog("Port removed from fleet, triggering fleet update\n");
+		update(egbase);
+	}
 }
 
 /**

=== modified file 'src/logic/ship.cc'
--- src/logic/ship.cc	2015-10-25 07:13:43 +0000
+++ src/logic/ship.cc	2015-11-02 20:20:50 +0000
@@ -237,17 +237,13 @@
 }
 
 /// updates a ships tasks in transport mode \returns false if failed to update tasks
-bool Ship::ship_update_transport(Game& game, Bob::State&) {
+bool Ship::ship_update_transport(Game& game, Bob::State& state) {
 	Map& map = game.map();
 
 	PortDock* dst = get_destination(game);
 	if (!dst) {
-		// The ship has lost its destination (port is gone perhaps) so
-		// stop and start being idle
-		start_task_idle(game, descr().main_animation(), 10000);
-		// ...but let the fleet recalcualte ships destinations (this ship
-		// needs new destination)
-		m_fleet->update(game);
+		// The ship has no destination, so let it sleep
+		ship_update_idle(game, state);
 		return true;
 	}
 
@@ -455,6 +451,7 @@
 }
 
 void Ship::ship_update_idle(Game& game, Bob::State& state) {
+
 	if (state.ivar1) {
 		// We've just completed one step, so give neighbours
 		// a chance to move away first

=== modified file 'test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua'
--- test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua	2015-04-07 13:33:16 +0000
+++ test/maps/expedition.wmf/scripting/test_ship_movement_controls.lua	2015-11-02 20:20:50 +0000
@@ -1,27 +1,31 @@
 run(function()
    game.desired_speed = 30 * 1000
-	p1:place_bob("ship", map:get_field(10, 10))
-
-	port = map:get_field(16, 16).immovable
- 	port:set_wares("log", 10) -- no sense to wait
-	port:set_wares("blackwood", 10)
-
+
+    -- placing a ship on coast
+	p1:place_bob("ship", map:get_field(8, 8))
+	sleep(1000)
+	
 	--getting table with all our ships (single one only)
 	ships = p1:get_ships()
 
-	--veryfing that ship is indeed placed where should be :)
-	assert_equal(10,ships[1].field.x)
-	assert_equal(10,ships[1].field.y)
-
 	--ships table should contain 1 item (1 ship)
   	assert_equal(1, #ships)
+	
+	--waiting till it is pulled from coast
+	while ships[1].field.x == 8 and ships[1].field.xy == 8 do
+		print ("ship still on coast")
+		sleep(1000)
+	end
+
+	port = map:get_field(16, 16).immovable
+ 	port:set_wares("log", 10) -- no sense to wait
+	port:set_wares("blackwood", 10)
 
   	--ship has no wares on it
   	assert_equal(0,ships[1]:get_wares())
 
   	--no destination is set
   	assert(not ships[1].destination)
-
   	--ships in transport state
   	assert_equal("transport", ships[1].state)
 
@@ -33,15 +37,17 @@
 	port:start_expedition()
 	sleep (300)
 	assert(port.expedition_in_progress)
+	assert(ships[1])
 
 	--ships changes state when exp ready
 	while ships[1].state == "transport" do sleep(2000) end
 	assert_equal("exp_waiting", ships[1].state)
-
  	--sending NW and verifying
 	ships[1].scouting_direction="nw"
+
 	sleep(6000)
   	assert_equal("nw", ships[1].scouting_direction)
+
   	assert_equal("exp_scouting", ships[1].state)
 
 	while ships[1].scouting_direction == "nw" do

=== modified file 'test/maps/ship_transportation.wmf/scripting/init.lua'
--- test/maps/ship_transportation.wmf/scripting/init.lua	2015-03-09 20:16:18 +0000
+++ test/maps/ship_transportation.wmf/scripting/init.lua	2015-11-02 20:20:50 +0000
@@ -16,11 +16,15 @@
    })
 end
 
-function port1()
-   return map:get_field(16, 16).immovable
-end
-
-function create_second_port()
+function southern_port()
+   return map:get_field(16, 16).immovable
+end
+
+function southern_port()
+   return map:get_field(16, 16).immovable
+end
+
+function create_northern_port()
    prefilled_buildings(p1,
    { "port", 16, 2,
       wares = {},
@@ -29,7 +33,15 @@
    })
 end
 
-function port2()
+function northern_port()
+   local o = map:get_field(16, 2).immovable
+   if o and o.descr.name == "port" then
+      return o
+   end
+   return nil
+end
+
+function northern_port()
    local o = map:get_field(16, 2).immovable
    if o and o.descr.name == "port" then
       return o

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_farm_with_ware_and_worker_in_transit.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_farm_with_ware_and_worker_in_transit.lua	2014-01-18 13:47:32 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_farm_with_ware_and_worker_in_transit.lua	2015-11-02 20:20:50 +0000
@@ -3,13 +3,13 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
-   port1():set_workers{
+   southern_port():set_workers{
       builder = 1,
    }
 
@@ -23,8 +23,8 @@
    -- kill the farm while the blackwood is in transit.
    farm():remove()
 
-   while not (port2():get_wares("blackwood") == 1
-      and port2():get_workers("builder") == 1) do
+   while not (northern_port():get_wares("blackwood") == 1
+      and northern_port():get_workers("builder") == 1) do
       sleep(100)
    end
 

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_first_port_with_ware_in_portdock.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_first_port_with_ware_in_portdock.lua	2014-01-18 13:47:32 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_first_port_with_ware_in_portdock.lua	2015-11-02 20:20:50 +0000
@@ -5,22 +5,22 @@
    create_first_port()
    local wh = p1:place_building("warehouse", map:get_field(13, 16), false, false)
    connected_road(p1, wh.flag, "r,r,r", true)
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
 
    -- Wait till the ware is in the portdock.
-   while port1():get_wares("blackwood") == 1 do
+   while southern_port():get_wares("blackwood") == 1 do
       sleep(200)
    end
    assert_equal(p1:get_wares("blackwood"), 1)
    sleep(8000)
 
 
-   port1():remove()
+   southern_port():remove()
 
    sleep(100)
    assert_equal(p1:get_wares("blackwood"), 0)

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_first_port_with_worker_in_portdock.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_first_port_with_worker_in_portdock.lua	2014-01-18 12:40:08 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_first_port_with_worker_in_portdock.lua	2015-11-02 20:20:50 +0000
@@ -5,20 +5,20 @@
    create_first_port()
    local wh = p1:place_building("warehouse", map:get_field(13, 16), false, false)
    connected_road(p1, wh.flag, "r,r,r", true)
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_workers{
+   southern_port():set_workers{
       builder = 1,
    }
 
    -- Wait till the worker transfers to the portdock.
-   while port1():get_workers("builder") == 1 do
+   while southern_port():get_workers("builder") == 1 do
       sleep(200)
    end
    sleep(3000)
 
-   port1():remove()
+   southern_port():remove()
    sleep(100)
    stable_save("port1_just_removed")
 

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua	2015-03-09 20:16:18 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_portdock_with_worker_and_ware_in_transit.lua	2015-11-02 20:20:50 +0000
@@ -3,51 +3,51 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
    
-   --removing builder from port2
-    port2():set_workers{
+   --removing builder from northern port
+    northern_port():set_workers{
       builder = 0,
    }  
 
-   assert_equal(port2():get_workers("builder"), 0)
+   assert_equal(northern_port():get_workers("builder"), 0)
 
    start_building_farm()
-   port1():set_workers{
+   southern_port():set_workers{
       builder = 1,
    }
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
 
-   assert_equal(port1():get_workers("builder"), 1)
-   assert_equal(port1():get_wares("blackwood"), 1)
+   assert_equal(southern_port():get_workers("builder"), 1)
+   assert_equal(southern_port():get_wares("blackwood"), 1)
    
    while ship:get_workers() == 0 or ship:get_wares() == 0  do
       sleep(500) 
    end
 
-   local flag_oversea = port2().flag   
+   local flag_oversea = northern_port().flag   
 
  	stable_save("restored_port")
    
    -- remove the portdock while the blackwood is in transit.
-   port2_portdock=port2().portdock
-   assert(port2_portdock)
-   port2_portdock:remove()
+   north_port_portdock=northern_port().portdock
+   assert(north_port_portdock)
+   north_port_portdock:remove()
    
    sleep(5000)
 
    assert_equal(p1:get_workers("builder"), 1)
    assert_equal(p1:get_wares("blackwood"), 1)
-   assert_equal(ship.debug_economy, port1().debug_economy)
+   assert_equal(ship.debug_economy, southern_port().debug_economy)
    assert_equal(ship.debug_economy, flag_oversea.debug_economy)
 
    sleep(5000)
    
-   --just wait till everything is gone to port2
+   --just wait till everything is gone to northern port
    while ship:get_workers() > 0 or ship:get_wares() > 0 or
-    port1():get_workers("builder") > 0 or  port1():get_wares("blackwood") > 0 do
+    southern_port():get_workers("builder") > 0 or  southern_port():get_wares("blackwood") > 0 do
       sleep(50)
    end
 

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_ports_with_ware_in_transit.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_ports_with_ware_in_transit.lua	2014-01-13 07:42:45 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_ports_with_ware_in_transit.lua	2015-11-02 20:20:50 +0000
@@ -3,10 +3,10 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
 
@@ -15,17 +15,17 @@
    assert_equal(p1:get_wares("blackwood"), 1)
 
    -- kill the port while the blackwood is in transit.
-   local flag_oversea = port2().flag
-   port2():remove()
+   local flag_oversea = northern_port().flag
+   northern_port():remove()
    assert_equal(p1:get_wares("blackwood"), 1)
    sleep(1000)
 
    assert_equal(p1:get_wares("blackwood"), 1)
-   assert_equal(ship.debug_economy, port1().debug_economy)
+   assert_equal(ship.debug_economy, southern_port().debug_economy)
    assert_not_equal(ship.debug_economy, flag_oversea.debug_economy)
 
    -- now kill the first port too.
-   port1():remove()
+   southern_port():remove()
    sleep(1000)
 
    stable_save("no_ports")
@@ -46,7 +46,7 @@
    assert_equal(p1:get_wares("blackwood"), 1)
 
    -- Wait (and hope) that the ship will eventually return the blackwood.
-   while port1():get_wares("blackwood") == 0 do
+   while southern_port():get_wares("blackwood") == 0 do
       sleep(5000)
    end
 

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_ports_with_worker_in_transit.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_ports_with_worker_in_transit.lua	2014-01-13 07:42:45 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_ports_with_worker_in_transit.lua	2015-11-02 20:20:50 +0000
@@ -3,10 +3,10 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_workers{
+   southern_port():set_workers{
       builder = 1,
    }
 
@@ -15,17 +15,17 @@
    assert_equal(p1:get_workers("builder"), 1)
 
    -- kill the port while the builder is in transit.
-   local flag_oversea = port2().flag
-   port2():remove()
+   local flag_oversea = northern_port().flag
+   northern_port():remove()
    assert_equal(p1:get_workers("builder"), 1)
    sleep(1000)
 
    assert_equal(p1:get_workers("builder"), 1)
-   assert_equal(ship.debug_economy, port1().debug_economy)
+   assert_equal(ship.debug_economy, southern_port().debug_economy)
    assert_not_equal(ship.debug_economy, flag_oversea.debug_economy)
 
    -- now kill the first port too.
-   port1():remove()
+   southern_port():remove()
    sleep(1000)
 
    stable_save("no_ports")
@@ -46,7 +46,7 @@
    assert_equal(p1:get_workers("builder"), 1)
 
    -- Wait (and hope) that the ship will eventually return the builder.
-   while port1():get_workers("builder") == 0 do
+   while southern_port():get_workers("builder") == 0 do
       sleep(5000)
    end
 

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_ware_in_portdock.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_ware_in_portdock.lua	2015-10-16 19:36:15 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_ware_in_portdock.lua	2015-11-02 20:20:50 +0000
@@ -3,10 +3,10 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
    
@@ -15,7 +15,7 @@
    -- The ship should not yet have picked up the ware from the
    -- portdock.
    assert_equal(1, p1:get_wares("blackwood"))
-   assert_equal(0, port1():get_wares("blackwood"))
+   assert_equal(0, southern_port():get_wares("blackwood"))
    
    while ship:get_wares() == 0 do
    	  -- ship still on the way to the bottom port
@@ -24,9 +24,9 @@
 
    --now it is loaded with something and port empty.. 
    sleep(2000)   
-   assert_equal(0, port1():get_wares("blackwood"))
+   assert_equal(0, southern_port():get_wares("blackwood"))
    -- ...and on the way to the north, so let remove the upper port
-   port2():remove()
+   northern_port():remove()
    sleep(100)
 
    stable_save("ware_in_portdock")
@@ -34,12 +34,12 @@
    --ship has to get to the place of former upper port and then return back to the bottom port
    sleep(30000)
 
-   -- Ware should be back in port.
+   -- Ware should be back in port now.
    assert_equal(1, p1:get_wares("blackwood"))
-   assert_equal(1, port1():get_wares("blackwood"))
+   assert_equal(1, southern_port():get_wares("blackwood"))
 
    -- Create port again.
-   create_second_port()
+   create_northern_port()
 	
    sleep (10000)
 	

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua	2015-03-10 20:10:31 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_second_port_with_worker_in_portdock.lua	2015-11-02 20:20:50 +0000
@@ -3,28 +3,35 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    --removing portdock first
-   portdock_fields=port2().portdock.fields
+   portdock_fields=northern_port().portdock.fields
    portdock_fields[1].immovable:remove()
    sleep(100)
    --portdock should be back, as port is still there
    assert (portdock_fields[1].immovable) 
    
    start_building_farm()
-   port1():set_workers{
+   southern_port():set_workers{
       builder = 1,
    }
-   sleep(6000)
-
+	
+	sleep(2000)
+	
    -- The ship should not yet have picked up the worker from the
    -- portdock.
-   assert_equal(p1:get_workers("builder"), 1)
-   assert_equal(port1():get_workers("builder"), 0)
-
-   portdock_fields=port2().portdock.fields
-   port2():remove()
+   assert_equal(1, p1:get_workers("builder"))
+   assert_equal(0, southern_port():get_workers("builder"))
+
+  -- so wait till buil is loaded on ship
+  while (ship:get_workers("builder") == 0) do
+   	sleep(100)
+  end
+  sleep(5000)
+
+   portdock_fields=northern_port().portdock.fields
+   northern_port():remove()
    sleep(100)
    --verify that also portdock was removed
    assert (not portdock_fields[1].immovable)
@@ -33,14 +40,18 @@
 
    stable_save("worker_in_portdock")
 
-   sleep(8000)
+   -- Wait till a ship unloads a worked at the souther port
+   while (ship:get_workers("builder") == 1) do
+   	 sleep(100)
+   end
+   sleep(2000)
 
-   -- Worker should be back in port.
+   -- Worker should be back in the southern port.
    assert_equal(p1:get_workers("builder"), 1)
-   assert_equal(port1():get_workers("builder"), 1)
+   assert_equal(southern_port():get_workers("builder"), 1)
 
    -- Create port again.
-   create_second_port()
+   create_northern_port()
 
    while ship:get_workers() == 0 do
       sleep(50)

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_ship_before_picking_up_transporting_ware.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_ship_before_picking_up_transporting_ware.lua	2014-01-15 21:12:05 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_ship_before_picking_up_transporting_ware.lua	2015-11-02 20:20:50 +0000
@@ -3,13 +3,13 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
-   port2():set_workers{
+   northern_port():set_workers{
       builder = 1,
    }
 
@@ -23,7 +23,7 @@
 
    assert_equal(1, p1:get_wares("blackwood"))
    -- It is not in the port (still in the dock)
-   assert_equal(0, port1():get_wares("blackwood"))
+   assert_equal(0, southern_port():get_wares("blackwood"))
 
    local new_ship = p1:place_bob("ship", map:get_field(10, 10))
    sleep(1000)

=== modified file 'test/maps/ship_transportation.wmf/scripting/test_rip_ship_while_transporting_ware.lua'
--- test/maps/ship_transportation.wmf/scripting/test_rip_ship_while_transporting_ware.lua	2014-01-15 21:12:05 +0000
+++ test/maps/ship_transportation.wmf/scripting/test_rip_ship_while_transporting_ware.lua	2015-11-02 20:20:50 +0000
@@ -3,13 +3,13 @@
    game.desired_speed = 10 * 1000
 
    create_first_port()
-   create_second_port()
+   create_northern_port()
 
    start_building_farm()
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
-   port2():set_workers{
+   northern_port():set_workers{
       builder = 1,
    }
 
@@ -27,7 +27,7 @@
    assert_equal(p1:get_wares("blackwood"), 0)
 
    local new_ship = p1:place_bob("ship", map:get_field(10, 10))
-   port1():set_wares{
+   southern_port():set_wares{
       blackwood = 1,
    }
    sleep(1000)


Follow ups