--	Stratagus Map - Single player campaign

Load("campaigns/human/level14h_c2.sms")

Briefing(
  title,
  objectives,
  "../campaigns/human/interface/introscreen5.png",
  "campaigns/human/level14h.txt",
  {"campaigns/human/level14h-intro1.wav"}
)

-- FIXME: Check if units are destroyed.

AddTrigger(
  function() return GetPlayerData(15, "UnitTypesCount", "unit-dark-portal") == 0 end,
  function() return ActionVictory() end)

AddTrigger(
  function() return GetPlayerData(GetThisPlayer(), "TotalNumUnits") == 0 end,
  function() return ActionDefeat() end)

--Units
local allowedHumanUnits = {"unit-farm",
	"unit-town-hall", "unit-keep", "unit-castle", "unit-peasant",
	"unit-human-barracks", "unit-footman", "unit-archer", "unit-ranger", "unit-ballista",
	"unit-elven-lumber-mill", "upgrade-arrow1", "upgrade-arrow2",
	"upgrade-ranger", "upgrade-longbow", "upgrade-ranger-scouting", "upgrade-ranger-marksmanship",
	"unit-human-blacksmith", "upgrade-sword1", "upgrade-human-shield1", "upgrade-ballista1",
	"upgrade-sword2", "upgrade-human-shield2", "upgrade-ballista2",
	"unit-human-watch-tower", "unit-human-guard-tower", "unit-human-cannon-tower",
	"unit-human-shipyard", "unit-human-oil-tanker", "unit-human-destroyer", "unit-human-transport",
	"unit-battleship", "unit-human-submarine",
	"unit-human-oil-platform", "unit-human-refinery",
	"unit-human-foundry", "upgrade-human-ship-cannon1", "upgrade-human-ship-armor1",
	"upgrade-human-ship-cannon2", "upgrade-human-ship-armor2",
	"unit-inventor", "unit-balloon", "unit-dwarves",
	"unit-stables", "unit-knight", "unit-paladin",
	"unit-church", "upgrade-paladin", "upgrade-holy-vision", "upgrade-healing", "upgrade-exorcism",
	"unit-mage-tower", "unit-mage",
	"upgrade-flame-shield", "upgrade-fireball", "upgrade-slow",
	"upgrade-invisibility", "upgrade-polymorph", "upgrade-blizzard",
	"unit-gryphon-aviary", "unit-gryphon-rider"
}
local allowedOrcUnits = {"unit-pig-farm",
	"unit-great-hall", "unit-stronghold", "unit-fortress", "unit-peon",
	"unit-orc-barracks", "unit-grunt", "unit-axethrower", "unit-berserker", "unit-catapult",
	"unit-troll-lumber-mill", "upgrade-throwing-axe1", "upgrade-throwing-axe2",
	"upgrade-berserker", "upgrade-light-axes", "upgrade-berserker-scouting", "upgrade-berserker-regeneration",
	"unit-orc-blacksmith", "upgrade-battle-axe1", "upgrade-orc-shield1", "upgrade-catapult1",
	"upgrade-battle-axe2", "upgrade-orc-shield2", "upgrade-catapult2",
	"unit-orc-watch-tower", "unit-orc-guard-tower", "unit-orc-cannon-tower",
	"unit-orc-shipyard", "unit-orc-oil-tanker", "unit-orc-destroyer", "unit-orc-transport",
	"unit-ogre-juggernaught", "unit-orc-submarine",
	"unit-orc-oil-platform", "unit-orc-refinery",
	"unit-orc-foundry", "upgrade-orc-ship-cannon1", "upgrade-orc-ship-armor1",
	"upgrade-orc-ship-cannon2", "upgrade-orc-ship-armor2",
	"unit-alchemist", "unit-zeppelin", "unit-goblin-sappers",
	"unit-ogre-mound", "unit-ogre", "unit-ogre-mage",
	"unit-altar-of-storms", "upgrade-ogre-mage", "upgrade-eye-of-kilrogg", "upgrade-bloodlust", "upgrade-runes",
	"unit-temple-of-the-damned", "unit-death-knight",
	"upgrade-death-coil", "upgrade-haste", "upgrade-raise-dead",
	"upgrade-whirlwind", "upgrade-unholy-armor", "upgrade-death-and-decay",
	"unit-dragon-roost", "unit-dragon"
}
local function DefineAllowTable(table, flags)
	for i, unitName in ipairs(table) do
		DefineAllow(unitName, flags)
	end
end

DefineAllowNormalHumanUnits("FFFFFFFFFFFFFFFF")
DefineAllowExtraHumanUnits("FFFFFFFFFFFFFFFF")
DefineAllowNormalOrcUnits("FFFFFFFFFFFFFFFF")
DefineAllowExtraOrcUnits("FFFFFFFFFFFFFFFF")
DefineAllowSpecialUnits("FFFFFFFFFFFFFFFF")

DefineAllowTable(allowedHumanUnits, "AAAAAAAAAAAAAAAA")
DefineAllowTable(allowedOrcUnits, "AAAAAAAAAAAAAAAA")

-- Default Spells Researched
DefineAllowHumanAlways()
DefineAllowOrcAlways()

-- AI

--=============================================================================
-- This AI script builds only workers and ogres.

local hum_14_orange_loop_funcs = {
	function() DebugPrint("Looping !\n") return false end,
	function() return AiSleep(30000) end,
	function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false end,
}

