Skip to content

Commit

Permalink
More iterator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubb13 committed Sep 29, 2023
1 parent ed38b32 commit 1d34e70
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 1 deletion.
163 changes: 163 additions & 0 deletions EEex/copy/EEex_Resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ function EEex_Resource_GetSpellAbilityForLevel(spellHeader, casterLevel)
end
Spell_Header_st.getAbilityForLevel = EEex_Resource_GetSpellAbilityForLevel

-- spellResRefIterator is expected to return <string spellResRef>
-- Iterator returns <string spellResRef, Spell_Header_st spellHeader>
function EEex_Resource_GetValidSpellsIterator(spellResRefIterator)
return function()
for spellResRef in spellResRefIterator do
local spellHeader = EEex_Resource_Demand(spellResRef, "SPL")
if spellHeader ~= nil then
return spellResRef, spellHeader
end
end
return nil
end
end
EEex_Resource_GetValidSpellsItr = EEex_Resource_GetValidSpellsIterator

---------
-- 2DA --
---------
Expand Down Expand Up @@ -213,6 +228,7 @@ end
C2DArray.findRowLabel = EEex_Resource_Find2DARowLabel

function EEex_Resource_Free2DA(array)
EEex_SetUDGCFunc(array, nil)
array:Destruct()
EEex_FreeUD(array)
end
Expand Down Expand Up @@ -302,9 +318,156 @@ function EEex_Resource_Load2DA(resref)
function(manager)
array:Load(manager:getUD("resref"))
end)
EEex_SetUDGCFunc(array, EEex_Resource_Free2DA)
return array
end

-- Iterator returns:
-- bIncludeLabel == true : {array:getRowLabel(y), ...}
-- else : {...}
function EEex_Resource_Get2DARowTableIterator(array, bIncludeLabel)
local sizeX, sizeY = array:getDimensions()
sizeX = sizeX - 1
local y = 0
if bIncludeLabel then
return function()
while true do
if y >= sizeY then return nil end
local rowValues = {array:getRowLabel(y)}
for i = 1, sizeX do
rowValues[i + 1] = array:getAtPoint(i - 1, y)
end
y = y + 1
return rowValues
end
end
else
return function()
while true do
if y >= sizeY then return nil end
local rowValues = {}
for i = 1, sizeX do
rowValues[i] = array:getAtPoint(i - 1, y)
end
y = y + 1
return rowValues
end
end
end
end
EEex_Resource_Get2DARowTableItr = EEex_Resource_Get2DARowTableIterator
C2DArray.getRowTableIterator = EEex_Resource_Get2DARowTableItr
C2DArray.getRowTableItr = EEex_Resource_Get2DARowTableItr

-- Iterator returns:
-- bIncludeLabel == true : <array:getRowLabel(y), ...>
-- else : <...>
function EEex_Resource_Get2DARowValuesIterator(array, bIncludeLabel)
return EEex_Utility_ApplyItr(array:getRowTableItr(bIncludeLabel), function(t)
return table.unpack(t)
end)
end
EEex_Resource_Get2DARowValuesItr = EEex_Resource_Get2DARowValuesIterator
C2DArray.getRowValuesIterator = EEex_Resource_Get2DARowValuesItr
C2DArray.getRowValuesItr = EEex_Resource_Get2DARowValuesItr

