Skip to content

Commit

Permalink
Streamline Some Things
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Apr 4, 2024
1 parent 951e489 commit d56803f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/evac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1895,10 +1895,10 @@ function Evac:setup(config)
Evac._internal.beacons.generateFMFrequencies()

timer.scheduleFunction(function()
timer.scheduleFunction(Evac._internal.doSpawns, nil, timer.getTime() + 5)
timer.scheduleFunction(Evac._internal.beacons.killDead, nil, timer.getTime() + 5)
timer.scheduleFunction(Evac._internal.smoke.refresh, nil, timer.getTime() + 5)
timer.scheduleFunction(Evac._internal.menu.updateF10, nil, timer.getTime() + 5)
timer.scheduleFunction(Evac._internal.doSpawns, nil, timer.getTime() + 1)
timer.scheduleFunction(Evac._internal.beacons.killDead, nil, timer.getTime() + 1)
timer.scheduleFunction(Evac._internal.smoke.refresh, nil, timer.getTime() + 1)
timer.scheduleFunction(Evac._internal.menu.updateF10, nil, timer.getTime() + 1)
end, nil, timer.getTime() + 1)

for _name, _def in pairs(Evac._internal.handlers) do
Expand Down
12 changes: 10 additions & 2 deletions src/urgency.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,20 @@ Urgency._internal.doCountdowns = function()

local _now = timer.getTime()

local _needsStart = {}
for _name, _countdown in pairs(Urgency._state.countdowns.pending) do
if (_countdown.startTrigger.type == 'time' and _countdown.startTrigger.value <= _now)
or (_countdown.startTrigger.type == 'flag' and trigger.misc.getUserFlag(_countdown.startTrigger.value) ~= 0)
then
Urgency._internal.startCountdown(_name)
table.insert(_needsStart, _name)
Gremlin.log.trace(Urgency.Id, string.format('%s-Based Countdown Started : %s', _countdown.startTrigger.type, _name))
end
end
for _, _name in pairs(_needsStart) do
Urgency._internal.startCountdown(_name)
end

local _needsEnd = {}
for _name, _countdown in pairs(Urgency._state.countdowns.active) do
local _endTime
if _countdown.endTrigger.type == 'time' then
Expand All @@ -133,10 +138,13 @@ Urgency._internal.doCountdowns = function()
end

if _endTime ~= nil and _now >= _endTime then
Urgency._internal.endCountdown(_name)
table.insert(_needsEnd, _name)
Gremlin.log.trace(Urgency.Id, string.format('Time-Based Countdown Complete : %s', _name))
end
end
for _, _name in pairs(_needsEnd) do
Urgency._internal.endCountdown(_name)
end

