Traversal checks, plus flavortext
[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 ;; TODO: Subclass from container?
64 (define-class <room> (<gameobj>)
65   ;; A list of <exit>
66   (exits #:init-value '()
67          #:init-keyword #:exits
68          #:getter room-exits)
69
70   (container-dom-commands
71    #:allocation #:each-subclass
72    #:init-thunk
73    (build-commands
74     (("l" "look") ((empty-command cmd-look-room)))
75     ("go" ((empty-command cmd-go-where)
76            (loose-direct-command cmd-go)))
77     (("say" "\"" "'") ((greedy-command cmd-say)))
78     (("emote" "/me") ((greedy-command cmd-emote)))))
79
80   (container-sub-commands
81    #:allocation #:each-subclass
82    #:init-thunk
83    (build-commands
84     (("l" "look") ((loose-direct-command cmd-look-at-from-room)))))
85
86   (actions #:allocation #:each-subclass
87            #:init-thunk
88            (build-actions
89             (cmd-go room-cmd-go)
90             (cmd-go-where room-cmd-go-where)
91             (announce-entrance room-announce-entrance)
92             (look-room room-look-room)
93             (tell-room room-act-tell-room)
94             ;; in this case the command is the same version as the normal
95             ;; look-room version
96             (cmd-look-room room-look-room)
97             (cmd-look-at-from-room room-look-dont-see-it)
98             (cmd-say room-cmd-say)
99             (cmd-emote room-cmd-emote))))
100
101 (define* (room-cmd-go room message #:key direct-obj)
102   (define exit
103     (find
104      (lambda (exit)
105        (equal? (exit-name exit) direct-obj))
106      (room-exits room)))
107   (define to-address (if exit
108                          ;; Get the exit, but resolve it dynamically
109                          ;; in case it's a special
110                          (dyn-ref room (slot-ref exit 'to))
111                          #f))
112   (define player (message-from message))
113   (define player-name
114     (mbody-val (<-wait player 'get-name)))
115   (cond
116    (exit
117     (call-with-values (lambda ()
118                         (exit-can-traverse? exit room player))
119       (lambda* (can-traverse? #:optional player-flavortext
120                               room-flavortext)
121         (if can-traverse?
122             ;; looks like we can go, so let's go!
123             (begin
124               ;; Set the player's new location
125               (<-wait player 'set-loc!
126                       #:loc to-address)
127               (when player-flavortext
128                 (<-wait player 'tell
129                         #:text player-flavortext))
130               ;; Tell everyone else the person walked away
131               (room-tell-room
132                room (or room-flavortext
133                         (format #f "~a wanders ~a.\n"
134                                 player-name direct-obj)))
135               (<- to-address 'announce-entrance
136                   #:who-entered player)
137               ;; Have the new room update the player to the new location
138               (<- to-address 'look-room
139                   #:to-id player))
140             ;; Otherwise, if we can't go...
141             (begin
142               (<- player 'tell
143                   #:text (or player-flavortext
144                              `("You try to go " ,direct-obj " but something "
145                                "seems to block you.")))
146               (when room-flavortext
147                 (room-tell-room room room-flavortext
148                                 #:exclude player)))))))
149    (else
150     (<- player 'tell
151         #:text "You don't see any way to go there.\n"))))
152
153 (define (room-cmd-go-where room message)
154   (<- (message-from message) 'tell
155       #:text "Go where?\n"))
156
157 ;;; look commands
158
159 (define (room-player-looks-around room player-id)
160   "Handle looking around the room"
161   ;; Get the room text
162   (define room-text
163     `((strong "=> " ,(slot-ref room 'name) " <=")
164       (p ,(slot-ref room 'desc))))
165
166   ;; Get a list of other things the player would see in the room
167   (define occupant-names-all
168     (map
169      (lambda (occupant)
170        (call-with-message (<-wait occupant 'visible-name
171                                   #:whos-looking player-id)
172                           (lambda* (_ #:key text)
173                             text)))
174      (remove
175       (lambda (x) (equal? x player-id))
176       (hash-map->list (lambda (x _) x)
177                       (slot-ref room 'occupants)))))
178
179   ;; Strip out the #f responses (these aren't listed because they lack a name
180   ;; or they aren't "obviously visible" to the player)
181   (define occupant-names-filtered
182     (filter identity occupant-names-all))
183
184   (define occupant-names-string
185     (if (eq? occupant-names-filtered '())
186         #f
187         (format #f "You see here: ~a.\n"
188                 (string-join occupant-names-filtered
189                              ", "))))
190
191   (define final-text
192     (if occupant-names-string
193         `(,@room-text
194           (p (em ,occupant-names-string)))
195         room-text))
196   
197   (<- player-id 'tell
198       #:text final-text))
199
200
201 (define* (room-look-room room message
202                          ;; Either send it to the #:to-id of the message,
203                          ;; or to the sender of the message
204                          #:key (to-id (message-from message)))
205   "Command: Player asks to look around the room"
206   (room-player-looks-around room to-id))
207
208 (define (room-find-thing-called room called-this)
209   "Find something called CALLED-THIS in the room, if any."
210   (call/ec
211    (lambda (return)
212      (for-each
213       (lambda (occupant)
214         (define goes-by (mbody-val (<-wait occupant 'goes-by)))
215         (if (ci-member called-this goes-by)
216             (return occupant)))
217       (hash-map->list (lambda (key val) key)
218                       (slot-ref room 'occupants)))
219      #f)))
220
221 (define* (room-look-dont-see-it room message #:key direct-obj)
222   "In general, if we get to this point, we didn't find something to look at."
223   (<- (message-from message) 'tell
224       #:text "You don't see that here, so you can't look at it.\n"))
225
226
227 (define* (room-tell-room room text #:key exclude wait)
228   (define who-to-tell (gameobj-occupants room #:exclude exclude))
229   (for-each
230    (lambda (tell-me)
231      ;; @@: Does anything really care?
232      (define deliver-method
233        (if wait
234            <-wait
235            <-))
236      (deliver-method tell-me 'tell
237                      #:text text))
238    who-to-tell))
239
240 (define* (room-act-tell-room room message #:key text exclude wait)
241   "Tell the room some messages."
242   (room-tell-room room text
243                   #:exclude exclude
244                   #:wait wait))
245
246 (define* (room-cmd-say room message #:key phrase)
247   "Command: Say something to room participants."
248   (define player-name
249     (mbody-val (<-wait (message-from message) 'get-name)))
250   (define message-to-send
251     `((b "<" ,player-name ">") " " ,phrase))
252   (room-tell-room room message-to-send))
253
254 (define* (room-cmd-emote room message #:key phrase)
255   "Command: Say something to room participants."
256   (define player-name
257     (mbody-val (<-wait (message-from message) 'get-name)))
258   (define message-to-send
259     `((b "* " ,player-name) " " ,phrase))
260   (room-tell-room room message-to-send))
261
262 (define* (room-announce-entrance room message #:key who-entered)
263   (define player-name
264     (mbody-val (<-wait who-entered 'get-name)))
265   (define message-to-send
266     (format #f "~a enters the room.\n" player-name))
267   (room-tell-room room message-to-send
268                   #:exclude who-entered))