honeylisp/game/tiles.fnl

53 lines
1.7 KiB
Fennel

(local util (require :lib.util))
(local lume (require :lib.lume))
(local flags [:walkable :neutable :debris])
(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 append-map [map org]
(org:append :map [:bytes (map.map:fromhex)] :map-entity-count [:db (length map.objects)]))
(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}