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