(local util (require :lib.util)) (local {: lo : hi : readjson} util) (local lume (require :lib.lume)) (local tile (util.reload :game.tiles)) (local {: prg : vm : org : mapw : maph : itile : controlstate} (util.reload :game.defs)) (util.reload :game.gfx) (util.reload :game.footer) (util.reload :game.map) (util.reload :game.entity) (local {: walkable : neutable : debris : sittable} tile.flag-to-bit) (vm:word :movement-dir ; key -- dyx (vm:case [(string.byte "I") 0xff00] [(string.byte "J") 0x00ff] [(string.byte "K") 0x0001] [(string.byte "M") 0x0100] [:else 0x0000])) (vm:def :yx+ ; yx yx -- yx [:lda vm.TOP :x] [:clc] [:adc vm.ST1 :x] [:sta vm.ST1 :x] [:lda vm.TOPH :x] [:clc] [:adc vm.ST1H :x] [:sta vm.ST1H :x] (vm:drop)) (vm:var :jaye-yx 0x0a0a) (vm:var :jaye-dir 0xff00) (vm:var :neut-yx 0x0b08) (vm:var :rexx-yx 0xffff) (vm:var :gord-yx 0xffff) (vm:var :gord-dir 0x0000) (vm:var :gord-sitting vm.false) (vm:var :controlstate [:db controlstate.jaye]) (vm:word :is-jaye? :controlstate :bget controlstate.jaye :=) (vm:word :is-neut? :controlstate :bget controlstate.neut :=) (vm:word :is-rexx? :controlstate :bget controlstate.rexx :=) (vm:word :is-prog? :is-neut? :is-rexx? :|) (vm:word :is-walking? :movable-player-flag walkable :=) (vm:word :neut-hidden? :neut-yx :get 0xffff :=) (vm:word :rexx-active? :rexx-yx :get 0xffff := :not) (vm:word :gord-hidden? :gord-yx :get 0xffff :=) (vm:word :gord-following? :gord-hidden? :gord-sitting :get :| :not) (vm:word :set-rexx ; e -- :dup (vm:if [:get controlstate.rexx] [:drop 0xffff controlstate.neut]) :controlstate :bset :rexx-yx :set) (vm:word :player-tile ; -- ptile :controlstate :bget (vm:case [controlstate.jaye :jaye-tile] [controlstate.neut :neut-tile] [controlstate.gord :gord-tile] [:else :lit :t-rexx])) (vm:word :player-yx ; -- pyx :controlstate :bget (vm:case [controlstate.jaye :jaye-yx] [controlstate.neut :neut-yx] [controlstate.gord :gord-yx] [:else :rexx-yx])) (vm:word :draw-player ; -- :player-yx :dup (vm:if [:get :dup 0xffff := (vm:if [:drop] [:yx>screen :player-tile :drawtile])] [:drop])) (vm:word :set-player-dir ; dir -- :is-jaye? (vm:if [:jaye-dir :set] [:drop])) (vm:word :movable-player-flag ; -- flag :is-neut? (vm:if [neutable] [walkable])) (vm:word :move-player-to ; yx -- :player-yx :dup :get :dup 0xffff := (vm:if [:drop] [:drawtile-at]) :set :draw-player) (vm:word :transition-gord-sitting ; yx f -- controlstate.gord :controlstate :bset :gord-sitting :set :move-player-to controlstate.jaye :controlstate :bset) (vm:word :handle-special-move ; yx -- f (vm:if-and [[:is-rexx?] [:dup debris :flag-at?]] [(itile :t-floor) :update-itile vm.false] [(vm:if-and [[:is-jaye?] [:gord-following?] [:dup sittable :flag-at?]] [vm.true :transition-gord-sitting vm.true] [(vm:if-and [[:is-jaye?] [:gord-sitting :get] [:dup :gord-yx :get :=]] [:drop :jaye-yx :get vm.false :transition-gord-sitting 0 :gord-dir :set vm.true] [:drop vm.false])])])) (vm:def :yxclip? ; yx -- f [:block [:lda vm.TOP :x] [:cmp mapw] [:bcs :clipped] [:lda vm.TOPH :x] [:cmp maph] [:bcs :clipped] [:lda 0] [:sta vm.TOP :x] [:sta vm.TOPH :x] (vm:ret) :clipped [:lda 0xff] [:sta vm.TOP :x] [:sta vm.TOPH :x]]) (vm:word :try-move-player ; dir -- :dup :set-player-dir ; dir :player-yx :get :yx+ ; yxnew (vm:if-or [[:dup :yxclip?] [:dup :touch-entity] [:dup :handle-special-move] [:dup :movable-player-flag :flag-at? :not]] [:drop :player-yx :get]) ; always "move" so that player can visibly change direction ; touch-entity can modify player-yx so we have to refetch :move-player-to) (vm:word :jaye-tile ; ptile :jaye-dir :get (vm:case [0xff00 :lit :jaye-n] [0x0100 :lit :jaye-s] [0x00ff :lit :jaye-w] [:else :lit :jaye-e])) (vm:word :gord-tile ; ptile :gord-sitting :get (vm:if [:lit :gord-sit] [:gord-dir :get (vm:case [0xff00 :lit :gord-n] [0x0100 :lit :gord-s] [0x00ff :lit :gord-w] [:else :lit :gord-e])])) (vm:word :neut-tile :lit :neut1) ; todo: animate (vm:word :flag-at? ; yx flag -- f :swap :itile-at :lookup-flags :&) (vm:word :toggle-player (vm:ifchain [:is-prog?] [controlstate.jaye] [:rexx-active?] [controlstate.rexx] [:neut-hidden?] [controlstate.jaye] [controlstate.neut]) :controlstate :bset) (vm:word :party-follow (vm:if-and [[:is-jaye?] [:gord-following?]] [controlstate.gord :controlstate :bset :gord-yx :get :gord-dir :get :yx+ :move-player-to :jaye-dir :get :gord-dir :set controlstate.jaye :controlstate :bset])) (vm:word :player-key ; key -- (vm:ifchain [:dup (string.byte " ") :=] [:drop :toggle-player] [:dup (string.byte "Z") :=] [:drop :trigger-sidekick] [:movement-dir :dup] [:player-yx :get :swap ; oldyx dir :try-move-player :dup :player-yx :get := (vm:if [:drop] [:party-follow :untouch-entity])] [:drop])) (vm:word :full-redraw :drawmap :player-redraw) (vm:word :player-redraw :controlstate :bget controlstate.count (vm:for (vm:i) :controlstate :bset :draw-player) :controlstate :bset) (tile.appendtiles org.tiles) (tile.appendgfx org.font (tile.loadgfx tile.fn-font)) ; thought: ; hotswap-safe debug stub at root of call stack ; but REPL debug stub should be very available as a task ; 20x12 means full map is 240 bytes - we have an extra 16 bytes at the end to mess with? (vm:word :handle-key :read-key :player-key :hide-footer) (vm:word :load-level :lit :map-jaye-yx :get :jaye-yx :set :lit :map-neut-yx :get :neut-yx :set :lit :map-gord-yx :get :gord-yx :set 0 :gord-dir :set 0xffff :rexx-yx :set :full-redraw) (vm.code:append :main [:jsr :reset] [:jsr :interpret] [:vm :hires :load-level (vm:forever (vm:hotswap-sync :load-level) :interactive-eval-checkpoint :handle-key ) :quit]) (util.reload :game.level5) (prg:assemble)