honeylisp/game/tiles.fnl
2020-12-11 20:53:46 -05:00

61 lines
2 KiB
Fennel

(local util (require :lib.util))
(local lume (require :lib.lume))
(local flags [:walkable :neutable :debris :sittable])
(local flag-to-bit {})
(each [iflag flag (ipairs flags)]
(tset flag-to-bit flag (bit.lshift 1 (- iflag 1))))
(fn deserialize [tile]
(match (type tile)
:string {:gfx (tile:fromhex) :flags {}}
:table (doto tile (tset :gfx (tile.gfx:fromhex)))))
(fn serialize [tile] (doto (lume.clone tile) (tset :gfx (tile.gfx:tohex))))
(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)))
(fn appendgfx [org gfx]
(each [_ g (ipairs gfx)]
(when g.label (org:append g.label))
(org:append [:bytes g.gfx])))
(fn appendtiles [org]
(local tiles (loadgfx fn-tiles))
(appendgfx org tiles)
(appendgfx org (loadgfx fn-portraits))
(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])))
(fn encode-yx [xy]
(if xy (bit.bor (bit.lshift (- xy.y 1) 8) (- xy.x 1)) 0xffff))
(fn append-map [map org]
(org:append
:map [:bytes (map.map:fromhex)]
:map-entity-count [:db (length map.objects)]
:map-jaye-yx [:dw (encode-yx map.jaye)]
:map-neut-yx [:dw (encode-yx map.neut)]
:map-gord-yx [:dw (if map.gord-following (encode-yx map.jaye) 0xffff)]))
(fn find-itile [tiles label ?itilenext]
(local itile (or ?itilenext 1))
(local tile (. tiles itile))
(assert (not= tile nil) (.. "No such tile " label))
(if (= tile.label label)
(bit.bor (bit.lshift (bit.band (- itile 1) 0x07) 5) (bit.rshift (bit.band (- itile 1) 0xf8) 3))
(find-itile tiles label (+ itile 1))))
{: loadgfx : savegfx : appendtiles : appendgfx : append-map : flags : flag-to-bit : find-itile
: fn-tiles : fn-portraits : fn-font : encode-yx}