Looping a Patrol

How to loop a block of code

Information and discussion for custom maps and mods.
Post Reply
AmericanBlunt_
Posts: 56
Joined: Wed Apr 02, 2014 8:42 pm
Location: Mars

Looping a Patrol

Post by AmericanBlunt_ »

I'm trying to loop through a piece of code after the scripted behavior is finished. On my map, "Purgatory" https://resource.openra.net/maps/35829/
I have it scripted to where this code

Code: Select all

EggMonsterEntryPoints = { MonsterWaypoint }
EggSpawnPoints = { Waypoint, Waypoint1, Waypoint2, Waypoint3, Waypoint4, Waypoint5, Waypoint6, Waypoint7 }
 
-----------Egg Get Monsters sets
 
if Map.LobbyOption("difficulty") == "hard" then
    Mutants = { "pvice", "pvice" }
    Mutantsx2 = { "pvice", "pvice", "pvice"  }
    Arachnotron = { "bspi", "bspi", }
    Arachnotronx2 = { "bspi", "bspi", "bspi" }
    Reve = { "reve", "reve" }
    Revex2 = { "reve", "reve", "reve" }
    Imp = { "imp", "imp" }
    Impx2 = { "imp", "imp", "imp" }
    Demon = { "pinky", "pinky" }
    Demonx2 = { "pinky", "pinky", "pinky" }
    Mancubus = { "manc", "manc" }
    Mancubusx2 = { "manc", "manc" }
elseif Map.LobbyOption("difficulty") == "normal" then
    Mutants = { "pvice", "pvice" }
    Mutantsx2 = { "pvice", "pvice", "pvice" }
    Arachnotron = { "bspi", "bspi" }
    Arachnotronx2 = { "bspi", "pvice", "pvice" }
    Reve = { "reve", "reve", "bspi" }
    Revex2 = { "reve", "pvice", "pvice", "bspi" }
    Imp = { "imp", "imp", "bspi", "pvice" }
    Impx2 = { "imp", "pvice", "pvice", "reve" }
    Demon = { "pinky", "pinky" }
    Demonx2 = { "pinky", "pvice", "pvice", "imp" }
    Mancubus = { "manc", "manc" }
    Mancubusx2 = { "manc", "pvice", "pvice", "pinky" }
elseif Map.LobbyOption("difficulty") == "easy" then
    Mutants = { "pvice" }
    Mutantsx2 = { "pvice" }
    Arachnotron = { "bspi" }
    Arachnotronx2 = { "bspi" }
    Reve = { "reve" }
    Revex2 = { "reve" }
    Imp = { "imp" }
    Impx2 = { "imp" }
    Demon = { "pinky" }
    Demonx2 = { "pinky" }
    Mancubus = { "manc" }
    Mancubusx2 = { "manc" }
end
-----------Egg Get Monsters sets
EggWave = 0
EggWaves =
{
    { delay = 1, units = { } },--00
    { delay = 1, units = { Mutants } },--45
    { delay = 1, units = { Mutants, Mutants, Mutantsx2, Arachnotron, Arachnotronx2 } },--2:00
    { delay = 1, units = { Mutants, Mutants, Mutantsx2, Mutantsx2, Arachnotron, Arachnotron } },--240
    { delay = 1, units = { Mutants, Arachnotron, Mutantsx2, Mutantsx2, Mutantsx2, Arachnotronx2, Arachnotron, Arachnotron } },--320
    { delay = 1, units = { Mutantsx2, Mutantsx2, Reve, Arachnotron, Reve, Arachnotron, Mutants, Mutants, } },--440
    { delay = 1, units = { Mutantsx2, Arachnotron, Reve, Mutantsx2, Arachnotronx2, Arachnotronx2, Arachnotron, Mutants } },--420
    { delay = 1, units = { } },
    { delay = 1, units = { Mutantsx2, Mutants, Mutantsx2, Arachnotron, Arachnotron, Arachnotronx2, Revex2, Reve, Reve, Mutantsx2 } },--500
    { delay = 1, units = { Arachnotronx2, Mutantsx2, Revex2, Reve, Imp, Mutants, Mutants } },--540
    { delay = 1, units = { Revex2, Revex2, Imp, Imp, Arachnotronx2, Arachnotronx2, Mutantsx2, Mutantsx2, Mutantsx2 } },--620
    { delay = 1, units = { } },
    { delay = 1, units = { Imp, Imp, Imp, Imp, Imp, Mutantsx2, Mutantsx2, Revex2 } },--700
    { delay = 1, units = { Impx2, Impx2, Impx2, Mutantsx2, Mutantsx2, Mutantsx2, Revex2 } },--740
    { delay = 1, units = { Impx2, Revex2, Arachnotronx2,Mutantsx2 } },--820
    { delay = 1, units = { Demon, Demon, Demon, Demon, Reve, Reve, Mancubus } },--920
    { delay = 1, units = { } },--1000
    { delay = 1, units = { Demonx2, Demon, Demon, Impx2, Revex2 } },--10:40
    { delay = 1, units = { Demon, Demon, Mutantsx2 } },--11:20
    { delay = 1, units = { Mutants, Mutants, Mancubus, Mutantsx2, Reve, Reve, Reve, Reve } },--240
    { delay = 1, units = { } },
    { delay = 1, units = { Mancubus, Mancubus, Reve } }
}
--send patrol units
SendUnits = function(entryCell, unitTypes, targetCell)
    Reinforcements.Reinforce(Monsters, unitTypes, { entryCell }, 40, function(a)
        if not a.HasProperty("AttackMove") then
            Trigger.OnIdle(a, function(a)
                a.Move(targetCell)
            end)
            return
        end
 
        a.AttackMove(targetCell)
        Trigger.OnIdle(a, function(a)
            a.Hunt()
        end)
    end)
