room.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. ;;; Mudsync --- Live hackable MUD
  2. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
  3. ;;;
  4. ;;; This file is part of Mudsync.
  5. ;;;
  6. ;;; Mudsync is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Mudsync is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (mudsync room)
  19. #:use-module (mudsync command)
  20. #:use-module (mudsync gameobj)
  21. #:use-module (mudsync utils)
  22. #:use-module (8sync actors)
  23. #:use-module (8sync agenda)
  24. #:use-module (oop goops)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (ice-9 control)
  27. #:export (<room> <exit>))
  28. ;;; Exits
  29. ;;; =====
  30. (define-class <exit> ()
  31. (to #:init-keyword #:to
  32. #:init-value #f)
  33. ;; Name of the room (@@: Should this be names?)
  34. (name #:getter exit-name
  35. #:init-keyword #:name)
  36. (desc #:init-keyword #:desc
  37. #:init-value #f)
  38. ;; *Note*: These two methods have an extra layer of indirection, but
  39. ;; it's for a good reason.
  40. (visible-check #:init-value (const #t)
  41. #:init-keyword #:visible-check)
  42. ;; By default all exits can be traversed
  43. (traverse-check #:init-value (const #t)
  44. #:init-keyword #:traverse-check))
  45. ;; @@: Should we make whos-exiting optional? Would there ever be any
  46. ;; reason?
  47. (define* (exit-can-traverse? exit room whos-exiting)
  48. ((slot-ref exit 'traverse-check) exit room whos-exiting))
  49. (define* (exit-is-visible? exit room whos-exiting)
  50. ((slot-ref exit 'visible-check) exit room whos-exiting))
  51. ;;; Rooms
  52. ;;; =====
  53. (define (exit-shorthand name)
  54. (lambda (room message)
  55. (room-cmd-go room message #:direct-obj name)))
  56. ;; TODO: Subclass from container?
  57. (define-class <room> (<gameobj>)
  58. ;; A list of <exit>
  59. (exits #:init-value '()
  60. #:init-keyword #:exits
  61. #:getter room-exits)
  62. (container-dom-commands
  63. #:allocation #:each-subclass
  64. #:init-thunk
  65. (build-commands
  66. (("l" "look") ((empty-command cmd-look-room)))
  67. ("go" ((empty-command cmd-go-where)
  68. (loose-direct-command cmd-go)))
  69. (("say" "\"" "'") ((greedy-command cmd-say)))
  70. (("emote" "/me") ((greedy-command cmd-emote)))
  71. ;; movement aliases
  72. (("n" "north") ((empty-command go-north)))
  73. (("ne" "northeast") ((empty-command go-northeast)))
  74. (("e" "east") ((empty-command go-east)))
  75. (("se" "southeast") ((empty-command go-southeast)))
  76. (("s" "south") ((empty-command go-south)))
  77. (("sw" "southwest") ((empty-command go-southwest)))
  78. (("w" "west") ((empty-command go-west)))
  79. (("nw" "northwest") ((empty-command go-northwest)))
  80. (("u" "up") ((empty-command go-up)))
  81. (("d" "down") ((empty-command go-down)))))
  82. (container-sub-commands
  83. #:allocation #:each-subclass
  84. #:init-thunk
  85. (build-commands
  86. (("l" "look") ((loose-direct-command cmd-look-at-from-room)))))
  87. (actions #:allocation #:each-subclass
  88. #:init-thunk
  89. (build-actions
  90. (cmd-go room-cmd-go)
  91. (cmd-go-where room-cmd-go-where)
  92. (announce-entrance room-announce-entrance)
  93. (look-room room-look-room)
  94. (tell-room room-act-tell-room)
  95. ;; in this case the command is the same version as the normal
  96. ;; look-room version
  97. (cmd-look-room room-look-room)
  98. (cmd-look-at-from-room room-look-dont-see-it)
  99. (cmd-say room-cmd-say)
  100. (cmd-emote room-cmd-emote)
  101. ;; movement aliases
  102. (go-north (exit-shorthand "north"))
  103. (go-northeast (exit-shorthand "northeast"))
  104. (go-east (exit-shorthand "east"))
  105. (go-southeast (exit-shorthand "southeast"))
  106. (go-south (exit-shorthand "south"))
  107. (go-southwest (exit-shorthand "southwest"))
  108. (go-west (exit-shorthand "west"))
  109. (go-northwest (exit-shorthand "northwest"))
  110. (go-up (exit-shorthand "up"))
  111. (go-down (exit-shorthand "down")))))
  112. (define common-exit-aliases
  113. '(("n" . "north")
  114. ("ne" . "northeast")
  115. ("e" . "east")
  116. ("se" . "southeast")
  117. ("s" . "south")
  118. ("sw" . "southwest")
  119. ("w" . "west")
  120. ("nw" . "northwest")
  121. ("u" . "up")
  122. ("d" . "down")))
  123. (define (dealias-exit-name exit-name)
  124. (or (assoc-ref common-exit-aliases exit-name)
  125. exit-name))
  126. (define* (room-cmd-go room message #:key direct-obj)
  127. (define exit
  128. (find
  129. (lambda (exit)
  130. (equal? (exit-name exit) (dealias-exit-name direct-obj)))
  131. (room-exits room)))
  132. (define to-address (if exit
  133. ;; Get the exit, but resolve it dynamically
  134. ;; in case it's a special
  135. (dyn-ref room (slot-ref exit 'to))
  136. #f))
  137. (define player (message-from message))
  138. (define player-name
  139. (mbody-val (<-wait player 'get-name)))
  140. (cond
  141. (exit
  142. (call-with-values (lambda ()
  143. (exit-can-traverse? exit room player))
  144. (lambda* (can-traverse? #:optional player-flavortext
  145. room-flavortext)
  146. (cond
  147. ;; The exit itself objects to moving
  148. ((not can-traverse?)
  149. (<- player 'tell
  150. #:text (or player-flavortext
  151. `("You try to go " ,direct-obj " but something "
  152. "seems to block you.")))
  153. (when room-flavortext
  154. (room-tell-room room room-flavortext
  155. #:exclude player)))
  156. ;; to-address points nowhere, or exit not set.
  157. ((not exit)
  158. (<- player 'tell
  159. #:text '((i "Yikes!") " Something weird is going on. "
  160. "It seems like this exit leads nowhere, "
  161. "in a programming bug kind of way. "
  162. "Maybe tell an administrator?")))
  163. ;; looks like we can go, so let's go!
  164. (else
  165. ;; Set the player's new location
  166. (<-wait player 'set-loc!
  167. #:loc to-address)
  168. (when player-flavortext
  169. (<-wait player 'tell
  170. #:text player-flavortext))
  171. ;; Tell everyone else the person walked away
  172. (room-tell-room
  173. room (or room-flavortext
  174. (format #f "~a wanders ~a.\n"
  175. player-name direct-obj)))
  176. (<- to-address 'announce-entrance
  177. #:who-entered player)
  178. ;; Have the new room update the player to the new location
  179. (<- to-address 'look-room
  180. #:to-id player))))))
  181. (else
  182. (<- player 'tell
  183. #:text "You don't see any way to go there.\n"))))
  184. (define (room-cmd-go-where room message)
  185. (<- (message-from message) 'tell
  186. #:text "Go where?\n"))
  187. ;;; look commands
  188. (define (room-player-looks-around room player-id)
  189. "Handle looking around the room"
  190. ;; Get the room text
  191. (define room-text
  192. `((strong "=> " ,(slot-ref room 'name) " <=")
  193. (p ,(gameobj-desc room))))
  194. ;; Get a list of other things the player would see in the room
  195. (define occupant-names-all
  196. (map
  197. (lambda (occupant)
  198. (call-with-message (<-wait occupant 'visible-name
  199. #:whos-looking player-id)
  200. (lambda* (_ #:key text)
  201. text)))
  202. (remove
  203. (lambda (x) (equal? x player-id))
  204. (hash-map->list (lambda (x _) x)
  205. (slot-ref room 'occupants)))))
  206. ;; Strip out the #f responses (these aren't listed because they lack a name
  207. ;; or they aren't "obviously visible" to the player)
  208. (define occupant-names-filtered
  209. (filter identity occupant-names-all))
  210. (define occupant-names-string
  211. (if (eq? occupant-names-filtered '())
  212. #f
  213. (format #f "You see here: ~a.\n"
  214. (string-join occupant-names-filtered
  215. ", "))))
  216. (define final-text
  217. (if occupant-names-string
  218. `(,@room-text
  219. (p (em ,occupant-names-string)))
  220. room-text))
  221. (<- player-id 'tell
  222. #:text final-text))
  223. (define* (room-look-room room message
  224. ;; Either send it to the #:to-id of the message,
  225. ;; or to the sender of the message
  226. #:key (to-id (message-from message)))
  227. "Command: Player asks to look around the room"
  228. (room-player-looks-around room to-id))
  229. (define (room-find-thing-called room called-this)
  230. "Find something called CALLED-THIS in the room, if any."
  231. (call/ec
  232. (lambda (return)
  233. (for-each
  234. (lambda (occupant)
  235. (define goes-by (mbody-val (<-wait occupant 'goes-by)))
  236. (if (ci-member called-this goes-by)
  237. (return occupant)))
  238. (hash-map->list (lambda (key val) key)
  239. (slot-ref room 'occupants)))
  240. #f)))
  241. (define* (room-look-dont-see-it room message #:key direct-obj)
  242. "In general, if we get to this point, we didn't find something to look at."
  243. (<- (message-from message) 'tell
  244. #:text "You don't see that here, so you can't look at it.\n"))
  245. (define* (room-tell-room room text #:key exclude wait)
  246. (define who-to-tell (gameobj-occupants room #:exclude exclude))
  247. (for-each
  248. (lambda (tell-me)
  249. ;; @@: Does anything really care?
  250. (define deliver-method
  251. (if wait
  252. <-wait
  253. <-))
  254. (deliver-method tell-me 'tell
  255. #:text text))
  256. who-to-tell))
  257. (define* (room-act-tell-room room message #:key text exclude wait)
  258. "Tell the room some messages."
  259. (room-tell-room room text
  260. #:exclude exclude
  261. #:wait wait))
  262. (define* (room-cmd-say room message #:key phrase)
  263. "Command: Say something to room participants."
  264. (define player-name
  265. (mbody-val (<-wait (message-from message) 'get-name)))
  266. (define message-to-send
  267. `((b "<" ,player-name ">") " " ,phrase))
  268. (room-tell-room room message-to-send))
  269. (define* (room-cmd-emote room message #:key phrase)
  270. "Command: Say something to room participants."
  271. (define player-name
  272. (mbody-val (<-wait (message-from message) 'get-name)))
  273. (define message-to-send
  274. `((b "* " ,player-name) " " ,phrase))
  275. (room-tell-room room message-to-send))
  276. (define* (room-announce-entrance room message #:key who-entered)
  277. (define player-name
  278. (mbody-val (<-wait who-entered 'get-name)))
  279. (define message-to-send
  280. (format #f "~a enters the room.\n" player-name))
  281. (room-tell-room room message-to-send
  282. #:exclude who-entered))