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