--__________         __    __  .__           ___________
--\______   \_____ _/  |__/  |_|  |   ____   \_   _____/__________
-- |    |  _/\__  \\   __\   __\  | _/ __ \   |    __)/  _ \_  __ \
-- |    |   \ / __ \|  |  |  | |  |_\  ___/   |     \(  <_> )  | \/
-- |______  /(____  /__|  |__| |____/\___  >  \___  / \____/|__|
--        \/      \/                     \/       \/
--   _____                     .___.__
--  /     \ _____    ____    __| _/|__| ____  ___________
-- /  \ /  \\__  \  /    \  / __ | |  |/ ___\/  _ \_  __ \
--/    Y    \/ __ \|   |  \/ /_/ | |  \  \__(  <_> )  | \/
--\____|__  (____  /___|  /\____ | |__|\___  >____/|__|
--        \/     \/     \/      \/         \/

--      land_attack.lua - Define the land attack AI.
--
--      (c) Copyright 2005 by Kimmo Merikivi
--
--      This program is free software; you can redistribute it and/or modify
--      it under the terms of the GNU General Public License as published by
--      the Free Software Foundation; either version 2 of the License, or
--      (at your option) any later version.
--  
--      This program is distributed in the hope that it will be useful,
--      but WITHOUT ANY WARRANTY; without even the implied warranty of
--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--      GNU General Public License for more details.
--  
--      You should have received a copy of the GNU General Public License
--      along with this program; if not, write to the Free Software
--      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
--
--      $Id: land_attack-dwarves.lua,v 1.2 2005/04/03 10:58:48 mikkommm Exp $

ai_land_attack_func = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
ai_land_attack_end_loop_func = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

local player

local end_loop_funcs = {
	function() print("Looping !") return false end,
	function() return AiForce(0, {"unit-dwarves-swordman", 60, "unit-dwarves-bowman", 15}) end,
	function() return AiForce(1, {"unit-dwarves-swordman", 60, "unit-dwarves-bowman", 15}) end,
	function() return AiWaitForce(1) end,
	function() return AiAttackWithForce(1) end,
	function() return AiAttackWithForce(1) end,
	function() return AiSleep(50) end,
	function() ai_land_attack_end_loop_func[player] = 0; return false end,
}

function AiLandAttackDwarvesEndloop()
	local ret

	while (true) do
		ret = end_loop_funcs[ai_land_attack_end_loop_func[player]]()
		if (ret) then
			break
		end
		ai_land_attack_end_loop_func[player] = ai_land_attack_end_loop_func[player] + 1
	end
	return true
end

local land_funcs = {
	function() AiDebug(false) return false end,
	function() return AiNeed("unit-dwarves-town-center") end,
	function() return AiWait("unit-dwarves-town-center") end,

	function() return AiSet("unit-dwarves-worker", 20) end,
	function() return AiNeed("unit-dwarves-blacksmith") end,
	function() return AiWait("unit-dwarves-blacksmith") end,
	function() return AiResearch("upgrade-dwarves-axes") end,
	function() return AiResearch("upgrade-dwarves-daggers") end,
	function() return AiNeed("unit-dwarves-guard-tower") end,
	function() return AiNeed("unit-dwarves-guard-tower") end,
	function() return AiSet("unit-dwarves-warrior", 20) end,
	function() return AiNeed("unit-dwarves-lumber-mill") end,
	function() return AiNeed("unit-dwarves-market") end,
	function() return AiWait("unit-dwarves-guard-tower") end,
	function() return AiWait("unit-dwarves-market") end,
	function() return AiWait("unit-dwarves-lumber-mill") end,

	function() return AiResearch("upgrade-dwarves-level2") end,
	function() return AiSleep(550) end,

	function() return AiNeed("unit-dwarves-blacksmith") end,
	function() return AiUpgradeTo("unit-dwarves-town-hall") end,
	function() return AiResearch("upgrade-dwarves-advanced-worker") end,
	function() return AiResearch("upgrade-dwarves-swordman") end,
	function() return AiResearch("upgrade-dwarves-steel") end,
	function() return AiResearch("upgrade-dwarves-bowman") end,
	function() return AiNeed("unit-dwarves-guard-tower") end,

	function() return AiSet("unit-dwarves-advanced-worker", 25) end,
	function() return AiSet("unit-dwarves-warrior", 30) end,
	function() return AiForce(1, {"unit-dwarves-warrior", 25, "unit-dwarves-scout", 5}) end,
	function() return AiWaitForce(1) end,
	function() return AiAttackWithForce(1) end,
	function() return AiAttackWithForce(1) end,

	function() return AiForce(0, {"unit-dwarves-swordman", 40, "unit-dwarves-bowman", 20}) end,
	function() return AiForce(1, {"unit-dwarves-swordman", 30}) end,
	function() return AiWaitForce(1) end,
	function() return AiAttackWithForce(1) end,
	function() return AiAttackWithForce(1) end,

	function() return AiResearch("upgrade-dwarves-agriculture") end,



  -- Everything researched...

	function() return AiLandAttackDwarvesEndloop() end,
}

function AiLandAttackDwarves()
	local ret

	player = AiPlayer() + 1

	while (true) do
--    print("Executing land_funcs[" .. ai_land_attack_func[player] .. "]")
		ret = land_funcs[ai_land_attack_func[player]]()
		if (ret) then
			break
		end
		ai_land_attack_func[player] = ai_land_attack_func[player] + 1
	end
end

DefineAi("wc2-land-attack-dwarves", "dwarves", "land-attack", AiLandAttackDwarves)
