Add clerk and bell
[mudsync.git] / mudsync / gameobj.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 ;;; Game actor
20 ;;; ==========
21
22 (define-module (mudsync gameobj)
23   #:use-module (mudsync command)
24   #:use-module (8sync systems actors)
25   #:use-module (8sync agenda)
26   #:use-module (srfi srfi-1)
27   #:use-module (ice-9 format)
28   #:use-module (ice-9 match)
29   #:use-module (oop goops)
30   #:export (<gameobj>
31
32             gameobj-loc
33             gameobj-gm
34
35             gameobj-act-init
36             gameobj-set-loc!
37             gameobj-occupants
38             gameobj-actions
39             gameobj-self-destruct
40
41             slot-ref-maybe-runcheck
42             val-or-run
43
44             dyn-ref))
45
46 ;;; Gameobj
47 ;;; =======
48
49
50 ;;; Actions supported by all gameobj
51 (define gameobj-actions
52   (build-actions
53    (init (wrap-apply gameobj-act-init))
54    ;; Commands for co-occupants
55    (get-commands (wrap-apply gameobj-get-commands))
56    ;; Commands for participants in a room
57    (get-container-commands (wrap-apply gameobj-get-container-commands))
58    ;; Commands for inventory items, etc (occupants of the gameobj commanding)
59    (get-contained-commands (wrap-apply gameobj-get-contained-commands))
60    (get-occupants (wrap-apply gameobj-get-occupants))
61    (add-occupant! (wrap-apply gameobj-add-occupant!))
62    (remove-occupant! (wrap-apply gameobj-remove-occupant!))
63    (get-loc (wrap-apply gameobj-act-get-loc))
64    (set-loc! (wrap-apply gameobj-act-set-loc!))
65    (get-name (wrap-apply gameobj-get-name))
66    (set-name! (wrap-apply gameobj-act-set-name!))
67    (get-desc (wrap-apply gameobj-get-desc))
68    (goes-by (wrap-apply gameobj-act-goes-by))
69    (visible-name (wrap-apply gameobj-visible-name))
70    (self-destruct (wrap-apply gameobj-act-self-destruct))
71    (tell (wrap-apply gameobj-tell-no-op))
72    (assist-replace (wrap-apply gameobj-act-assist-replace))))
73
74 ;;; *all* game components that talk to players should somehow
75 ;;; derive from this class.
76 ;;; And all of them need a GM!
77
78 (define-class <gameobj> (<actor>)
79   ;; location id
80   (loc #:init-value #f
81        #:getter gameobj-loc)
82   
83   ;; Uses a hash table like a set (values ignored)
84   (occupants #:init-thunk make-hash-table)
85
86   ;; game master id
87   (gm #:init-keyword #:gm
88       #:getter gameobj-gm)
89   ;; a name to be known by
90   (name #:init-keyword #:name
91         #:init-value #f)
92   (goes-by #:init-keyword #:goes-by
93            #:init-value #f)
94
95   (desc #:init-value #f
96         #:init-keyword #:desc)
97
98   ;; Commands we can handle
99   (commands #:init-value '())
100
101   ;; Commands we can handle by being something's container
102   (container-commands #:init-value '())
103
104   ;; Commands we can handle by being contained by something else
105   (contained-commands #:init-value '())
106
107   (message-handler
108    #:init-value
109    (simple-dispatcher gameobj-actions))
110
111   ;; Most objects are generally visible by default
112   (generally-visible #:init-value #t
113                      #:init-keyword #:generally-visible)
114   ;; @@: Would be preferable to be using generic methods for this...
115   ;;   Hopefully we can port this to Guile 2.2 soon...
116   (visible-to-player?
117    #:init-value (wrap-apply gameobj-visible-to-player?))
118
119   ;; Set this on self-destruct
120   ;; (checked by some "long running" game routines)
121   (destructed #:init-value #f))
122
123
124 ;;; gameobj message handlers
125 ;;; ========================
126
127 ;; Kind of a useful utility, maybe?
128 (define (simple-slot-getter slot)
129   (lambda (actor message)
130     (reply-message actor message
131                    #:val (slot-ref actor slot))))
132
133 (define (gameobj-replace-step-occupants actor replace-reply)
134   (define occupants
135     (message-ref replace-reply 'occupants #f))
136   ;; Snarf all the occupants!
137   (display "replacing occupant\n")
138   (when occupants
139     (for-each
140      (lambda (occupant)
141        (<-wait actor occupant 'set-loc!
142                #:loc (actor-id actor)))
143      occupants)))
144
145 (define gameobj-replace-steps*
146   (list gameobj-replace-step-occupants))
147
148 (define (run-replacement actor message replace-steps)
149   (define replaces (message-ref message 'replace #f))
150   (when replaces
151     (let ((replace-reply
152            (<-wait actor replaces 'assist-replace)))
153       (for-each
154        (lambda (replace-step)
155          (replace-step actor replace-reply))
156        replace-steps))))
157
158 ;; @@: This could be kind of a messy way of doing gameobj-act-init
159 ;;   stuff.  If only we had generic methods :(
160 (define-mhandler (gameobj-act-init actor message)
161   "Your most basic game object init procedure.
162 Assists in its replacement of occupants if necessary and nothing else."
163   (run-replacement actor message gameobj-replace-steps*))
164
165 (define (gameobj-goes-by gameobj)
166   "Find the name we go by.  Defaults to #:name if nothing else provided."
167   (cond ((slot-ref gameobj 'goes-by) =>
168          identity)
169         ((slot-ref gameobj 'name) =>
170          (lambda (name)
171            (list name)))
172         (else '())))
173
174 (define (gameobj-act-goes-by actor message)
175   "Reply to a message requesting what we go by."
176   (<-reply actor message
177            #:goes-by (gameobj-goes-by actor)))
178
179 (define (val-or-run val-or-proc)
180   "Evaluate if a procedure, or just return otherwise"
181   (if (procedure? val-or-proc)
182       (val-or-proc)
183       val-or-proc))
184
185 (define (filter-commands commands verb)
186   (filter
187    (lambda (cmd)
188      (equal? (command-verbs cmd)
189              verb))
190    commands))
191
192 (define-mhandler (gameobj-get-commands actor message verb)
193   "Get commands a co-occupant of the room might execute for VERB"
194   (define filtered-commands
195     (filter-commands (val-or-run (slot-ref actor 'commands))
196                      verb))
197   (<-reply actor message
198            #:commands filtered-commands
199            #:goes-by (gameobj-goes-by actor)))
200
201 (define-mhandler (gameobj-get-container-commands actor message verb)
202   "Get commands as the container / room of message's sender"
203   (define filtered-commands
204     (filter-commands (val-or-run (slot-ref actor 'container-commands))
205                      verb))
206   (<-reply actor message #:commands filtered-commands))
207
208 (define-mhandler (gameobj-get-contained-commands actor message verb)
209   "Get commands as being contained (eg inventory) of commanding gameobj"
210   (define filtered-commands
211     (filter-commands (val-or-run (slot-ref actor 'contained-commands))
212                      verb))
213   (<-reply actor message
214            #:commands filtered-commands
215            #:goes-by (gameobj-goes-by actor)))
216
217 (define-mhandler (gameobj-add-occupant! actor message who)
218   "Add an actor to our list of present occupants"
219   (hash-set! (slot-ref actor 'occupants)
220              who #t))
221
222 (define-mhandler (gameobj-remove-occupant! actor message who)
223   "Remove an occupant from the room."
224   (hash-remove! (slot-ref actor 'occupants) who))
225
226 (define* (gameobj-occupants gameobj #:key exclude)
227   (hash-fold
228    (lambda (occupant _ prev)
229      (define exclude-it?
230        (match exclude
231          ;; Empty list and #f are non-exclusion
232          (() #f)
233          (#f #f)
234          ;; A list of addresses... since our address object is (annoyingly)
235          ;; currently a simple cons cell...
236          ((exclude-1 ... exclude-rest)
237           (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
238          ;; Must be an individual address!
239          (_ (equal? occupant exclude))))
240      (if exclude-it?
241          prev
242          (cons occupant prev)))
243    '()
244    (slot-ref gameobj 'occupants)))
245
246 (define-mhandler (gameobj-get-occupants actor message)
247   "Get all present occupants of the room."
248   (define exclude (message-ref message 'exclude #f))
249   (define occupants
250     (gameobj-occupants actor #:exclude exclude))
251
252   (<-reply actor message
253            #:occupants occupants))
254
255 (define-mhandler (gameobj-act-get-loc actor message)
256   (<-reply actor message
257            #:val (slot-ref actor 'loc)))
258
259 (define (gameobj-set-loc! gameobj loc)
260   "Set the location of this object."
261   (define old-loc (gameobj-loc gameobj))
262   (format #t "DEBUG: Location set to ~s for ~s\n"
263           loc (actor-id-actor gameobj))
264
265   (slot-set! gameobj 'loc loc)
266   ;; Change registation of where we currently are
267   (if loc
268       (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
269   (if old-loc
270       (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
271
272 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
273 (define-mhandler (gameobj-act-set-loc! actor message loc)
274   "Action routine to set the location."
275   (gameobj-set-loc! actor loc))
276
277 (define (slot-ref-maybe-runcheck gameobj slot whos-asking)
278   "Do a slot-ref on gameobj, evaluating it including ourselves
279 and whos-asking, and see if we should just return it or run it."
280   (match (slot-ref gameobj slot)
281     ((? procedure? slot-val-proc)
282      (slot-val-proc gameobj whos-asking))
283     (anything-else anything-else)))
284
285 (define gameobj-get-name (simple-slot-getter 'name))
286
287 (define-mhandler (gameobj-act-set-name! actor message val)
288   (slot-set! actor 'name val))
289
290 (define-mhandler (gameobj-get-desc actor message whos-looking)
291   (define desc-text
292     (match (slot-ref actor 'desc)
293       ((? procedure? desc-proc)
294        (desc-proc actor whos-looking))
295       (desc desc)))
296   (<-reply actor message #:val desc-text))
297
298 (define (gameobj-visible-to-player? gameobj whos-looking)
299   "Check to see whether we're visible to the player or not.
300 By default, this is whether or not the generally-visible flag is set."
301   (slot-ref gameobj 'generally-visible))
302
303 (define-mhandler (gameobj-visible-name actor message whos-looking)
304   ;; Are we visible?
305   (define we-are-visible
306     ((slot-ref actor 'visible-to-player?) actor whos-looking))
307
308   (define name-to-return
309     (if we-are-visible
310         ;; Return our name
311         (match (slot-ref actor 'name)
312           ((? procedure? name-proc)
313            (name-proc actor whos-looking))
314           ((? string? name)
315            name)
316           (#f #f))
317         #f))
318   (<-reply actor message #:text name-to-return))
319
320 (define (gameobj-self-destruct gameobj)
321   "General gameobj self destruction routine"
322   ;; Unregister from being in any particular room
323   (gameobj-set-loc! gameobj #f)
324   (slot-set! gameobj 'destructed #t)
325   ;; Boom!
326   (self-destruct gameobj))
327
328 (define-mhandler (gameobj-act-self-destruct gameobj message)
329   "Action routine for self destruction"
330   (gameobj-self-destruct gameobj))
331
332 ;; Unless an actor has a tell message, we just ignore it
333 (define gameobj-tell-no-op
334   (const 'no-op))
335
336 (define (gameobj-replace-data-occupants actor)
337   "The general purpose list of replacement data"
338   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
339                                     (slot-ref actor 'occupants))))
340
341 (define (gameobj-replace-data* actor)
342   ;; For now, just call gameobj-replace-data-occupants.
343   ;; But there may be more in the future!
344   (gameobj-replace-data-occupants actor))
345
346 ;; So sad that objects must assist in their replacement ;_;
347 ;; But that's life in a live hacked game!
348 (define (gameobj-act-assist-replace actor message)
349   "Vanilla method for assisting in self-replacement for live hacking"
350   (apply <-reply actor message
351          (gameobj-replace-data* actor)))
352
353 \f
354 ;;; Utilities every gameobj has
355 ;;; ---------------------------
356
357 (define (dyn-ref gameobj special-symbol)
358   "Dynamically look up a special object from the gm"
359   (match special-symbol
360     ;; if it's a symbol, look it up dynamically
361     ((? symbol? _)
362      (message-ref
363       (<-wait gameobj (slot-ref gameobj 'gm) 'lookup-special
364               #:symbol special-symbol)
365       'val))
366     ;; if it's false, return nothing
367     ((#f #f))
368     ;; otherwise it's probably an address, return it as-is
369     (_ special-symbol)))