--	Stratagus Map - Single player campaign

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

Briefing(
  title,
  objectives,
  "../campaigns/human/interface/introscreen3.png",
  "campaigns/human/level08h.txt",
  {"campaigns/human/level08h-intro1.wav",
   "campaigns/human/level08h-intro2.wav"}
)

AddTrigger(
  function() return true end,
  function() SetDiplomacy(4, "enemy", 2)
    SetDiplomacy(2, "enemy", 4)
    SetDiplomacy(4, "enemy", 6)
    SetDiplomacy(6, "enemy", 4)
    return false end)

AddTrigger(
  function() return GetPlayerData(GetThisPlayer(), "UnitTypesCount", "unit-castle") >= 1 and
    GetPlayerData(2, "UnitTypesCount", "unit-peasant") == 0 and
    GetPlayerData(4, "UnitTypesCount", "unit-peasant") == 0 and
    GetNumOpponents(GetThisPlayer()) == 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-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-stables", "unit-knight", "unit-paladin",
	"unit-church", "upgrade-paladin", "upgrade-holy-vision", "upgrade-healing"
}
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-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-ogre-mound", "unit-ogre", "unit-ogre-mage",
	"unit-altar-of-storms", "upgrade-ogre-mage", "upgrade-eye-of-kilrogg", "upgrade-bloodlust"
}
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")
DefineAllowHumanAlways()
DefineAllowOrcAlways()
DefineAllowTable(allowedHumanUnits, "AAAAAAAAAAAAAAAA")
DefineAllowTable(allowedOrcUnits, "AAAAAAAAAAAAAAAA")

-- AI
--=============================================================================
--	This AI script builds only workers, blacksmith.
--		Also if needed a farm.
--		Upgrades weapon and missile.
--		Attacks with soldier, shooter and cavalrie.

local hum_08_loop_funcs = {
	function() DebugPrint("Looping !\n") return false end,
	function() return AiSleep(16000) end,
	function() return AiForce(1, {AiSoldier(), 1, AiShooter(), 4, AiCavalry(), 3, AiCatapult(), 1}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); AiAttackWithForce(1); return false; end,
	function() return AiSleep(14000) end,
	function() return AiSleep(12000) end,
	function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false end,
}

local hum_08_funcs = {
	function() return AiSet(AiWorker(), 3) end,
	function() return AiWait(AiWorker()) end,
	function() return AiForce(1, {AiSoldier(), 5, AiShooter(), 4}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); AiAttackWithForce(1); return false; end,
	function() return AiSleep(14000) end,
	function() return AiForce(1, {AiSoldier(), 5, AiShooter(), 5, AiCavalry(), 3}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); AiAttackWithForce(1); return false; end,
	function() return AiSleep(10000) end,
	function() return AiSleep(15000) end,
	function() return AiResearch(AiUpgradeCavalryMage()) end,
	function() return AiResearch(AiCavalryMageSpell1()) end,
	function() return AiResearch(AiUpgradeMissile1()) end,
	function() return AiForce(1, {AiSoldier(), 2, AiShooter(), 5, AiCavalry(), 4, AiCatapult(), 1}) end,
	function() return AiWaitForce(1) end,
	function() DebugMessage("Attacking..."); AiAttackWithForce(1); return false; end,
	function() return AiSleep(15000) end,
	function() return AiResearch(AiUpgradeWeapon1()) end,
	function() return AiResearch(AiUpgradeArmor1()) end,
	function() return AiSleep(21000) end,
	function() return AiResearch(AiUpgradeEliteShooter()) end,
	function() return AiSleep(12000) end,
	function() return AiLoop(hum_08_loop_funcs, stratagus.gameData.AIState.loop_index) end,
}

function AiHuman08() return AiLoop(hum_08_funcs, stratagus.gameData.AIState.index) end
DefineAi("hum-08", "*", "hum-08", AiHuman08)

local hum_08_peasant_funcs = {
    function() return AiForce(1, {AiWorker(), 7}) end,
    function() return AiWaitForce(1) end,
    function() return AiAttackWithForce(1) end,
    function() return AiSleep(12000) end,
    function() stratagus.gameData.AIState.index[1 + AiPlayer()] = 3; return false end,
}

function AiHuman08Peasant() return AiLoop(hum_08_peasant_funcs, stratagus.gameData.AIState.index) end
DefineAi("hum-08-peasant", "*", "hum-08-peasant", AiHuman08Peasant)


-- Map

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

DefinePlayerTypes("computer", "computer", "rescue-active", "computer", "computer", "nobody", "person")
SetAiType(4, "hum-08-peasant")