-- Iterator returns:
-- labelI == nil : <...>
-- else : <..., array:getRowLabel(y), ...>
function EEex_Resource_Get2DARowColumnsIterator(array, labelI, ...)
local columnIndexes = {...}
local _, sizeY = array:getDimensions()
local y = 0
if labelI == nil then
return function()
while true do
if y >= sizeY then return nil end
local rowValues = {}
for i, columnIndex in ipairs(columnIndexes) do
rowValues[i] = array:getAtPoint(columnIndex, y)
end
y = y + 1
return table.unpack(rowValues)
end
end
else
return function()
while true do
if y >= sizeY then return nil end
local rowValues = {}
for i = 1, labelI - 1 do
rowValues[i] = array:getAtPoint(columnIndexes[i], y)
end
rowValues[labelI] = array:getRowLabel(y)
local i = labelI
while true do
local columnIndex = columnIndexes[i]
if columnIndex == nil then break end
i = i + 1
rowValues[i] = array:getAtPoint(columnIndex, y)
end
y = y + 1
return table.unpack(rowValues)
end
end
end
end
EEex_Resource_Get2DARowColumnsItr = EEex_Resource_Get2DARowColumnsIterator
C2DArray.getRowColumnsIterator = EEex_Resource_Get2DARowColumnsItr
C2DArray.getRowColumnsItr = EEex_Resource_Get2DARowColumnsItr

-- Iterator returns:
-- labelI == nil : <...>
-- else : <..., array:getRowLabel(y), ...>
function EEex_Resource_Get2DARowColumnsByLabelIterator(array, labelI, ...)
local columnIndexes = {}
local insertI = 1
while true do
local label = select(insertI, ...)
if label == nil then break end
columnIndexes[insertI] = array:findColumnLabel(label)
insertI = insertI + 1
end
local _, sizeY = array:getDimensions()
local y = 0
if labelI == nil then
return function()
while true do
if y >= sizeY then return nil end
local rowValues = {}
for i, columnIndex in ipairs(columnIndexes) do
rowValues[i] = array:getAtPoint(columnIndex, y)
end
y = y + 1
return table.unpack(rowValues)
end
end
else
return function()
while true do
if y >= sizeY then return nil end
local rowValues = {}
for i = 1, labelI - 1 do
rowValues[i] = array:getAtPoint(columnIndexes[i], y)
end
rowValues[labelI] = array:getRowLabel(y)
local i = labelI
while true do
local columnIndex = columnIndexes[i]
if columnIndex == nil then break end
i = i + 1
rowValues[i] = array:getAtPoint(columnIndex, y)
end
y = y + 1
return table.unpack(rowValues)
end
end
end
end
EEex_Resource_Get2DARowColumnsByLabelItr = EEex_Resource_Get2DARowColumnsByLabelIterator
C2DArray.getRowColumnsByLabelIterator = EEex_Resource_Get2DARowColumnsByLabelItr
C2DArray.getRowColumnsByLabelItr = EEex_Resource_Get2DARowColumnsByLabelItr

---------
-- IDS --
---------
Expand Down
25 changes: 25 additions & 0 deletions EEex/copy/EEex_Sprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,31 @@ EEex_Sprite_GetValidKnownInnateSpellsItr = EEex_Sprite_GetValidKnownInnateSpells
CGameSprite.getValidKnownInnateSpellsIterator = EEex_Sprite_GetValidKnownInnateSpellsItr
CGameSprite.getValidKnownInnateSpellsItr = EEex_Sprite_GetValidKnownInnateSpellsItr

-- validSpellsIterator is expected to return <string spellResRef, Spell_Header_st spellHeader>
-- Iterator returns <string spellResRef, Spell_Header_st spellHeader, Spell_ability_st spellAbility>
function EEex_Sprite_GetSpellsWithAbilityIterator(sprite, validSpellsIterator)
return function()
for spellResRef, spellHeader in validSpellsIterator do
local spellAbility = spellHeader:getAbilityForLevel(sprite:getCasterLevelForSpell(spellResRef, true))
if spellAbility ~= nil then
return spellResRef, spellHeader, spellAbility
end
end
end
end
EEex_Sprite_GetSpellsWithAbilityItr = EEex_Sprite_GetSpellsWithAbilityIterator
CGameSprite.getSpellsWithAbilityIterator = EEex_Sprite_GetSpellsWithAbilityItr
CGameSprite.getSpellsWithAbilityItr = EEex_Sprite_GetSpellsWithAbilityItr

