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

--      air_attack.lua - Define the air 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: air_attack.lua,v 1.5 2005/02/26 11:00:07 kimmomerikivi Exp $

ai_air_attack_func = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
ai_air_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-darkness-spearman", 30, "unit-darkness-bowman", 20}) end,
  function() return AiForce(1, {"unit-darkness-warrior", 50, "unit-darkness-spearman", 50, "unit-darkness-bowman", 50}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(1) end,
  function() return AiSleep(50) end,
  function() ai_air_attack_end_loop_func[player] = 0; return false end,
}

function AiairAttackDarknessEndloop()
  local ret

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

local air_funcs = {
  function() AiDebug(false) return false end,
  function() return AiNeed("unit-darkness-camp") end,
  function() return AiWait("unit-darkness-camp") end,

  function() return AiSet("unit-darkness-worker", 30) end,
  function() return AiNeed("unit-darkness-garrison") end,
  function() return AiNeed("unit-darkness-garrison") end,
  function() return AiNeed("unit-darkness-garrison") end,
  function() return AiForce(0, {"unit-darkness-warrior", 10}) end,
  function() return AiForce(1, {"unit-darkness-warrior", 10}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiResearch("upgrade-darkness-level2") end,
  function() return AiSleep(550) end,
  function() return AiSet("unit-darkness-worker", 40) end,

  function() return AiNeed("unit-darkness-blacksmith") end,
  function() return AiNeed("unit-darkness-blacksmith") end,
  function() return AiNeed("unit-darkness-lumber-storage") end,
  function() return AiResearch("upgrade-darkness-better-swords") end,
  function() return AiResearch("upgrade-darkness-better-shields") end,
  function() return AiResearch("upgrade-darkness-better-helmets") end,
  function() return AiResearch("upgrade-darkness-bows") end,
  function() return AiResearch("upgrade-darkness-spears") end,
  function() return AiNeed("unit-darkness-garrison") end,
  function() return AiNeed("unit-darkness-garrison") end,
  function() return AiForce(0, {"unit-darkness-spearman", 10, "unit-darkness-bowman", 10}) end,
  function() return AiForce(1, {"unit-darkness-warrior", 40, "unit-darkness-spearman", 30, "unit-darkness-bowman", 30}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(1) end,


  -- Everything researched...

  function() return AiairAttackDarknessEndloop() end,
}

function AiairAttackDarkness()
  local ret

  player = AiPlayer() + 1

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

DefineAi("wc2-air-attack-darkness", "darkness", "air-attack", AiairAttackDarkness)


