GunChleoc has proposed merging lp:~widelands-dev/widelands/prevent_ai_deadlocks 
into lp:widelands/build19.

Commit message:
Added function to starting conditions to make sure that the AI never runs out 
of basic building materials in order to prevent deadlocks.

Requested reviews:
  Widelands Developers (widelands-dev)

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

Deadlocks with log production have been reported on the forum by multiple 
users. Rather than messing with the AI code and potentially messing up 
something else, I have added a script to the starting conditions that will 
supply all wares that can cause deadlocks - same trick as used by Trading 
Outpost. The wares supplied are log and granite for all tribes, marble for 
Empire and spidercloth for Atlanteans. Let me know if I forgot any.
-- 
Your team Widelands Developers is requested to review the proposed merge of 
lp:~widelands-dev/widelands/prevent_ai_deadlocks into lp:widelands/build19.
=== modified file 'data/tribes/scripting/starting_conditions/atlanteans/fortified_village.lua'
--- data/tribes/scripting/starting_conditions/atlanteans/fortified_village.lua	2016-03-30 07:23:59 +0000
+++ data/tribes/scripting/starting_conditions/atlanteans/fortified_village.lua	2016-11-01 07:24:37 +0000
@@ -3,6 +3,7 @@
 -- =======================================================================
 
 include "scripting/infrastructure.lua"
+include "tribes/scripting/starting_conditions/prevent_deadlocks.lua"
 
 set_textdomain("tribes")
 
@@ -99,5 +100,10 @@
       place_building_in_region(plr, "atlanteans_sawmill", sf:region(11), {
          wares = { log = 1 }
       })
+
+      if plr.ai ~= "" then
+            print("Player #" .. plr.number .. " is an AI")
+            prevent_deadlocks(plr)
+      end
    end
 }

=== modified file 'data/tribes/scripting/starting_conditions/atlanteans/headquarters.lua'
--- data/tribes/scripting/starting_conditions/atlanteans/headquarters.lua	2016-03-30 07:23:59 +0000
+++ data/tribes/scripting/starting_conditions/atlanteans/headquarters.lua	2016-11-01 07:24:37 +0000
@@ -3,6 +3,7 @@
 -- =======================================================================
 
 include "scripting/infrastructure.lua"
+include "tribes/scripting/starting_conditions/prevent_deadlocks.lua"
 
 set_textdomain("tribes")
 
@@ -79,6 +80,11 @@
          [{0,0,0,0}] = 35,
       }
    })
+
+   if plr.ai ~= "" then
+      print("Player #" .. plr.number .. " is an AI")
+      prevent_deadlocks(plr)
+   end
 end
 }
 

=== modified file 'data/tribes/scripting/starting_conditions/barbarians/fortified_village.lua'
--- data/tribes/scripting/starting_conditions/barbarians/fortified_village.lua	2016-03-30 07:23:59 +0000
+++ data/tribes/scripting/starting_conditions/barbarians/fortified_village.lua	2016-11-01 07:24:37 +0000
@@ -3,6 +3,7 @@
 -- =======================================================================
 
 include "scripting/infrastructure.lua"
+include "tribes/scripting/starting_conditions/prevent_deadlocks.lua"
 
 set_textdomain("tribes")
 
@@ -92,5 +93,9 @@
       place_building_in_region(plr, "barbarians_lime_kiln", sf:region(12), {
          wares = { granite = 6, coal = 3 },
       })
+      if plr.ai ~= "" then
+            print("Player #" .. plr.number .. " is an AI")
+            prevent_deadlocks(plr)
+      end
    end
 }

=== modified file 'data/tribes/scripting/starting_conditions/barbarians/headquarters.lua'
--- data/tribes/scripting/starting_conditions/barbarians/headquarters.lua	2016-03-30 07:23:59 +0000
+++ data/tribes/scripting/starting_conditions/barbarians/headquarters.lua	2016-11-01 07:24:37 +0000
@@ -3,6 +3,7 @@
 -- =======================================================================
 
 include "scripting/infrastructure.lua"
+include "tribes/scripting/starting_conditions/prevent_deadlocks.lua"
 
 set_textdomain("tribes")
 
@@ -70,6 +71,10 @@
          [{0,0,0,0}] = 45,
       }
    })
+   if player.ai ~= "" then
+      print("Player #" .. player.number .. " is an AI")
+      prevent_deadlocks(player)
+   end
 end
 }
 

=== modified file 'data/tribes/scripting/starting_conditions/empire/fortified_village.lua'
--- data/tribes/scripting/starting_conditions/empire/fortified_village.lua	2016-03-30 07:23:59 +0000
+++ data/tribes/scripting/starting_conditions/empire/fortified_village.lua	2016-11-01 07:24:37 +0000
@@ -3,6 +3,7 @@
 -- =======================================================================
 
 include "scripting/infrastructure.lua"
+include "tribes/scripting/starting_conditions/prevent_deadlocks.lua"
 
 set_textdomain("tribes")
 
@@ -117,5 +118,9 @@
       })
 
       place_building_in_region(plr, "empire_stonemasons_house", sf:region(11))
+      if plr.ai ~= "" then
+            print("Player #" .. plr.number .. " is an AI")
+            prevent_deadlocks(plr)
+      end
    end
 }

=== modified file 'data/tribes/scripting/starting_conditions/empire/headquarters.lua'
--- data/tribes/scripting/starting_conditions/empire/headquarters.lua	2016-03-30 07:23:59 +0000
+++ data/tribes/scripting/starting_conditions/empire/headquarters.lua	2016-11-01 07:24:37 +0000
@@ -3,6 +3,7 @@
 -- =======================================================================
 
 include "scripting/infrastructure.lua"