-- spellResRefIterator is expected to return <string spellResRef>
-- Iterator returns <string spellResRef, Spell_Header_st spellHeader, Spell_ability_st spellAbility>
function EEex_Sprite_GetValidSpellsWithAbilityIterator(sprite, spellResRefIterator)
return sprite:getSpellsWithAbilityIterator(EEex_Resource_GetValidSpellsIterator(spellResRefIterator))
end
EEex_Sprite_GetValidSpellsWithAbilityItr = EEex_Sprite_GetValidSpellsWithAbilityIterator
CGameSprite.getValidSpellsWithAbilityIterator = EEex_Sprite_GetValidSpellsWithAbilityItr
CGameSprite.getValidSpellsWithAbilityItr = EEex_Sprite_GetValidSpellsWithAbilityItr

-- Iterator returns <number spellLevel, number knownSpellIndex, string spellResRef, Spell_Header_st spellHeader, Spell_ability_st spellAbility>
function EEex_Sprite_Private_GetValidKnownSpellsWithAbilityIterator(sprite, validKnownSpellsIterator)
return function()
Expand Down
58 changes: 57 additions & 1 deletion EEex/copy/EEex_Utility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function EEex_Utility_MutateIterator(iterator, func)
return table.unpack(values)
end
end
EEex_Utility_ApplyItr = EEex_Utility_ApplyIterator
EEex_Utility_MutateItr = EEex_Utility_MutateIterator

-- Expects - <lowerBound, upperBound, [stepFunc], [startI]>
-- Returns - Itr such that:
Expand Down Expand Up @@ -384,6 +384,62 @@ function EEex_Utility_FilterIterator(iterator, filterFunc)
end
EEex_Utility_FilterItr = EEex_Utility_FilterIterator

-- Expects - <itr, func>
-- Calls func with {itr[1]}, {itr[2]}, ..., {itr[n]}
function EEex_Utility_ProcessIteratorValues(iterator, func)
while true do
local values = {iterator()}
if values[1] == nil then
return
end
func(values)
end
end
EEex_Utility_ProcessItrValues = EEex_Utility_ProcessIteratorValues

-- Expects - <itr>
-- Returns - table that collects all values returned by itr such that:
-- * n = <number of itr elements>
-- -> t[1] = {itr[1]}, t[2] = {itr[2]}, ..., t[n] = {itr[n]}
function EEex_Utility_CollectIteratorValues(iterator)
local t = {}
local insertI = 1
EEex_Utility_ProcessIteratorValues(iterator, function(t2)
t[insertI] = t2
insertI = insertI + 1
end)
return t
end
EEex_Utility_CollectItrValues = EEex_Utility_CollectIteratorValues

-- Expects - <i, itr>
-- Returns - table that collects a specific value returned by itr such that:
-- * n = <number of itr elements>
-- -> t[1] = select(i, itr[1]), t[2] = select(i, itr[2]), ..., t[n] = select(i, itr[n])
function EEex_Utility_CollectIteratorValue(i, iterator)
local t = {}
local insertI = 1
for value in EEex_Utility_SelectItr(i, iterator) do
t[insertI] = value
insertI = insertI + 1
end
return t
end
EEex_Utility_CollectItrValue = EEex_Utility_CollectIteratorValue

-- Expects - <i, itr>
-- Returns - table that maps all values returned by itr such that:
-- * n = <number of itr elements>
-- -> t[select(i, itr[1])] = {itr[1]}, t[select(i, itr[2])] = {itr[2]}, ..., t[select(i, itr[n])] = {itr[n]}
function EEex_Utility_MapIteratorValues(i, iterator)
local t = {}
EEex_Utility_ProcessIteratorValues(iterator, function(t2)
t[t2[i]] = t2
end)
return t
end
EEex_Utility_MapItrValues = EEex_Utility_MapIteratorValues

--[[
function EEex_Utility_AugmentIterator(inputItr, augStart, augLength, mainItrGen, augmentFunc)
Expand Down

0 comments on commit 1d34e70

Please sign in to comment.