2020-10-12 15:48:14 +00:00
|
|
|
(local util (require :lib.util))
|
|
|
|
(local lume (require :lib.lume))
|
|
|
|
|
2020-12-12 01:53:46 +00:00
|
|
|
(local flags [:walkable :neutable :debris :sittable])
|
2020-11-16 16:09:14 +00:00
|
|
|
(local flag-to-bit {})
|
|
|
|
(each [iflag flag (ipairs flags)]
|
|
|
|
(tset flag-to-bit flag (bit.lshift 1 (- iflag 1))))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2021-01-17 02:40:04 +00:00
|
|
|
(local encoded-tile-fields [:gfx :neut :mask])
|
2020-12-30 17:47:07 +00:00
|
|
|
(fn convert [tile field method]
|
|
|
|
(local oldval (. tile field))
|
|
|
|
(when oldval
|
|
|
|
(tset tile field (: oldval method)))
|
|
|
|
tile)
|
|
|
|
(fn convert-all [tile method]
|
|
|
|
(each [_ field (ipairs encoded-tile-fields)]
|
|
|
|
(convert tile field method))
|
|
|
|
tile)
|
|
|
|
|
2020-10-29 02:59:47 +00:00
|
|
|
(fn deserialize [tile]
|
|
|
|
(match (type tile)
|
|
|
|
:string {:gfx (tile:fromhex) :flags {}}
|
2020-12-30 17:47:07 +00:00
|
|
|
:table (convert-all tile :fromhex)))
|
2020-10-29 02:59:47 +00:00
|
|
|
|
2020-12-30 17:47:07 +00:00
|
|
|
(fn serialize [tile] (convert-all (lume.clone tile) :tohex))
|
2020-10-29 02:59:47 +00:00
|
|
|
|
2020-11-23 00:44:56 +00:00
|
|
|
(local fn-tiles "game/tiles.json")
|
|
|
|
(local fn-portraits "game/portraits.json")
|
|
|
|
(local fn-font "game/font.json")
|
|
|
|
|
|
|
|
(fn loadgfx [filename] (lume.map (util.readjson filename) deserialize))
|
|
|
|
(fn savegfx [filename gfx] (util.writejson filename (lume.map gfx serialize)))
|
|
|
|
|
2020-12-24 03:17:33 +00:00
|
|
|
(fn appendgfx [org gfx ?key ?ignore-labels]
|
2020-11-23 00:44:56 +00:00
|
|
|
(each [_ g (ipairs gfx)]
|
2020-12-24 03:17:33 +00:00
|
|
|
(when (and g.label (not ?ignore-labels)) (org:append g.label))
|
|
|
|
(org:append [:bytes (. g (or ?key :gfx))])))
|
2020-11-23 00:44:56 +00:00
|
|
|
|
|
|
|
(fn appendtiles [org]
|
|
|
|
(local tiles (loadgfx fn-tiles))
|
2021-01-08 01:14:51 +00:00
|
|
|
(org:append [:align 0x100] :jaye-tileset)
|
2020-11-23 00:44:56 +00:00
|
|
|
(appendgfx org tiles)
|
2021-01-03 19:01:48 +00:00
|
|
|
(org:append [:align 0x100] :neut-tileset)
|
2020-12-24 03:17:33 +00:00
|
|
|
(appendgfx org tiles :neut true)
|
2020-11-23 00:44:56 +00:00
|
|
|
(appendgfx org (loadgfx fn-portraits))
|
2020-11-16 16:09:14 +00:00
|
|
|
(org:append :tileflags)
|
|
|
|
(each [_ tile (ipairs tiles)]
|
|
|
|
(var flags 0)
|
|
|
|
(each [flag _ (pairs tile.flags)]
|
|
|
|
(set flags (bit.bor flags (. flag-to-bit flag))))
|
|
|
|
(org:append [:db flags])))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
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]
|
|
|
|
(bit.bor (bit.lshift (bit.band (- itile 1) 0x07) 5) (bit.rshift (bit.band (- itile 1) 0xf8) 3)))
|
|
|
|
|
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-01-03 19:01:48 +00:00
|
|
|
{: loadgfx : savegfx : appendtiles : appendgfx : flags : flag-to-bit : find-itile
|
2021-01-24 16:32:49 +00:00
|
|
|
: fn-tiles : fn-portraits : fn-font : encode-yx : encode-itile}
|
2020-10-12 15:48:14 +00:00
|
|
|
|