Google_Frog wrote:Lua can have loops but unit defs do not have loops, each unit is an array. Copy the lua unitdef interpreting code from CAEdit or Spring if you really want to read them for modit.
when i last time watched CA lua files, they had some global lua file that readed some other lua file data and reformatted it with fancy multipliers etc. thats when i stopped trying to do the lua reader.
i dont just need unit files, thats only 1 thing out of 20 i have in Modit.
Code: Select all
local armorDefs = {
SUBS = {
"armsub",
"corsub",
"armsubk",
"corshark",
"tawf009",
"corssub",
"armacsub",
"coracsub",
"armrecl",
"correcl",
},
...
}
-- add anything that can fly to the PLANES category
for name, ud in pairs(DEFS.unitDefs) do
if (tobool(ud.canfly)) then
table.insert(armorDefs.PLANES, name)
end
end
-- put any unit that doesn't go in any other category in ELSE
for name, ud in pairs(DEFS.unitDefs) do
local found
for categoryName, categoryTable in pairs(armorDefs) do
for _, usedName in pairs(categoryTable) do
if (usedName == name) then
found = true
end
end
end
if (not found) then
table.insert(armorDefs.ELSE, name)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- convert to named maps (does anyone know what 99 is for? :)
for categoryName, categoryTable in pairs(armorDefs) do
local t = {}
for _, unitName in pairs(categoryTable) do
t[unitName] = 99
end
armorDefs[categoryName] = t
end
Code: Select all
local moveDefs = {
KBOT1 = {
footprintx = 1,
footprintz = 1,
maxwaterdepth = 15,
maxslope = 36,
crushstrength = 5,
},
...
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- convert from map format to the expected array format
local array = {}
local i = 1
for k,v in pairs(moveDefs) do
array[i] = v
v.name = k
i = i + 1
end
Code: Select all
local function ProcessUnitDef(udName, ud)
local wds = ud.weapondefs
if (not istable(wds)) then
return
end
-- add this unitDef's weaponDefs
for wdName, wd in pairs(wds) do
if (isstring(wdName) and istable(wd)) then
local fullName = udName .. '_' .. wdName
WeaponDefs[fullName] = wd
wd.filename = ud.filename
end
end
-- convert the weapon names
local weapons = ud.weapons
if (istable(weapons)) then
for i = 1, 16 do
local w = weapons[i]
if (istable(w)) then
if (isstring(w.def)) then
local ldef = string.lower(w.def)
local fullName = udName .. '_' .. ldef
local wd = WeaponDefs[fullName]
if (istable(wd)) then
w.name = fullName
end
end
w.def = nil
end
end
end
-- convert the death explosions
if (isstring(ud.explodeas)) then
local fullName = udName .. '_' .. ud.explodeas
if (WeaponDefs[fullName]) then
ud.explodeas = fullName
end
end
if (isstring(ud.selfdestructas)) then
local fullName = udName .. '_' .. ud.selfdestructas
if (WeaponDefs[fullName]) then
ud.selfdestructas = fullName
end
end
end
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Fix the canAttack tag
--
do
local processed = {}
local RawCanAttack
local FacCanAttack
local CanAttack
RawCanAttack = function(ud)
if (ud.weapons) then
for i, weapon in pairs(ud.weapons) do
local wd = WeaponDefs[weapon.name:lower()]
if ((not wd.isshield) and
(not wd.interceptor)) then
return true
end
end
end
if (ud.kamikaze) then
return not ud.yardmap
end
return false
end
FacCanAttack = function(ud)
for _, name in pairs(ud.buildoptions) do
if (CanAttack(UnitDefs[name:lower()])) then
return true
end
end
return false
end
CanAttack = function(ud)
if (processed[ud] ~= nil) then
return processed[ud]
end
local canAttack = false
if (RawCanAttack(ud)) then
canAttack = true
elseif (ud.tedclass == 'PLANT') then
if (FacCanAttack(ud)) then
canAttack = true
end
end
processed[ud] = canAttack
return canAttack
end
-- loop through the unit defs
for name, ud in pairs(UnitDefs) do
ud.canattack = CanAttack(ud)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Calculate mincloakdistance based on unit footprint size
--
local sqrt = math.sqrt
for name, ud in pairs(UnitDefs) do
if (not ud.mincloakdistance) then
local fx = ud.footprintx and tonumber(ud.footprintx) or 1
local fz = ud.footprintz and tonumber(ud.footprintz) or 1
local radius = 8 * sqrt((fx * fx) + (fz * fz))
ud.mincloakdistance = (radius + 48)
end
end
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Special Air
--
if (modOptions and tobool(modOptions.specialair)) then
local replacements = VFS.Include("LuaRules/Configs/specialair.lua")
if (replacements[modOptions.specialair]) then
replacements = replacements[modOptions.specialair]
for name, ud in pairs(UnitDefs) do
if (ud.buildoptions) then
for buildKey, buildOption in pairs(ud.buildoptions) do
if (replacements[buildOption]) then
ud.buildoptions[buildKey] = replacements[buildOption];
end
end
end
end
end
end
>_> "Fix the canAttack tag" nuff said.
I would do parser for those specific formats, but who the fuck says everyone will do it exactly like CA? Thats my fucking point here, we need 1 format that everyone uses, no lua shit that creates its own format because they think lua = IDE.