lispjam24/interpret.rkt

55 lines
2 KiB
Racket

#lang racket/gui
(require "structs.rkt"
"look.rkt"
"utils.rkt")
(provide interpret)
;temporary hard-coded player and room with items for testing, before implementing procgen
(define room1 (room "A DARK ROOM"
(pos 0 0)
(list (armor "LEATHER CAP" (pos 5 9) 1 'helmet 2))
(dim 0 0)))
(add-to-inventory! room1 (entity "CHEST"
room1
(list (weapon "BIG SWORD" (pos 5 5) 4 'sword 5))))
(define plyr (player "YOU"
room1
'()
'human 20 '() 50))
(add-to-inventory! room1 plyr)
(define (interpret in)
(let ([command (string-upcase in)])
(if (or (string=? command "TYPE HERE...") (string=? command ""))
"You need to type something in the box."
(let* ([tokens (string-split command)]
[verb (first tokens)]
[args (rest tokens)])
(case verb
[("LOOK") (look args room1)]
[("TAKE") (take args room1 plyr)]
[else (~a "I don't understand the verb " verb ".")])))))
(define (take args where who)
(if (empty? args)
"You need to say what to take."
(let* ([args-str (string-join args)]
[split-str (string-split args-str " FROM ")]
[what-str (first split-str)]
[from-str (if (= 1 (length split-str))
'()
(last split-str))]
[from (if (empty? from-str)
where
(find-object-by-name from-str where))]
[what (find-object-by-name what-str from)])
(when (empty? from-str)
(set! from-str (object-name where)))
(if what
(if (creature? what)
"You can't take a living thing."
(if (entity? what)
"You can't take something with an inventory."
(move-obj! what from who)))
(~a "There isn't a \"" what-str "\" in this area.")))))