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