2020-10-12 15:48:14 +00:00
|
|
|
(local util (require :lib.util))
|
|
|
|
(local lume (require :lib.lume))
|
2021-04-25 03:39:50 +00:00
|
|
|
(local files (require :game.files))
|
2021-06-26 01:55:15 +00:00
|
|
|
|
|
|
|
(fn flags [] (or files.game.tileflags [:walkable]))
|
|
|
|
(fn flag-to-bit []
|
|
|
|
(collect [iflag flag (ipairs (flags))] (values flag (bit.lshift 1 (- iflag 1)))))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2021-04-25 03:39:50 +00:00
|
|
|
(fn appendgfx [org gfx ?key ?label-prefix]
|
2020-11-23 00:44:56 +00:00
|
|
|
(each [_ g (ipairs gfx)]
|
2021-04-25 03:39:50 +00:00
|
|
|
(when g.label (org:append (.. (or ?label-prefix "") g.label)))
|
2020-12-24 03:17:33 +00:00
|
|
|
(org:append [:bytes (. g (or ?key :gfx))])))
|
2020-11-23 00:44:56 +00:00
|
|
|
|
|
|
|
(fn appendtiles [org]
|
2021-04-25 03:39:50 +00:00
|
|
|
(local tiles files.game.tiles)
|
2021-06-26 01:55:15 +00:00
|
|
|
(local flag-lookup (flag-to-bit))
|
2021-06-26 02:30:15 +00:00
|
|
|
(each [tileset key (pairs (or files.game.tilesets {:tileset :gfx}))]
|
|
|
|
(org:append [:align 0x100] tileset)
|
|
|
|
(appendgfx org tiles key (if (= key :gfx) nil (.. key :-))))
|
2021-04-25 03:39:50 +00:00
|
|
|
(appendgfx org files.game.portraits nil :portrait-)
|
2020-11-16 16:09:14 +00:00
|
|
|
(org:append :tileflags)
|
|
|
|
(each [_ tile (ipairs tiles)]
|
|
|
|
(var flags 0)
|
|
|
|
(each [flag _ (pairs tile.flags)]
|
2021-06-26 01:55:15 +00:00
|
|
|
(set flags (bit.bor flags (. flag-lookup flag))))
|
2020-11-16 16:09:14 +00:00
|
|
|
(org:append [:db flags])))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2021-02-13 19:32:54 +00:00
|
|
|
(fn append-portraitwords [vm ?overrides]
|
|
|
|
(local overrides (or ?overrides {}))
|
2021-04-25 03:39:50 +00:00
|
|
|
(each [_ p (ipairs files.game.portraits)]
|
|
|
|
(let [wordname (.. :draw-portrait- p.label)
|
2021-02-13 19:32:54 +00:00
|
|
|
override (. overrides p.label)]
|
2021-04-25 03:39:50 +00:00
|
|
|
(vm:word wordname :show-footer
|
|
|
|
(if override (override p.label) [:vm :lit (.. :portrait- p.label)])
|
2021-02-13 19:32:54 +00:00
|
|
|
:draw-portrait))))
|
|
|
|
|
2020-12-03 01:08:10 +00:00
|
|
|
(fn encode-yx [xy]
|
|
|
|
(if xy (bit.bor (bit.lshift (- xy.y 1) 8) (- xy.x 1)) 0xffff))
|
|
|
|
|
2021-01-24 16:32:49 +00:00
|
|
|
(fn encode-itile [itile]
|
2021-02-11 04:26:08 +00:00
|
|
|
(bit.bor
|
|
|
|
(bit.lshift (bit.band (- itile 1) 0x07) 5)
|
|
|
|
(bit.rshift (bit.band (- itile 1) 0xf8) 3)))
|
|
|
|
|
|
|
|
(fn decode-itile [enctile]
|
|
|
|
(+ 1 (bit.bor
|
|
|
|
(bit.lshift (bit.band enctile 0x1f) 3)
|
|
|
|
(bit.rshift (bit.band enctile 0xe0) 5))))
|
2021-01-24 16:32:49 +00:00
|
|
|
|
2020-11-27 02:34:05 +00:00
|
|
|
(fn find-itile [tiles label ?itilenext]
|
|
|
|
(local itile (or ?itilenext 1))
|
|
|
|
(local tile (. tiles itile))
|
|
|
|
(assert (not= tile nil) (.. "No such tile " label))
|
2021-01-24 16:32:49 +00:00
|
|
|
(if (= tile.label label) (encode-itile itile)
|
2020-11-27 02:34:05 +00:00
|
|
|
(find-itile tiles label (+ itile 1))))
|
|
|
|
|
2021-04-25 03:39:50 +00:00
|
|
|
{: appendtiles : appendgfx : append-portraitwords : flags : flag-to-bit : find-itile
|
|
|
|
: encode-yx : encode-itile : decode-itile}
|
2020-10-12 15:48:14 +00:00
|
|
|
|