//GLOBAL DECELERATION
//------------------------------------------------
globals
player user = Player(0)
player debug_user = Player(2)
unit array enemy_halls
integer enemy_halls_length = 0
endglobals
//-------------------------------------------------------------------------------------------------
// DEBUG FUNCTIONS
//-------------------------------------------------------------------------------------------------
function B2S takes boolean b returns string
if b then
return "true"
else
return "false"
endif
endfunction
function Dig2Str takes integer i returns string
if i == 1 then
return "1"
elseif i == 2 then
return "2"
elseif i == 3 then
return "3"
elseif i == 4 then
return "4"
elseif i == 5 then
return "5"
elseif i == 6 then
return "6"
elseif i == 7 then
return "7"
elseif i == 8 then
return "8"
elseif i == 9 then
return "9"
else
return "0"
endif
endfunction
// Courtesy of AIAndy and Tommi. See source:
// http://www.hiveworkshop.com/threads/two-custom-campaign-ais-examples.8939/
function Int2Str takes integer ic returns string
local string s = ""
local integer i = ic
local integer ialt = 0
local boolean neg = false
if i == 0 then
return "0"
endif
if i < 0 then
set neg = true
set i = (-1)*i
endif
loop
exitwhen i == 0
set ialt = i
set i = i / 10
set s = Dig2Str( ialt - 10*i ) + s
endloop
if neg then
set s = "-"+s
endif
return s
endfunction
function Msg takes string message returns nothing
local integer i = 0
loop
call DisplayTextToPlayer(Player(i),0,0,message)
set i = i + 1
exitwhen i == 12
endloop
endfunction
//-------------------------------------------------------------------------------------------------
function SaveEnemyHalls takes nothing returns nothing
local group enemy_units = CreateGroup()
local unit u
local integer id
set enemy_halls_length = 0
call Msg("Checking for enemy halls")
call GroupEnumUnitsOfPlayer(enemy_units, user, null)
set u = FirstOfGroup(enemy_units)
loop
exitwhen u == null
if UnitAlive(u) then
set id = GetUnitTypeId(u)
if (id == TOWN_HALL or id == KEEP or id == CASTLE) or (id == GREAT_HALL or id == FORTRESS or id == STRONGHOLD) or (id == NECROPOLIS_1 or id == NECROPOLIS_2 or id == NECROPOLIS_3) or (id == TREE_LIFE or id == TREE_AGES or id == TREE_ETERNITY) then
set enemy_halls_length = enemy_halls_length + 1
set enemy_halls[enemy_halls_length] = u
endif
endif
call GroupRemoveUnit(enemy_units,u)
set u = FirstOfGroup(enemy_units)
endloop
call Msg((Int2Str(enemy_halls_length) + " halls in array"))
call DestroyGroup(enemy_units)
set enemy_units = null
set u = null
endfunction
function LoadEnemyHall takes nothing returns unit
local unit u
if enemy_halls_length == 0 then
call Msg("No enemy halls in array")
return null
else
set u = enemy_halls[GetRandomInt(1,enemy_halls_length)]
if UnitAlive(u) then
call Msg("Enemy base acquired")
return u
else
call Msg("Dead enemy hall")
return null
endif
endif
endfunction
function AttackEnemyHall takes unit hall returns nothing
local integer timeout_seconds = M3
if hall == null then
call Msg("No enemy hall, exiting attack function")
call Sleep(3)
return
endif
call AttackMoveKill(hall)
call Msg("Starting attack check loop")
loop
if not UnitAlive(hall) then
call Msg("Enemy hall dead, exiting attack loop")
exitwhen true
endif
if CaptainGroupSize() == 0 then
call Msg("All units dead, exiting attack loop")
exitwhen true
endif
if (timeout_seconds <= 0) and not CaptainInCombat(true) then
call Msg("3 minutes timeout passed, exiting attack loop")
exitwhen true
endif
call Sleep(2.0)
set timeout_seconds = timeout_seconds - 2
endloop
call Sleep(2.0)
call CaptainGoHome()
endfunction
//ATTACK LOOP
function Attackloop takes nothing returns nothing
local unit base
call Sleep( 30 )
loop
//FIRST WAVE
call Msg("setting up the first attack wave")
call InitAssaultGroup()
call CampaignAttackerEx( 4, 4, 4, 'ogru' ) //GRUNTS
call Msg("units added to the first wave, now waiting")
call SuicideOnPlayerEx(M2,M2,M2,user)
call Msg("first wave attack finished")
//SECOND WAVE
call Msg("setting up the second attack wave")
call Sleep(5.0)
loop
call SaveEnemyHalls()
exitwhen enemy_halls_length > 0
call Sleep(2.0)
endloop
call InitAssaultGroup()
call CampaignAttackerEx( 2, 3, 3, 'ohun' )
call Msg("units added to the second wave, now waiting")
call FormGroup( M2, true )
call AttackEnemyHall(LoadEnemyHall())
call Msg("second wave attack fnished")
endloop
endfunction
//BUILD ORDER
function BuildOrder takes nothing returns nothing
local integer mines = GetMinesOwned()
call SetBuildUnitEx( 10,10,10, 'opeo' )
call SetBuildUnitEx( 5, 5, 5, 'otrb' )
call BasicExpansion( mines < 2, 'ogre' )
call GuardSecondary( 1, 2, 'owtw' )
call GuardSecondary( 0, 2, 'owtw' )
call SetBuildUnitEx( 1, 1, 1, 'oalt' )
endfunction
//BUILD LOOP
function Guardloop takes nothing returns nothing
call BuildOrder()
call StaggerSleep(1,2)
loop
call BuildOrder()
call Sleep(2)
endloop
endfunction
//MAIN
function main takes nothing returns nothing
call CampaignAI('otrb', null)
call DoCampaignFarms(false)
call Msg("AI started")
call BuildOrder()
call CampaignDefenderEx(4,4,4, 'ogru')
call SetCaptainHome(ATTACK_CAPTAIN, -5800.,-2000.)
call StartThread(function Guardloop)
call Attackloop()
endfunction