0379d3da71d268586f42c0d22c594b99e7ef04c2
[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 (define* (exit-can-traverse? exit actor
51                              #:optional (target-actor (actor-id actor)))
52   ((slot-ref exit 'traverse-check) exit actor target-actor))
53
54 (define* (exit-is-visible? exit actor
55                            #:optional (target-actor (actor-id actor)))
56   ((slot-ref exit 'traverse-check) exit actor target-actor))
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") ((loose-direct-command cmd-look-at)
75              (empty-command cmd-look-room)))
76     ("go" ((empty-command cmd-go-where)
77            (loose-direct-command cmd-go)))
78     (("say" "\"" "'") ((greedy-command cmd-say)))
79     (("emote" "/me") ((greedy-command cmd-emote)))))
80
81   (actions #:allocation #:each-subclass
82            #:init-thunk
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 (room-player-looks-around room player-id)
135   "Handle looking around the room"
136   ;; Get the room text
137   (define room-text
138     `((strong "=> " ,(slot-ref room 'name) " <=")
139       (p ,(slot-ref room 'desc))))
140
141   ;; Get a list of other things the player would see in the room
142   (define occupant-names-all
143     (map
144      (lambda (occupant)
145        (call-with-message (<-wait occupant 'visible-name
146                                   #:whos-looking player-id)
147                           (lambda* (_ #:key text)
148                             text)))
149      (remove
150       (lambda (x) (equal? x player-id))
151       (hash-map->list (lambda (x _) x)
152                       (slot-ref room 'occupants)))))
153
154   ;; Strip out the #f responses (these aren't listed because they lack a name
155   ;; or they aren't "obviously visible" to the player)
156   (define occupant-names-filtered
157     (filter identity occupant-names-all))
158
159   (define occupant-names-string
160     (if (eq? occupant-names-filtered '())
161         #f
162         (format #f "You see here: ~a.\n"
163                 (string-join occupant-names-filtered
164                              ", "))))
165
166   (define final-text
167     (if occupant-names-string
168         `(,@room-text
169           (p (em ,occupant-names-string)))
170         room-text))
171   
172   (<- player-id 'tell
173       #:text final-text))
174
175
176 (define* (room-look-room room message
177                             ;; Either send it to the #:to-id of the message,
178                             ;; or to the sender of the message
179                             #:key (to-id (message-from message)))
180   "Command: Player asks to look around the room"
181   (room-player-looks-around room to-id))
182
183 (define (room-find-thing-called room called-this)
184   "Find something called CALLED-THIS in the room, if any."
185   (call/ec
186    (lambda (return)
187      (for-each
188       (lambda (occupant)
189         (define goes-by (mbody-val (<-wait occupant 'goes-by)))
190         (if (ci-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))