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