1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
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.
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.
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/>.
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)
40 slot-ref-maybe-runcheck
49 ;;; *all* game components that talk to players should somehow
50 ;;; derive from this class.
51 ;;; And all of them need a GM!
53 (define-class <gameobj> (<actor>)
58 ;; Uses a hash table like a set (values ignored)
59 (occupants #:init-thunk make-hash-table)
62 (gm #:init-keyword #:gm
64 ;; a name to be known by
65 (name #:init-keyword #:name
67 (goes-by #:init-keyword #:goes-by
71 #:init-keyword #:desc)
73 ;; Commands we can handle
74 (commands #:init-value '())
76 ;; Commands we can handle by being something's container
77 (container-commands #:init-value '())
79 ;; Commands we can handle by being contained by something else
80 (contained-commands #:init-value '())
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...
88 #:init-value (wrap-apply gameobj-visible-to-player?))
90 ;; Set this on self-destruct
91 ;; (checked by some "long running" game routines)
92 (destructed #:init-value #f)
94 (actions #:allocation #:each-subclass
95 ;;; Actions supported by all gameobj
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))))
120 ;;; gameobj message handlers
121 ;;; ========================
123 ;; Kind of a useful utility, maybe?
124 (define (simple-slot-getter slot)
125 (lambda (actor message)
126 (<-reply message (slot-ref actor slot))))
128 (define (gameobj-replace-step-occupants actor occupants)
129 ;; Snarf all the occupants!
130 (display "replacing occupant\n")
134 (<-wait occupant 'set-loc!
135 #:loc (actor-id actor)))
138 (define gameobj-replace-steps*
139 (list gameobj-replace-step-occupants))
141 (define (run-replacement actor replaces replace-steps)
143 (mbody-receive (_ #:key occupants)
144 (<-wait replaces 'assist-replace)
146 (lambda (replace-step)
147 (replace-step actor occupants))
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*))
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) =>
161 ((slot-ref gameobj 'name) =>
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)))
170 (define (val-or-run val-or-proc)
171 "Evaluate if a procedure, or just return otherwise"
172 (if (procedure? val-or-proc)
176 (define (filter-commands commands verb)
179 (equal? (command-verbs cmd)
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))
189 #:commands filtered-commands
190 #:goes-by (gameobj-goes-by actor)))
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))
197 (<-reply message #:commands filtered-commands))
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))
205 #:commands filtered-commands
206 #:goes-by (gameobj-goes-by actor)))
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)
213 (define* (gameobj-remove-occupant! actor message #:key who)
214 "Remove an occupant from the room."
215 (hash-remove! (slot-ref actor 'occupants) who))
217 (define* (gameobj-occupants gameobj #:key exclude)
219 (lambda (occupant _ prev)
222 ;; Empty list and #f are non-exclusion
225 ;; A list of addresses... since our address object is (annoyingly)
226 ;; currently a simple cons cell...
227 ((exclude-1 ... exclude-rest)
228 (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
229 ;; Must be an individual address!
230 (_ (equal? occupant exclude))))
233 (cons occupant prev)))
235 (slot-ref gameobj 'occupants)))
237 (define* (gameobj-get-occupants actor message #:key exclude)
238 "Get all present occupants of the room."
240 (gameobj-occupants actor #:exclude exclude))
242 (<-reply message #:occupants occupants))
244 (define (gameobj-act-get-loc actor message)
245 (<-reply message (slot-ref actor 'loc)))
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))
253 (when (not (equal? old-loc loc))
254 (slot-set! gameobj 'loc loc)
255 ;; Change registation of where we currently are
257 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
259 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
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))
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)))
274 (define gameobj-get-name (simple-slot-getter 'name))
276 (define* (gameobj-act-set-name! actor message val)
277 (slot-set! actor 'name val))
279 (define* (gameobj-get-desc actor message #:key whos-looking)
281 (match (slot-ref actor 'desc)
282 ((? procedure? desc-proc)
283 (desc-proc actor whos-looking))
285 (<-reply message desc-text))
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))
292 (define* (gameobj-visible-name actor message #:key whos-looking)
294 (define we-are-visible
295 ((slot-ref actor 'visible-to-player?) actor whos-looking))
297 (define name-to-return
300 (match (slot-ref actor 'name)
301 ((? procedure? name-proc)
302 (name-proc actor whos-looking))
307 (<-reply message #:text name-to-return))
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)
315 (self-destruct gameobj))
317 (define (gameobj-act-self-destruct gameobj message)
318 "Action routine for self destruction"
319 (gameobj-self-destruct gameobj))
321 ;; Unless an actor has a tell message, we just ignore it
322 (define gameobj-tell-no-op
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))))
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))
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)))
343 ;;; Utilities every gameobj has
344 ;;; ---------------------------
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
351 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
352 #:symbol special-symbol)))
353 ;; if it's false, return nothing
355 ;; otherwise it's probably an address, return it as-is