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