Port to the remove-define-mhandler 8sync branch
[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 actor message
131                    #:val (slot-ref actor slot))))
132
133 (define (gameobj-replace-step-occupants actor occupants)
134   ;; Snarf all the occupants!
135   (display "replacing occupant\n")
136   (when occupants
137     (for-each
138      (lambda (occupant)
139        (<-wait actor occupant 'set-loc!
140                #:loc (actor-id actor)))
141      occupants)))
142
143 (define gameobj-replace-steps*
144   (list gameobj-replace-step-occupants))
145
146 (define (run-replacement actor replaces replace-steps)
147   (when replaces
148     (msg-receive (_ #:key occupants)
149         (<-wait actor replaces 'assist-replace)
150       (for-each
151        (lambda (replace-step)
152          (replace-step actor occupants))
153        replace-steps))))
154
155 ;; @@: This could be kind of a messy way of doing gameobj-act-init
156 ;;   stuff.  If only we had generic methods :(
157 (define* (gameobj-act-init actor message #:key replace)
158   "Your most basic game object init procedure.
159 Assists in its replacement of occupants if necessary and nothing else."
160   (run-replacement actor replace gameobj-replace-steps*))
161
162 (define (gameobj-goes-by gameobj)
163   "Find the name we go by.  Defaults to #:name if nothing else provided."
164   (cond ((slot-ref gameobj 'goes-by) =>
165          identity)
166         ((slot-ref gameobj 'name) =>
167          (lambda (name)
168            (list name)))
169         (else '())))
170
171 (define (gameobj-act-goes-by actor message)
172   "Reply to a message requesting what we go by."
173   (<-reply actor message
174            #:goes-by (gameobj-goes-by actor)))
175
176 (define (val-or-run val-or-proc)
177   "Evaluate if a procedure, or just return otherwise"
178   (if (procedure? val-or-proc)
179       (val-or-proc)
180       val-or-proc))
181
182 (define (filter-commands commands verb)
183   (filter
184    (lambda (cmd)
185      (equal? (command-verbs cmd)
186              verb))
187    commands))
188
189 (define* (gameobj-get-commands actor message #:key verb)
190   "Get commands a co-occupant of the room might execute for VERB"
191   (define filtered-commands
192     (filter-commands (val-or-run (slot-ref actor 'commands))
193                      verb))
194   (<-reply actor message
195            #:commands filtered-commands
196            #:goes-by (gameobj-goes-by actor)))
197
198 (define* (gameobj-get-container-commands actor message #:key verb)
199   "Get commands as the container / room of message's sender"
200   (define filtered-commands
201     (filter-commands (val-or-run (slot-ref actor 'container-commands))
202                      verb))
203   (<-reply actor message #:commands filtered-commands))
204
205 (define* (gameobj-get-contained-commands actor message #:key verb)
206   "Get commands as being contained (eg inventory) of commanding gameobj"
207   (define filtered-commands
208     (filter-commands (val-or-run (slot-ref actor 'contained-commands))
209                      verb))
210   (<-reply actor message
211            #:commands filtered-commands
212            #:goes-by (gameobj-goes-by actor)))
213
214 (define* (gameobj-add-occupant! actor message #:key who)
215   "Add an actor to our list of present occupants"
216   (hash-set! (slot-ref actor 'occupants)
217              who #t))
218
219 (define* (gameobj-remove-occupant! actor message #:key who)
220   "Remove an occupant from the room."
221   (hash-remove! (slot-ref actor 'occupants) who))
222
223 (define* (gameobj-occupants gameobj #:key exclude)
224   (hash-fold
225    (lambda (occupant _ prev)
226      (define exclude-it?
227        (match exclude
228          ;; Empty list and #f are non-exclusion
229          (() #f)
230          (#f #f)
231          ;; A list of addresses... since our address object is (annoyingly)
232          ;; currently a simple cons cell...
233          ((exclude-1 ... exclude-rest)
234           (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
235          ;; Must be an individual address!
236          (_ (equal? occupant exclude))))
237      (if exclude-it?
238          prev
239          (cons occupant prev)))
240    '()
241    (slot-ref gameobj 'occupants)))
242
243 (define* (gameobj-get-occupants actor message #:key exclude)
244   "Get all present occupants of the room."
245   (define occupants
246     (gameobj-occupants actor #:exclude exclude))
247
248   (<-reply actor message
249            #:occupants occupants))
250
251 (define (gameobj-act-get-loc actor message)
252   (<-reply actor message
253            #:val (slot-ref actor 'loc)))
254
255 (define (gameobj-set-loc! gameobj loc)
256   "Set the location of this object."
257   (define old-loc (gameobj-loc gameobj))
258   (format #t "DEBUG: Location set to ~s for ~s\n"
259           loc (actor-id-actor gameobj))
260
261   (when (not (equal? old-loc loc))
262     (slot-set! gameobj 'loc loc)
263     ;; Change registation of where we currently are
264     (if old-loc
265         (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj)))
266     (if loc
267         (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))))
268
269 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
270 (define* (gameobj-act-set-loc! actor message #:key loc)
271   "Action routine to set the location."
272   (gameobj-set-loc! actor loc))
273
274 (define (slot-ref-maybe-runcheck gameobj slot whos-asking)
275   "Do a slot-ref on gameobj, evaluating it including ourselves
276 and whos-asking, and see if we should just return it or run it."
277   (match (slot-ref gameobj slot)
278     ((? procedure? slot-val-proc)
279      (slot-val-proc gameobj whos-asking))
280     (anything-else anything-else)))
281
282 (define gameobj-get-name (simple-slot-getter 'name))
283
284 (define* (gameobj-act-set-name! actor message #:key val)
285   (slot-set! actor 'name val))
286
287 (define* (gameobj-get-desc actor message #:key whos-looking)
288   (define desc-text
289     (match (slot-ref actor 'desc)
290       ((? procedure? desc-proc)
291        (desc-proc actor whos-looking))
292       (desc desc)))
293   (<-reply actor message #:val desc-text))
294
295 (define (gameobj-visible-to-player? gameobj whos-looking)
296   "Check to see whether we're visible to the player or not.
297 By default, this is whether or not the generally-visible flag is set."
298   (slot-ref gameobj 'generally-visible))
299
300 (define* (gameobj-visible-name actor message #:key whos-looking)
301   ;; Are we visible?
302   (define we-are-visible
303     ((slot-ref actor 'visible-to-player?) actor whos-looking))
304
305   (define name-to-return
306     (if we-are-visible
307         ;; Return our name
308         (match (slot-ref actor 'name)
309           ((? procedure? name-proc)
310            (name-proc actor whos-looking))
311           ((? string? name)
312            name)
313           (#f #f))
314         #f))
315   (<-reply actor message #:text name-to-return))
316
317 (define (gameobj-self-destruct gameobj)
318   "General gameobj self destruction routine"
319   ;; Unregister from being in any particular room
320   (gameobj-set-loc! gameobj #f)
321   (slot-set! gameobj 'destructed #t)
322   ;; Boom!
323   (self-destruct gameobj))
324
325 (define (gameobj-act-self-destruct gameobj message)
326   "Action routine for self destruction"
327   (gameobj-self-destruct gameobj))
328
329 ;; Unless an actor has a tell message, we just ignore it
330 (define gameobj-tell-no-op
331   (const 'no-op))
332
333 (define (gameobj-replace-data-occupants actor)
334   "The general purpose list of replacement data"
335   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
336                                     (slot-ref actor 'occupants))))
337
338 (define (gameobj-replace-data* actor)
339   ;; For now, just call gameobj-replace-data-occupants.
340   ;; But there may be more in the future!
341   (gameobj-replace-data-occupants actor))
342
343 ;; So sad that objects must assist in their replacement ;_;
344 ;; But that's life in a live hacked game!
345 (define (gameobj-act-assist-replace actor message)
346   "Vanilla method for assisting in self-replacement for live hacking"
347   (apply <-reply actor message
348          (gameobj-replace-data* actor)))
349
350 \f
351 ;;; Utilities every gameobj has
352 ;;; ---------------------------
353
354 (define (dyn-ref gameobj special-symbol)
355   "Dynamically look up a special object from the gm"
356   (match special-symbol
357     ;; if it's a symbol, look it up dynamically
358     ((? symbol? _)
359      (msg-receive (_ #:key val)
360          (<-wait gameobj (slot-ref gameobj 'gm) 'lookup-special
361                  #:symbol special-symbol)
362        val))
363     ;; if it's false, return nothing
364     (#f #f)
365     ;; otherwise it's probably an address, return it as-is
366     (_ special-symbol)))