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