added dynamic linking approach to exits. live hacking rooms works! :D :D
[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-mhandler (room-cmd-go room message 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     (message-ref (<-wait room (message-from message)
123                          'get-name) 'val))
124   (cond
125    (exit
126     ;; Set the player's new location
127     (<-wait room (message-from message) 'set-loc!
128             #:loc to-address)
129     ;; Tell everyone else the person walked away
130     (room-tell-room
131      room
132      (format #f "~a wanders ~a.\n"
133              player-name direct-obj))
134     (<- room to-address 'announce-entrance
135         #:who-entered (message-from message))
136     ;; Have the new room update the player to the new location
137     (<- room to-address 'look-room
138         #:to-id (message-from message)))
139    (else
140     (<- room (message-from message) 'tell
141         #:text "You don't see any way to go there.\n"))))
142
143 (define-mhandler (room-cmd-go-where room message)
144   (<- room (message-from message) 'tell
145       #:text "Go where?\n"))
146
147 ;;; look commands
148
149 (define (list-words-as-string words)
150   "A little utility for listing a bunch of words in an English-style list"
151   ;; TODO: This could be made faster by traversing the O(n)
152   ;;   list once, not twice
153   (let ((word-length (length words)))
154     (cond 
155      ((eqv? word-length 0) "")
156      ((eqv? word-length 1) (car words))
157      (else
158       ;; TODO: and this is NOT efficient
159       (string-append
160        (string-join
161         (drop-right words 1)
162         ", ")
163        " and "
164        (last words))))))
165
166 (define (room-player-looks-around room player-id)
167   "Handle looking around the room"
168   ;; Get the room text
169   (define room-text
170     (format #f "**~a**\n~a\n"
171             (slot-ref room 'name)
172             (slot-ref room 'desc)))
173
174   ;; Get a list of other things the player would see in the room
175   (define occupant-names-all
176     (map
177      (lambda (occupant)
178        (message-ref
179         (<-wait room occupant 'visible-name
180                 #:whos-looking player-id)
181         'text))
182      (remove
183       (lambda (x) (equal? x player-id))
184       (hash-map->list (lambda (x _) x)
185                       (slot-ref room 'occupants)))))
186
187   ;; Strip out the #f responses (these aren't listed because they lack a name
188   ;; or they aren't "obviously visible" to the player)
189   (define occupant-names-filtered
190     (filter identity occupant-names-all))
191
192   (define occupant-names-string
193     (if (eq? occupant-names-filtered '())
194         #f
195         (format #f "You see here: ~a.\n"
196                 (list-words-as-string occupant-names-filtered))))
197
198   (define final-text
199     (if occupant-names-string
200         (string-append room-text occupant-names-string)
201         room-text))
202   
203   (<- room player-id 'tell
204       #:text final-text))
205
206
207 (define-mhandler (room-look-room room message)
208   "Command: Player asks to look around the room"
209   (room-player-looks-around
210    room
211    ;; Either send it to the #:to-id of the message, or to the
212    ;; sender of the message
213    (message-ref message 'to-id
214                 (message-from message))))
215
216 (define (room-find-thing-called room called-this)
217   "Find something called CALLED-THIS in the room, if any."
218   (call/ec
219    (lambda (return)
220      (for-each
221       (lambda (occupant)
222         (define goes-by
223           (message-ref (<-wait room occupant 'goes-by)
224                        'goes-by #f))
225         (if (member called-this goes-by)
226             (return occupant)))
227       (hash-map->list (lambda (key val) key)
228                       (slot-ref room 'occupants)))
229      #f)))
230
231 (define %formless-desc
232   "You don't see anything special.")
233
234 (define-mhandler (room-look-at room message direct-obj)
235   "Look at a specific object in the room."
236   (define matching-object
237     (room-find-thing-called room direct-obj))
238
239   (cond
240    (matching-object
241     (let ((obj-desc
242            (message-ref
243             (<-wait room matching-object 'get-desc
244                     #:whos-looking (message-from message))
245             'val)))
246       (if obj-desc
247           (<- room (message-from message) 'tell
248               #:text (string-append obj-desc "\n"))
249           (<- room (message-from message) 'tell
250               #:text (string-append %formless-desc "\n")))))
251    (else
252     (<- room (message-from message) 'tell
253         #:text "You don't see that here, so you can't look at it.\n"))))
254
255
256 (define* (room-tell-room room text #:key exclude wait)
257   (define who-to-tell (gameobj-occupants room #:exclude exclude))
258   (for-each
259    (lambda (tell-me)
260      ;; @@: Does anything really care?
261      (define deliver-method
262        (if wait
263            <-wait
264            <-))
265      (deliver-method room tell-me 'tell
266                      #:text text))
267    who-to-tell))
268
269 (define-mhandler (room-act-tell-room room message text)
270   "Tell the room some messages."
271   (define exclude (message-ref message 'exclude #f))
272   (define wait-delivery (message-ref message 'wait #f))
273   (room-tell-room room text
274                   #:exclude exclude
275                   #:wait wait-delivery))
276
277 (define-mhandler (room-cmd-say room message phrase)
278   "Command: Say something to room participants."
279   (define player-name
280     (message-ref (<-wait room (message-from message)
281                          'get-name) 'val))
282   (define message-to-send
283     (format #f "~a says: ~a\n" player-name phrase))
284   (room-tell-room room message-to-send))
285
286 (define-mhandler (room-cmd-emote room message phrase)
287   "Command: Say something to room participants."
288   (define player-name
289     (message-ref (<-wait room (message-from message)
290                          'get-name) '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-mhandler (room-announce-entrance room message who-entered)
296   (define player-name
297     (message-ref (<-wait room who-entered 'get-name)
298                  'val))
299   (define message-to-send
300     (format #f "~a enters the room.\n" player-name))
301   (room-tell-room room message-to-send
302                   #:exclude who-entered))