You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/txtadv-demo/expander.rkt

290 lines
9.1 KiB
Racket

9 years ago
#lang br
(require (for-syntax racket/string))
(provide define-verbs
define-thing
define-place
define-everywhere
show-current-place
show-inventory
have-thing?
take-thing!
drop-thing!
thing-state
set-thing-state!
9 years ago
(except-out (all-from-out br) #%module-begin)
(rename-out [module-begin #%module-begin]))
;; ============================================================
;; Overall module:
9 years ago
(define-macro (module-begin _lines ...)
#'(#%module-begin
9 years ago
_lines ...
9 years ago
(provide do-verb do-place)
(module+ main
(parameterize ([cmd-line-mode? #t])
(do-place)))))
9 years ago
9 years ago
;; ==============================================================
;; Process parse trees from the reader:
9 years ago
(provide txtadv-program)
9 years ago
(define-macro txtadv-program #'module-begin)
9 years ago
(provide verb-section)
9 years ago
(define-macro-cases verb-section
[(_ ((NAME0 . TRANSITIVE0?) (NAME . _) ... DESC) ...)
9 years ago
(with-shared-id
9 years ago
(in-verbs)
#'(define-verbs in-verbs
[(NAME0 . TRANSITIVE0?) (= NAME ...) DESC] ...))])
(provide everywhere-section)
9 years ago
(define-macro (everywhere-section [ID DESC] ...)
#'(define-everywhere everywhere-actions
9 years ago
([ID DESC] ...)))
(provide things-section)
9 years ago
(define-macro (things-section (THINGNAME (ACTIONNAME ACTIONDESC) ...) ...)
#'(begin (define-thing THINGNAME [ACTIONNAME ACTIONDESC] ...) ...))
(provide places-section)
9 years ago
(define-macro (places-section (PLACE-ID PLACE-DESC [PLACE-ITEM ...] [ACTIONNAME ACTIONDESC] ...) ...)
#'(begin (define-place PLACE-ID PLACE-DESC [PLACE-ITEM ...] ([ACTIONNAME ACTIONDESC] ...)) ...))
;; todo: consolidate the game-starters.
;; `start-game-at` works with s-exp language,
;; `start-section` works with text lang.
(provide start-game-at)
9 years ago
(define-macro (start-game-at WHERE)
#'(init-game WHERE
in-verbs
everywhere-actions))
(provide start-section)
9 years ago
(define-macro (start-section WHERE)
9 years ago
(with-shared-id
(in-verbs)
#'(init-game WHERE
in-verbs
everywhere-actions)))
;; ============================================================
;; Model:
;; Elements of the world:
(struct verb (aliases ; list of symbols
desc ; string
9 years ago
transitive?) #:transparent) ; boolean
(struct thing (name ; symbol
[state #:mutable] ; any value
9 years ago
actions) #:transparent) ; list of verb--thunk pairs
9 years ago
(struct place (desc ; string
[things #:mutable] ; list of things
9 years ago
actions) #:transparent) ; list of verb--thunk pairs
9 years ago
(define action-verb car)
(define action-response cdr)
;; Tables mapping names<->things for save and load
(define names (make-hash))
(define elements (make-hash))
(define (record-element! name val)
(hash-set! names name val)
(hash-set! elements val name))
(define (name->element name) (hash-ref names name #f))
(define (element->name obj) (hash-ref elements obj #f))
;; ============================================================
;; Macros for constructing and registering elements:
9 years ago
(define-macro (define-verbs ALL-ID [(ID . MAYBE-UNDERSCORE) SPEC ...] ...)
9 years ago
#'(begin
9 years ago
(define-one-verb (ID . MAYBE-UNDERSCORE) SPEC ...) ...
(record-element! 'ID ID) ...
(define ALL-ID (list ID ...))))
9 years ago
;; todo: the underscore arguments in cases 2 & 4 should be literal underscores, not wildcards
9 years ago
(define-macro (define-one-verb (ID . MAYBE-UNDERSCORE) . REST)
(with-pattern
([TRANSITIVE? (equal? '("_") (syntax->datum #'MAYBE-UNDERSCORE))]
[VERB-ARGS (syntax-case #'REST ()
[((= ALIAS ...) DESC)
#'((list 'ID 'ALIAS ...) DESC TRANSITIVE?)]
[else
#'((list 'ID) (symbol->string 'ID) TRANSITIVE?)])])
#'(define ID (verb . VERB-ARGS))))
(define-macro (define-thing ID [VERB-ARG EXPR] ...)
9 years ago
#'(begin
9 years ago
(define ID
(thing 'ID #f (list (cons VERB-ARG (λ () EXPR)) ...)))
(record-element! 'ID ID)))
9 years ago
9 years ago
(define-macro (define-place ID DESC (THING-ARG ...) ([VERB-ARG EXPR] ...))
9 years ago
#'(begin
9 years ago
(define ID (place DESC
(list THING-ARG ...)
(list (cons VERB-ARG (λ () EXPR)) ...)))
(record-element! 'ID ID)))
9 years ago
9 years ago
(define-macro (define-everywhere ID ([VERB-ARG EXPR] ...))
#'(define ID (list (cons VERB-ARG (λ () EXPR)) ...)))
;; ============================================================
;; Game state
(define cmd-line-mode? (make-parameter #f))
9 years ago
;; Initialized on startup:
9 years ago
(define game-verbs null) ; list of verbs
(define everywhere-actions null) ; list of verb--thunk pairs
;; Things carried by the player:
9 years ago
(define player-inventory null) ; list of things
;; Current location:
(define current-place #f) ; place (or #f until started)
;; Fuctions to be used by verb responses:
9 years ago
(define (have-thing? thing)
(memq thing player-inventory))
(define (take-thing! thing)
(set-place-things! current-place (remq thing (place-things current-place)))
(set! player-inventory (cons thing player-inventory)))
(define (drop-thing! thing)
(set-place-things! current-place (cons thing (place-things current-place)))
(set! player-inventory (remq thing player-inventory)))
;; ============================================================
;; Game execution
;; Show the player the current place, then get a command:
(define (do-place)
(show-current-place)
9 years ago
(when (cmd-line-mode?) (do-verb)))
;; Show the current place:
(define (show-current-place)
(printf "~a\n" (place-desc current-place))
9 years ago
(for-each
(λ (thing) (printf "There is a ~a here.\n" (thing-name thing)))
(place-things current-place)))
;; Get and handle a command:
(define (get-line)
(printf "> ")
(flush-output)
(read-line))
(define (do-verb [line (and (cmd-line-mode?) (get-line))])
(define input (if (eof-object? line)
'(quit)
(let ([port (open-input-string line)])
(for/list ([v (in-port read port)]) v))))
(if (and (list? input)
(andmap symbol? input)
(<= 1 (length input) 2))
9 years ago
(let* ([verb (car input)]
[response
(case (length input)
[(2) (handle-transitive-verb verb (cadr input))]
[(1) (handle-intransitive-verb verb)])]
[result (response)])
(cond
[(place? result)
(set! current-place result)
(do-place)]
[(string? result)
(printf "~a\n" result)
(when (cmd-line-mode?) (do-verb))]
[else (when (cmd-line-mode?) (do-verb))]))
(begin
(printf "I don't undertand what you mean.\n")
(when (cmd-line-mode?) (do-verb)))))
;; Handle an intransitive-verb command:
(define (handle-intransitive-verb verb)
(or
(find-verb verb (place-actions current-place))
(find-verb verb everywhere-actions)
(using-verb
9 years ago
verb game-verbs
(λ (verb)
9 years ago
(λ () (if (verb-transitive? verb)
(format "~a what?" (string-titlecase (verb-desc verb)))
(format "Can't ~a here." (verb-desc verb))))))
(λ () (format "I don't know how to ~a." verb))))
;; Handle a transitive-verb command:
9 years ago
(define (handle-transitive-verb verb-in obj)
(or (using-verb
9 years ago
verb-in game-verbs
(λ (verb)
(and
(verb-transitive? verb)
(cond
[(for/first ([thing (in-list (append (place-things current-place)
player-inventory))]
#:when (eq? (thing-name thing) obj))
thing)
=> (λ (thing)
9 years ago
(or (find-verb verb-in (thing-actions thing))
(λ ()
(format "Don't know how to ~a ~a."
(verb-desc verb) obj))))]
[else
9 years ago
(λ () (format "There's no ~a here to ~a." obj
(verb-desc verb)))]))))
(λ () (format "I don't know how to ~a ~a." verb-in obj))))
;; Show what the player is carrying:
(define (show-inventory)
(printf "You have")
(if (zero? (length player-inventory))
(printf " no items.")
(for-each (λ (thing)
(printf "\n a ~a" (thing-name thing)))
9 years ago
player-inventory))
(printf "\n"))
;; Look for a command match in a list of verb--response pairs,
;; and returns the response thunk if a match is found:
(define (find-verb cmd actions)
9 years ago
(for/first ([action (in-list actions)]
#:when (memq cmd (verb-aliases (action-verb action))))
(action-response action)))
;; Looks for a command in a list of verbs, and
9 years ago
;; applies `success-func' to the verb if one is found:
(define (using-verb cmd verbs success-func)
(for/first ([verb (in-list verbs)]
#:when (memq cmd (verb-aliases verb)))
(success-func verb)))
;; ============================================================
;; To go:
(define (init-game in-place
9 years ago
in-verbs
in-everywhere-actions)
(set! current-place in-place)
9 years ago
(set! game-verbs in-verbs)
(set! everywhere-actions in-everywhere-actions))