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