end
--increase difficulty after loop
EggIncreaseDifficulty = function()
    local additions = { Mutants, Arachnotron, Reve, Imp, Demon, Mancubus }
    Utils.Do(EggWaves, function(wave)
        wave.units[#wave.units + 1] = Utils.Random(additions)
    end)
end
--calls patrol
EggSendWave = function()
    if Egg.IsDead then
        return
    end
   
       
    EggWave = EggWave + 1
    local wave = EggWaves[EggWave]
    Trigger.AfterDelay(wave.delay, function()
        Utils.Do(wave.units, function(units)
            local entry = Utils.Random(EggMonsterEntryPoints).Location
            local target = Utils.Random(EggSpawnPoints).Location
            SendUnits(entry, units, target)
        end)
        if (EggWave < #EggWaves) then
            delayFirstTeam = 55
            local delay = delayFirstTeam
            Trigger.AfterDelay(DateTime.Seconds(delay), function()
                EggSendWave()
                Trigger.AfterDelay(DateTime.Minutes(14), EggIncreaseDifficulty)
            end)
        end
    end)
end
 
if (EggWave == null) then
    EggSendWave()
end
it calls a monster patrol and sends them from "Egg" to a waypoint with a certain behavior. After running through the list/array:

Code: Select all

EggWave = 0
EggWaves =
{
    { delay = 1, units = { } },--00
    { delay = 1, units = { Mutants } },--45
    { delay = 1, units = { Mutants, Mutants, Mutantsx2, Arachnotron, Arachnotronx2 } },--2:00
    { delay = 1, units = { Mutants, Mutants, Mutantsx2, Mutantsx2, Arachnotron, Arachnotron } },--240
    { delay = 1, units = { Mutants, Arachnotron, Mutantsx2, Mutantsx2, Mutantsx2, Arachnotronx2, Arachnotron, Arachnotron } },--320
    { delay = 1, units = { Mutantsx2, Mutantsx2, Reve, Arachnotron, Reve, Arachnotron, Mutants, Mutants, } },--440
    { delay = 1, units = { Mutantsx2, Arachnotron, Reve, Mutantsx2, Arachnotronx2, Arachnotronx2, Arachnotron, Mutants } },--420
    { delay = 1, units = { } },
    { delay = 1, units = { Mutantsx2, Mutants, Mutantsx2, Arachnotron, Arachnotron, Arachnotronx2, Revex2, Reve, Reve, Mutantsx2 } },--500
    { delay = 1, units = { Arachnotronx2, Mutantsx2, Revex2, Reve, Imp, Mutants, Mutants } },--540
    { delay = 1, units = { Revex2, Revex2, Imp, Imp, Arachnotronx2, Arachnotronx2, Mutantsx2, Mutantsx2, Mutantsx2 } },--620
    { delay = 1, units = { } },
    { delay = 1, units = { Imp, Imp, Imp, Imp, Imp, Mutantsx2, Mutantsx2, Revex2 } },--700
    { delay = 1, units = { Impx2, Impx2, Impx2, Mutantsx2, Mutantsx2, Mutantsx2, Revex2 } },--740
    { delay = 1, units = { Impx2, Revex2, Arachnotronx2,Mutantsx2 } },--820
    { delay = 1, units = { Demon, Demon, Demon, Demon, Reve, Reve, Mancubus } },--920
    { delay = 1, units = { } },--1000
    { delay = 1, units = { Demonx2, Demon, Demon, Impx2, Revex2 } },--10:40
    { delay = 1, units = { Demon, Demon, Mutantsx2 } },--11:20
    { delay = 1, units = { Mutants, Mutants, Mancubus, Mutantsx2, Reve, Reve, Reve, Reve } },--240
    { delay = 1, units = { } },
    { delay = 1, units = { Mancubus, Mancubus, Reve } }
}
If not destroyed, the eggs stop producing more monsters over time. I want to know if there's a way I can loop indefinitely through this code. I tried here:

Code: Select all

if (EggWave == null) then
    EggSendWave()
end
with no success. :)

abcdefg30
Posts: 641
Joined: Mon Aug 18, 2014 6:00 pm

Re: Looping a Patrol

Post by abcdefg30 »

The simplest would be to reset "EggWave" to zero again, by changing

Code: Select all

    if (EggWave < #EggWaves) then
        delayFirstTeam = 55
        local delay = delayFirstTeam
        Trigger.AfterDelay(DateTime.Seconds(delay), function()
            EggSendWave()
            Trigger.AfterDelay(DateTime.Minutes(14), EggIncreaseDifficulty)
        end)
    end
to

Code: Select all

    if EggWave >= #EggWaves then
        EggWave = 0
    end

    delayFirstTeam = 55
    local delay = delayFirstTeam
    Trigger.AfterDelay(DateTime.Seconds(delay), function()
        EggSendWave()
        Trigger.AfterDelay(DateTime.Minutes(14), EggIncreaseDifficulty)
    end)
Btw, in Lua "null" is written "nil".

AmericanBlunt_
Posts: 56
Joined: Wed Apr 02, 2014 8:42 pm
Location: Mars

Re: Looping a Patrol

Post by AmericanBlunt_ »

Thanks!

Post Reply