prandipadaro wrote:Any idea on how to update the /BA/unittable.lua
anyone have a script?
thanks in advance
Here is a script that does 99% of the work, BUT, i know for fact that a few things are wrong, (airRange is one of them, bigexplosions is probably incomplete)
There are also multipliers for radarrange etc, youre gonna have to cross-check those.
The widget writes the info into the infolog.
You can also use the to_string(table or anything) to pretty print tables for fixing.
EDIT: crap, i missed techlevel too
Code: Select all
function widget:GetInfo()
    return {
        name      = "Shard Unittable exporter", --(v0.5)
        desc      = "Exports the unittable for shard",
        author    = "Beherith",
        date      = "2016-03-22",
        license   = "GPL V2",
        layer     = -10000,
        enabled   = true,
    }
end
--name	radarRadius	techLevel	isBuilding	needsWater	extractsMetal	bigExplosion	airLosRadius	losRadius	sonarRadius	metalCost	xsize	totalEnergyOut	jammerRadius	wreckName	zsize	mtype	airRange	submergedRange	stealth	groundRange	isWeapon	factoriesCanBuild	"armaap"	"armplat"	"armlab"	"armalab"	"armsy"	"armasy"	"asubpen"	"armavp"	"armvp"	"armap"	"armhp"	"armfhp"	"coraap"	"corplat"	"corlab"	"coralab"	"corsy"	"corasy"	"csubpen"	"coravp"	"corvp"	"corap"	"corhp"	"corfhp"
--hard ones: mtype,factoriesCanBuild
function widget:Initialize()
	bigExplosions = {ATOMIC_BLAST = true, COMMANDER_BLAST = true,}
	for id,unitDef in pairs(UnitDefs) do
		str= "unitTable[\"" .. unitDef.name..'\"] = {'
		
		str = str .. 'radarRadius = ' .. tostring(unitDef.radarRadius * 0.1 ) .. ','
		str = str .. 'isBuilding = ' .. tostring(unitDef.isBuilding) .. ','
		str = str .. 'needsWater = ' .. tostring(unitDef.minWaterDepth >0 ) .. ','
		str = str .. 'extractsMetal = ' .. tostring(unitDef.extractsMetal) .. ','
		
		if bigExplosions[unitDef.explodeas ] == true then
			str = str .. 'bigExplosion = ' .. tostring(true ) .. ','
		else
			str = str .. 'bigExplosion = ' .. tostring(false) .. ','
		end
		str = str .. 'airLosRadius = ' .. tostring(unitDef.airLosRadius  * 0.1 ) .. ','
		str = str .. 'losRadius = ' .. tostring(unitDef.losRadius * 0.1 ) .. ','
		str = str .. 'sonarRadius = ' .. tostring(unitDef.sonarRadius * 0.1 ) .. ','
		str = str .. 'metalCost = ' .. tostring(unitDef.metalCost) .. ','
		str = str .. 'xsize = ' .. tostring(unitDef.xsize ) .. ','
		str = str .. 'totalEnergyOut = ' .. tostring(unitDef.totalEnergyOut) .. ','
		str = str .. 'jammerRadius = ' .. tostring(unitDef.jammerRadius * 0.1 ) .. ','
		if unitDef.wreckName ~= '' then str = str .. 'wreckName = \"' .. tostring(unitDef.wreckName) .. '\",' end
		
		
		groundRange = 0
		submergedRange = 0
		airRange = 0
		isWeapon = false
		for weaponindex, weaponInfo in ipairs(unitDef.weapons) do
			isWeapon = true
			weaponDefID = weaponInfo.weaponDef
			--Spring.Echo(unitDef.name,'onlytargets:',to_string(weaponInfo.onlyTargets))
			if weaponInfo.onlyTargets["vtol"] then
				airRange = math.max(airRange,WeaponDefs[weaponDefID].range)
			elseif WeaponDefs[weaponDefID].type == "TorpedoLauncher" then
				submergedRange = math.max(submergedRange,WeaponDefs[weaponDefID].range)
			else
				groundRange = math.max(groundRange,WeaponDefs[weaponDefID].range)
			end
		
		str = str .. 'airRange = ' .. tostring(airRange) .. ','
		str = str .. 'groundRange = ' .. tostring(groundRange) .. ','
		str = str .. 'submergedRange = ' .. tostring(submergedRange) .. ','
		str = str .. 'isWeapon = ' .. tostring(isWeapon) .. ','
		
		
		str = str .. 'buildOptions = ' .. tostring(unitDef.buildOptions ~= nil) .. ','
		
		factoriesCanBuild = ''
		for id2,unitDef2 in pairs(UnitDefs) do
			if unitDef2.buildOptions and unitDef2.buildOptions[unitDef] then 
			factoriesCanBuild = factoriesCanBuild .. unitDef2.name .. ','
			end
		end
		if factoriesCanBuild ~= '' then
			str = str .. 'factoriesCanBuild = {' .. tostring(factoriesCanBuild) .. '},'
		end
		
		mtype = 'veh' -- buildings are veh, possible types are veh,bot,shp,hov,sub,air,amp
		
		if unitDef.moveDef and unitDef.moveDef.name then
			-- Spring.Echo(unitDef.name,"unitDef.moveDef",to_string(unitDef.moveDef))
			if unitDef.moveDef.name:find('uboat') ~= nil then 
				mtype = 'sub'
			elseif  unitDef.moveDef.name:find('boat') ~= nil then 
				mtype = 'shp'
			elseif  unitDef.moveDef.name:find('hover') ~= nil then 
				mtype = 'hov'
			elseif  unitDef.moveDef.name:find('akbot') ~= nil then 
				mtype = 'amp'
			elseif  unitDef.moveDef.name:find('atank') ~= nil then 
				mtype = 'amp'
			end
		end
		if unitDef.canfly then
			mtype = 'air'
		end
		str = str .. 'mtype = ' .. tostring(mtype) .. ','
		
		str = str .. '}'
		Spring.Echo(str)
		str = ""
		end
	end
end
function to_string(data, indent)
	local str = ""
	if(indent == nil) then
		indent = 0
	end
	-- Check the type
	if(type(data) == "string") then
		str = str .. ("    "):rep(indent) .. data .. "\n"
	elseif(type(data) == "number") then
		str = str .. ("    "):rep(indent) .. data .. "\n"
	elseif(type(data) == "boolean") then
		if(data == true) then
			str = str .. "true"
		else
			str = str .. "false"
		end
	elseif(type(data) == "table") then
		local i, v
		for i, v in pairs(data) do
			-- Check for a table in a table
			if(type(v) == "table") then
				str = str .. ("    "):rep(indent) .. i .. ":\n"
				str = str .. to_string(v, indent + 2)
			else
				str = str .. ("    "):rep(indent) .. i .. ": " .. to_string(v, 0)
			end
		end
	elseif (data ==nil) then
		str=str..'nil'
	else
		--print_debug(1, "Error: unknown data type: %s", type(data))
		str=str.. "Error: unknown data type:" .. type(data)
		Spring.Echo('X data type')
	end
	return str
end