local hum_14_orange_funcs = {
	function() AiForceRole(0, "defend"); AiForceRole(1, "attack"); return false end,
	function() return AiSet(AiWorker(), 5) end,
	function() return AiWait(AiWorker()) end,
	function() return AiForce(1, {AiCavalry(), 15}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); AiAttackWithForce(1); return false; end,
	function() return AiLoop(hum_14_orange_loop_funcs, stratagus.gameData.AIState.loop_index) end,
}

function AiHuman14Orange() return AiLoop(hum_14_orange_funcs, stratagus.gameData.AIState.index) end
DefineAi("hum-14-orange", "*", "hum-14-orange", AiHuman14Orange)

--=============================================================================
--	This AI script builds nothing.
--		Does only upgrades.
--

local hum_14_red_loop_funcs = {
	function() DebugPrint("Looping !\n") return false end,
	function() return AiSleep(30000) end,
	function() return AiSleep(30000) end,
	function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false end,
}

local hum_14_red_funcs = {
	function() AiForceRole(0, "defend"); AiForceRole(1, "attack"); return false end,
	function() return AiResearch(AiMageSpell1()) end,
	function() return AiResearch(AiMageSpell2()) end,
	function() return AiResearch(AiMageSpell3()) end,
	function() return AiResearch(AiMageSpell4()) end,
	function() return AiResearch(AiMageSpell5()) end,
	function() return AiLoop(hum_14_red_loop_funcs, stratagus.gameData.AIState.loop_index) end,
}

function AiHuman14Red() return AiLoop(hum_14_red_funcs, stratagus.gameData.AIState.index) end
DefineAi("hum-14-red", "*", "hum-14-red", AiHuman14Red)

--=============================================================================
--	This AI script builds only workers and dragons.
--		Does only air attacks.
--
--	FIXME: This AI should only collect GOLD.
local hum_14_white_loop_funcs = {
	function() DebugPrint("Looping !\n") return false end,
	function() return AiForce(1, {AiFlyer(), 10}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); return AiAttackWithForce(1) end,
	function() return AiSleep(19000) end,
	function() return AiForce(1, {AiFlyer(), 10}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); return AiAttackWithForce(1) end,
	function() return AiSleep(20000) end,
	function() return AiForce(1, {AiFlyer(), 12}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); return AiAttackWithForce(1) end,
	function() return AiSleep(21000) end,
	function() return AiForce(1, {AiFlyer(), 12}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); return AiAttackWithForce(1) end,
	function() return AiSleep(20000) end,
	function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false end,
}

local hum_14_white_funcs = {
	function() AiForceRole(0, "defend"); AiForceRole(1, "attack"); return false end,
	function() return AiSet(AiWorker(), 9) end,
	function() return AiSetCollect({0, 100, 0, 0, 0, 0, 0}) end,
	function() return AiWait(AiWorker()) end,
	function() return AiForce(1, {AiFlyer(), 10}) end,
	function() return AiSleep(64000) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); return AiAttackWithForce(1) end,
	function() return AiSleep(12000) end,
	function() return AiForce(1, {AiFlyer(), 10}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); return AiAttackWithForce(1) end,
	function() return AiSleep(18000) end,
	function() return AiLoop(hum_14_white_loop_funcs, stratagus.gameData.AIState.loop_index) end,
}

function AiHuman14White() return AiLoop(hum_14_white_funcs, stratagus.gameData.AIState.index) end
DefineAi("hum-14-white", "*", "hum-14-white", AiHuman14White)

--=============================================================================
--	This AI script builds only tanker.
--		Does only upgrades.
--
--	FIXME: This AI should only collect GOLD.
--

local hum_14_black_loop_funcs = {
	function() DebugPrint("Looping !\n") return false end,
	function() return AiSleep(30000) end,
	function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false end,
}

local hum_14_black_funcs = {
	function() AiForceRole(0, "defend"); AiForceRole(1, "attack"); return false end,
	function() return AiSet(AiTanker(), 1) end,
	function() return AiSetCollect({0, 100, 0, 0, 0, 0, 0}) end,
	function() return AiResearch(AiUpgradeCavalryMage()) end,
	function() return AiResearch(AiCavalryMageSpell1()) end,
	function() return AiResearch(AiCavalryMageSpell2()) end,
	function() return AiResearch(AiMageSpell1()) end,
	function() return AiResearch(AiMageSpell2()) end,
	function() return AiResearch(AiMageSpell3()) end,
	function() return AiResearch(AiMageSpell4()) end,
	function() return AiResearch(AiMageSpell5()) end,
	function() return AiResearch(AiUpgradeWeapon1()) end,
	function() return AiResearch(AiUpgradeArmor1()) end,
	function() return AiResearch(AiUpgradeWeapon2()) end,
	function() return AiResearch(AiUpgradeArmor2()) end,
	function() return AiResearch(AiUpgradeEliteShooter()) end,
	function() return AiResearch(AiUpgradeEliteShooter1()) end,
	function() return AiResearch(AiUpgradeEliteShooter2()) end,
	function() return AiResearch(AiUpgradeEliteShooter3()) end,
	function() return AiResearch(AiUpgradeShipCannon1()) end,
	function() return AiResearch(AiUpgradeShipArmor1()) end,
	function() return AiResearch(AiUpgradeShipCannon2()) end,
	function() return AiResearch(AiUpgradeShipArmor2()) end,
	function() return AiLoop(hum_14_black_loop_funcs, stratagus.gameData.AIState.loop_index) end,
}

function AiHuman14Black() return AiLoop(hum_14_black_funcs, stratagus.gameData.AIState.index) end
DefineAi("hum-14-black", "*", "hum-14-black", AiHuman14Black)

-- Map

Load("campaigns/human/level14h.sms")
