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