Use simple comma-join when listing items in a room.
[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 (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            (build-actions
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 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 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     (mbody-receive (_ #:key occupants)
144         (<-wait 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 message #:goes-by (gameobj-goes-by actor)))
169
170 (define (val-or-run val-or-proc)
171   "Evaluate if a procedure, or just return otherwise"
172   (if (procedure? val-or-proc)
173       (val-or-proc)
174       val-or-proc))
175
176 (define (filter-commands commands verb)
177   (filter
178    (lambda (cmd)
179      (equal? (command-verbs cmd)
180              verb))
181    commands))
182
183 (define* (gameobj-get-commands actor message #:key verb)
184   "Get commands a co-occupant of the room might execute for VERB"
185   (define filtered-commands
186     (filter-commands (val-or-run (slot-ref actor 'commands))
187                      verb))
188   (<-reply message
189            #:commands filtered-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 filtered-commands
195     (filter-commands (val-or-run (slot-ref actor 'container-commands))
196                      verb))
197   (<-reply message #:commands filtered-commands))
198
199 (define* (gameobj-get-contained-commands actor message #:key verb)
200   "Get commands as being contained (eg inventory) of commanding gameobj"
201   (define filtered-commands
202     (filter-commands (val-or-run (slot-ref actor 'contained-commands))
203                      verb))
204   (<-reply message
205            #:commands filtered-commands
206            #:goes-by (gameobj-goes-by actor)))
207
208 (define* (gameobj-add-occupant! actor message #:key who)
209   "Add an actor to our list of present occupants"
210   (hash-set! (slot-ref actor 'occupants)
211              who #t))
212
213 (define* (gameobj-remove-occupant! actor message #:key who)
214   "Remove an occupant from the room."
215   (hash-remove! (slot-ref actor 'occupants) who))
216
217 (define* (gameobj-occupants gameobj #:key exclude)
218   (hash-fold
219    (lambda (occupant _ prev)
220      (define exclude-it?
221        (match exclude
222          ;; Empty list and #f are non-exclusion
223          (() #f)
224          (#f #f)
225          ;; A list of addresses... since our address object is (annoyingly)
226          ;; currently a simple cons cell...
227          ((exclude-1 ... exclude-rest)
228           (member occupant exclude))
229          ;; Must be an individual address!
230          (_ (equal? occupant exclude))))
231      (if exclude-it?
232          prev
233          (cons occupant prev)))
234    '()
235    (slot-ref gameobj 'occupants)))
236
237 (define* (gameobj-get-occupants actor message #:key exclude)
238   "Get all present occupants of the room."
239   (define occupants
240     (gameobj-occupants actor #:exclude exclude))
241
242   (<-reply message #:occupants occupants))
243
244 (define (gameobj-act-get-loc actor message)
245   (<-reply message (slot-ref actor 'loc)))
246
247 (define (gameobj-set-loc! gameobj loc)
248   "Set the location of this object."
249   (define old-loc (gameobj-loc gameobj))
250   (format #t "DEBUG: Location set to ~s for ~s\n"
251           loc (actor-id-actor gameobj))
252
253   (when (not (equal? old-loc loc))
254     (slot-set! gameobj 'loc loc)
255     ;; Change registation of where we currently are
256     (if old-loc
257         (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
258     (if loc
259         (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
260
261 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
262 (define* (gameobj-act-set-loc! actor message #:key loc)
263   "Action routine to set the location."
264   (gameobj-set-loc! actor loc))
265
266 (define (slot-ref-maybe-runcheck gameobj slot whos-asking)
267   "Do a slot-ref on gameobj, evaluating it including ourselves
268 and whos-asking, and see if we should just return it or run it."
269   (match (slot-ref gameobj slot)
270     ((? procedure? slot-val-proc)
271      (slot-val-proc gameobj whos-asking))
272     (anything-else anything-else)))
273
274 (define gameobj-get-name (simple-slot-getter 'name))
275
276 (define* (gameobj-act-set-name! actor message val)
277   (slot-set! actor 'name val))
278
279 (define* (gameobj-get-desc actor message #:key whos-looking)
280   (define desc-text
281     (match (slot-ref actor 'desc)
282       ((? procedure? desc-proc)
283        (desc-proc actor whos-looking))
284       (desc desc)))
285   (<-reply message desc-text))
286
287 (define (gameobj-visible-to-player? gameobj whos-looking)
288   "Check to see whether we're visible to the player or not.
289 By default, this is whether or not the generally-visible flag is set."
290   (slot-ref gameobj 'generally-visible))
291
292 (define* (gameobj-visible-name actor message #:key whos-looking)
293   ;; Are we visible?
294   (define we-are-visible
295     ((slot-ref actor 'visible-to-player?) actor whos-looking))
296
297   (define name-to-return
298     (if we-are-visible
299         ;; Return our name
300         (match (slot-ref actor 'name)
301           ((? procedure? name-proc)
302            (name-proc actor whos-looking))
303           ((? string? name)
304            name)
305           (#f #f))
306         #f))
307   (<-reply message #:text name-to-return))
308
309 (define (gameobj-self-destruct gameobj)
310   "General gameobj self destruction routine"
311   ;; Unregister from being in any particular room
312   (gameobj-set-loc! gameobj #f)
313   (slot-set! gameobj 'destructed #t)
314   ;; Boom!
315   (self-destruct gameobj))
316
317 (define (gameobj-act-self-destruct gameobj message)
318   "Action routine for self destruction"
319   (gameobj-self-destruct gameobj))
320
321 ;; Unless an actor has a tell message, we just ignore it
322 (define gameobj-tell-no-op
323   (const 'no-op))
324
325 (define (gameobj-replace-data-occupants actor)
326   "The general purpose list of replacement data"
327   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
328                                     (slot-ref actor 'occupants))))
329
330 (define (gameobj-replace-data* actor)
331   ;; For now, just call gameobj-replace-data-occupants.
332   ;; But there may be more in the future!
333   (gameobj-replace-data-occupants actor))
334
335 ;; So sad that objects must assist in their replacement ;_;
336 ;; But that's life in a live hacked game!
337 (define (gameobj-act-assist-replace actor message)
338   "Vanilla method for assisting in self-replacement for live hacking"
339   (apply <-reply message
340          (gameobj-replace-data* actor)))
341
342 \f
343 ;;; Utilities every gameobj has
344 ;;; ---------------------------
345
346 (define (dyn-ref gameobj special-symbol)
347   "Dynamically look up a special object from the gm"
348   (match special-symbol
349     ;; if it's a symbol, look it up dynamically
350     ((? symbol? _)
351      (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
352                         #:symbol special-symbol)))
353     ;; if it's false, return nothing
354     (#f #f)
355     ;; otherwise it's probably an address, return it as-is
356     (_ special-symbol)))