Port to the remove-define-mhandler 8sync branch
[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 systems 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>
28             room-actions
29             room-actions*
30
31             <exit>))
32
33 \f
34 ;;; Exits
35 ;;; =====
36
37 (define-class <exit> ()
38   (to #:init-keyword #:to)
39   ;; Name of the room (@@: Should this be names?)
40   (name #:getter exit-name
41         #:init-keyword #:name)
42   (desc #:init-keyword #:desc
43         #:init-value #f)
44
45   ;; *Note*: These two methods have an extra layer of indirection, but
46   ;;   it's for a good reason.
47   (visible-check #:init-value (const #t)
48                  #:init-keyword #:visible-check)
49   ;; By default all exits can be traversed
50   (traverse-check #:init-value (const #t)
51                   #:init-keyword #:traverse-check))
52
53 (define* (exit-can-traverse? exit actor
54                              #:optional (target-actor (actor-id actor)))
55   ((slot-ref exit 'traverse-check) exit actor target-actor))
56
57 (define* (exit-is-visible? exit actor
58                            #:optional (target-actor (actor-id actor)))
59   ((slot-ref exit 'traverse-check) exit actor target-actor))
60
61
62 \f
63 ;;; Rooms
64 ;;; =====
65
66 (define %room-contain-commands
67   (list
68    (loose-direct-command "look" 'cmd-look-at)
69    (empty-command "look" 'cmd-look-room)
70    (empty-command "go" 'cmd-go-where)
71    (loose-direct-command "go" 'cmd-go)
72    (greedy-command "say" 'cmd-say)
73    (greedy-command "emote" 'cmd-emote)))
74
75 (define room-actions
76   (build-actions
77    (cmd-go (wrap-apply room-cmd-go))
78    (cmd-go-where (wrap-apply room-cmd-go-where))
79    (announce-entrance (wrap-apply room-announce-entrance))
80    (look-room (wrap-apply room-look-room))
81    (tell-room (wrap-apply room-act-tell-room))
82    ;; in this case the command is the same version as the normal
83    ;; look-room version
84    (cmd-look-room (wrap-apply room-look-room))
85    (cmd-look-at (wrap-apply room-look-at))
86    (cmd-say (wrap-apply room-cmd-say))
87    (cmd-emote (wrap-apply room-cmd-emote))))
88
89 (define room-actions*
90   (append room-actions gameobj-actions))
91
92 (define room-action-dispatch
93   (simple-dispatcher room-actions*))
94
95 ;; TODO: Subclass from container?
96 (define-class <room> (<gameobj>)
97   ;; A list of <exit>
98   (exits #:init-value '()
99          #:init-keyword #:exits
100          #:getter room-exits)
101
102   (container-commands
103    #:init-value (wrap %room-contain-commands))
104
105   (message-handler
106    #:allocation #:each-subclass
107    ;; @@: Can remove this indirection once things settle
108    #:init-value (wrap-apply room-action-dispatch)))
109
110 (define* (room-cmd-go room message #:key direct-obj)
111   (define exit
112     (find
113      (lambda (exit)
114        (equal? (exit-name exit) direct-obj))
115      (room-exits room)))
116   (define to-address (if exit
117                          ;; Get the exit, but resolve it dynamically
118                          ;; in case it's a special
119                          (dyn-ref room (slot-ref exit 'to))
120                          #f))
121   (define player-name
122     (msg-receive (_ #:key val)
123         (<-wait room (message-from message) 'get-name)
124       val))
125   (cond
126    (exit
127     ;; Set the player's new location
128     (<-wait room (message-from message) 'set-loc!
129             #:loc to-address)
130     ;; Tell everyone else the person walked away
131     (room-tell-room
132      room
133      (format #f "~a wanders ~a.\n"
134              player-name direct-obj))
135     (<- room to-address 'announce-entrance
136         #:who-entered (message-from message))
137     ;; Have the new room update the player to the new location
138     (<- room to-address 'look-room
139         #:to-id (message-from message)))
140    (else
141     (<- room (message-from message) 'tell
142         #:text "You don't see any way to go there.\n"))))
143
144 (define (room-cmd-go-where room message)
145   (<- room (message-from message) 'tell
146       #:text "Go where?\n"))
147
148 ;;; look commands
149
150 (define (list-words-as-string words)
151   "A little utility for listing a bunch of words in an English-style list"
152   ;; TODO: This could be made faster by traversing the O(n)
153   ;;   list once, not twice
154   (let ((word-length (length words)))
155     (cond 
156      ((eqv? word-length 0) "")
157      ((eqv? word-length 1) (car words))
158      (else
159       ;; TODO: and this is NOT efficient
160       (string-append
161        (string-join
162         (drop-right words 1)
163         ", ")
164        " and "
165        (last words))))))
166
167 (define (room-player-looks-around room player-id)
168   "Handle looking around the room"
169   ;; Get the room text
170   (define room-text
171     (format #f "**~a**\n~a\n"
172             (slot-ref room 'name)
173             (slot-ref room 'desc)))
174
175   ;; Get a list of other things the player would see in the room
176   (define occupant-names-all
177     (map
178      (lambda (occupant)
179        (call-with-message (<-wait room occupant 'visible-name
180                                   #:whos-looking player-id)
181                           (lambda* (_ #:key text)
182                             text)))
183      (remove
184       (lambda (x) (equal? x player-id))
185       (hash-map->list (lambda (x _) x)
186                       (slot-ref room 'occupants)))))
187
188   ;; Strip out the #f responses (these aren't listed because they lack a name
189   ;; or they aren't "obviously visible" to the player)
190   (define occupant-names-filtered
191     (filter identity occupant-names-all))
192
193   (define occupant-names-string
194     (if (eq? occupant-names-filtered '())
195         #f
196         (format #f "You see here: ~a.\n"
197                 (list-words-as-string occupant-names-filtered))))
198
199   (define final-text
200     (if occupant-names-string
201         (string-append room-text occupant-names-string)
202         room-text))
203   
204   (<- room player-id 'tell
205       #:text final-text))
206
207
208 (define* (room-look-room room message
209                             ;; Either send it to the #:to-id of the message,
210                             ;; or to the sender of the message
211                             #:key (to-id (message-from message)))
212   "Command: Player asks to look around the room"
213   (room-player-looks-around room to-id))
214
215 (define (room-find-thing-called room called-this)
216   "Find something called CALLED-THIS in the room, if any."
217   (call/ec
218    (lambda (return)
219      (for-each
220       (lambda (occupant)
221         (msg-receive (_ #:key goes-by)
222             (<-wait room occupant 'goes-by)
223           (if (member called-this goes-by)
224               (return occupant))))
225       (hash-map->list (lambda (key val) key)
226                       (slot-ref room 'occupants)))
227      #f)))
228
229 (define %formless-desc
230   "You don't see anything special.")
231
232 (define* (room-look-at room message #:key direct-obj)
233   "Look at a specific object in the room."
234   (define matching-object
235     (room-find-thing-called room direct-obj))
236
237   (cond
238    (matching-object
239     (let ((obj-desc
240            (msg-receive (_ #:key val)
241                (<-wait room matching-object 'get-desc
242                        #:whos-looking (message-from message))
243              val)))
244       (if obj-desc
245           (<- room (message-from message) 'tell
246               #:text (string-append obj-desc "\n"))
247           (<- room (message-from message) 'tell
248               #:text (string-append %formless-desc "\n")))))
249    (else
250     (<- room (message-from message) 'tell
251         #:text "You don't see that here, so you can't look at it.\n"))))
252
253
254 (define* (room-tell-room room text #:key exclude wait)
255   (define who-to-tell (gameobj-occupants room #:exclude exclude))
256   (for-each
257    (lambda (tell-me)
258      ;; @@: Does anything really care?
259      (define deliver-method
260        (if wait
261            <-wait
262            <-))
263      (deliver-method room tell-me 'tell
264                      #:text text))
265    who-to-tell))
266
267 (define* (room-act-tell-room room message #:key text exclude wait)
268   "Tell the room some messages."
269   (room-tell-room room text
270                   #:exclude exclude
271                   #:wait wait))
272
273 (define* (room-cmd-say room message #:key phrase)
274   "Command: Say something to room participants."
275   (define player-name
276     (msg-receive (_ #:key val)
277         (<-wait room (message-from message)
278                 'get-name)
279       val))
280   (define message-to-send
281     (format #f "~a says: ~a\n" player-name phrase))
282   (room-tell-room room message-to-send))
283
284 (define* (room-cmd-emote room message #:key phrase)
285   "Command: Say something to room participants."
286   (define player-name
287     (msg-receive (_ #:key val)
288         (<-wait room (message-from message)
289                 'get-name)
290       val))
291   (define message-to-send
292     (format #f "* ~a ~a\n" player-name phrase))
293   (room-tell-room room message-to-send))
294
295 (define* (room-announce-entrance room message #:key who-entered)
296   (define player-name
297     (msg-receive (_ #:key val)
298         (<-wait room who-entered 'get-name)
299       val))
300   (define message-to-send
301     (format #f "~a enters the room.\n" player-name))
302   (room-tell-room room message-to-send
303                   #:exclude who-entered))