JD2022-TU1/data/EngineData/Helpers/helpers.ilu

544 lines
14 KiB
Text
Raw Permalink Blame History

function math.round(x)
return math.floor(x + 0.5)
end
function getValue(a,default)
if a == nil then
return default
else
return a
end
end
function appendTable(a,b)
table.foreach(b,
function(x,y)
if type(y) == "table" then
y.__TablePath = ITF_LocalFile
end
table.insert(a,y)
end)
end
function getTable(path)
TABLE = nil
includeReference(path)
local tabIn = TABLE
TABLE = nil
if tabIn == nil or type(tabIn) ~= "table" then
return nil
end
tabIn.__TablePath = path
return tabIn
end
function getTableList(path)
TABLELIST = nil
includeReference(path)
local tabListIn = TABLELIST
TABLELIST = nil
if tabListIn == nil or type(tabListIn) ~= "table" or #tabListIn == 0 then
return nil
end
tabListIn.__ExpandedPath = path
return tabListIn
end
function getExpandedTable(tab)
if type(tab) ~= "table" then
return tab
end
local newTab = {}
local tabIdxLen = #tab
if tabIdxLen > 0 then -- table indexed
for i=1, tabIdxLen do
local tIn = tab[i]
if type(tIn) == "table" and tIn.__ExpandedPath then
for j=1, #tIn do
local tInIn = tIn[j]
tInIn.__TableListPath = tIn.__ExpandedPath
table.insert(newTab, getExpandedTable(tInIn))
end
else
table.insert(newTab, getExpandedTable(tIn))
end
end
end
for k, v in pairs(tab) do
if type(k) ~= "number" then
newTab[k] = getExpandedTable(v)
end
end
return newTab
end
function ExpandTable(tabName)
_G[tabName] = getExpandedTable(_G[tabName])
end
function cloneTable(Var1, IndexT)
if not IndexT then IndexT = {} end
if type(Var1)=="table" and not IndexT[tostring(Var1)] then
IndexT[tostring(Var1)] = Var1
local ReturnValue = {}
for i,j in pairs(Var1) do
ReturnValue[i] = cloneTable(Var1[i],IndexT)
end
return ReturnValue
elseif type(Var1)=="userdata" then
return Var1:Clone()
else
return Var1
end
end
function shiftTable(tbl, offset)
local count = #tbl
local tmp = {}
for i=1, count do
shiftedI = (i + offset - 1) % count + 1
tmp[shiftedI] = tbl[i]
end
for i=1, count do
tbl[i] = tmp[i]
end
end
function insertTable(a, b, i)
table.foreach(b,
function(x,y)
table.insert(a, i, y)
i = i + 1
end)
end
function isTableEmpty(tbl)
return next(tbl) == nil
end
function appendReference(path,a,b)
includeReference(path)
appendTable(a,_G[b])
end
function replaceMapElement(map, key, value, add_val)
add_val = add_val or (add_val == nil and true)
local searchKey = string.lower(key)
local keyvalue
if add_val == true then
keyvalue = {VAL=value}
else
keyvalue = value
end
keyvalue.KEY = key
for i=1, #map do
local lowerKey = map[i].KEY
if lowerKey and (string.lower(lowerKey) == searchKey) then
map[i] = keyvalue
end
end
end
function addMapElement(map, key, value, add_val)
add_val = add_val or (add_val == nil and true)
local searchKey = string.lower(key)
local keyvalue
if add_val == true then
keyvalue = {VAL=value}
else
keyvalue = value
end
keyvalue.KEY = key
for i=1, #map do
local lowerKey = map[i].KEY
if lowerKey and (string.lower(lowerKey) == searchKey) then
map[i] = keyvalue
return
end
end
appendTable(map, {keyvalue})
end
function getMapElement(map, key)
local searchKey = string.lower(key)
for i=1, #map do
local lowerKey = map[i].KEY
if lowerKey and (string.lower(lowerKey) == searchKey) then
return map[i]
end
end
end
function appendComponents( components )
appendTable(params.Actor_Template.COMPONENTS, components )
end
function appendComponent( component )
appendTable(params.Actor_Template.COMPONENTS, {component} )
end
function appendTags( tags )
if params.Actor_Template.TAGS == nil then
params.Actor_Template.TAGS = {}
end
appendTable(params.Actor_Template.TAGS, tags )
end
function appendTag( tag )
if params.Actor_Template.TAGS == nil then
params.Actor_Template.TAGS = {}
end
appendTable(params.Actor_Template.TAGS, {tag} )
end
function appendFXs( fxs )
appendTable(getComponent("FxBankComponent_Template").Fx, fxs)
end
function appendFX( fx )
appendTable(getComponent("FxBankComponent_Template").Fx,
{
{
FxDescriptor_Template = fx,
},
}
)
end
function insertComponents( components, i )
insertTable(params.Actor_Template.COMPONENTS, components, i )
end
function insertComponent( component, i )
insertTable(params.Actor_Template.COMPONENTS, {component}, i )
end
function getConvertedTab(T)
if type(T) ~= "table" then
return T
end
local newT = {}
for k, v in pairs(T) do
if type(k) == "string" then
newT[k:upper()] = getConvertedTab(v)
else
newT[k] = getConvertedTab(v)
end
end
return newT
end
function getComponent(name)
if params == nil or params.Actor_Template == nil or params.Actor_Template.COMPONENTS == nil then
return nil
end
for i=1, #params.Actor_Template.COMPONENTS do
local lowerIndexName = params.Actor_Template.COMPONENTS[i].NAME
if lowerIndexName == nil then
lowerIndexName = getConvertedTab(params.Actor_Template.COMPONENTS[i]).NAME
if lowerIndexName then
displayErrorMessage("Name of ".. lowerIndexName .. " is not declared like this NAME=\"" .. lowerIndexName .. "\" (in capital letters) in file %s. \nPlease fix this")
end
end
if lowerIndexName and string.lower(lowerIndexName) == string.lower(name) then
return params.Actor_Template.COMPONENTS[i][lowerIndexName]
end
end
-- printDebugger("component " .. name .. " not found")
return nil
end
function getComponentIndex(name)
if params == nil or params.Actor_Template == nil or params.Actor_Template.COMPONENTS == nil then
return nil
end
for i=1, #params.Actor_Template.COMPONENTS do
s = params.Actor_Template.COMPONENTS[i].NAME
if s then
if string.lower(s) == string.lower(name) then return i end
-- if string.lower(params.Actor_Template.COMPONENTS[i].NAME) == string.lower(name) then
-- return i
end
end
return nil
end
function appendAnimation( anim_tbl )
animlight_component = getComponent( "AnimLightComponent_Template" )
animated_component = getComponent( "AnimatedComponent_Template" )
if animlight_component ~= nil then
appendTable( animlight_component.animSet.SubAnimSet_Template.animations, anim_tbl )
elseif animated_component ~= nil then
appendTable( animated_component.animSet.SubAnimSet_Template.animations, anim_tbl )
end
end
function appendAnimationAndNode( anim_tbl, node_tbl )
animlight_component = getComponent( "AnimLightComponent_Template" )
animated_component = getComponent( "AnimatedComponent_Template" )
if animlight_component ~= nil then
appendTable( animlight_component.animSet.SubAnimSet_Template.animations, anim_tbl )
elseif animated_component ~= nil then
appendTable( animated_component.animSet.SubAnimSet_Template.animations, anim_tbl )
appendTable( animated_component.tree.AnimTree_Template.nodes, node_tbl )
end
end
function appendNodeTransition( transition_tbl )
animated_component = getComponent( "AnimatedComponent_Template" )
if animated_component ~= nil then
appendTable( animated_component.tree.AnimTree_Template.nodeTransitions, transition_tbl )
end
end
function appendFeedbackTag( newTag )
fxControllerCmpTpl = getComponent("FXControllerComponent_Template")
if fxControllerCmpTpl ~= nil then
if fxControllerCmpTpl.feedbackTags == nil then
fxControllerCmpTpl.feedbackTags = {}
end
appendTable(fxControllerCmpTpl.feedbackTags, {VAL=newTag})
end
end
-- Color expressed with 0..255 values
function color(r, g, b, a)
local sa = string.format("%02x", a or 255)
local sr = string.format("%02x", r or 255)
local sg = string.format("%02x", g or 255)
local sb = string.format("%02x", b or 255)
return "0x"..sa..sr..sg..sb
end
-- Color expressed with 0..1 values
function colorf(r, g, b, a)
local sa = string.format("%02x", a and a*255 or 255)
local sr = string.format("%02x", r and r*255 or 255)
local sg = string.format("%02x", g and g*255 or 255)
local sb = string.format("%02x", b and b*255 or 255)
return "0x"..sa..sr..sg..sb
end
includeReference("EngineData/Helpers/sound_base.ilu")
includeReference("EngineData/Helpers/GFXMaterial_types.ilu")
includeReference("world/ui/fonts/sets.ilu")
includeReference("EngineData/Helpers/spriteAnim_helpers.ilu")
function platformValue(default,paramList)
if ITF_Platform then
if paramList[ITF_Platform] ~= nil then
return paramList[ITF_Platform]
end
end
return default
end
function cleanCharacterDebugger()
i = getComponentIndex("characterdebuggercomponent_template")
if i ~= nil then table.remove(params.Actor_Template.COMPONENTS,i) end
end
function cleanFxController()
i = getComponentIndex("FXControllerComponent_Template")
if i ~= nil then
b = getComponentIndex("FxBankComponent_Template")
s = getComponentIndex("SoundComponent_Template")
if b == nil and s == nil then
printDebugger("Removed FXControllerComponent")
table.remove(params.Actor_Template.COMPONENTS,i)
end
end
end
function cleanEditorParams()
if params.ActorEditorParams then params.ActorEditorParams = nil end
end
function ITF_cleanActor()
if params and params.Actor_Template then
if ITF_Platform ~= "PC" then
cleanCharacterDebugger()
cleanEditorParams()
end
cleanFxController()
end
end
function ITF_addDefaultFeedbackTag()
fxControllerCmpTpl = getComponent("FXControllerComponent_Template")
if fxControllerCmpTpl ~= nil then
if fxControllerCmpTpl.feedbackTags ~= nil then
table.insert(fxControllerCmpTpl.feedbackTags,{VAL="DefaultActor"})
else
fxControllerCmpTpl.feedbackTags = {{VAL="DefaultActor"}}
end
end
if params then
gmatTpl = nil
if params.GameMaterial_Template ~= nil then
gmatTpl = params.GameMaterial_Template
elseif params.RO2_GameMaterial_Template ~= nil then
gmatTpl = params.RO2_GameMaterial_Template
end
if gmatTpl ~= nil then
if gmatTpl.feedbackTags ~= nil then
table.insert(gmatTpl.feedbackTags,{VAL="DefaultMaterial"})
else
gmatTpl.feedbackTags = {{VAL="DefaultMaterial"}}
end
end
end
end
function setGpeRoot( path )
__gpeRoot = path
end
function gpeRoot()
if __gpeRoot == nil then
-- print(ITF_OwnerPath)
__gpeRoot = ITF_OwnerPath
__gpeRoot = string.gsub(__gpeRoot, "components/$", "")
__gpeRoot = string.gsub(__gpeRoot, "extras/$", "")
__gpeRoot = string.gsub(__gpeRoot, "sampledemo/$", "")
-- print(__gpeRoot)
end
return __gpeRoot
end
function gpePath( localPath )
return gpeRoot()..localPath
end
function includeBreakableStackHelper()
includeReference("world/common/breakable/breakablestackelement/components/breakableStackHelper.ilu")
end
includeReference("EngineData/Helpers/StateMachine_helpers.ilu")
LIGHT_PRESET =
{
ANIMLIGHT = "AnimLightComponent",
ANIMATED = "AnimatedComponent",
ANIMMESHVERTEX = "AnimMeshVertexComponent",
ATLASANIM = "AtlasAnimationComponent",
FXBANK = "FxBankComponent",
MESH3D = "Mesh3DComponent",
PARTICLE = "ParticleGeneratorComponent",
TEXTURE = "TextureGraphicComponent",
TRAIL = "Trail3DComponent",
}
LIGHT_PRESET_TEMPLATE_INV = {}
for k, v in pairs(LIGHT_PRESET) do
LIGHT_PRESET_TEMPLATE_INV[v.."_Template"] = k
end
-- Example <20> mettre dans un ilu <20> inclure
-- LightPreset =
-- {
-- FrontLightBrightness = 1,
-- FrontLightContrast = 1,
-- BackLightBrightness = 1,
-- BackLightContrast = 1,
-- }
function setLightPresets(file, data)
if not params or not params.Actor or not params.Actor.LUA then
return
end
includeReference(file)
if not LightPreset then
return
end
-- first seach for main component
local paramsAct = params
local lua = params.Actor.LUA
params = nil
includeReference(lua)
if not params then
params = paramsAct
return
end
local paramsTPL = params
params = paramsAct
local templateToProcess = {}
for i=1, #paramsTPL.Actor_Template.COMPONENTS do
local comp = paramsTPL.Actor_Template.COMPONENTS[i]
if (data and comp.NAME == data.."_Template") or
((not data) and LIGHT_PRESET_TEMPLATE_INV[comp.NAME]) then
table.insert(templateToProcess, LIGHT_PRESET[LIGHT_PRESET_TEMPLATE_INV[comp.NAME]])
end
end
for i=1, #templateToProcess do
local curTemplate = templateToProcess[i]
local templateFound = nil
if not params.Actor.COMPONENTS then
params.Actor.COMPONENTS = {}
end
for compIdx=1, #params.Actor.COMPONENTS do
if params.Actor.COMPONENTS[compIdx].NAME == curTemplate then
templateFound = params.Actor.COMPONENTS[compIdx]
break
end
end
if templateFound then
local template = templateFound[curTemplate]
if not template.PrimitiveParameters or not template.PrimitiveParameters.GFXPrimitiveParam then
template.PrimitiveParameters =
{
GFXPrimitiveParam = LightPreset
}
else
for k, v in pairs(LightPreset) do
template.PrimitiveParameters.GFXPrimitiveParam[k] = v
end
end
else
local compData =
{
NAME = curTemplate,
[curTemplate] =
{
PrimitiveParameters =
{
GFXPrimitiveParam = LightPreset
}
}
}
table.insert(params.Actor.COMPONENTS, compData)
end
end
end