// @1 = color to do AI for, @2 = color of enemy, @3 = value for @1Action variable
macro killer_ai_enemy 3
{
	// these events setup the @1Action variable

	// before attacking @2 make sure @2 is in the game and is not an ally
	if @1SetAttack@2 testvar IsColorInGame @2 > 0 and IsAlly @1 @2 = 0 then
		setvar @1Action 1,
		trigger @1Attack@2,
		trigger @1StartTimeOut

	// these events do the actual attack orders

	if @1Attack@2 testvar @1Action = @3 then
		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
}

// @1 = color of army to do AI for, @2-@4 = other colors @5 = color number of army
macro killer_ai_macro 5
{
	// This variable holds the current action for @1
	// 0 means no action, 1 means attack @2, 2 means attack @3, 3 means attack @4,
	// 4 means follow black flag

	if startup1 then
		triggerdelay 1000 @1ChooseNewAction,
		triggerdelay 1000 @1Group1Target,
		triggerdelay 2000 @1RegularOrders

	if @1Group1Target THen
		order @1 group 1 follow blackflag1 inmode attack,
		triggerdelay 1000 @1Group1Target

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

	// if @1 has no current action then pick a random action
	// keep doing this until an action is set
	if @1ChooseAction testvar @1Action = -1 and  hasBlackFlag = @5  then // if we have the flag
		trigger @1PickRandomAction,
		triggerdelay 1000 @1ChooseAction

	if @1ChooseAction testvar @1Action = -1 and  hasBlackFlag <> @5 then // if we dont have the flag
		trigger @1FollowBlackFlag

	if @1PickRandomAction then random
		trigger @1SetAttack@2,
		trigger @1SetAttack@3,
		trigger @1SetAttack@4

	expand killer_ai_enemy ( @1 @2 1 )
	expand killer_ai_enemy ( @1 @3 2 )
	expand killer_ai_enemy ( @1 @4 3 )

	if @1FollowBlackFlag then
		setvar @1Action 4,
		trigger @1StartTimeOut,
		order @1 group 2 follow blackflag1 inmode attack,
		order @1 group 3 follow blackflag1 inmode attack,
		order @1 group 4 follow blackflag1 inmode attack

	if pickedup blackflag1 by @1 trooper then
		triggerdelay 10 @1KeepFlag whoby,
		triggerdelay 100 @1ChooseNewAction

	// black flag holder runs away from front to allow more time for fellows to kill

	if @1Keepflag testvar hasitem me blackflag1 = 1 THEN
		order me goto @1base inmode ignore,
		triggerdelay 2000 @1KeepFlag


	// 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 120000 @1CheckTimeOut

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

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

