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 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>))
33 (define-class <exit> ()
34 (to #:init-keyword #:to)
35 ;; Name of the room (@@: Should this be names?)
36 (name #:getter exit-name
37 #:init-keyword #:name)
38 (desc #:init-keyword #:desc
41 ;; *Note*: These two methods have an extra layer of indirection, but
42 ;; it's for a good reason.
43 (visible-check #:init-value (const #t)
44 #:init-keyword #:visible-check)
45 ;; By default all exits can be traversed
46 (traverse-check #:init-value (const #t)
47 #:init-keyword #:traverse-check))
49 (define* (exit-can-traverse? exit actor
50 #:optional (target-actor (actor-id actor)))
51 ((slot-ref exit 'traverse-check) exit actor target-actor))
53 (define* (exit-is-visible? exit actor
54 #:optional (target-actor (actor-id actor)))
55 ((slot-ref exit 'traverse-check) exit actor target-actor))
62 (define %room-contain-commands
64 (loose-direct-command "look" 'cmd-look-at)
65 (empty-command "look" 'cmd-look-room)
66 (empty-command "go" 'cmd-go-where)
67 (loose-direct-command "go" 'cmd-go)
68 (greedy-command "say" 'cmd-say)
69 (greedy-command "\"" 'cmd-say)
70 (greedy-command "'" 'cmd-say)
71 (greedy-command "emote" 'cmd-emote)
72 (greedy-command "/me" 'cmd-emote)))
74 ;; TODO: Subclass from container?
75 (define-class <room> (<gameobj>)
77 (exits #:init-value '()
78 #:init-keyword #:exits
82 #:init-value (wrap %room-contain-commands))
84 (actions #:allocation #:each-subclass
88 (cmd-go-where room-cmd-go-where)
89 (announce-entrance room-announce-entrance)
90 (look-room room-look-room)
91 (tell-room room-act-tell-room)
92 ;; in this case the command is the same version as the normal
94 (cmd-look-room room-look-room)
95 (cmd-look-at room-look-at)
96 (cmd-say room-cmd-say)
97 (cmd-emote room-cmd-emote))))
99 (define* (room-cmd-go room message #:key direct-obj)
103 (equal? (exit-name exit) direct-obj))
105 (define to-address (if exit
106 ;; Get the exit, but resolve it dynamically
107 ;; in case it's a special
108 (dyn-ref room (slot-ref exit 'to))
111 (mbody-val (<-wait (message-from message) 'get-name)))
114 ;; Set the player's new location
115 (<-wait (message-from message) 'set-loc!
117 ;; Tell everyone else the person walked away
120 (format #f "~a wanders ~a.\n"
121 player-name direct-obj))
122 (<- to-address 'announce-entrance
123 #:who-entered (message-from message))
124 ;; Have the new room update the player to the new location
125 (<- to-address 'look-room
126 #:to-id (message-from message)))
128 (<- (message-from message) 'tell
129 #:text "You don't see any way to go there.\n"))))
131 (define (room-cmd-go-where room message)
132 (<- (message-from message) 'tell
133 #:text "Go where?\n"))
137 (define (list-words-as-string words)
138 "A little utility for listing a bunch of words in an English-style list"
139 ;; TODO: This could be made faster by traversing the O(n)
140 ;; list once, not twice
141 (let ((word-length (length words)))
143 ((eqv? word-length 0) "")
144 ((eqv? word-length 1) (car words))
146 ;; TODO: and this is NOT efficient
154 (define (room-player-looks-around room player-id)
155 "Handle looking around the room"
158 `((strong "=> " ,(slot-ref room 'name) " <=")
159 (p ,(slot-ref room 'desc))))
161 ;; Get a list of other things the player would see in the room
162 (define occupant-names-all
165 (call-with-message (<-wait occupant 'visible-name
166 #:whos-looking player-id)
167 (lambda* (_ #:key text)
170 (lambda (x) (equal? x player-id))
171 (hash-map->list (lambda (x _) x)
172 (slot-ref room 'occupants)))))
174 ;; Strip out the #f responses (these aren't listed because they lack a name
175 ;; or they aren't "obviously visible" to the player)
176 (define occupant-names-filtered
177 (filter identity occupant-names-all))
179 (define occupant-names-string
180 (if (eq? occupant-names-filtered '())
182 (format #f "You see here: ~a.\n"
183 (list-words-as-string occupant-names-filtered))))
186 (if occupant-names-string
188 (p (i ,occupant-names-string)))
195 (define* (room-look-room room message
196 ;; Either send it to the #:to-id of the message,
197 ;; or to the sender of the message
198 #:key (to-id (message-from message)))
199 "Command: Player asks to look around the room"
200 (room-player-looks-around room to-id))
202 (define (room-find-thing-called room called-this)
203 "Find something called CALLED-THIS in the room, if any."
208 (mbody-receive (_ #:key goes-by)
209 (<-wait occupant 'goes-by)
210 (if (member called-this goes-by)
212 (hash-map->list (lambda (key val) key)
213 (slot-ref room 'occupants)))
216 (define %formless-desc
217 "You don't see anything special.")
219 (define* (room-look-at room message #:key direct-obj)
220 "Look at a specific object in the room."
221 (define matching-object
222 (room-find-thing-called room direct-obj))
227 (mbody-val (<-wait matching-object 'get-desc
228 #:whos-looking (message-from message)))))
230 (<- (message-from message) 'tell
231 #:text (string-append obj-desc "\n"))
232 (<- (message-from message) 'tell
233 #:text (string-append %formless-desc "\n")))))
235 (<- (message-from message) 'tell
236 #:text "You don't see that here, so you can't look at it.\n"))))
239 (define* (room-tell-room room text #:key exclude wait)
240 (define who-to-tell (gameobj-occupants room #:exclude exclude))
243 ;; @@: Does anything really care?
244 (define deliver-method
248 (deliver-method tell-me 'tell
252 (define* (room-act-tell-room room message #:key text exclude wait)
253 "Tell the room some messages."
254 (room-tell-room room text
258 (define* (room-cmd-say room message #:key phrase)
259 "Command: Say something to room participants."
261 (mbody-val (<-wait (message-from message) 'get-name)))
262 (define message-to-send
263 `((b "<" ,player-name ">") " " ,phrase))
264 (room-tell-room room message-to-send))
266 (define* (room-cmd-emote room message #:key phrase)
267 "Command: Say something to room participants."
269 (mbody-val (<-wait (message-from message) 'get-name)))
270 (define message-to-send
271 `((b "* " ,player-name) " " ,phrase))
272 (room-tell-room room message-to-send))
274 (define* (room-announce-entrance room message #:key who-entered)
276 (mbody-val (<-wait who-entered 'get-name)))
277 (define message-to-send
278 (format #f "~a enters the room.\n" player-name))
279 (room-tell-room room message-to-send
280 #:exclude who-entered))