You can now look at things
[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 systems 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>
28             room-actions
29             room-actions*
30
31             <exit>))
32
33 \f
34 ;;; Exits
35 ;;; =====
36
37 (define-class <exit> ()
38   ;; Used for wiring
39   (to-symbol #:init-keyword #:to-symbol)
40   ;; The actual address we use
41   (to-address #:init-keyword #:address)
42   ;; Name of the room (@@: Should this be names?)
43   (name #:getter exit-name
44         #:init-keyword #:name)
45   (desc #:init-keyword #:desc
46         #:init-value #f)
47
48   ;; *Note*: These two methods have an extra layer of indirection, but
49   ;;   it's for a good reason.
50   (visible-check #:init-value (const #t)
51                  #:init-keyword #:visible-check)
52   ;; By default all exits can be traversed
53   (traverse-check #:init-value (const #t)
54                   #:init-keyword #:traverse-check))
55
56 (define* (exit-can-traverse? exit actor
57                              #:optional (target-actor (actor-id actor)))
58   ((slot-ref exit 'traverse-check) exit actor target-actor))
59
60 (define* (exit-is-visible? exit actor
61                            #:optional (target-actor (actor-id actor)))
62   ((slot-ref exit 'traverse-check) exit actor target-actor))
63
64
65 \f
66 ;;; Rooms
67 ;;; =====
68
69 (define %room-contain-commands
70   (list
71    (loose-direct-command "look" 'cmd-look-at)
72    (empty-command "look" 'cmd-look-room)
73    (empty-command "go" 'cmd-go-where)
74    (loose-direct-command "go" 'cmd-go)))
75
76 (define room-actions
77   (build-actions
78    ;; desc == description
79    (init (wrap-apply room-init))
80    (wire-exits! (wrap-apply room-wire-exits!))
81    (cmd-go (wrap-apply room-cmd-go))
82    (cmd-go-where (wrap-apply room-cmd-go-where))
83    (look-room (wrap-apply room-look-room))
84    ;; in this case the command is the same version as the normal
85    ;; look-room version
86    (cmd-look-room (wrap-apply room-look-room))
87    (cmd-look-at (wrap-apply room-look-at))))
88
89 (define room-actions*
90   (append room-actions gameobj-actions))
91
92 (define room-action-dispatch
93   (simple-dispatcher room-actions*))
94
95 ;; TODO: Subclass from container?
96 (define-class <room> (<gameobj>)
97   ;; A list of <exit>
98   (exits #:init-value '()
99          #:init-keyword #:exits
100          #:getter room-exits)
101
102   (container-commands
103    #:init-value (wrap %room-contain-commands))
104
105   (message-handler
106    #:allocation #:each-subclass
107    ;; @@: Can remove this indirection once things settle
108    #:init-value (wrap-apply room-action-dispatch)))
109
110 (define (room-init room message)
111   (room-wire-exits! room))
112
113 (define (room-wire-exits! room)
114   "Actually hook up the rooms' exit addresses to the rooms they
115 claim to point to."
116   (for-each
117    (lambda (exit)
118      (define new-exit
119        (message-ref
120         (<-wait room (gameobj-gm room) 'lookup-special
121                 #:symbol (slot-ref exit 'to-symbol))
122         'room-id))
123
124      (slot-set! exit 'to-address new-exit))
125
126    (room-exits room)))
127
128 (define-mhandler (room-cmd-go room message direct-obj)
129   (define exit
130     (find
131      (lambda (exit)
132        (equal? (exit-name exit) direct-obj))
133      (room-exits room)))
134   (cond
135    (exit
136     ;; Set the player's new location
137     (<-wait room (message-from message) 'set-loc!
138             #:loc (slot-ref exit 'to-address))
139     ;; Have the new room update the player to the new location
140     (<- room (slot-ref exit 'to-address) 'look-room
141         #:to-id (message-from message)))
142    (else
143     (<- room (message-from message) 'tell
144         #:text "You don't see any way to go there.\n"))))
145
146 (define-mhandler (room-cmd-go-where room message)
147   (<- room (message-from message) 'tell
148       #:text "Go where?\n"))
149
150 ;;; look commands
151
152 (define (list-words-as-string words)
153   "A little utility for listing a bunch of words in an English-style list"
154   ;; TODO: This could be made faster by traversing the O(n)
155   ;;   list once, not twice
156   (let ((word-length (length words)))
157     (cond 
158      ((eqv? word-length 0) "")
159      ((eqv? word-length 1) (car words))
160      (else
161       ;; TODO: and this is NOT efficient
162       (string-append
163        (string-join
164         (drop-right words 1)
165         ", ")
166        " and "
167        (last words))))))
168
169 (define (room-player-looks-around room player-id)
170   "Handle looking around the room"
171   ;; Get the room text
172   (define room-text
173     (format #f "**~a**\n~a\n"
174             (slot-ref room 'name)
175             (slot-ref room 'desc)))
176
177   ;; Get a list of other things the player would see in the room
178   (define occupant-names-all
179     (map
180      (lambda (occupant)
181        (message-ref
182         (<-wait room occupant 'visible-name
183                 #:whos-looking player-id)
184         'text))
185      (remove
186       (lambda (x) (equal? x player-id))
187       (hash-map->list (lambda (x _) x)
188                       (slot-ref room 'occupants)))))
189
190   ;; Strip out the #f responses (these aren't listed because they lack a name
191   ;; or they aren't "obviously visible" to the player)
192   (define occupant-names-filtered
193     (filter identity occupant-names-all))
194
195   (define occupant-names-string
196     (if (eq? occupant-names-filtered '())
197         #f
198         (format #f "You see here: ~a.\n"
199                 (list-words-as-string occupant-names-filtered))))
200
201   (define final-text
202     (if occupant-names-string
203         (string-append room-text occupant-names-string)
204         room-text))
205   
206   (<- room player-id 'tell
207       #:text final-text))
208
209
210 (define-mhandler (room-look-room room message)
211   "Command: Player asks to look around the room"
212   (room-player-looks-around
213    room
214    ;; Either send it to the #:to-id of the message, or to the
215    ;; sender of the message
216    (message-ref message 'to-id
217                 (message-from message))))
218
219 (define (room-find-thing-called room called-this)
220   "Find something called CALLED-THIS in the room, if any."
221   (call/ec
222    (lambda (return)
223      (for-each
224       (lambda (occupant)
225         (define goes-by
226           (message-ref (<-wait room occupant 'goes-by)
227                        'goes-by #f))
228         (display "here!\n")
229         (if (member called-this goes-by)
230             (return occupant)))
231       (hash-map->list (lambda (key val) key)
232                       (slot-ref room 'occupants)))
233      #f)))
234
235 (define %formless-desc
236   "You don't see anything special about it.")
237
238 (define-mhandler (room-look-at room message direct-obj)
239   "Look at a specific object in the room."
240   (define matching-object
241     (room-find-thing-called room direct-obj))
242
243   (cond
244    (matching-object
245     (let ((obj-desc
246            (message-ref
247             (<-wait room matching-object 'get-desc)
248             'val)))
249       (if obj-desc
250           (<- room (message-from message) 'tell
251               #:text (string-append obj-desc "\n"))
252           (<- room (message-from message) 'tell
253               #:text (string-append %formless-desc "\n")))))
254    (else
255     (<- room (message-from message) 'tell
256         #:text "You don't see that here, so you can't look at it.\n"))))