turn_count = 0;

function Start()
    startThread(checkWinAttacker);
    startThread(checkWinDefender);
    sleep(5);
    EnableAutoFinish(nil);
end;

function DefenderCreatureMove(creature)
    turn_count = turn_count + 1;
    if turn_count == 1 then
        if GetUnitManaPoints(creature) >= 20 then
            UnitCastGlobalSpell(creature, SPELL_ARMAGEDDON);
            print("Armageddon casted 1/10");
            sleep(120);
            return 1;
        end;
    end;
    if turn_count == 5 then
        if GetUnitManaPoints(creature) >= 20 then
            UnitCastGlobalSpell(creature, SPELL_ARMAGEDDON);
            print("Armageddon casted 2/10");
            sleep(120);
            return 1;
        end;
    end;
    if turn_count == 10 then
        if GetUnitManaPoints(creature) >= 20 then
            UnitCastGlobalSpell(creature, SPELL_ARMAGEDDON);
            print("Armageddon casted 3/10");
            sleep(120);
            return 1;
        end;
    end;
    if turn_count == 15 then
        if GetUnitManaPoints(creature) >= 20 then
            UnitCastGlobalSpell(creature, SPELL_ARMAGEDDON);
            print("Armageddon casted 4/10");
            sleep(120);
            return 1;
        end;
    end;
    if turn_count == 20 then
        if GetUnitManaPoints(creature) >= 20 then
            UnitCastGlobalSpell(creature, SPELL_ARMAGEDDON);
            print("Armageddon casted 5/10");
            sleep(120);
            turn_count = 0;
            return 1;
        end;
    end;
end;

function checkWinAttacker()
    repeat sleep(1); until length(GetDefenderCreatures()) == 0;

    sleep(30);
    Finish(ATTACKER);
end;

function checkWinDefender()
    repeat sleep(1); until length(GetAttackerCreatures()) == 0;
    
    sleep(30);
    Finish(DEFENDER);
end;