Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

595 строки
18KB

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