build.lua map loader: add the option of canonicalizing sprite order.

This is mostly for debugging, since currently, Mapster32 restores sprites
not in the same order as the original sprite index order.  Also, expose
this option from map2text.lua and mapdiff.sh.

git-svn-id: https://svn.eduke32.com/eduke32@2968 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-08-26 22:13:57 +00:00
parent b73d16b8cc
commit 948b4f82fc
3 changed files with 97 additions and 9 deletions

View file

@ -5,11 +5,14 @@
local ffi = require "ffi" local ffi = require "ffi"
local io = require "io" local io = require "io"
local bit = require "bit" local bit = require "bit"
local string = require "string"
local table = require "table"
local error = error local error = error
local assert = assert local assert = assert
local print = print local print = print
local setmetatable = setmetatable local setmetatable = setmetatable
local tostring = tostring
module(...) module(...)
@ -130,6 +133,52 @@ local function get_numyaxbunches(map)
return numbunches return numbunches
end end
--== sprite canonicalizer ==--
local function sprite2str(s)
local FMT = "%+11d_"
-- NOTE: this canonicalization isn't very useful except for debugging
-- copy-paste in the editor.
-- tostring(s): make sort stable
return string.format(FMT:rep(4).."%s", s.x, s.y, s.z, s.ang, tostring(s))
end
local function canonicalize_sprite_order(map)
local numsprites = map.numsprites
map.spriten2o = {} -- mapping of new to old sprite index
if (numsprites == 0) then
return
end
local spriteidx = {}
for i=0,numsprites-1 do -- 0->1 based indexing
spriteidx[i+1] = i
end
table.sort(spriteidx,
function(i1, i2)
return sprite2str(map.sprite[i1]) < sprite2str(map.sprite[i2])
end)
-- deep-copied sprite structs
local spritedup = {}
for i=0,numsprites-1 do
-- save sorting permutation (0-based -> 0-based)
map.spriten2o[i] = assert(spriteidx[i+1])
-- back up struct
spritedup[i] = ffi.new("spritetype")
ffi.copy(spritedup[i], map.sprite[i], ffi.sizeof("spritetype"))
end
for i=0,numsprites-1 do -- do the actual rearrangement
map.sprite[i] = spritedup[spriteidx[i+1]]
end
end
--== LOADBOARD ==-- --== LOADBOARD ==--
-- returns: -- returns:
-- on failure, nil, errmsg -- on failure, nil, errmsg
@ -144,7 +193,7 @@ end
-- { x=<num>, y=<num>, z=<num>, ang=<num>, sectnum=<num> }, -- { x=<num>, y=<num>, z=<num>, ang=<num>, sectnum=<num> },
-- numbunches = <num>, -- numbunches = <num>,
-- } -- }
function loadboard(filename) function loadboard(filename, do_canonicalize_sprite)
local fh, errmsg = io.open(filename) local fh, errmsg = io.open(filename)
if (fh==nil) then if (fh==nil) then
@ -223,6 +272,11 @@ function loadboard(filename)
end end
fh:close() fh:close()
if (do_canonicalize_sprite) then
-- must do this before setting metatable
canonicalize_sprite_order(map)
end
map.sector = set_secwalspr_mt(map.sector, map.numsectors) map.sector = set_secwalspr_mt(map.sector, map.numsectors)
map.wall = set_secwalspr_mt(map.wall, map.numwalls) map.wall = set_secwalspr_mt(map.wall, map.numwalls)
map.sprite = set_secwalspr_mt(map.sprite, map.numsprites) map.sprite = set_secwalspr_mt(map.sprite, map.numsprites)

View file

@ -3,10 +3,29 @@
local B = require("build") local B = require("build")
local string = require("string") local string = require("string")
local fn = arg[1] local opt, fn
local do_canonicalize_sprite
if (arg[1] and arg[1]:sub(1,1)=="-") then
opt = arg[1]
fn = arg[2]
else
fn = arg[1]
end
if (opt) then
if (opt=="-c" or opt=="-C") then
-- -C means to canonicalize but without adding the new->old mapping
-- comment
do_canonicalize_sprite = opt
else
print("Error: unrecognized option "..opt)
return 1
end
end
if (fn==nil) then if (fn==nil) then
print("Usage: map2text <somefile.map>") print("Usage: map2text [-c/-C] <somefile.map>")
return 1 return 1
end end
@ -14,7 +33,7 @@ local function printf(fmt, ...)
print(string.format(fmt, ...)) print(string.format(fmt, ...))
end end
local map, errmsg = B.loadboard(fn) local map, errmsg = B.loadboard(fn, do_canonicalize_sprite)
if (map == nil) then if (map == nil) then
printf("Couldn't load \"%s\": %s", fn, errmsg) printf("Couldn't load \"%s\": %s", fn, errmsg)
return 1 return 1
@ -68,13 +87,21 @@ local sprite_members = {
local function print_members(map, struct, members) local function print_members(map, struct, members)
printf("%s = {", struct) printf("%s = {", struct)
for i=0,map["num"..struct.."s"]-1 do for i=0,map["num"..struct.."s"]-1 do
printf("[%d]={", i) local comment = ""
if (struct=="sprite" and do_canonicalize_sprite=="-c") then
comment = " --"..tostring(map.spriten2o[i])
end
printf("[%d]={%s", i, comment)
for j=1,#members do for j=1,#members do
local member = members[j] local member = members[j]
printf("%s = %d", member, map[struct][i][member]) printf("%s = %d", member, map[struct][i][member])
end end
print("}") print("}")
end end
print("}") print("}")
end end

View file

@ -1,8 +1,10 @@
#!/bin/sh #!/bin/sh
DIFF="git diff --no-index --color-words" DIFF="git diff -U2 --no-index --color-words"
CMD="/usr/bin/env luajit ./map2text.lua" CMD="/usr/bin/env luajit ./map2text.lua"
opt=""
if [ `uname -s` != "Linux" ]; then if [ `uname -s` != "Linux" ]; then
# I think 'tempfile' isn't in POSIX. Feel free to use 'mktemp' or something # I think 'tempfile' isn't in POSIX. Feel free to use 'mktemp' or something
# but absolutely test it before. # but absolutely test it before.
@ -10,16 +12,21 @@ if [ `uname -s` != "Linux" ]; then
return 1 return 1
fi fi
if [ "$1" = "-c" -o "$1" = "-C" ]; then
opt="$1"
shift
fi
if [ -z "$1" -o -z "$2" ]; then if [ -z "$1" -o -z "$2" ]; then
echo "Usage: ./mapdiff.sh <file.map> <file2.map>" echo "Usage: ./mapdiff.sh [-c] <file.map> <file2.map>"
exit 1 exit 1
fi fi
tf1=`tempfile` tf1=`tempfile`
tf2=`tempfile` tf2=`tempfile`
$CMD "$1" > "$tf1" $CMD $opt "$1" > "$tf1"
$CMD "$2" > "$tf2" $CMD $opt "$2" > "$tf2"
$DIFF "$tf1" "$tf2" $DIFF "$tf1" "$tf2"