-- LuaDC version 0.9.19
-- 5/23/2004 11:08:05 PM
-- LuaDC by Age2uN
-- on error send source file (compiled lua) and this outputfile to Age2uN@gmx.net
--
aitrace("LOADING CPU MILITARY")
function CpuMilitary_Init()
    cp_attackPercent = 100
    if  g_LOD==0 then
        cp_attackPercent = 100
    end 

    cp_minSquadGroupSize = 5
    cp_minSquadGroupValue = 200
    cp_maxGroupSize = 14
    cp_maxGroupValue = 200
    cp_forceAttackGroupSize = 14
    if  g_LOD==1 then
        cp_forceAttackGroupSize = 12
    end 

    if  g_LOD==0 then
        cp_forceAttackGroupSize = 8
    end 

    cp_initThreatModifier = 0.95

    sg_moreEnemies = 0
    sg_militaryRand = Rand(100)
    if  Override_MilitaryInit then
        Override_MilitaryInit()
    end 

end

function CpuMilitary_Process()
    local numEnemies = PlayersAlive(player_enemy)
    local numAllies = PlayersAlive(player_ally)
    sg_moreEnemies = (numEnemies - numAllies)
    Logic_military_groupvars()
    Logic_military_attackrules()
    Logic_military_setattacktimer()
end

function Logic_military_groupvars()
    cp_minSquadGroupSize = 4
    cp_minSquadGroupValue = 50
    if  sg_moreEnemies>0 and s_selfTotalValue<s_enemyTotalValue * 2 then
        cp_minSquadGroupSize = (cp_minSquadGroupSize + 2)
        cp_minSquadGroupValue = (cp_minSquadGroupValue + 75)
    elseif  s_militaryStrength>120 then
        cp_minSquadGroupSize = 3
        cp_minSquadGroupValue = 120
    end 

end

function Logic_military_attackrules()
   

        if  s_selfTotalValue * 2<s_enemyTotalValue and s_selfTotalValue>250 then
            cp_attackPercent = 0
            aitrace("I'm Losing!! Go on defence")
        end 


end

function attack_now_timer()
    aitrace("Script:calling attack_now_timer")
    AttackNow()
    Rule_Remove("attack_now_timer")
end

function Logic_military_setattacktimer()
    local timedelay = 160
    local wavedelay=timedelay/2
    if  g_LOD==1 then
        timedelay = 100
        wavedelay = (90 + sg_militaryRand * 0.4)
    end 

    if  g_LOD>=2 then
        timedelay = 0
        wavedelay = (45 + sg_militaryRand * 0.3)
    end 

    local gametime = gameTime()
    if  gametime>=timedelay or HaveBeenAttacked()==1 then
        if  Rule_Exists("attack_now_timer")==0 then
            aitrace("Script: Attacktimer added")
		print("wave delay"..wavedelay)
            Rule_AddInterval("attack_now_timer", wavedelay)
        end 

    end 

end
