You can now kiss the frog!
[mudsync.git] / mudsync / room.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christine Lemmer-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 (mudsync utils)
23   #:use-module (8sync actors)
24   #:use-module (8sync agenda)
25   #:use-module (oop goops)
26   #:use-module (srfi srfi-1)
27   #:use-module (ice-9 control)
28   #:export (<room> <exit>))
29
30 \f
31 ;;; Exits
32 ;;; =====
33
34 (define-class <exit> ()
35   (to #:init-keyword #:to
36       #:init-value #f)
37   ;; Name of the room (@@: Should this be names?)
38   (name #:getter exit-name
39         #:init-keyword #:name)
40   (desc #:init-keyword #:desc
41         #:init-value #f)
42
43   ;; *Note*: These two methods have an extra layer of indirection, but
44   ;;   it's for a good reason.
45   (visible-check #:init-value (const #t)
46                  #:init-keyword #:visible-check)
47   ;; By default all exits can be traversed
48   (traverse-check #:init-value (const #t)
49                   #:init-keyword #:traverse-check))
50
51 ;; @@: Should we make whos-exiting optional?  Would there ever be any
52 ;;   reason?
53 (define* (exit-can-traverse? exit room whos-exiting)
54   ((slot-ref exit 'traverse-check) exit room whos-exiting))
55
56 (define* (exit-is-visible? exit room whos-exiting)
57   ((slot-ref exit 'visible-check) exit room whos-exiting))
58
59
60 \f
61 ;;; Rooms
62 ;;; =====
63
64 (define (exit-shorthand name)
65   (lambda (room message)
66     (room-cmd-go room message #:direct-obj name)))
67
68 ;; TODO: Subclass from container?
69 (define-class <room> (<gameobj>)
70   ;; A list of <exit>
71   (exits #:init-value '()
72          #:init-keyword #:exits
73          #:getter room-exits)
74
75   (container-dom-commands
76    #:allocation #:each-subclass
77    #:init-thunk
78    (build-commands
79     (("l" "look") ((empty-command cmd-look-room)))
80     ("go" ((empty-command cmd-go-where)
81            (loose-direct-command cmd-go)))
82     (("say" "\"" "'") ((greedy-command cmd-say)))
83     (("emote" "/me") ((greedy-command cmd-emote)))
84     ;; movement aliases
85     (("n" "north") ((empty-command go-north)))
86     (("ne" "northeast") ((empty-command go-northeast)))
87     (("e" "east") ((empty-command go-east)))
88     (("se" "southeast") ((empty-command go-southeast)))
89     (("s" "south") ((empty-command go-south)))
90     (("sw" "southwest") ((empty-command go-southwest)))
91     (("w" "west") ((empty-command go-west)))
92     (("nw" "northwest") ((empty-command go-northwest)))
93     (("u" "up") ((empty-command go-up)))
94     (("d" "down") ((empty-command go-down)))))
95
96   (container-sub-commands
97    #:allocation #:each-subclass
98    #:init-thunk
99    (build-commands
100     (("l" "look") ((loose-direct-command cmd-look-at-from-room)))))
101
102   (actions #:allocation #:each-subclass
103            #:init-thunk
104            (build-actions
105             (cmd-go room-cmd-go)
106             (cmd-go-where room-cmd-go-where)
107             (announce-entrance room-announce-entrance)
108             (look-room room-look-room)
109             (tell-room room-act-tell-room)
110             ;; in this case the command is the same version as the normal
111             ;; look-room version
112             (cmd-look-room room-look-room)
113             (cmd-look-at-from-room room-look-dont-see-it)
114             (cmd-say room-cmd-say)
115             (cmd-emote room-cmd-emote)
116             ;; movement aliases
117             (go-north (exit-shorthand "north"))
118             (go-northeast (exit-shorthand "northeast"))
119             (go-east (exit-shorthand "east"))
120             (go-southeast (exit-shorthand "southeast"))
121             (go-south (exit-shorthand "south"))
122             (go-southwest (exit-shorthand "southwest"))
123             (go-west (exit-shorthand "west"))
124             (go-northwest (exit-shorthand "northwest"))
125             (go-up (exit-shorthand "up"))
126             (go-down (exit-shorthand "down")))))
127
128 (define common-exit-aliases
129   '(("n" . "north")
130     ("ne" . "northeast")
131     ("e" . "east")
132     ("se" . "southeast")
133     ("s" . "south")
134     ("sw" . "southwest")
135     ("w" . "west")
136     ("nw" . "northwest")
137     ("u" . "up")
138     ("d" . "down")))
139
140 (define (dealias-exit-name exit-name)
141   (or (assoc-ref common-exit-aliases exit-name)
142       exit-name))
143
144 (define* (room-cmd-go room message #:key direct-obj)
145   (define exit
146     (find
147      (lambda (exit)
148        (equal? (exit-name exit) (dealias-exit-name direct-obj)))
149      (room-exits room)))
150   (define to-address (if exit
151                          ;; Get the exit, but resolve it dynamically
152                          ;; in case it's a special
153                          (dyn-ref room (slot-ref exit 'to))
154                          #f))
155   (define player (message-from message))
156   (define player-name
157     (mbody-val (<-wait player 'get-name)))
158   (cond
159    (exit
160     (call-with-values (lambda ()
161                         (exit-can-traverse? exit room player))
162       (lambda* (can-traverse? #:optional player-flavortext
163                               room-flavortext)
164         (cond
165          ;; The exit itself objects to moving
166          ((not can-traverse?)
167           (<- player 'tell
168               #:text (or player-flavortext
169                          `("You try to go " ,direct-obj " but something "
170                            "seems to block you.")))
171           (when room-flavortext
172             (room-tell-room room room-flavortext
173                             #:exclude player)))
174          ;; to-address points nowhere, or exit not set.
175          ((not exit)
176           (<- player 'tell
177               #:text '((i "Yikes!") " Something weird is going on. "
178                        "It seems like this exit leads nowhere, "
179                        "in a programming bug kind of way. "
180                        "Maybe tell an administrator?")))
181          ;; looks like we can go, so let's go!
182          (else
183           ;; Set the player's new location
184           (<-wait player 'set-loc!
185                   #:loc to-address)
186           (when player-flavortext
187             (<-wait player 'tell
188                     #:text player-flavortext))
189           ;; Tell everyone else the person walked away
190           (room-tell-room
191            room (or room-flavortext
192                     (format #f "~a wanders ~a.\n"
193                             player-name direct-obj)))
194           (<- to-address 'announce-entrance
195               #:who-entered player)
196           ;; Have the new room update the player to the new location
197           (<- to-address 'look-room
198               #:to-id player))))))
199    (else
200     (<- player 'tell
201         #:text "You don't see any way to go there.\n"))))
202
203 (define (room-cmd-go-where room message)
204   (<- (message-from message) 'tell
205       #:text "Go where?\n"))
206
207 ;;; look commands
208
209 (define (room-player-looks-around room player-id)
210   "Handle looking around the room"
211   ;; Get the room text
212   (define room-text
213     `((strong "=> " ,(slot-ref room 'name) " <=")
214       (p ,(gameobj-desc room))))
215
216   ;; Get a list of other things the player would see in the room
217   (define occupant-names-all
218     (map
219      (lambda (occupant)
220        (call-with-message (<-wait occupant 'visible-name
221                                   #:whos-looking player-id)
222                           (lambda* (_ #:key text)
223                             text)))
224      (remove
225       (lambda (x) (equal? x player-id))
226       (hash-map->list (lambda (x _) x)
227                       (slot-ref room 'occupants)))))
228
229   ;; Strip out the #f responses (these aren't listed because they lack a name
230   ;; or they aren't "obviously visible" to the player)
231   (define occupant-names-filtered
232     (filter identity occupant-names-all))
233
234   (define occupant-names-string
235     (if (eq? occupant-names-filtered '())
236         #f
237         (format #f "You see here: ~a.\n"
238                 (string-join occupant-names-filtered
239                              ", "))))
240
241   (define final-text
242     (if occupant-names-string
243         `(,@room-text
244           (p (em ,occupant-names-string)))
245         room-text))
246   
247   (<- player-id 'tell
248       #:text final-text))
249
250
251 (define* (room-look-room room message
252                          ;; Either send it to the #:to-id of the message,
253                          ;; or to the sender of the message
254                          #:key (to-id (message-from message)))
255   "Command: Player asks to look around the room"
256   (room-player-looks-around room to-id))
257
258 (define (room-find-thing-called room called-this)
259   "Find something called CALLED-THIS in the room, if any."
260   (call/ec
261    (lambda (return)
262      (for-each
263       (lambda (occupant)
264         (define goes-by (mbody-val (<-wait occupant 'goes-by)))
265         (if (ci-member called-this goes-by)
266             (return occupant)))
267       (hash-map->list (lambda (key val) key)
268                       (slot-ref room 'occupants)))
269      #f)))
270
271 (define* (room-look-dont-see-it room message #:key direct-obj)
272   "In general, if we get to this point, we didn't find something to look at."
273   (<- (message-from message) 'tell
274       #:text "You don't see that here, so you can't look at it.\n"))
275
276
277 (define* (room-tell-room room text #:key exclude wait)
278   (define who-to-tell (gameobj-occupants room #:exclude exclude))
279   (for-each
280    (lambda (tell-me)
281      ;; @@: Does anything really care?
282      (define deliver-method
283        (if wait
284            <-wait
285            <-))
286      (deliver-method tell-me 'tell
287                      #:text text))
288    who-to-tell))
289
290 (define* (room-act-tell-room room message #:key text exclude wait)
291   "Tell the room some messages."
292   (room-tell-room room text
293                   #:exclude exclude
294                   #:wait wait))
295
296 (define* (room-cmd-say room message #:key phrase)
297   "Command: Say something to room participants."
298   (define player-name
299     (mbody-val (<-wait (message-from message) 'get-name)))
300   (define message-to-send
301     `((b "<" ,player-name ">") " " ,phrase))
302   (room-tell-room room message-to-send))
303
304 (define* (room-cmd-emote room message #:key phrase)
305   "Command: Say something to room participants."
306   (define player-name
307     (mbody-val (<-wait (message-from message) 'get-name)))
308   (define message-to-send
309     `((b "* " ,player-name) " " ,phrase))
310   (room-tell-room room message-to-send))
311
312 (define* (room-announce-entrance room message #:key who-entered)
313   (define player-name
314     (mbody-val (<-wait who-entered 'get-name)))
315   (define message-to-send
316     (format #f "~a enters the room.\n" player-name))
317   (room-tell-room room message-to-send
318                   #:exclude who-entered))