--       _________ __                 __                               
--      /   _____//  |_____________ _/  |______     ____  __ __  ______
--      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
--      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \ 
--     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
--             \/                  \/          \//_____/            \/ 
--  ______________________                           ______________________
--                        T H E   W A R   B E G I N S
--         Stratagus - A free fantasy real time strategy game engine
--
--      land_attack.lua - Define the land attack AI.
--
--      (c) Copyright 2000-2004 by Lutz Sammer and Jimmy Salmon
--
--      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.lua,v 1.5 2004/03/04 16:28:04 mohydine 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, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 4, AiCatapult(), 1, AiMage(), 4}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 4, AiCatapult(), 1, AiMage(), 2}) end,
  function() return AiForce(2, {AiFlyer(), 1}) end,
  function() return AiWaitForce(2) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(2) end,
  function() return AiSleep(SleepFactor*500) end,
  function() ai_land_attack_end_loop_func[player] = 0; return false end,
}

function AiLandAttackEndloop()
  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 AiSleep(SleepFactor*AiGetSleepCycles()) end,
  function() return AiNeedUnit(AiCityCenter()) end,
  function() return AiSet(AiWorker(), 1) end,
  function() return AiWait(AiCityCenter()) end,
  function() return AiWait(AiWorker()) end, -- start hangs if nothing available

  function() return AiSet(AiWorker(), 4) end,
  
  function() return AiNeedUnit(AiBarracks()) end,
  function() return AiForce(0, {AiSoldier(), 1}) end,
  function() return AiForce(1, {AiSoldier(), 2}) end,
  function() return AiWaitForce(1) end,
  function() return AiSleep(SleepFactor*500) end,
  function() return AiAttackWithForce(1) end,
  

  function() return AiSet(AiWorker(), 9) end,
