From af243f1cd698bf0496763c37d21d31eecb61ae7d Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 11 Aug 2013 15:28:50 +0000 Subject: [PATCH] Lunatic util: add foreachmap module 'plax', looking for maps w/ multiple psky tiles. In foreachmap.lua, make init() be able to return a start index for cmdline args, for the case where the run worker script wants to handle options, for example. git-svn-id: https://svn.eduke32.com/eduke32@4005 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/test.lua | 4 +- .../source/lunatic/util/foreachmap.lua | 14 +- .../eduke32/source/lunatic/util/mapypan.lua | 4 +- polymer/eduke32/source/lunatic/util/plax.lua | 146 ++++++++++++++++++ 4 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 polymer/eduke32/source/lunatic/util/plax.lua diff --git a/polymer/eduke32/source/lunatic/test.lua b/polymer/eduke32/source/lunatic/test.lua index d0f1867e3..23800d923 100644 --- a/polymer/eduke32/source/lunatic/test.lua +++ b/polymer/eduke32/source/lunatic/test.lua @@ -280,8 +280,8 @@ gameevent -- NOTE: setting colors partially is bad! E.g. after an item is -- picked up, col[0] and col[1] remain and tint everything greenish. if (DBG_ ~= nil) then - -- XXX (unrelated to these lines): issuing tinting makes it flicker - -- sometimes in the GL modes. + -- XXX (unrelated to these lines): issuing tinting sometimes makes + -- it flicker in the GL modes. ps._pals[2] = 20 ps._pals.f = 30 end diff --git a/polymer/eduke32/source/lunatic/util/foreachmap.lua b/polymer/eduke32/source/lunatic/util/foreachmap.lua index eab7c774c..c006b2a82 100755 --- a/polymer/eduke32/source/lunatic/util/foreachmap.lua +++ b/polymer/eduke32/source/lunatic/util/foreachmap.lua @@ -110,13 +110,23 @@ else mod = require(modname) end +-- The return value from the module's .init(). +local initret + if (mod.init) then - if (mod.init(arg) ~= nil) then + initret = mod.init(arg) + if (type(initret)=="number" and initret < 0) then + -- A negative return value from .init() is taken as a request to exit, + -- e.g. because an error occurred. os.exit(1) end end -for i=2,#arg do +-- A positive return value from .init() is taken to start counting map names +-- from that 'arg' index. +local startargi = (type(initret)=="number" and initret > 0 and initret) or 2 + +for i=startargi,#arg do local fn = arg[i] local map, errmsg = B.loadboard(fn) diff --git a/polymer/eduke32/source/lunatic/util/mapypan.lua b/polymer/eduke32/source/lunatic/util/mapypan.lua index 0d4280655..5b87e3dc8 100644 --- a/polymer/eduke32/source/lunatic/util/mapypan.lua +++ b/polymer/eduke32/source/lunatic/util/mapypan.lua @@ -34,7 +34,7 @@ function init(arg) if (artargend==nil or artargend==0) then printf("Usage: luajit ./foreachmap.lua [, ...] -- ...\n") - return 1 + return -1 end local artfns = {} @@ -56,7 +56,7 @@ function init(arg) local tile_, errmsg = B.loadarts(artfns) if (tile_ == nil) then printf("%s", errmsg) - return 2 + return -2 end tile = tile_ -- set 'tile' file-scope var diff --git a/polymer/eduke32/source/lunatic/util/plax.lua b/polymer/eduke32/source/lunatic/util/plax.lua new file mode 100644 index 000000000..fabaf0d00 --- /dev/null +++ b/polymer/eduke32/source/lunatic/util/plax.lua @@ -0,0 +1,146 @@ + +-- Search for maps that have parallaxed ceilings with different tile numbers. +-- foreachmap module. + +local bit = require "bit" +local string = require "string" +local table = require "table" +local io = require "io" + +local print = print +local tostring = tostring +local type = type + +local function printf(fmt, ...) + print(string.format(fmt, ...)) +end + + +module(...) + + +local MULTI_PSKY_TILES = { 80, 84, 89 } + +local IS_MULTI_PSKY_TILE = { + [80] = true, -- MOONSKY1 + [84] = true, -- BIGORBIT1 + [89] = true, -- LA +} + +local tilecnt + +-- foreachmap initialization +local opts = { p=false, s=false } -- pretty? simple? + +function init(args) + if (#args == 1) then + local wr = function(s) io.stderr:write(s) end + wr("Usage: "..args[0].." "..args[1].." [-s] [-p] *.map ...\n") + wr(" -s: short format\n") + wr(" -p: pretty-print using ANSI escapes\n\n") + wr("The long format is as follows:\n") + wr(" : (): ...\n") + wr("Each reads\n") + wr(" #:(s)\n") + wr("where is the index of an arbitrary sector containing such a psky ceiling.\n") + return + end + + for i=2,#args do + local arg = args[i] + + if (arg:sub(1,1)=="-") then + local letter = arg:sub(2,2) + + if (#arg==2 and type(opts[letter])=="boolean") then + opts[letter] = true + else + io.stderr:write("Unrecognized option "..arg.."\n") + end + else + return i + end + end +end + +-- Sorting function for tiles, true if "less-than" . +-- "less-than" here means appearing earlier in the resulting table. +local function sortskytiles(tile1, tile2) + local ismulti1 = IS_MULTI_PSKY_TILE[tile1] + local ismulti2 = IS_MULTI_PSKY_TILE[tile2] + + -- First, check if is a multi-psky tile and not. + if (ismulti1 and not ismulti2) then + return true + end + + -- Next, check if appears more often in the map. + if (tilecnt[tile1] > tilecnt[tile2]) then + return true + end + + -- Now, (tilecnt[tile1] >= tilecnt[tile2]) and (not ismulti1 or ismulti2). + return false +end + +local function bold(s) + return opts.p and ("\x1b[1m"..s.."\x1b[0m") or tostring(s) +end + +function success(map, fn) + -- [] = + tilecnt = {} + + -- [] = + local tiles = {} + -- [] = + local tilesect = {} + + for i=0,map.numsectors-1 do + local sec = map.sector[i] + + if (bit.band(sec.ceilingstat, 1) ~= 0) then + local tile = sec.ceilingpicnum + + if (tilecnt[tile] == nil) then + tiles[#tiles+1] = tile + end + + tilecnt[tile] = 1 + (tilecnt[tile] or 0) + tilesect[tile] = i + end + end + + if (#tiles > 0) then + -- Do an in-place sort. + table.sort(tiles, sortskytiles) + + local strbuf = {} + + for i=1,#tiles do + local tile = tiles[i] + local ismulti = IS_MULTI_PSKY_TILE[tile] + + if (i == 4) then + strbuf[#strbuf+1] = "..." + break + end + + strbuf[#strbuf+1] = (ismulti and "*" or "#")..tile.. + ":"..tilecnt[tile].. + (opts.s and "" or ("(s"..tilesect[tile]..")")) + end + + local nummultiskies = 0 + for i=1,#MULTI_PSKY_TILES do + if (tilecnt[MULTI_PSKY_TILES[i]]) then + nummultiskies = nummultiskies+1 + end + end + + -- DCPT: distinct ceiling-psky tiles + printf("%s: %s%sDCPT: %s", fn, bold(#tiles), + opts.s and " " or bold(" ("..nummultiskies..") "), + table.concat(strbuf," ")) + end +end