Skip to content

Commit

Permalink
ffi int/float casting, with some Sheikah help.
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Balthazar committed Jul 17, 2024
1 parent 55c5645 commit 90738aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 80 deletions.
2 changes: 1 addition & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ local formats = {
table.insert(progressChannels, {channel=love.thread.getChannel(filename), name=filename, id=filename})
end

local heightmap, minHeight, maxHeight = scmapUtils.readHeightmap(data.heightmap[1], data.size[1], data.size[2], heightmapScale)--data.heightmapScale is currently an unparsed float.
local heightmap, minHeight, maxHeight = scmapUtils.readHeightmap(data.heightmap[1], data.size[1], data.size[2], data.heightmapScale)
drawcanvas = scmapUtils.renderHeightmapToCanvas(nil, heightmap, minHeight, maxHeight)
scmapUtils.renderBlockingToCanvas(drawcanvas, scmapUtils.getBlockingData(heightmap))

Expand Down
1 change: 0 additions & 1 deletion scmapwrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ if utilityTextures and utilityTextures[1] then
end

local data = components['data.lua']
data.waveGenerators = data.waveGenerators or data.waveGenerators.waterSettings--NOTE: Legacy
local progressTotal = (type(data.waveGenerators)=='table' and #data.waveGenerators or 0)
+ (type(data.decals) =='table' and #data.decals or 0)
+ (type(data.decalGroups) =='table' and #data.decalGroups or 0)
Expand Down
50 changes: 15 additions & 35 deletions utils/maths.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,25 @@ function math.formatBytes(bytes)
bytes:sub(4,4):byte()
)
end
function math.IBMShort(bytes)
return tonumber(('%0.2x%0.2x'):format(bytes:sub(2,2):byte(), bytes:sub(1,1):byte()), 16)
end
function math.IBMShort2(little, big)
return tonumber(('%0.2x%0.2x'):format(big:byte(), little:byte()), 16)
end
function math.IMBIntUnsigned(bytes)
return tonumber(('%0.2x%0.2x%0.2x%0.2x'):format(
function math.flipFormatBytes(bytes)
return ('%0.2x%0.2x%0.2x%0.2x'):format(
bytes:sub(4,4):byte(),
bytes:sub(3,3):byte(),
bytes:sub(2,2):byte(),
bytes:sub(1,1):byte()
), 16)
end
function math.IMBFloat(bytes)
return 'FLOAT:'..math.formatBytes(bytes)
end
function math.IMBInt(bytes)
local num = math.IMBIntUnsigned(bytes)
if num==4294967295 then return -1 end
if num>2147483647 then
--TODO negative int unconverted.
return 'INT:'..math.formatBytes(bytes)
end
return num
)
end

local function hex2bin(hex) return string.char(tonumber(hex, 16)) end
local function hex2bin2(a,b) return hex2bin(a)..hex2bin(b) end
local function hex2bin4(a,b,c,d) return hex2bin(a)..hex2bin(b)..hex2bin(c)..hex2bin(d) end
local function hexSplit4(val) return hex2bin4(val:sub(-8,-7),val:sub(-6,-5),val:sub(-4,-3),val:sub(-2,-1)) end
local function hexSplitFlip4(val) return hex2bin4(val:sub(-2,-1),val:sub(-4,-3),val:sub(-6,-5),val:sub(-8,-7)) end
local fficast = require'ffi'.cast
local ffistring = require'ffi'.string
local ffinew = require'ffi'.new

function math.intToIBM(val)
if type(val)=='string' then
return hexSplit4(val)
elseif val==-1 then
return '\255\255\255\255'
elseif type(val)=='number' then
return hexSplitFlip4(('%0.8x'):format(val))
end
end
function math.IBMShort(bytes) return fficast('uint16_t*', bytes)[0] end
function math.IMBUInt(bytes) return fficast('uint32_t*', bytes)[0] end
function math.IMBInt(bytes) return fficast('int32_t*', bytes)[0] end
function math.IMBFloat(bytes) return fficast('float*', bytes)[0] end

function math.shortToIBM(val) return ffistring(fficast('uint8_t(*)[4]', ffinew("uint16_t[1]", {val}))[0], 4) end
function math.uIntToIBM(val) return ffistring(fficast('uint8_t(*)[4]', ffinew("uint32_t[1]", {val}))[0], 4) end
function math.intToIBM(val) return ffistring(fficast('uint8_t(*)[4]', ffinew("int32_t[1]", {val}))[0], 4) end
function math.floatToIBM(val) return ffistring(fficast('uint8_t(*)[4]', ffinew("float[1]", {val}))[0], 4) end
78 changes: 35 additions & 43 deletions utils/scmap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,9 @@ function scmapUtils.readDatastream(scmapData)
end

local function hex2bin(hex) return string.char(tonumber(hex, 16)) end
local function hex2bin2(a,b) return hex2bin(a)..hex2bin(b) end
local function hex2bin4(a,b,c,d) return hex2bin(a)..hex2bin(b)..hex2bin(c)..hex2bin(d) end
local function hexSplit4(val) return hex2bin4(val:sub(-8,-7),val:sub(-6,-5),val:sub(-4,-3),val:sub(-2,-1)) end
local function hexSplitFlip4(val) return hex2bin4(val:sub(-2,-1),val:sub(-4,-3),val:sub(-6,-5),val:sub(-8,-7)) end

local function rfloat(val) return hexSplit4(val) end
local function rvec2(vec) return rfloat(vec[1])..rfloat(vec[2]) end
local function rvec3(vec) return rfloat(vec[1])..rfloat(vec[2])..rfloat(vec[3]) end
local function rvec4(vec) return rfloat(vec[1])..rfloat(vec[2])..rfloat(vec[3])..rfloat(vec[4]) end
local function rstringNull(str) return (str or '')..'\000' end
local function rint(val) return math.intToIBM(val) end
local function rvec3(vec) return math.floatToIBM(vec[1])..math.floatToIBM(vec[2])..math.floatToIBM(vec[3]) end

local function progressReport(dir, filename, message, i, t)
love.thread.getChannel(dir):push(-1)
Expand All @@ -335,13 +327,13 @@ function scmapUtils.writeDatastream(files, filename, dir)

progressReport(dir, filename, "starting packing")

local function float(val) table.insert(fileData, hexSplit4(val)) end
local function vec2(vec) table.insert(fileData, rvec2(vec)) end
local function vec3(vec) table.insert(fileData, rvec3(vec)) end
local function vec4(vec) table.insert(fileData, rvec4(vec)) end
local function int(val) table.insert(fileData, rint(val)) end
local function intFile(file) table.insert(fileData, rint(file:len())..file) end
local function stringNull(str) table.insert(fileData, rstringNull(str)) end
local function float(val) table.insert(fileData, math.floatToIBM(val)) end
local function vec2(vec) table.insert(fileData, math.floatToIBM(vec[1])..math.floatToIBM(vec[2])) end
local function vec3(vec) table.insert(fileData, math.floatToIBM(vec[1])..math.floatToIBM(vec[2])..math.floatToIBM(vec[3])) end
local function vec4(vec) table.insert(fileData, math.floatToIBM(vec[1])..math.floatToIBM(vec[2])..math.floatToIBM(vec[3])..math.floatToIBM(vec[4])) end
local function int(val) table.insert(fileData, math.intToIBM(val)) end
local function intFile(file) table.insert(fileData, math.intToIBM(file:len())..file) end
local function stringNull(str) table.insert(fileData, (str or '')..'\000') end

float(data.floatWidth)
float(data.floatHeight)
Expand Down Expand Up @@ -415,33 +407,33 @@ function scmapUtils.writeDatastream(files, filename, dir)

progressReport(dir, filename, "Processing wave generators")
local waveGenCount = #data.waveGenerators
local waveGenStrings = {rint(waveGenCount)}
local waveGenStrings = {math.intToIBM(waveGenCount)}
for i, v in ipairs(data.waveGenerators) do
progressReport(dir, filename, "Processing wave generators", i, waveGenCount)
table.insert(waveGenStrings, table.concat{
rstringNull(v.textureName),
rstringNull(v.rampName),
(v.textureName or ''),'\000',
(v.rampName or ''),'\000',
rvec3(v.position),
rfloat(v.rotation),
math.floatToIBM(v.rotation),
rvec3(v.velocity),

rfloat(v.lifeTimeFirst),
rfloat(v.lifeTimeSecond),
rfloat(v.periodFirst),
rfloat(v.periodSecond),
rfloat(v.scaleFirst),
rfloat(v.scaleSecond),
rfloat(v.frameCount),
rfloat(v.frameRateFirst),
rfloat(v.frameRateSecond),
rfloat(v.stripCount),
math.floatToIBM(v.lifeTimeFirst),
math.floatToIBM(v.lifeTimeSecond),
math.floatToIBM(v.periodFirst),
math.floatToIBM(v.periodSecond),
math.floatToIBM(v.scaleFirst),
math.floatToIBM(v.scaleSecond),
math.floatToIBM(v.frameCount),
math.floatToIBM(v.frameRateFirst),
math.floatToIBM(v.frameRateSecond),
math.floatToIBM(v.stripCount),
})
end
table.insert(fileData, table.concat(waveGenStrings))

progressReport(dir, filename, "Processing minimap data")
table.insert(fileData, table.concat{
rint(data.miniMapContourInterval),
math.intToIBM(data.miniMapContourInterval),
hexSplit4(data.miniMapDeepWaterColor),
hexSplit4(data.miniMapContourColor),
hexSplit4(data.miniMapShoreColor),
Expand All @@ -465,26 +457,26 @@ function scmapUtils.writeDatastream(files, filename, dir)

progressReport(dir, filename, "Processing decals")
local decalCount = #data.decals
local decalsStrings = {rint(decalCount)}
local decalsStrings = {math.intToIBM(decalCount)}
for i, decal in ipairs(data.decals) do
progressReport(dir, filename, "Processing decals", i, decalCount)
local decalBuffer = {
rint(decal.id),
rint(decal.type),
rint(#decal.textures),
math.intToIBM(decal.id),
math.intToIBM(decal.type),
math.intToIBM(#decal.textures),
}
for i, path in ipairs(decal.textures) do
table.insert(decalBuffer, rint(#path))
table.insert(decalBuffer, math.intToIBM(#path))
table.insert(decalBuffer, path)
end
table.insert(decalsStrings, table.concat(decalBuffer))
table.insert(decalsStrings, table.concat{
rvec3(decal.scale),
rvec3(decal.position),
rvec3(decal.rotation),
rfloat(decal.LODCutoff),
rfloat(decal.LODCutoffMin),
rint(decal.army),
math.floatToIBM(decal.LODCutoff),
math.floatToIBM(decal.LODCutoffMin),
math.intToIBM(decal.army),
})
end
table.insert(fileData, table.concat(decalsStrings))
Expand Down Expand Up @@ -595,11 +587,11 @@ function scmapUtils.writeDatastream(files, filename, dir)
progressReport(dir, filename, "Processing props")
if type(data.props)=='table' then
local propCount = #data.props
local propStrings = {rint(propCount)}
local propStrings = {math.intToIBM(propCount)}
for i, prop in ipairs(data.props) do
progressReport(dir, filename, "Processing props", i, propCount)
table.insert(propStrings, table.concat{
rstringNull(prop.path),
(prop.path or ''), '\000',
rvec3(prop.position),
rvec3(prop.rotationX),
rvec3(prop.rotationY),
Expand Down Expand Up @@ -629,15 +621,15 @@ function scmapUtils.readHeightmap(heightmapRaw, width, height, heightmapScale)
local index = -1
local yIndex = 0

for little, big in heightmapRaw:gmatch'(.)(.)' do--Look brothers, TITS!
for short in heightmapRaw:gmatch'(..)' do
index=index+1
if index>width then index=0 end
if index==0 then
currentRow = {}
heightmap[yIndex] = currentRow
yIndex = yIndex+1
end
height = math.IBMShort2(little, big)/(heightmapScale and (1/heightmapScale) or 128)
height = math.IBMShort(short)/(heightmapScale and (1/heightmapScale) or 128)
min = math.min(min, height)
max = math.max(max, height)
currentRow[index] = height
Expand Down

0 comments on commit 90738aa

Please sign in to comment.