Use simple comma-join when listing items in a room.
[mudsync.git] / mudsync / room.scm
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
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>))
28
29 \f
30 ;;; Exits
31 ;;; =====
32
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
39         #:init-value #f)
40
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))
48
49 (define* (exit-can-traverse? exit actor
50                              #:optional (target-actor (actor-id actor)))
51   ((slot-ref exit 'traverse-check) exit actor target-actor))
52
53 (define* (exit-is-visible? exit actor
54                            #:optional (target-actor (actor-id actor)))
55   ((slot-ref exit 'traverse-check) exit actor target-actor))
56
57
58 \f
59 ;;; Rooms
60 ;;; =====
61
62 (define %room-contain-commands
63   (list
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)))
73
74 ;; TODO: Subclass from container?
75 (define-class <room> (<gameobj>)
76   ;; A list of <exit>
77   (exits #:init-value '()
78          #:init-keyword #:exits
79          #:getter room-exits)
80
81   (container-commands
82    #:init-value (wrap %room-contain-commands))
83
84   (actions #:allocation #:each-subclass
85            #:init-value
86            (build-actions
87             (cmd-go room-cmd-go)
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
93             ;; look-room version
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))))
98
99 (define* (room-cmd-go room message #:key direct-obj)
100   (define exit
101     (find
102      (lambda (exit)
103        (equal? (exit-name exit) direct-obj))
104      (room-exits room)))
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))
109                          #f))
110   (define player-name
111     (mbody-val (<-wait (message-from message) 'get-name)))
112   (cond
113    (exit
114     ;; Set the player's new location
115     (<-wait (message-from message) 'set-loc!
116             #:loc to-address)
117     ;; Tell everyone else the person walked away
118     (room-tell-room
119      room
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)))
127    (else
128     (<- (message-from message) 'tell
129         #:text "You don't see any way to go there.\n"))))
130
131 (define (room-cmd-go-where room message)
132   (<- (message-from message) 'tell
133       #:text "Go where?\n"))
134
135 ;;; look commands
136
137 (define (room-player-looks-around room player-id)
138   "Handle looking around the room"
139   ;; Get the room text
140   (define room-text
141     `((strong "=> " ,(slot-ref room 'name) " <=")
142       (p ,(slot-ref room 'desc))))
143
144   ;; Get a list of other things the player would see in the room
145   (define occupant-names-all
146     (map
147      (lambda (occupant)
148        (call-with-message (<-wait occupant 'visible-name
149                                   #:whos-looking player-id)
150                           (lambda* (_ #:key text)
151                             text)))
152      (remove
153       (lambda (x) (equal? x player-id))
154       (hash-map->list (lambda (x _) x)
155                       (slot-ref room 'occupants)))))
156
157   ;; Strip out the #f responses (these aren't listed because they lack a name
158   ;; or they aren't "obviously visible" to the player)
159   (define occupant-names-filtered
160     (filter identity occupant-names-all))
161
162   (define occupant-names-string
163     (if (eq? occupant-names-filtered '())
164         #f
165         (format #f "You see here: ~a.\n"
166                 (string-join occupant-names-filtered
167                              ", "))))
168
169   (define final-text
170     (if occupant-names-string
171         `(,@room-text
172           (p (em ,occupant-names-string)))
173         room-text))
174   
175   (<- player-id 'tell
176       #:text final-text))
177
178
179 (define* (room-look-room room message
180                             ;; Either send it to the #:to-id of the message,
181                             ;; or to the sender of the message
182                             #:key (to-id (message-from message)))
183   "Command: Player asks to look around the room"
184   (room-player-looks-around room to-id))
185
186 (define (room-find-thing-called room called-this)
187   "Find something called CALLED-THIS in the room, if any."
188   (call/ec
189    (lambda (return)
190      (for-each
191       (lambda (occupant)
192         (mbody-receive (_ #:key goes-by)
193             (<-wait occupant 'goes-by)
194           (if (member called-this goes-by)
195               (return occupant))))
196       (hash-map->list (lambda (key val) key)
197                       (slot-ref room 'occupants)))
198      #f)))
199
200 (define %formless-desc
201   "You don't see anything special.")
202
203 (define* (room-look-at room message #:key direct-obj)
204   "Look at a specific object in the room."
205   (define matching-object
206     (room-find-thing-called room direct-obj))
207
208   (cond
209    (matching-object
210     (let ((obj-desc
211            (mbody-val (<-wait matching-object 'get-desc
212                             #:whos-looking (message-from message)))))
213       (if obj-desc
214           (<- (message-from message) 'tell
215               #:text (string-append obj-desc "\n"))
216           (<- (message-from message) 'tell
217               #:text (string-append %formless-desc "\n")))))
218    (else
219     (<- (message-from message) 'tell
220         #:text "You don't see that here, so you can't look at it.\n"))))
221
222
223 (define* (room-tell-room room text #:key exclude wait)
224   (define who-to-tell (gameobj-occupants room #:exclude exclude))
225   (for-each
226    (lambda (tell-me)
227      ;; @@: Does anything really care?
228      (define deliver-method
229        (if wait
230            <-wait
231            <-))
232      (deliver-method tell-me 'tell
233                      #:text text))
234    who-to-tell))
235
236 (define* (room-act-tell-room room message #:key text exclude wait)
237   "Tell the room some messages."
238   (room-tell-room room text
239                   #:exclude exclude
240                   #:wait wait))
241
242 (define* (room-cmd-say room message #:key phrase)
243   "Command: Say something to room participants."
244   (define player-name
245     (mbody-val (<-wait (message-from message) 'get-name)))
246   (define message-to-send
247     `((b "<" ,player-name ">") " " ,phrase))
248   (room-tell-room room message-to-send))
249
250 (define* (room-cmd-emote room message #:key phrase)
251   "Command: Say something to room participants."
252   (define player-name
253     (mbody-val (<-wait (message-from message) 'get-name)))
254   (define message-to-send
255     `((b "* " ,player-name) " " ,phrase))
256   (room-tell-room room message-to-send))
257
258 (define* (room-announce-entrance room message #:key who-entered)
259   (define player-name
260     (mbody-val (<-wait who-entered 'get-name)))
261   (define message-to-send
262     (format #f "~a enters the room.\n" player-name))
263   (room-tell-room room message-to-send
264                   #:exclude who-entered))