You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

639 lines
19KB

  1. (import comparse)
  2. (import srfi-1)
  3. (import srfi-14)
  4. (import (chicken io))
  5. (import srfi-13)
  6. (import matchable)
  7. (import fmt)
  8. (import fmt-color)
  9. (import fmt-unicode)
  10. (import ansi-escape-sequences)
  11. (import (chicken file))
  12. (import breadline)
  13. (import ncurses)
  14. (import util)
  15. (import lisp)
  16. (define (lift fn parser)
  17. (bind parser (compose result fn)))
  18. (define (is-not x)
  19. (satisfies (lambda (y)
  20. (not (eqv? x y)))))
  21. (define parse-whitespace
  22. (one-or-more (is #\space)))
  23. (define skip-whitespace
  24. (skip (zero-or-more (is #\space))))
  25. (define +letter-char-set+
  26. (string->char-set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVwXYZ"))
  27. (define +symbol-char-set+
  28. (char-set-union +letter-char-set+ (string->char-set "-0123456789")))
  29. (define parse-symbol
  30. (lift (compose string->symbol string-downcase list->string (applied append))
  31. (sequence (lift list (in +letter-char-set+)) (zero-or-more (in +symbol-char-set+)))))
  32. (define parse-number
  33. (lift (compose string->number list->string) (one-or-more (in char-set:digit))))
  34. (define parse-string
  35. (lift list->string (enclosed-by (is #\") (one-or-more (is-not #\")) (is #\"))))
  36. (define (followed-by-consuming parser separator)
  37. (sequence* ((value parser) (_ separator))
  38. (result value)))
  39. (define (separated-by separator parser)
  40. (one-or-more (any-of (followed-by-consuming parser separator) parser)))
  41. (define parse-symbol-or-number-or-string
  42. (any-of parse-number parse-symbol parse-string))
  43. (define (completely-parse parser)
  44. (followed-by parser end-of-input))
  45. (define parse-statement
  46. (all-of skip-whitespace (separated-by parse-whitespace parse-symbol-or-number-or-string)))
  47. (define (just fn)
  48. (lambda args
  49. (fn)))
  50. (define (perhaps fn arg)
  51. (if arg
  52. (fn arg)
  53. arg))
  54. (define display-newline
  55. (compose (just newline) display))
  56. (define (display-lines ln)
  57. (perhaps (curry map display-newline) ln))
  58. (define (parse-input)
  59. (parse (completely-parse parse-statement) (read-line)))
  60. (define parse-formatter
  61. (recursive-parser (one-or-more (any-of (followed-by-consuming (char-seq "<b>") (lift fmt-bold parser))
  62. (is-not #\<)))))
  63. (define (type-of elem)
  64. (cond ((pair? elem) 'pair)
  65. ((symbol? elem) 'symbol)
  66. ((number? elem) 'number)
  67. ((char? elem) 'char)
  68. ((string? elem) 'string)
  69. ((boolean? elem) 'boolean)))
  70. (define (show str)
  71. (fmt #t (dsp (wrap-lines str))))
  72. (define (prompt str)
  73. (newline)
  74. (let ((result (readline str)))
  75. (if (equal? "" result)
  76. (prompt str)
  77. (begin
  78. (add-history! result)
  79. result))))
  80. (define (prompt-yn str)
  81. (newline)
  82. (let ((result (string-downcase (readline str))))
  83. (cond ((equal? "yes" result) #t)
  84. ((equal? "no" result) #f)
  85. (else (begin
  86. (newline)
  87. (display "Please answer yes or no.")
  88. (prompt-yn str))))))
  89. (define (prompt-default str default)
  90. (map stuff-char (string->list default))
  91. (let loop ()
  92. (let ((result (readline str)))
  93. (if (equal? "" result)
  94. (loop)
  95. result))))
  96. (define +articles-prepositions+
  97. '(a an the into on to at as))
  98. (define (adventure-prompt)
  99. (let ((result (parse (completely-parse parse-statement) (prompt "> "))))
  100. (if result
  101. (let ((grug-result (filter (compose not (cut member <> +articles-prepositions+)) result)))
  102. (if (not (null? grug-result))
  103. grug-result
  104. (begin (display "I didn't quite understand that.")
  105. (adventure-prompt))))
  106. (begin (display "I didn't quite understand that.")
  107. (adventure-prompt)))))
  108. (define (compose-symbols . ln)
  109. (string->symbol
  110. (let loop ((ln ln))
  111. (case (length ln)
  112. ((0) '())
  113. ((1) (symbol->string (car ln)))
  114. (else (string-append (symbol->string (car ln)) "-" (loop (cdr ln))))))))
  115. (define *database* '())
  116. (define (database-set name key value)
  117. (set! *database* (let loop ((kv *database*))
  118. (if (null? kv)
  119. (list (cons name (list (cons key value))))
  120. (if (equal? name (caar kv))
  121. (cons (cons name (let loop ((kv (cdar kv)))
  122. (if (null? kv)
  123. (list (cons key value))
  124. (if (equal? key (caar kv))
  125. (cons (cons key value) (cdr kv))
  126. (cons (car kv) (loop (cdr kv))))))) (cdr kv))
  127. (cons (car kv) (loop (cdr kv))))))))
  128. (define (database-get name key default)
  129. (let loop ((kv *database*))
  130. (if (null? kv)
  131. default
  132. (if (equal? name (caar kv))
  133. (let loop ((kv (cdar kv)))
  134. (if (null? kv)
  135. default
  136. (if (equal? key (caar kv))
  137. (cdar kv)
  138. (loop (cdr kv)))))
  139. (loop (cdr kv))))))
  140. (define (database-save filename)
  141. (with-output-to-file filename (thunk (write *database*))))
  142. (define (database-load filename)
  143. (with-input-from-file filename (thunk (set! *database* (car (read-list))))))
  144. (define (database-remove name)
  145. (let loop ((kv *database*))
  146. (if (null? kv)
  147. '()
  148. (if (equal? name (caar kv))
  149. (cdr kv)
  150. (cons (car kv) (loop (cdr kv)))))))
  151. (define (get-all-objects)
  152. (map car *database*))
  153. (define (object-exists? object)
  154. (member object (get-all-objects)))
  155. (define (has-property? object property)
  156. (database-get object property #f))
  157. (define (toggle-flag object flag)
  158. (if (has-property? object flag)
  159. (database-set object flag #f)
  160. (database-set object flag #t)))
  161. (define (get-location object)
  162. (database-get object 'location #f))
  163. (define (set-name object name)
  164. (database-set object 'name name))
  165. (define (set-description object description)
  166. (database-set object 'description description))
  167. (define (get-name object)
  168. (database-get object 'name (symbol->string object)))
  169. (define (get-description object)
  170. (database-get object 'description "You see the swirling void of creation."))
  171. (define (get-container object)
  172. (database-get object 'container #f))
  173. (define (get-contents object)
  174. (database-get object 'contents '()))
  175. (define (set-destination object destination)
  176. (database-set object 'destination destination))
  177. (define (get-destination object)
  178. (database-get object 'destination #f))
  179. (define (set-enter-message object msg)
  180. (database-set object 'enter-message msg))
  181. (define (get-enter-message object)
  182. (database-get object 'enter-message #f))
  183. (define (get-aliases object)
  184. (database-get object 'aliases '()))
  185. (define (set-aliases object alias-list)
  186. (database-set object 'aliases alias-list))
  187. (define (add-alias object alias)
  188. (let ((aliases (get-aliases object)))
  189. (if (not (member alias aliases))
  190. (set-aliases object (cons alias aliases)))))
  191. (define (remove-alias object alias)
  192. (let ((aliases (get-aliases object)))
  193. (if (member alias aliases)
  194. (set-aliases object (remove (curry eq? alias) aliases)))))
  195. (define (set-hidden object value)
  196. (database-set object 'hidden value))
  197. (define (get-hidden object)
  198. (database-get object 'hidden #f))
  199. (define (set-fixed object value)
  200. (database-set object 'fixed value))
  201. (define (get-fixed object value)
  202. (database-get object 'hidden #f))
  203. (define (get-put-message object)
  204. (database-get object 'put-message "You put the ~a into the ~a."))
  205. ;; Is development mode enabled?
  206. (define (devmode-enabled?)
  207. (has-property? 'you 'devmode))
  208. (define (toggle-devmode)
  209. (toggle-flag 'you 'devmode))
  210. ;; Is an object fixed in place (e.g. cannot be picked up?)
  211. (define (fixed? object)
  212. (has-property? object 'fixed))
  213. (define (toggle-fixed object)
  214. (toggle-flag object 'fixed))
  215. ;; Match a tag against a list of objects, checking for its tag and its aliases.
  216. (define (match-object tag objects)
  217. (let loop ((objects objects))
  218. (if (null? objects)
  219. #f
  220. (let ((taglist (cons (car objects) (get-aliases (car objects)))))
  221. (if (member tag taglist)
  222. (car objects)
  223. (loop (cdr objects)))))))
  224. (define (create-object tag name description)
  225. (set-name tag name)
  226. (set-description tag description))
  227. (define (move-object object container)
  228. (let ((prev-container (get-container object)))
  229. (database-set object 'container container)
  230. (let ((contents (get-contents container)))
  231. (if (not (member object contents))
  232. (begin
  233. (database-set container 'contents (cons object contents))
  234. (database-set prev-container 'contents (remove (curry eq? object) (get-contents prev-container))))))))
  235. ;; Determine the objects visible to a source object, zork-style
  236. (define (visible-objects source)
  237. (let ((result (get-container source)))
  238. (if (and result (object-exists? result))
  239. (cons (get-container source) (get-contents (get-container source)))
  240. (error "Tried to determine visible objects for object without a container."))))
  241. (define (print-room-description room)
  242. (newline)
  243. (display (set-text '(bold) (get-name room)))
  244. (if (devmode-enabled?) (display (set-text '(bold fg-green) (string-append " [" (symbol->string room) "]"))))
  245. (newline)
  246. (display " ")
  247. (fmt #t (dsp (wrap-lines (get-description room))))
  248. (newline)
  249. (display "You see: ")
  250. (map (lambda (n) (if (not (get-hidden n)) (begin (display (get-name n)) (display " ") (if (devmode-enabled?) (begin (display (set-text '(bold fg-green) (string-append "[" (symbol->string n) "] ")))))))) (remove (curry eq? 'you) (get-contents room)))
  251. (newline))
  252. (define (do-command-enter tag)
  253. (let ((object (match-object tag (visible-objects 'you))))
  254. (if (not object)
  255. (show "You cannot go that way.")
  256. (let ((destination (get-destination object)))
  257. (if (not destination)
  258. (show "You cannot enter that.")
  259. (begin
  260. (move-object 'you destination)
  261. (perhaps show (get-enter-message object))
  262. (print-room-description (get-container 'you))))))))
  263. (define (do-command-save)
  264. (let ((save-name (prompt-default "Enter save name: " "kekkonen.sav")))
  265. (if (or (not (file-exists? save-name)) (prompt-yn "That file already exists. Overwrite? "))
  266. (begin
  267. (show "Saving database, please wait...")
  268. (database-save save-name)
  269. (show "Done.")))))
  270. (define (do-command-load)
  271. (let ((save-name (prompt-default "Enter save file name to load: " "kekkonen.sav")))
  272. (if (not (file-exists? save-name))
  273. (show "That file does not exist.")
  274. (begin
  275. (show "Loading database, please wait...")
  276. (database-load save-name)
  277. (show "Done.")))))
  278. (define (do-command-look)
  279. (print-room-description (get-container 'you)))
  280. (define (do-command-examine tag)
  281. (let ((object (match-object tag (visible-objects 'you))))
  282. (if (not object)
  283. (show "You cannot see that here.")
  284. (show (get-description object)))))
  285. (define (do-command-inventory)
  286. (map (compose show get-name) (get-contents 'you)))
  287. (define (do-command-take tag)
  288. (if (not (symbol? tag))
  289. (show "I didn't quite understand that.")
  290. (let ((object (match-object tag (if (devmode-enabled?)
  291. (get-all-objects)
  292. (visible-objects 'you)))))
  293. (if (or (not object) (and (fixed? object) (not (devmode-enabled?))))
  294. (if object
  295. (show "That is fixed in place.")
  296. (show "You cannot see that here."))
  297. (begin
  298. (show (string-append "You get " (get-name object) "."))
  299. (move-object object 'you))))))
  300. (define (do-command-drop tag)
  301. (if (not (symbol? tag))
  302. (show "I didn't quite understand that.")
  303. (let ((object (match-object tag (get-contents 'you))))
  304. (if (not object)
  305. (show "You are not carrying that.")
  306. (begin
  307. (show (string-append "You drop " (get-name object) "."))
  308. (move-object object (get-container 'you)))))))
  309. (define (do-command-put tag destination-tag)
  310. (let ((object (match-object tag (get-contents 'you))))
  311. (if (not object)
  312. (show "You are not carrying that.")
  313. (let ((destination-object (match-object destination-tag (visible-objects 'you))))
  314. (if (not destination-object)
  315. (show "You cannot see that here.")
  316. (move-object object (get-destination destination-object)))))))
  317. (define (do-command-devmode)
  318. (toggle-devmode)
  319. (if (devmode-enabled?)
  320. (show "Development mode enabled.")
  321. (show "Development mode disabled.")))
  322. (define (do-command-create tag name description)
  323. (if (not (and (symbol? tag) (string? name) (string? description)))
  324. (show "I didn't quite understand that.")
  325. (if (object-exists? tag)
  326. (show "That object already exists.")
  327. (begin
  328. (create-object tag name description)
  329. (move-object tag (get-container 'you))))))
  330. (define (do-setter-command tag value type? setter)
  331. (if (not (and (symbol? tag) (type? value)))
  332. (show "I didn't quite understand that.")
  333. (let ((object (match-object tag (visible-objects 'you))))
  334. (if (not object)
  335. (show "You can't see that here.")
  336. (begin
  337. (setter object value)
  338. (show "You set a value."))))))
  339. (define (do-command-rename tag name)
  340. (do-setter-command tag name string? set-name))
  341. (define (do-command-describe tag description)
  342. (do-setter-command tag description string? set-description))
  343. (define (do-command-flag tag flag)
  344. (if (not (and (symbol? tag) (symbol? flag)))
  345. (show "I didn't quite understand that.")
  346. (let ((object (match-object tag (visible-objects 'you))))
  347. (if (not object)
  348. (show "You can't see that here.")
  349. (begin
  350. (case flag
  351. ((fixed) (set-fixed object #t))
  352. ((hidden) (set-hidden object #t))
  353. (else (show "Invalid flag name."))))))))
  354. (define (do-command-unflag tag flag)
  355. (if (not (and (symbol? tag) (symbol? flag)))
  356. (show "I didn't quite understand that.")
  357. (let ((object (match-object tag (visible-objects 'you))))
  358. (if (not object)
  359. (show "You can't see that here.")
  360. (begin
  361. (case flag
  362. ((fixed) (set-fixed object #f))
  363. ((hidden) (set-hidden object #f))
  364. (else (show "Invalid flag name."))))))))
  365. (define (do-command-alias tag alias)
  366. (if (not (and (symbol? tag) (symbol? alias)))
  367. (show "I didn't quite understand that.")
  368. (let ((object (match-object tag (visible-objects 'you))))
  369. (if (not object)
  370. (show "You can't see that here.")
  371. (begin
  372. (add-alias object alias)
  373. (show "You add an alias."))))))
  374. (define (do-command-unalias tag alias)
  375. (if (not (and (symbol? tag) (symbol? alias)))
  376. (show "I didn't quite understand that.")
  377. (let ((object (match-object tag (visible-objects 'you))))
  378. (if (not object)
  379. (show "You can't see that here.")
  380. (begin
  381. (remove-alias object alias)
  382. (show "You remove an alias."))))))
  383. (define (do-command-destroy tag)
  384. (if (not (symbol? tag))
  385. (show "I didn't quite understand that.")
  386. (database-remove tag)))
  387. (define (do-command-aliases tag)
  388. (if (not (symbol? tag))
  389. (show "I didn't quite understand that.")
  390. (let ((object (match-object tag (visible-objects 'you))))
  391. (if (not object)
  392. (show "You can't see that here.")
  393. (begin
  394. (newline)
  395. (map (lambda (x) (display x) (display " ")) (get-aliases object))
  396. (newline))))))
  397. (define (do-command-message tag message-tag message)
  398. (if (not (and (symbol? tag) (symbol? message-tag) (string? message)))
  399. (show "I didn't quite understand that.")
  400. (let ((object (match-object tag (visible-objects 'you))))
  401. (if (not object)
  402. (show "You can't see that here.")
  403. (case message-tag
  404. ((enter) (set-enter-message object message))
  405. (else (show "Invalid message name.")))))))
  406. (define (do-command-goto tag)
  407. (if (not (symbol? tag))
  408. (show "I didn't quite understand that.")
  409. (begin
  410. (move-object 'you tag)
  411. (print-room-description (get-container 'you)))))
  412. (define +cardinal-sets+
  413. '((north n)
  414. (northeast ne north-east)
  415. (east e)
  416. (southeast se south-east)
  417. (south s)
  418. (southwest sw south-west)
  419. (west w)
  420. (northwest nw north-west)
  421. (up u)
  422. (down d)))
  423. (define +cardinal-opposites+
  424. '((north . south)
  425. (northeast . southwest)
  426. (east . west)
  427. (southeast . northwest)
  428. (south . north)
  429. (southwest . northeast)
  430. (west . east)
  431. (northwest . southeast)
  432. (up . down)
  433. (down . up)))
  434. (define (get-cardinal-set direction)
  435. (find (curry member direction) +cardinal-sets+))
  436. (define (get-cardinal-aliases direction)
  437. (perhaps (curry remove (curry eq? direction)) (get-cardinal-set direction)))
  438. (define (cardinal-direction? direction)
  439. (list? (member direction (flatten +cardinal-sets+))))
  440. (define (get-inverse-direction direction)
  441. (perhaps cdr (assoc direction +cardinal-opposites+)))
  442. (define (get-canonical-cardinal-direction-name direction)
  443. (perhaps car (get-cardinal-set direction)))
  444. (define (do-command-dig direction destination)
  445. (if (not (and (symbol? direction) (symbol? destination)))
  446. (show "I didn't quite understand that.")
  447. (if (not (cardinal-direction? direction))
  448. (show "You must specify a compass rose direction or up and down.")
  449. (let ((canonical-direction (get-canonical-cardinal-direction-name direction)))
  450. (let ((exit-tag (compose-symbols canonical-direction (get-container 'you) destination)))
  451. (if (object-exists? exit-tag)
  452. (show "An exit like that already exists.")
  453. (begin
  454. (move-object exit-tag (get-container 'you))
  455. (set-hidden exit-tag #t)
  456. (set-destination exit-tag destination)
  457. (map (curry add-alias exit-tag) (get-cardinal-set direction))
  458. (show "You create a passage."))))))))
  459. (define (do-command-exit)
  460. (show "Goodbye, see you later...")
  461. (set! *exit-adventure* #t))
  462. (define (alias-transform input)
  463. (match input
  464. (('quit) '(exit))
  465. (('i) '(inventory))
  466. (('inv) '(inventory))
  467. (('look x) `(examine ,x))
  468. (('go x) `(enter ,x))
  469. (('get x) `(take ,x))
  470. ((x) (if (cardinal-direction? x)
  471. `(enter ,x)
  472. input))
  473. (_ input)))
  474. (define (dispatch-command input)
  475. (let ((success #t))
  476. (match input
  477. (('look) (do-command-look))
  478. (('save) (do-command-save))
  479. (('load) (do-command-load))
  480. (('devmode) (do-command-devmode))
  481. (('exit) (do-command-exit))
  482. (('enter x) (do-command-enter x))
  483. (('take x) (do-command-take x))
  484. (('drop x) (do-command-drop x))
  485. (('inventory) (do-command-inventory))
  486. (('examine x) (do-command-examine x))
  487. (('put x y) (do-command-put x y))
  488. (_ (if (devmode-enabled?)
  489. (match input
  490. (('create x y z) (do-command-create x y z))
  491. (('rename x y) (do-command-rename x y))
  492. (('describe x y) (do-command-describe x y))
  493. (('dig x y) (do-command-dig x y))
  494. (('flag x y) (do-command-flag x y))
  495. (('unflag x y) (do-command-unflag x y))
  496. (('alias x y) (do-command-alias x y))
  497. (('unalias x y) (do-command-unalias x y))
  498. (('destroy x) (do-command-destroy x))
  499. (('aliases x) (do-command-aliases x))
  500. (('message x y z) (do-command-message x y z))
  501. (('goto x) (do-command-goto x))
  502. (_ (set! success #f)))
  503. (set! success #f))))
  504. success))
  505. (create-object 'garden "A Well-Kept Garden" "A french-style garden with topiary in the shape of various animals. A fountain gurgles happily in the middle. A trail leads off into a forest to the north.")
  506. (create-object 'unicorn "a frolicking unicorn" "A white unicorn, with a long spiral horn.")
  507. (create-object 'forest "A Foreboding Forest" "Tall trees bunch around a winding path.")
  508. (create-object 'trail "a trail" "A winding trail.")
  509. (add-alias 'trail 'winding)
  510. (add-alias 'trail 'north)
  511. (add-alias 'trail 'n)
  512. (set-hidden 'trail #t)
  513. (set-enter-message 'trail "You walk along the winding trail...")
  514. (move-object 'you 'garden)
  515. (move-object 'trail 'garden)
  516. (toggle-fixed 'trail)
  517. (set-destination 'trail 'forest)
  518. (move-object 'unicorn 'garden)
  519. (define *exit-adventure* #f)
  520. (define (adventure)
  521. (let ((success (dispatch-command (alias-transform (adventure-prompt)))))
  522. (if (not success)
  523. (begin
  524. (show "I didn't quite understand that.")
  525. (adventure))
  526. (if *exit-adventure*
  527. (display *database*)
  528. (adventure)))))
  529. (print-room-description (get-container 'you))
  530. (adventure)