1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
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.
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.
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/>.
19 (define-module (mudsync room)
20 #:use-module (mudsync command)
21 #:use-module (mudsync gameobj)
22 #:use-module (8sync systems actors)
23 #:use-module (8sync agenda)
24 #:use-module (oop goops)
25 #:use-module (srfi srfi-1)
26 #:use-module (ice-9 control)
37 (define-class <exit> ()
39 (to-symbol #:init-keyword #:to-symbol)
40 ;; The actual address we use
41 (to-address #:init-keyword #:address)
42 ;; Name of the room (@@: Should this be names?)
43 (name #:getter exit-name
44 #:init-keyword #:name)
45 (desc #:init-keyword #:desc
48 ;; *Note*: These two methods have an extra layer of indirection, but
49 ;; it's for a good reason.
50 (visible-check #:init-value (const #t)
51 #:init-keyword #:visible-check)
52 ;; By default all exits can be traversed
53 (traverse-check #:init-value (const #t)
54 #:init-keyword #:traverse-check))
56 (define* (exit-can-traverse? exit actor
57 #:optional (target-actor (actor-id actor)))
58 ((slot-ref exit 'traverse-check) exit actor target-actor))
60 (define* (exit-is-visible? exit actor
61 #:optional (target-actor (actor-id actor)))
62 ((slot-ref exit 'traverse-check) exit actor target-actor))
69 (define %room-contain-commands
71 (loose-direct-command "look" 'cmd-look-at)
72 (empty-command "look" 'cmd-look-room)
73 (empty-command "go" 'cmd-go-where)
74 (loose-direct-command "go" 'cmd-go)
75 (greedy-command "say" 'cmd-say)))
79 ;; desc == description
80 (init (wrap-apply room-init))
81 (wire-exits! (wrap-apply room-wire-exits!))
82 (cmd-go (wrap-apply room-cmd-go))
83 (cmd-go-where (wrap-apply room-cmd-go-where))
84 (announce-entrance (wrap-apply room-announce-entrance))
85 (look-room (wrap-apply room-look-room))
86 (tell-room (wrap-apply room-act-tell-room))
87 ;; in this case the command is the same version as the normal
89 (cmd-look-room (wrap-apply room-look-room))
90 (cmd-look-at (wrap-apply room-look-at))
91 (cmd-say (wrap-apply room-cmd-say))))
94 (append room-actions gameobj-actions))
96 (define room-action-dispatch
97 (simple-dispatcher room-actions*))
99 ;; TODO: Subclass from container?
100 (define-class <room> (<gameobj>)
102 (exits #:init-value '()
103 #:init-keyword #:exits
107 #:init-value (wrap %room-contain-commands))
110 #:allocation #:each-subclass
111 ;; @@: Can remove this indirection once things settle
112 #:init-value (wrap-apply room-action-dispatch)))
114 (define (room-init room message)
115 (room-wire-exits! room))
117 (define (room-wire-exits! room)
118 "Actually hook up the rooms' exit addresses to the rooms they
124 (<-wait room (gameobj-gm room) 'lookup-special
125 #:symbol (slot-ref exit 'to-symbol))
128 (slot-set! exit 'to-address new-exit))
132 (define-mhandler (room-cmd-go room message direct-obj)
136 (equal? (exit-name exit) direct-obj))
138 (define to-address (slot-ref exit 'to-address))
140 (message-ref (<-wait room (message-from message)
144 ;; Set the player's new location
145 (<-wait room (message-from message) 'set-loc!
147 ;; Tell everyone else the person walked away
150 (format #f "~a wanders ~a.\n"
151 player-name direct-obj))
152 (<- room to-address 'announce-entrance
153 #:who-entered (message-from message))
154 ;; Have the new room update the player to the new location
155 (<- room (slot-ref exit 'to-address) 'look-room
156 #:to-id (message-from message)))
158 (<- room (message-from message) 'tell
159 #:text "You don't see any way to go there.\n"))))
161 (define-mhandler (room-cmd-go-where room message)
162 (<- room (message-from message) 'tell
163 #:text "Go where?\n"))
167 (define (list-words-as-string words)
168 "A little utility for listing a bunch of words in an English-style list"
169 ;; TODO: This could be made faster by traversing the O(n)
170 ;; list once, not twice
171 (let ((word-length (length words)))
173 ((eqv? word-length 0) "")
174 ((eqv? word-length 1) (car words))
176 ;; TODO: and this is NOT efficient
184 (define (room-player-looks-around room player-id)
185 "Handle looking around the room"
188 (format #f "**~a**\n~a\n"
189 (slot-ref room 'name)
190 (slot-ref room 'desc)))
192 ;; Get a list of other things the player would see in the room
193 (define occupant-names-all
197 (<-wait room occupant 'visible-name
198 #:whos-looking player-id)
201 (lambda (x) (equal? x player-id))
202 (hash-map->list (lambda (x _) x)
203 (slot-ref room 'occupants)))))
205 ;; Strip out the #f responses (these aren't listed because they lack a name
206 ;; or they aren't "obviously visible" to the player)
207 (define occupant-names-filtered
208 (filter identity occupant-names-all))
210 (define occupant-names-string
211 (if (eq? occupant-names-filtered '())
213 (format #f "You see here: ~a.\n"
214 (list-words-as-string occupant-names-filtered))))
217 (if occupant-names-string
218 (string-append room-text occupant-names-string)
221 (<- room player-id 'tell
225 (define-mhandler (room-look-room room message)
226 "Command: Player asks to look around the room"
227 (room-player-looks-around
229 ;; Either send it to the #:to-id of the message, or to the
230 ;; sender of the message
231 (message-ref message 'to-id
232 (message-from message))))
234 (define (room-find-thing-called room called-this)
235 "Find something called CALLED-THIS in the room, if any."
241 (message-ref (<-wait room occupant 'goes-by)
243 (if (member called-this goes-by)
245 (hash-map->list (lambda (key val) key)
246 (slot-ref room 'occupants)))
249 (define %formless-desc
250 "You don't see anything special.")
252 (define-mhandler (room-look-at room message direct-obj)
253 "Look at a specific object in the room."
254 (define matching-object
255 (room-find-thing-called room direct-obj))
261 (<-wait room matching-object 'get-desc
262 #:whos-looking (message-from message))
265 (<- room (message-from message) 'tell
266 #:text (string-append obj-desc "\n"))
267 (<- room (message-from message) 'tell
268 #:text (string-append %formless-desc "\n")))))
270 (<- room (message-from message) 'tell
271 #:text "You don't see that here, so you can't look at it.\n"))))
274 (define* (room-tell-room room text #:key exclude wait)
275 (define who-to-tell (gameobj-occupants room #:exclude exclude))
278 ;; @@: Does anything really care?
279 (define deliver-method
283 (deliver-method room tell-me 'tell
287 (define-mhandler (room-act-tell-room room message text)
288 "Tell the room some messages."
289 (define exclude (message-ref message 'exclude #f))
290 (define wait-delivery (message-ref message 'wait #f))
291 (room-tell-room room text
293 #:wait wait-delivery))
295 (define-mhandler (room-cmd-say room message phrase)
296 "Command: Say something to room participants."
298 (message-ref (<-wait room (message-from message)
300 (define message-to-send
301 (format #f "~a says: ~a\n" player-name phrase))
302 (room-tell-room room message-to-send))
304 (define-mhandler (room-announce-entrance room message who-entered)
306 (message-ref (<-wait room who-entered 'get-name)
308 (define message-to-send
309 (format #f "~a enters the room.\n" player-name))
310 (room-tell-room room message-to-send
311 #:exclude who-entered))