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.

572 lines
17KB

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