1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christine Lemmer-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 (mudsync utils)
23 #:use-module (8sync actors)
24 #:use-module (8sync agenda)
25 #:use-module (oop goops)
26 #:use-module (srfi srfi-1)
27 #:use-module (ice-9 control)
28 #:export (<room> <exit>))
34 (define-class <exit> ()
35 (to #:init-keyword #:to
37 ;; Name of the room (@@: Should this be names?)
38 (name #:getter exit-name
39 #:init-keyword #:name)
40 (desc #:init-keyword #:desc
43 ;; *Note*: These two methods have an extra layer of indirection, but
44 ;; it's for a good reason.
45 (visible-check #:init-value (const #t)
46 #:init-keyword #:visible-check)
47 ;; By default all exits can be traversed
48 (traverse-check #:init-value (const #t)
49 #:init-keyword #:traverse-check))
51 ;; @@: Should we make whos-exiting optional? Would there ever be any
53 (define* (exit-can-traverse? exit room whos-exiting)
54 ((slot-ref exit 'traverse-check) exit room whos-exiting))
56 (define* (exit-is-visible? exit room whos-exiting)
57 ((slot-ref exit 'visible-check) exit room whos-exiting))
64 (define (exit-shorthand name)
65 (lambda (room message)
66 (room-cmd-go room message #:direct-obj name)))
68 ;; TODO: Subclass from container?
69 (define-class <room> (<gameobj>)
71 (exits #:init-value '()
72 #:init-keyword #:exits
75 (container-dom-commands
76 #:allocation #:each-subclass
79 (("l" "look") ((empty-command cmd-look-room)))
80 ("go" ((empty-command cmd-go-where)
81 (loose-direct-command cmd-go)))
82 (("say" "\"" "'") ((greedy-command cmd-say)))
83 (("emote" "/me") ((greedy-command cmd-emote)))
85 (("n" "north") ((empty-command go-north)))
86 (("ne" "northeast") ((empty-command go-northeast)))
87 (("e" "east") ((empty-command go-east)))
88 (("se" "southeast") ((empty-command go-southeast)))
89 (("s" "south") ((empty-command go-south)))
90 (("sw" "southwest") ((empty-command go-southwest)))
91 (("w" "west") ((empty-command go-west)))
92 (("nw" "northwest") ((empty-command go-northwest)))
93 (("u" "up") ((empty-command go-up)))
94 (("d" "down") ((empty-command go-down)))))
96 (container-sub-commands
97 #:allocation #:each-subclass
100 (("l" "look") ((loose-direct-command cmd-look-at-from-room)))))
102 (actions #:allocation #:each-subclass
106 (cmd-go-where room-cmd-go-where)
107 (announce-entrance room-announce-entrance)
108 (look-room room-look-room)
109 (tell-room room-act-tell-room)
110 ;; in this case the command is the same version as the normal
112 (cmd-look-room room-look-room)
113 (cmd-look-at-from-room room-look-dont-see-it)
114 (cmd-say room-cmd-say)
115 (cmd-emote room-cmd-emote)
117 (go-north (exit-shorthand "north"))
118 (go-northeast (exit-shorthand "northeast"))
119 (go-east (exit-shorthand "east"))
120 (go-southeast (exit-shorthand "southeast"))
121 (go-south (exit-shorthand "south"))
122 (go-southwest (exit-shorthand "southwest"))
123 (go-west (exit-shorthand "west"))
124 (go-northwest (exit-shorthand "northwest"))
125 (go-up (exit-shorthand "up"))
126 (go-down (exit-shorthand "down")))))
128 (define common-exit-aliases
140 (define (dealias-exit-name exit-name)
141 (or (assoc-ref common-exit-aliases exit-name)
144 (define* (room-cmd-go room message #:key direct-obj)
148 (equal? (exit-name exit) (dealias-exit-name direct-obj)))
150 (define to-address (if exit
151 ;; Get the exit, but resolve it dynamically
152 ;; in case it's a special
153 (dyn-ref room (slot-ref exit 'to))
155 (define player (message-from message))
157 (mbody-val (<-wait player 'get-name)))
160 (call-with-values (lambda ()
161 (exit-can-traverse? exit room player))
162 (lambda* (can-traverse? #:optional player-flavortext
165 ;; The exit itself objects to moving
168 #:text (or player-flavortext
169 `("You try to go " ,direct-obj " but something "
170 "seems to block you.")))
171 (when room-flavortext
172 (room-tell-room room room-flavortext
174 ;; to-address points nowhere, or exit not set.
177 #:text '((i "Yikes!") " Something weird is going on. "
178 "It seems like this exit leads nowhere, "
179 "in a programming bug kind of way. "
180 "Maybe tell an administrator?")))
181 ;; looks like we can go, so let's go!
183 ;; Set the player's new location
184 (<-wait player 'set-loc!
186 (when player-flavortext
188 #:text player-flavortext))
189 ;; Tell everyone else the person walked away
191 room (or room-flavortext
192 (format #f "~a wanders ~a.\n"
193 player-name direct-obj)))
194 (<- to-address 'announce-entrance
195 #:who-entered player)
196 ;; Have the new room update the player to the new location
197 (<- to-address 'look-room
201 #:text "You don't see any way to go there.\n"))))
203 (define (room-cmd-go-where room message)
204 (<- (message-from message) 'tell
205 #:text "Go where?\n"))
209 (define (room-player-looks-around room player-id)
210 "Handle looking around the room"
213 `((strong "=> " ,(slot-ref room 'name) " <=")
214 (p ,(gameobj-desc room))))
216 ;; Get a list of other things the player would see in the room
217 (define occupant-names-all
220 (call-with-message (<-wait occupant 'visible-name
221 #:whos-looking player-id)
222 (lambda* (_ #:key text)
225 (lambda (x) (equal? x player-id))
226 (hash-map->list (lambda (x _) x)
227 (slot-ref room 'occupants)))))
229 ;; Strip out the #f responses (these aren't listed because they lack a name
230 ;; or they aren't "obviously visible" to the player)
231 (define occupant-names-filtered
232 (filter identity occupant-names-all))
234 (define occupant-names-string
235 (if (eq? occupant-names-filtered '())
237 (format #f "You see here: ~a.\n"
238 (string-join occupant-names-filtered
242 (if occupant-names-string
244 (p (em ,occupant-names-string)))
251 (define* (room-look-room room message
252 ;; Either send it to the #:to-id of the message,
253 ;; or to the sender of the message
254 #:key (to-id (message-from message)))
255 "Command: Player asks to look around the room"
256 (room-player-looks-around room to-id))
258 (define (room-find-thing-called room called-this)
259 "Find something called CALLED-THIS in the room, if any."
264 (define goes-by (mbody-val (<-wait occupant 'goes-by)))
265 (if (ci-member called-this goes-by)
267 (hash-map->list (lambda (key val) key)
268 (slot-ref room 'occupants)))
271 (define* (room-look-dont-see-it room message #:key direct-obj)
272 "In general, if we get to this point, we didn't find something to look at."
273 (<- (message-from message) 'tell
274 #:text "You don't see that here, so you can't look at it.\n"))
277 (define* (room-tell-room room text #:key exclude wait)
278 (define who-to-tell (gameobj-occupants room #:exclude exclude))
281 ;; @@: Does anything really care?
282 (define deliver-method
286 (deliver-method tell-me 'tell
290 (define* (room-act-tell-room room message #:key text exclude wait)
291 "Tell the room some messages."
292 (room-tell-room room text
296 (define* (room-cmd-say room message #:key phrase)
297 "Command: Say something to room participants."
299 (mbody-val (<-wait (message-from message) 'get-name)))
300 (define message-to-send
301 `((b "<" ,player-name ">") " " ,phrase))
302 (room-tell-room room message-to-send))
304 (define* (room-cmd-emote room message #:key phrase)
305 "Command: Say something to room participants."
307 (mbody-val (<-wait (message-from message) 'get-name)))
308 (define message-to-send
309 `((b "* " ,player-name) " " ,phrase))
310 (room-tell-room room message-to-send))
312 (define* (room-announce-entrance room message #:key who-entered)
314 (mbody-val (<-wait who-entered 'get-name)))
315 (define message-to-send
316 (format #f "~a enters the room.\n" player-name))
317 (room-tell-room room message-to-send
318 #:exclude who-entered))