Gremlin.log.trace(Urgency.Id, string.format('Time Checked Against Countdowns'))
end
Expand Down
109 changes: 76 additions & 33 deletions src/waves.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Waves = {

config = {
adminPilotNames = {},
waves = {}
waves = {
time = {},
flag = {},
menu = {},
event = {},
}
},

_state = {
Expand Down Expand Up @@ -145,24 +150,49 @@ Waves._internal.getAdminUnits = function()
return _units
end

Waves._internal.initEvents = function()
Gremlin.log.trace(Waves.Id, string.format('Building Events'))

for _name, _wave in pairs(Waves.config.waves.event) do
if _wave.trigger.type == 'event' then
Waves._internal.handlers[_name] = {
event = _wave.trigger.value.id,
fn = function(_event)
if _wave.trigger.value.filter(_event) then
Waves.config.waves.event[_name].trigger.fired = true
Waves._internal.spawnWave(_name, _wave)
end

end
}
else
Gremlin.log.warn(Waves.Id, string.format('Non-event item in event list : %s', _name))
end
end

Gremlin.log.trace(Waves.Id, string.format('Events Ready'))
end

Waves._internal.initMenu = function()
Gremlin.log.trace(Waves.Id, string.format('Building Menu'))

for _name, _wave in pairs(Waves.config.waves) do
for _name, _wave in pairs(Waves.config.waves.menu) do
if _wave.trigger.type == 'menu' then
table.insert(Waves._internal.menu, {
text = _wave.trigger.value or ('Send In Reinforcements : ' .. _name),
func = Waves._internal.menuWave,
args = { _name },
when = {
func = function(_name)
return not Waves._state.paused and not Waves.config.waves[_name].trigger.fired
return not Waves._state.paused and not Waves.config.waves.menu[_name].trigger.fired
end,
args = { _name },
comp = 'equal',
value = true,
}
})
else
Gremlin.log.warn(Waves.Id, string.format('Non-menu item in menu list : %s', _name))
end
end

Expand All @@ -178,11 +208,11 @@ Waves._internal.updateF10 = function()
end

Waves._internal.menuWave = function(_name)
if not Waves._state.paused and not Waves.config.waves[_name].trigger.fired then
if not Waves._state.paused and not Waves.config.waves.menu[_name].trigger.fired then
Gremlin.log.trace(Waves.Id, string.format('Caling In Reinforcements : %s', _name))

Waves.config.waves[_name].trigger.fired = true
Waves._internal.spawnWave(_name, Waves.config.waves[_name])
Waves.config.waves.menu[_name].trigger.fired = true
Waves._internal.spawnWave(_name, Waves.config.waves.menu[_name])

Gremlin.log.trace(Waves.Id, string.format('Reinforcements En Route : %s', _name))
end
Expand All @@ -194,14 +224,21 @@ Waves._internal.timeWave = function()
if not Waves._state.paused then
Gremlin.log.trace(Waves.Id, string.format('Checking On Next Wave'))

for _name, _wave in pairs(Waves.config.waves) do
if (_wave.trigger.type == 'time' and not _wave.trigger.fired and _wave.trigger.value <= timer.getTime())
or (_wave.trigger.type == 'flag' and not _wave.trigger.fired and trigger.misc.getUserFlag(_wave.trigger.value) ~= 0)
then
Waves.config.waves[_name].trigger.fired = true
for _name, _wave in pairs(Waves.config.waves.time) do
if (_wave.trigger.type == 'time' and not _wave.trigger.fired and _wave.trigger.value <= timer.getTime()) then
Waves.config.waves.time[_name].trigger.fired = true
Waves._internal.spawnWave(_name, _wave)
end
end

for _name, _wave in pairs(Waves.config.waves.flag) do
if (_wave.trigger.type == 'flag' and not _wave.trigger.fired and trigger.misc.getUserFlag(_wave.trigger.value) ~= 0) then
Waves.config.waves.flag[_name].trigger.fired = true
Waves._internal.spawnWave(_name, _wave)
end
end

Gremlin.log.trace(Waves.Id, string.format('All Ready Waves Spawned'))
end
end

Expand Down Expand Up @@ -247,27 +284,27 @@ Waves._internal.menu = {
}

Waves._internal.handlers = {
eventTriggers = {
event = -1,
fn = function(_event)
if not Waves._state.paused then
Gremlin.log.trace(Waves.Id, string.format('Checking Event Against Waves : %s', Gremlin.events.idToName[_event.id] or _event.id))

for _name, _wave in pairs(Waves.config.waves) do
if _wave.trigger.type == 'event'
and (
_wave.trigger.value.id == _event.id
or _wave.trigger.value.id == -1
)
and _wave.trigger.value.filter(_event)
then
Waves.config.waves[_name].trigger.fired = true
Waves._internal.spawnWave(_name, _wave)
end
end
end
end
},
-- eventTriggers = {
-- event = -1,
-- fn = function(_event)
-- if not Waves._state.paused then
-- Gremlin.log.trace(Waves.Id, string.format('Checking Event Against Waves : %s', Gremlin.events.idToName[_event.id] or _event.id))

-- for _name, _wave in pairs(Waves.config.waves.event) do
-- if _wave.trigger.type == 'event'
-- and (
-- _wave.trigger.value.id == _event.id
-- or _wave.trigger.value.id == -1
-- )
-- and _wave.trigger.value.filter(_event)
-- then
-- Waves.config.waves.event[_name].trigger.fired = true
-- Waves._internal.spawnWave(_name, _wave)
-- end
-- end
-- end
-- end
-- },
}

function Waves:setup(config)
Expand All @@ -292,12 +329,18 @@ function Waves:setup(config)
-- start configuration
if not Waves._state.alreadyInitialized or config.forceReload then
Waves.config.adminPilotNames = config.adminPilotNames or {}
Waves.config.waves = config.waves or {}

local _waves = { time = {}, flag = {}, menu = {}, event = {} }
for _name, _wave in pairs(config.waves) do
_waves[_wave.type][_name] = _wave
end
Waves.config.waves = _waves

Gremlin.log.debug(Waves.Id, string.format('Configuration Loaded : %s', mist.utils.tableShowSorted(Waves.config)))
end
-- end configuration

Waves._internal.initEvents()
Waves._internal.initMenu()

timer.scheduleFunction(function()
Expand Down
43 changes: 25 additions & 18 deletions test/waves.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,16 @@ local setUp = function()
}

Waves.config.waves = {
[_testWaveTimedName] = mist.utils.deepCopy(_testWaveTimed),
[_testWaveMenuName] = mist.utils.deepCopy(_testWaveMenu),
[_testWaveEventName] = mist.utils.deepCopy(_testWaveEvent),
time = {
[_testWaveTimedName] = mist.utils.deepCopy(_testWaveTimed),
},
flag = {},
menu = {
[_testWaveMenuName] = mist.utils.deepCopy(_testWaveMenu),
},
event = {
[_testWaveEventName] = mist.utils.deepCopy(_testWaveEvent),
},
}
end

Expand All @@ -239,7 +246,7 @@ local tearDown = function()
Waves._state.alreadyInitialized = false
Waves._state.paused = false
Waves.config.adminPilotNames = {}
Waves.config.waves = {}
Waves.config.waves = { time = {}, flag = {}, menu = {}, event = {} }

mist.nextUnitId = 1
mist.nextGroupId = 1
Expand Down Expand Up @@ -295,20 +302,20 @@ local function matcherForNameId(_tbl)
}
end

TestWavesInternalHandlers = {
setUp = setUp,
testEventTriggers = function()
-- INIT
-- N/A?
-- TestWavesInternalHandlers = {
-- setUp = setUp,
-- testEventTriggers = function()
-- -- INIT
-- -- N/A?

-- TEST
lu.assertEquals(Waves._internal.handlers.eventTriggers.fn({ id = world.event.S_EVENT_INVALID }), nil)
-- -- TEST
-- lu.assertEquals(Waves._internal.handlers.eventTriggers.fn({ id = world.event.S_EVENT_INVALID }), nil)

-- SIDE EFFECTS
-- N/A?
end,
tearDown = tearDown,
}
-- -- SIDE EFFECTS
-- -- N/A?
-- end,
-- tearDown = tearDown,
-- }

TestWavesInternalMethods = {
setUp = setUp,
Expand Down Expand Up @@ -385,13 +392,13 @@ TestWavesInternalMethods = {
end,
testTimeWave = function()
-- INIT
lu.assertEquals(Waves.config.waves[_testWaveTimedName].trigger.fired, nil)
lu.assertEquals(Waves.config.waves.time[_testWaveTimedName].trigger.fired, nil)

-- TEST
lu.assertEquals(Waves._internal.timeWave(), nil)

-- SIDE EFFECTS
lu.assertEquals(Waves.config.waves[_testWaveTimedName].trigger.fired, true)
lu.assertEquals(Waves.config.waves.time[_testWaveTimedName].trigger.fired, true)
end,
testPause = function()
-- INIT
Expand Down

0 comments on commit d56803f

Please sign in to comment.