
// these macros get expanded on the host if the @1 army is AI controlled


// @1 = color of army to do AI for, @2 = enemy color,
// @3 = this colors owner number, @4 = enemy colors owner numbers
// @5 = action number for attcking this enemy
macro koth_enemy_ai 5
{
	if @1TakeHill testvar hillowner = @4 then
		setvar @1Action @5,
		order @1 group 1 follow ai@2 inmode defend,
		order @1 group 2 follow ai@2 inmode attack,
		order @1 group 3 follow ai@2 inmode attack,
		order @1 group 4 follow ai@2 inmode attack,
		order @1 group 5 follow ai@2 inmode attack,
		trigger @1StartTimeOut
}

// @1 = color of army to do AI for, @2-@4 = other colors,
// @5 = this colors owner number, @6-8 = other colors owner numbers
macro koth_ai_macro 8
{
	// This variable holds the current action for @1
	// 0 means no action, 1 means goto hill, 2 means get powerups, 3 means defend hill,
	// 4 means attack @2 , 5 means attack @3 , 6 means attack @4 

	expand koth_enemy_ai ( @1 @2 @5 @6 4 )
	expand koth_enemy_ai ( @1 @3 @5 @7 5 )
	expand koth_enemy_ai ( @1 @4 @5 @8 6 )

	if startup1 then
		triggerdelay 1000 @1ChooseNewAction

	if @1ChooseNewAction then
		setvar @1Action -1,
		trigger @1CheckIfOnHill,
		trigger @1CheckIfHillEmpty,
		trigger @1ChooseAction

	// if it's time to choose a new action but we are currently owning the hill then
	// we should always stay on the hill

	if @1CheckIfOnHill testvar @1Action = -1 and hillowner = @5 then
		trigger @1DefendHill
		
	// if it's time to choose a new action and the hill is not owned then head straight for it

	if @1CheckIfHillEmpty testvar @1Action = -1 and hillowner = 0 then
		trigger @1GotoHill
		
	// if @1 has no current action then pick an action
	// ideally we would base this on how well armed we are
	if @1ChooseAction testvar @1Action = -1 then 
		trigger @1TakeHill

	// these events setup the @1Action variable and do the actions

	if @1GotoHill then
		setvar @1Action 1,
		order @1 group 1 goto hill inmode defend,
		order @1 group 2 goto hill inmode attack,
		order @1 group 3 goto hill inmode attack,
		order @1 group 4 goto hill inmode attack,
		order @1 group 5 goto hill inmode attack,
		trigger @1StartTimeOut

	if @1DefendHill then
		setvar @1Action 3,
		order @1 group 1 goto @1hill inmode defend,
		order @1 group 2 goto @1hill inmode defend,
		order @1 group 3 goto @1hill inmode defend,
		order @1 group 4 goto @1hill inmode defend,
		order @1 group 5 goto @1hill inmode defend,
		trigger @1StartTimeOut

	// these events chose a new action if we get on the hill

	if hillOwnerChanged then
		trigger @1ChooseNewAction

	// timeout code - if mode has not changed for a while then change it

	variable @1TimeOutVar 0
	variable @1LastTimeOutVar 1

	if @1StartTimeOut then
		addvar @1TimeOutVar 1,
		triggerdelay 12000 @1CheckTimeOut

	if @1CheckTimeOut testvar @1TimeOutVar = @1LastTimeOutVar then
		trigger @1ChooseNewAction

	if @1CheckTimeOut testvar @1TimeOutVar <> @1LastTimeOutVar then
		addvar @1LastTimeOutVar 1
}