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