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 "emote" 'cmd-emote)))
71 ;; TODO: Subclass from container?
72 (define-class <room> (<gameobj>)
74 (exits #:init-value '()
75 #:init-keyword #:exits
79 #:init-value (wrap %room-contain-commands))
81 (actions #:allocation #:each-subclass
85 (cmd-go-where room-cmd-go-where)
86 (announce-entrance room-announce-entrance)
87 (look-room room-look-room)
88 (tell-room room-act-tell-room)
89 ;; in this case the command is the same version as the normal
91 (cmd-look-room room-look-room)
92 (cmd-look-at room-look-at)
93 (cmd-say room-cmd-say)
94 (cmd-emote room-cmd-emote))))
96 (define* (room-cmd-go room message #:key direct-obj)
100 (equal? (exit-name exit) direct-obj))
102 (define to-address (if exit
103 ;; Get the exit, but resolve it dynamically
104 ;; in case it's a special
105 (dyn-ref room (slot-ref exit 'to))
108 (mbody-val (<-wait (message-from message) 'get-name)))
111 ;; Set the player's new location
112 (<-wait (message-from message) 'set-loc!
114 ;; Tell everyone else the person walked away
117 (format #f "~a wanders ~a.\n"
118 player-name direct-obj))
119 (<- to-address 'announce-entrance
120 #:who-entered (message-from message))
121 ;; Have the new room update the player to the new location
122 (<- to-address 'look-room
123 #:to-id (message-from message)))
125 (<- (message-from message) 'tell
126 #:text "You don't see any way to go there.\n"))))
128 (define (room-cmd-go-where room message)
129 (<- (message-from message) 'tell
130 #:text "Go where?\n"))
134 (define (list-words-as-string words)
135 "A little utility for listing a bunch of words in an English-style list"
136 ;; TODO: This could be made faster by traversing the O(n)
137 ;; list once, not twice
138 (let ((word-length (length words)))
140 ((eqv? word-length 0) "")
141 ((eqv? word-length 1) (car words))
143 ;; TODO: and this is NOT efficient
151 (define (room-player-looks-around room player-id)
152 "Handle looking around the room"
155 (format #f "**~a**\n~a\n"
156 (slot-ref room 'name)
157 (slot-ref room 'desc)))
159 ;; Get a list of other things the player would see in the room
160 (define occupant-names-all
163 (call-with-message (<-wait occupant 'visible-name
164 #:whos-looking player-id)
165 (lambda* (_ #:key text)
168 (lambda (x) (equal? x player-id))
169 (hash-map->list (lambda (x _) x)
170 (slot-ref room 'occupants)))))
172 ;; Strip out the #f responses (these aren't listed because they lack a name
173 ;; or they aren't "obviously visible" to the player)
174 (define occupant-names-filtered
175 (filter identity occupant-names-all))
177 (define occupant-names-string
178 (if (eq? occupant-names-filtered '())
180 (format #f "You see here: ~a.\n"
181 (list-words-as-string occupant-names-filtered))))
184 (if occupant-names-string
185 (string-append room-text occupant-names-string)
192 (define* (room-look-room room message
193 ;; Either send it to the #:to-id of the message,
194 ;; or to the sender of the message
195 #:key (to-id (message-from message)))
196 "Command: Player asks to look around the room"
197 (room-player-looks-around room to-id))
199 (define (room-find-thing-called room called-this)
200 "Find something called CALLED-THIS in the room, if any."
205 (mbody-receive (_ #:key goes-by)
206 (<-wait occupant 'goes-by)
207 (if (member called-this goes-by)
209 (hash-map->list (lambda (key val) key)
210 (slot-ref room 'occupants)))
213 (define %formless-desc
214 "You don't see anything special.")
216 (define* (room-look-at room message #:key direct-obj)
217 "Look at a specific object in the room."
218 (define matching-object
219 (room-find-thing-called room direct-obj))
224 (mbody-val (<-wait matching-object 'get-desc
225 #:whos-looking (message-from message)))))
227 (<- (message-from message) 'tell
228 #:text (string-append obj-desc "\n"))
229 (<- (message-from message) 'tell
230 #:text (string-append %formless-desc "\n")))))
232 (<- (message-from message) 'tell
233 #:text "You don't see that here, so you can't look at it.\n"))))
236 (define* (room-tell-room room text #:key exclude wait)
237 (define who-to-tell (gameobj-occupants room #:exclude exclude))
240 ;; @@: Does anything really care?
241 (define deliver-method
245 (deliver-method tell-me 'tell
249 (define* (room-act-tell-room room message #:key text exclude wait)
250 "Tell the room some messages."
251 (room-tell-room room text
255 (define* (room-cmd-say room message #:key phrase)
256 "Command: Say something to room participants."
258 (mbody-val (<-wait (message-from message) 'get-name)))
259 (define message-to-send
260 (format #f "~a says: ~a\n" player-name phrase))
261 (room-tell-room room message-to-send))
263 (define* (room-cmd-emote room message #:key phrase)
264 "Command: Say something to room participants."
266 (mbody-val (<-wait (message-from message) 'get-name)))
267 (define message-to-send
268 (format #f "* ~a ~a\n" player-name phrase))
269 (room-tell-room room message-to-send))
271 (define* (room-announce-entrance room message #:key who-entered)
273 (mbody-val (<-wait who-entered 'get-name)))
274 (define message-to-send
275 (format #f "~a enters the room.\n" player-name))
276 (room-tell-room room message-to-send
277 #:exclude who-entered))