Moving looking to be primarily a gameobj action.
[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") ((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-name
113     (mbody-val (<-wait (message-from message) 'get-name)))
114   (cond
115    (exit
116     ;; Set the player's new location
117     (<-wait (message-from message) 'set-loc!
118             #:loc to-address)
119     ;; Tell everyone else the person walked away
120     (room-tell-room
121      room
122      (format #f "~a wanders ~a.\n"
123              player-name direct-obj))
124     (<- to-address 'announce-entrance
125         #:who-entered (message-from message))
126     ;; Have the new room update the player to the new location
127     (<- to-address 'look-room
128         #:to-id (message-from message)))
129    (else
130     (<- (message-from message) 'tell
131         #:text "You don't see any way to go there.\n"))))
132
133 (define (room-cmd-go-where room message)
134   (<- (message-from message) 'tell
135       #:text "Go where?\n"))
136
137 ;;; look commands
138
139 (define (room-player-looks-around room player-id)
140   "Handle looking around the room"
141   ;; Get the room text
142   (define room-text
143     `((strong "=> " ,(slot-ref room 'name) " <=")
144       (p ,(slot-ref room 'desc))))
145
146   ;; Get a list of other things the player would see in the room
147   (define occupant-names-all
148     (map
149      (lambda (occupant)
150        (call-with-message (<-wait occupant 'visible-name
151                                   #:whos-looking player-id)
152                           (lambda* (_ #:key text)
153                             text)))
154      (remove
155       (lambda (x) (equal? x player-id))
156       (hash-map->list (lambda (x _) x)
157                       (slot-ref room 'occupants)))))
158
159   ;; Strip out the #f responses (these aren't listed because they lack a name
160   ;; or they aren't "obviously visible" to the player)
161   (define occupant-names-filtered
162     (filter identity occupant-names-all))
163
164   (define occupant-names-string
165     (if (eq? occupant-names-filtered '())
166         #f
167         (format #f "You see here: ~a.\n"
168                 (string-join occupant-names-filtered
169                              ", "))))
170
171   (define final-text
172     (if occupant-names-string
173         `(,@room-text
174           (p (em ,occupant-names-string)))
175         room-text))
176   
177   (<- player-id 'tell
178       #:text final-text))
179
180
181 (define* (room-look-room room message
182                             ;; Either send it to the #:to-id of the message,
183                             ;; or to the sender of the message
184                             #:key (to-id (message-from message)))
185   "Command: Player asks to look around the room"
186   (room-player-looks-around room to-id))
187
188 (define (room-find-thing-called room called-this)
189   "Find something called CALLED-THIS in the room, if any."
190   (call/ec
191    (lambda (return)
192      (for-each
193       (lambda (occupant)
194         (define goes-by (mbody-val (<-wait occupant 'goes-by)))
195         (if (ci-member called-this goes-by)
196             (return occupant)))
197       (hash-map->list (lambda (key val) key)
198                       (slot-ref room 'occupants)))
199      #f)))
200
201 (define* (room-look-dont-see-it room message #:key direct-obj)
202   "In general, if we get to this point, we didn't find something to look at."
203   (<- (message-from message) 'tell
204       #:text "You don't see that here, so you can't look at it.\n"))
205
206
207 (define* (room-tell-room room text #:key exclude wait)
208   (define who-to-tell (gameobj-occupants room #:exclude exclude))
209   (for-each
210    (lambda (tell-me)
211      ;; @@: Does anything really care?
212      (define deliver-method
213        (if wait
214            <-wait
215            <-))
216      (deliver-method tell-me 'tell
217                      #:text text))
218    who-to-tell))
219
220 (define* (room-act-tell-room room message #:key text exclude wait)
221   "Tell the room some messages."
222   (room-tell-room room text
223                   #:exclude exclude
224                   #:wait wait))
225
226 (define* (room-cmd-say room message #:key phrase)
227   "Command: Say something to room participants."
228   (define player-name
229     (mbody-val (<-wait (message-from message) 'get-name)))
230   (define message-to-send
231     `((b "<" ,player-name ">") " " ,phrase))
232   (room-tell-room room message-to-send))
233
234 (define* (room-cmd-emote room message #:key phrase)
235   "Command: Say something to room participants."
236   (define player-name
237     (mbody-val (<-wait (message-from message) 'get-name)))
238   (define message-to-send
239     `((b "* " ,player-name) " " ,phrase))
240   (room-tell-room room message-to-send))
241
242 (define* (room-announce-entrance room message #:key who-entered)
243   (define player-name
244     (mbody-val (<-wait who-entered 'get-name)))
245   (define message-to-send
246     (format #f "~a enters the room.\n" player-name))
247   (room-tell-room room message-to-send
248                   #:exclude who-entered))