function() return AiNeedUnit(AiBlacksmith()) end,
  function() return AiSleep(SleepFactor*500) end,
  
  
  --function() return AiNeedUnit(AiTower()) end,
  
  function() return AiForce(0, {AiSoldier(), 2, AiShooter(), 1}) end,
  function() return AiForce(1, {AiSoldier(), 2, AiShooter(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiSleep(SleepFactor*100) end,
  function() return AiAttackWithForce(1) end,

  function() return AiSleep(SleepFactor*100) end,
  function() return AiUpgradeTo(AiBetterCityCenter()) end,
  function() return AiNeedUnit(AiLumberMill()) end,
  function() return AiResearch(AiUpgradeWeapon1()) end,
  function() return AiResearch(AiUpgradeArmor1()) end,
  function() return AiResearch(AiUpgradeMissile1()) end,
  function() return AiResearch(AiUpgradeWeapon2()) end,
  function() return AiResearch(AiUpgradeArmor2()) end,
  function() return AiResearch(AiUpgradeMissile2()) end,
  function() return AiResearch(AiUpgradeWorker1()) end,
  function() return AiNeedUnit(AiBarracks()) end,
  function() return AiNeedUnit(AiStables()) end,
  function() return AiForce(0, {AiSoldier(), 3, AiShooter(), 2,AiCavalry(), 1}) end,
  function() return AiForce(1, {AiSoldier(), 3, AiShooter(), 1,AiCavalry(), 3}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  


  function() return AiSleep(SleepFactor*100) end,
  function() return AiSet(AiWorker1(), 10) end,
  function() return AiForce(0, {AiSoldier(), 3, AiShooter(), 2}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 1, AiCavalry(), 6, AiCatapult(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiUpgradeTo(AiBestCityCenter()) end,
  ---function() return AiSet(AiWorker1(), 10) end,
  function() return AiWait(AiBestCityCenter()) end, 

  function() return AiForce(1, {AiSoldier(), 3, AiShooter(), 1, AiCatapult(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiSleep(SleepFactor*100) end,
  function() return AiNeedUnit(AiAirport()) end,
  function() return AiNeedUnit(AiCityCenter()) end,
  function() return AiNeedUnit(AiStables()) end,
  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 6, AiCatapult(), 1, AiFlyer(), 1}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 2, AiCatapult(), 1, AiFlyer(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,
    
  --function() return AiUpgradeTo(AiBestCityCenter()) end,
  ---function() return AiSet(AiWorker1(), 10) end,
  ---function() return AiWait(AiBestCityCenter()) end,  
    
  function() return AiSleep(SleepFactor*100) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiBarracks()) end,
  --function() return AiNeedUnit(AiAirport()) end,
  function() return AiNeedUnit(AiStables()) end,
  
  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 6, AiCatapult(), 1}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 2, AiCatapult(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiSleep(SleepFactor*500) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  --function() return AiNeedUnit(AiTemple()) end,
-- function() return AiResearch(AiUpgradeCavalryMage()) end,
  --function() return AiResearch(AiCavalryMageSpell1()) end,
  --function() return AiResearch(AiCavalryMageSpell2()) end,

  --function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 4, AiCavalryMage(), 0, AiCatapult(), 1}) end,
  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 4, AiCatapult(), 1}) end,
  --function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 4, AiCavalryMage(), 0, AiCatapult(), 1}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 4, AiCatapult(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiSleep(SleepFactor*500) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiMageTower()) end,
  function() return AiResearch(AiMageSpell4()) end,
--  function() return AiResearch(AiMageSpell5()) end,

  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 6, AiCatapult(), 3, AiMage(), 2}) end,
  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 2, AiCatapult(), 1, AiMage(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiSleep(SleepFactor*500) end,
  --function() return AiResearch(AiMageSpell1()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  --function() return AiResearch(AiMageSpell3()) end,

  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 6, AiCatapult(), 1, AiMage(), 2}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCavalry(), 2, AiCatapult(), 1, AiMage(), 1}) end,
  function() return AiWaitForce(1) end,
  function() return AiAttackWithForce(1) end,

  function() return AiSleep(SleepFactor*100) end,
  function() return AiNeedUnit(AiAirport()) end,

  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 2, AiCatapult(), 1, AiMage(), 5}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 2, AiCatapult(), 1, AiMage(), 1}) end,
  function() return AiForce(2, {AiFlyer(), 1}) end,
  function() return AiWaitForce(2) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(2) end,

  function() return AiSleep(SleepFactor*500) end,
  --function() return AiResearch(AiUpgradeEliteShooter()) end,
  --function() return AiResearch(AiUpgradeEliteShooter1()) end,
  --function() return AiResearch(AiMageSpell2()) end,
  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 0, AiShooter(), 2, AiCatapult(), 1, AiMage(), 5}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 0, AiShooter(), 2, AiCatapult(), 1, AiMage(), 1}) end,
  function() return AiForce(2, {AiFlyer(), 1}) end,
  function() return AiWaitForce(2) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(2) end,

  function() return AiSleep(SleepFactor*500) end,
  function() return AiNeedUnit(AiCityCenter()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiNeedUnit(AiScientific()) end,
--  function() return AiResearch(AiUpgradeEliteShooter2()) end,

  function() return AiForce(0, {AiSoldier(), 1, AiShooter(), 0, AiShooter(), 2, AiCatapult(), 1, AiMage(), 5}) end,
  function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 0, AiShooter(), 2, AiCatapult(), 1, AiMage(), 1}) end,
  function() return AiForce(2, {AiFlyer(), 1}) end,
  function() return AiWaitForce(2) end,
  function() return AiAttackWithForce(1) end,
  function() return AiAttackWithForce(2) end,

  function() return AiSleep(SleepFactor*500) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiCannonTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiResearch(AiUpgradeCatapult1()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiCannonTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiResearch(AiUpgradeCatapult2()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiCannonTower()) end,
  function() return AiNeedUnit(AiTower()) end,
  function() return AiUpgradeTo(AiGuardTower()) end,
  function() return AiSet(AiWorker1(), 25) end,

  -- Everything researched...

  function() return AiLandAttackEndloop() end,
}

function AiLandAttack()
  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", "*", "land-attack", AiLandAttack)
