honeylisp/game/entity.fnl

191 lines
6.8 KiB
Plaintext
Raw Normal View History

2020-11-27 02:34:05 +00:00
(local util (require :lib.util))
(local tiles (util.require :game.tiles))
2020-12-22 04:24:39 +00:00
(local {: vm : org : itile : say : say-runon : controlstate} (require :game.defs))
2020-11-27 02:34:05 +00:00
(local {: lo : hi} util)
2020-11-24 04:41:00 +00:00
; Entity memory layout:
; +0 - yx
; +2 - event handler
; +4 - link word
2020-11-27 02:34:05 +00:00
; +6 - link pointer
2020-11-24 04:41:00 +00:00
; All entities exist in a single page in RAM - with this structure we can have up to 32
; (players are handled specially and never require a link)
; if we really need more we could have one page for entities and one page for link data
; hellmaze level 2 from MS-DOS Neut Tower has 36 entities - good excuse to simplify IMO
; The entity count for a level is stored after the map.
(local ev {
:touch 0
:untouch 1
:act 2
:deact 3
:tog 4
2020-12-22 04:24:39 +00:00
:hack 5
:noop 6
2020-11-24 04:41:00 +00:00
})
2020-11-27 04:33:14 +00:00
(vm:def :lookup-entity ; i -- entity
[:lda vm.TOP :x]
[:asl :a] [:asl :a] [:asl :a] ; x8
[:sta vm.TOP :x]
[:lda :map-page] [:clc] [:adc 1]
2020-11-27 04:33:14 +00:00
[:sta vm.TOPH :x])
(vm:word :entity-at ; yx -- entity|0
:>r 0 :entity-count
(vm:while [:dup] :dec ; entity|0 i
:dup :lookup-entity :get :rtop :=
(vm:when :lookup-entity :swap)
) :drop :rdrop)
(vm:var :responder 0)
(vm:word :get-responder :responder :get)
2020-12-12 01:53:46 +00:00
(vm:word :entity-itile :get :itile-at)
(vm:word :responder-itile :get-responder :entity-itile)
2020-11-27 04:33:14 +00:00
(vm:word :entity>do ; entity ev --
:over :responder :dup :get :>r :set
2020-11-27 04:33:14 +00:00
:swap 2 :+ :get :execute
:r> :responder :set)
(vm:word :link-arg ; e -- a
6 :+ :get)
(vm:word :linked-entity :get-responder :dup 4 :+ :get :dup (vm:if [:execute] [:drop :link-arg]))
2020-12-12 01:53:46 +00:00
(vm:word :entity-at>do ; yx ev -- f
:>r :entity-at :dup (vm:if [:r> :entity>do vm.true] [:rdrop]))
2020-11-27 04:33:14 +00:00
(vm:word :touch-entity ; yx -- f
2020-12-12 01:53:46 +00:00
ev.touch :entity-at>do)
(vm:word :untouch-entity ; yx --
2020-12-12 01:53:46 +00:00
ev.untouch :entity-at>do :drop)
(vm:word :entity-around>do ; yx ev --
:over 0x0100 :yx+ :over :entity-at>do :drop
:over 0x0001 :yx+ :over :entity-at>do :drop
:over 0xff00 :yx+ :over :entity-at>do :drop
:swap 0x00ff :yx+ :swap :entity-at>do :drop)
2020-11-27 04:33:14 +00:00
(vm:word :set-entitytile ; e itile --
:swap :get :swap :update-itile)
2020-11-27 02:34:05 +00:00
2020-11-27 04:33:14 +00:00
(vm:word :set-respondertile ; itile --
:get-responder :get :swap :update-itile)
2020-11-27 02:34:05 +00:00
2020-11-27 04:33:14 +00:00
(vm:word :handle-onoff ; ev off on --
:<rot (vm:case
[ev.act :swap :drop :set-respondertile]
[ev.deact :drop :set-respondertile]
[ev.tog :dup :responder-itile := (vm:if [:drop :set-respondertile] [:set-respondertile :drop])]
[:else :drop :drop]))
(vm:word :activation-ev? ; ev -- f
:dup ev.act := :over ev.deact := :| :swap ev.tog := :|)
(vm:word :activate-link ; ev itile-on --
:swap :activation-ev? (vm:if [
:responder-itile := (vm:if [ev.act] [ev.deact])
2020-11-27 04:33:14 +00:00
:linked-entity :swap :entity>do
] [:drop]))
2020-11-27 02:34:05 +00:00
(vm:word :walking-through-door
2020-12-12 01:53:46 +00:00
(vm:if-and [[:is-walking?] [:dup ev.touch :=] [:responder-itile (itile :dooropen) :=]]
[vm.true] [vm.false]))
(vm:word :door ; ev --
:walking-through-door (vm:if
[:move-to-responder :drop]
2020-11-29 05:44:23 +00:00
[(itile :doorclosed) (itile :dooropen) :handle-onoff]))
2020-11-27 02:34:05 +00:00
(vm:word :exitdoor ; ev --
:walking-through-door (vm:if
[:move-to-responder :drop :get-responder 4 :+ :get :next-level :set]
[:door]))
(vm:word :move-to-responder :get-responder :get :move-player-to)
2020-11-27 04:33:14 +00:00
(vm:word :switch ; ev --
2020-12-10 14:11:46 +00:00
(vm:if-and [[:is-rexx? :not] [:dup ev.touch :=]]
[:drop ev.tog :is-neut? (vm:when :move-to-responder)])
2020-11-27 04:33:14 +00:00
:dup (itile :switchoff) (itile :switchon) :handle-onoff
(itile :switchon) :activate-link)
2020-11-27 02:34:05 +00:00
2020-11-27 04:33:14 +00:00
(vm:word :term ; ev --
2020-11-29 05:44:23 +00:00
:dup ev.touch := (vm:when
(vm:ifchain [:is-jaye?] [:drop ev.act]
[:is-neut?] [:linked-entity
2020-12-12 01:53:46 +00:00
(vm:if-and [[:dup :entity-itile (itile :termon) :=]
[:responder-itile (itile :termon) :=]]
[:get :move-player-to] [:drop])]
[]))
(itile :termoff) (itile :termon) :handle-onoff)
2020-11-24 04:41:00 +00:00
(vm:word :handle-scan ; ev --
:dup (itile :scanoff) (itile :scanon) :handle-onoff
:linked-entity :swap :entity>do)
2020-12-22 04:24:39 +00:00
(vm:word :libb-on-responder :libb-yx :get :get-responder :get :=)
(vm:word :scan ; ev --
:is-neut? (vm:if [
(vm:case
2020-12-22 04:24:39 +00:00
[ev.touch ev.act :handle-scan :libb-on-responder (vm:when controlstate.libb :controlstate :bset 0xffff :move-player-to controlstate.neut :controlstate :bset) :move-to-responder]
[ev.untouch :libb-on-responder :not (vm:when ev.deact :handle-scan)]
[ev.hack ev.act :handle-scan controlstate.libb :controlstate :bset :move-to-responder controlstate.neut :controlstate :bset]
[:else])
] [:drop]))
2020-12-02 22:19:23 +00:00
(vm:word :rexx ; ev --
ev.touch := (vm:when
2020-12-10 14:11:46 +00:00
(vm:if-and [[:is-neut?] [:responder-itile (itile :t-rexx) :=]]
2020-12-02 22:19:23 +00:00
[0xffff :move-player-to
(itile :t-rexxstop) :set-respondertile
:get-responder :set-rexx]
2020-12-10 14:11:46 +00:00
[(vm:if-and [[:is-rexx?] [:responder-itile (itile :t-rexxstop) :=]]
2020-12-02 22:19:23 +00:00
[0xffff :move-player-to
(itile :t-rexx) :set-respondertile
0 :set-rexx :move-to-responder])])))
(vm:word :read-digit ; -- digit
(vm:while [:read-key :dup 0x3a :< :over 0x30 :>= :& :not] :drop) 0x30 :-)
(vm:word :keypad-digit ; pscreen -- n
:read-digit :swap :over :draw-digit)
(vm:word :next-digit ; pscreen n -- pscreen n
:shl4 :over :keypad-digit :+ :swap 1 :+ :swap)
(vm:word :draw-single-keypad-hash ; pscreen -- pscreen
:dup (string.byte "#") :draw-char 1 :+)
(vm:word :read-keypad ; -- n
0x23e2 :dup
:draw-single-keypad-hash :draw-single-keypad-hash :draw-single-keypad-hash :draw-single-keypad-hash :drop
0 :next-digit :next-digit :next-digit :next-digit :swap :drop :cleartext)
(vm:word :keypad ; ev code --
:>r
2020-12-19 20:53:27 +00:00
:dup ev.touch := (vm:when
:is-jaye? (vm:when
:responder-itile (itile :t-keyoff) := (vm:if
[(say-runon :pady "ENTER YOUR 4-DIGIT DOOR CODE!" "AND HAVE A SUPER DAY!")
:read-keypad :rtop := (vm:if
[(say :pady "THAT'S RIGHT! HOORAY!" "YOU GET A GOLD STAR!")
:drop ev.act]
[(say :pady "OHHH, SORRY! THAT'S NOT IT." "BETTER LUCK NEXT TIME!")])]
[(say :pady "OH HI AGAIN! I MISSED YOU TOO!")]))
:is-neut? (vm:when :move-to-responder))
:rdrop
2020-12-22 04:24:39 +00:00
:dup ev.hack := (vm:when :drop ev.act)
:dup (itile :t-keyoff) (itile :t-keyon) :handle-onoff
(itile :t-keyon) :activate-link)
2020-12-12 01:53:46 +00:00
(vm:word :trigger-sidekick
(vm:if-and [[:is-jaye?] [:gord-sitting :get]]
2020-12-22 04:24:39 +00:00
[:gord-yx :get ev.touch :entity-around>do]
[(vm:if-and [[:is-neut?] [:libb-present :get] [:libb-yx :get 0xffff :=]]
[:neut-yx :get ev.hack :entity-at>do :drop])]))
2020-12-12 01:53:46 +00:00
(fn append-from-map [map entity-org prefix]
(entity-org:append [:align 0x100])
(each [ientity entity (ipairs map.objects)]
2020-11-24 04:41:00 +00:00
(when entity.name
(entity-org:append entity.name))
(entity-org:append
(.. prefix "-entity-" ientity)
2020-11-24 04:41:00 +00:00
[:db (- entity.x 1)] [:db (- entity.y 1)]
[:ref entity.func]
2020-11-27 02:34:05 +00:00
(if (and entity.linkword (> (length entity.linkword) 0)) [:ref entity.linkword] [:dw 0])
(if entity.link [:ref (.. prefix "-entity-" entity.link)] [:dw 0]))))
2020-11-24 04:41:00 +00:00
2020-11-27 04:33:14 +00:00
{: ev : append-from-map}
2020-11-24 04:41:00 +00:00