+include "tribes/scripting/starting_conditions/prevent_deadlocks.lua"
 
 set_textdomain("tribes")
 
@@ -77,5 +78,9 @@
          [{0,0,0,0}] = 45,
       }
    })
+   if p.ai ~= "" then
+      print("Player #" .. p.number .. " is an AI")
+      prevent_deadlocks(p)
+   end
 end
 }

=== added file 'data/tribes/scripting/starting_conditions/prevent_deadlocks.lua'
--- data/tribes/scripting/starting_conditions/prevent_deadlocks.lua	1970-01-01 00:00:00 +0000
+++ data/tribes/scripting/starting_conditions/prevent_deadlocks.lua	2016-11-01 07:24:37 +0000
@@ -0,0 +1,55 @@
+include "scripting/infrastructure.lua"
+
+function prevent_deadlocks(player)
+   -- Get all warehouse types
+   local warehouse_types = {}
+   for i, building in ipairs(wl.Game():get_tribe_description(player.tribe_name).buildings) do
+      if (building.type_name == "warehouse") then
+         table.insert(warehouse_types, building.name)
+      end
+   end
+
+   -- index of a warehouse we will add to. Used to 'rotate' warehouses
+   local idx = 1
+
+   for i=1,100000 do
+      sleep(300000)
+
+      -- collect all ~warehouses and pick one to insert the wares
+      local warehouses = {}
+      for i, building_name in ipairs(warehouse_types) do
+         warehouses = array_combine(warehouses, player:get_buildings(building_name))
+      end
+
+      if #warehouses > 0 then
+         -- adding to a warehouse with index idx, if out of range, adding to wh 1
+         if idx > #warehouses then
+            idx = 1
+         end
+
+         local wh = warehouses[idx]
+         local added = 0
+
+         if wh:get_wares("log") < 1 then
+            wh:set_wares("log", wh:get_wares("log") + 20)
+            added = added + 1
+         end
+         if wh:get_wares("granite") < 1 then
+            wh:set_wares("granite", wh:get_wares("granite") + 10)
+            added = added + 1
+         end
+         if player.tribe_name == "empire" and wh:get_wares("marble") < 1 then
+            wh:set_wares("marble", wh:get_wares("marble") + 10)
+            added = added + 1
+         end
+         if player.tribe_name == "atlanteans" and wh:get_wares("spidercloth") < 1 then
+            wh:set_wares("spidercloth", wh:get_wares("spidercloth") + 10)
+            added = added + 1
+         end
+         if (added > 0) then
+            print (player.number..": "..added.." types of ware added to warehouse "..idx.." of "..#warehouses.." (to prevent deadlock)")
+         end
+      end
+      idx = idx + 1
+   end
+end

=== modified file 'src/scripting/lua_game.cc'
--- src/scripting/lua_game.cc	2016-08-07 20:39:44 +0000
+++ src/scripting/lua_game.cc	2016-11-01 07:24:37 +0000
@@ -103,11 +103,17 @@
    {nullptr, nullptr},
 };
 const PropertyType<LuaPlayer> LuaPlayer::Properties[] = {
-   PROP_RO(LuaPlayer, name),       PROP_RO(LuaPlayer, allowed_buildings),
-   PROP_RO(LuaPlayer, objectives), PROP_RO(LuaPlayer, defeated),
-   PROP_RO(LuaPlayer, messages),   PROP_RO(LuaPlayer, inbox),
-   PROP_RW(LuaPlayer, team),       PROP_RO(LuaPlayer, tribe),
-   PROP_RW(LuaPlayer, see_all),    {nullptr, nullptr, nullptr},
+   PROP_RO(LuaPlayer, ai),
+   PROP_RO(LuaPlayer, name),
+   PROP_RO(LuaPlayer, allowed_buildings),
+   PROP_RO(LuaPlayer, objectives),
+   PROP_RO(LuaPlayer, defeated),
+   PROP_RO(LuaPlayer, messages),
+   PROP_RO(LuaPlayer, inbox),
+   PROP_RW(LuaPlayer, team),
+   PROP_RO(LuaPlayer, tribe),
+   PROP_RW(LuaPlayer, see_all),
+   {nullptr, nullptr, nullptr},
 };
 
 /*
@@ -115,6 +121,19 @@
  PROPERTIES
  ==========================================================
  */
+
+/* RST
+	.. attribute:: ai
+
+			(RO) The ai name of this Player if it is an ai, an empty string otherwise.
+*/
+int LuaPlayer::get_ai(lua_State* L) {
+	Game& game = get_game(L);
+	Player& p = get(L, game);
+	lua_pushstring(L, p.get_ai());
+	return 1;
+}
+
 /* RST
    .. attribute:: name
 

=== modified file 'src/scripting/lua_game.h'
--- src/scripting/lua_game.h	2016-08-04 15:49:05 +0000
+++ src/scripting/lua_game.h	2016-11-01 07:24:37 +0000
@@ -65,6 +65,7 @@
 	/*
 	 * Properties
 	 */
+	int get_ai(lua_State* L);
 	int get_name(lua_State* L);
 	int get_allowed_buildings(lua_State* L);
 	int get_objectives(lua_State* L);

_______________________________________________
Mailing list: https://launchpad.net/~widelands-dev
Post to     : widelands-dev@lists.launchpad.net
Unsubscribe : https://launchpad.net/~widelands-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to