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 systems actors)
25 #:use-module (8sync agenda)
26 #:use-module (srfi srfi-1)
27 #:use-module (ice-9 match)
28 #:use-module (oop goops)
39 slot-ref-maybe-runcheck
48 ;;; Actions supported by all gameobj
49 (define gameobj-actions
51 (init (wrap-apply gameobj-init))
52 ;; Commands for co-occupants
53 (get-commands (wrap-apply gameobj-get-commands))
54 ;; Commands for participants in a room
55 (get-container-commands (wrap-apply gameobj-get-container-commands))
56 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
57 (get-contained-commands (wrap-apply gameobj-get-contained-commands))
58 (get-occupants (wrap-apply gameobj-get-occupants))
59 (add-occupant! (wrap-apply gameobj-add-occupant!))
60 (remove-occupant! (wrap-apply gameobj-remove-occupant!))
61 (get-loc (wrap-apply gameobj-act-get-loc))
62 (set-loc! (wrap-apply gameobj-act-set-loc!))
63 (get-name (wrap-apply gameobj-get-name))
64 (set-name! (wrap-apply gameobj-act-set-name!))
65 (get-desc (wrap-apply gameobj-get-desc))
66 (goes-by (wrap-apply gameobj-act-goes-by))
67 (visible-name (wrap-apply gameobj-visible-name))
68 (self-destruct (wrap-apply gameobj-act-self-destruct))
69 (tell (wrap-apply gameobj-tell-no-op))
70 (assist-replace (wrap-apply gameobj-act-assist-replace))))
72 ;;; *all* game components that talk to players should somehow
73 ;;; derive from this class.
74 ;;; And all of them need a GM!
76 (define-class <gameobj> (<actor>)
81 ;; Uses a hash table like a set (values ignored)
82 (occupants #:init-thunk make-hash-table)
85 (gm #:init-keyword #:gm
87 ;; a name to be known by
88 (name #:init-keyword #:name
90 (goes-by #:init-keyword #:goes-by
94 #:init-keyword #:desc)
96 ;; Commands we can handle
97 (commands #:init-value '())
99 ;; Commands we can handle by being something's container
100 (container-commands #:init-value '())
102 ;; Commands we can handle by being contained by something else
103 (contained-commands #:init-value '())
107 (simple-dispatcher gameobj-actions))
109 ;; Most objects are generally visible by default
110 (generally-visible #:init-value #t
111 #:init-keyword #:generally-visible)
112 ;; @@: Would be preferable to be using generic methods for this...
113 ;; Hopefully we can port this to Guile 2.2 soon...
115 #:init-value (wrap-apply gameobj-visible-to-player?)))
118 ;;; gameobj message handlers
119 ;;; ========================
121 ;; Kind of a useful utility, maybe?
122 (define (simple-slot-getter slot)
123 (lambda (actor message)
124 (reply-message actor message
125 #:val (slot-ref actor slot))))
127 (define (gameobj-replace-step-occupants actor replace-reply)
129 (message-ref replace-reply 'occupants #f))
130 ;; Snarf all the occupants!
131 (display "replacing occupant\n")
135 (<-wait actor occupant 'set-loc!
136 #:loc (actor-id actor)))
139 (define gameobj-replace-steps*
140 (list gameobj-replace-step-occupants))
142 (define (run-replacement actor message replace-steps)
143 (define replaces (message-ref message 'replace #f))
146 (<-wait actor replaces 'assist-replace)))
148 (lambda (replace-step)
149 (replace-step actor replace-reply))
152 ;; @@: This could be kind of a messy way of doing gameobj-init
153 ;; stuff. If only we had generic methods :(
154 (define-mhandler (gameobj-init actor message)
155 "Your most basic game object init procedure.
156 Assists in its replacement of occupants if necessary and nothing else."
157 (run-replacement actor message gameobj-replace-steps*))
159 (define (gameobj-goes-by gameobj)
160 "Find the name we go by. Defaults to #:name if nothing else provided."
161 (cond ((slot-ref gameobj 'goes-by) =>
163 ((slot-ref gameobj 'name) =>
168 (define (gameobj-act-goes-by actor message)
169 "Reply to a message requesting what we go by."
170 (<-reply actor message
171 #:goes-by (gameobj-goes-by actor)))
173 (define (val-or-run val-or-proc)
174 "Evaluate if a procedure, or just return otherwise"
175 (if (procedure? val-or-proc)
179 (define (filter-commands commands verb)
182 (equal? (command-verbs cmd)
186 (define-mhandler (gameobj-get-commands actor message verb)
187 "Get commands a co-occupant of the room might execute for VERB"
188 (define filtered-commands
189 (filter-commands (val-or-run (slot-ref actor 'commands))
191 (<-reply actor message
192 #:commands filtered-commands
193 #:goes-by (gameobj-goes-by actor)))
195 (define-mhandler (gameobj-get-container-commands actor message verb)
196 "Get commands as the container / room of message's sender"
197 (define filtered-commands
198 (filter-commands (val-or-run (slot-ref actor 'container-commands))
200 (<-reply actor message #:commands filtered-commands))
202 (define-mhandler (gameobj-get-contained-commands actor message verb)
203 "Get commands as being contained (eg inventory) of commanding gameobj"
204 (define filtered-commands
205 (filter-commands (val-or-run (slot-ref actor 'contained-commands))
207 (<-reply actor message
208 #:commands filtered-commands
209 #:goes-by (gameobj-goes-by actor)))
211 (define-mhandler (gameobj-add-occupant! actor message who)
212 "Add an actor to our list of present occupants"
213 (hash-set! (slot-ref actor 'occupants)
216 (define-mhandler (gameobj-remove-occupant! actor message who)
217 "Remove an occupant from the room."
218 (hash-remove! (slot-ref actor 'occupants) who))
220 (define* (gameobj-occupants gameobj #:key exclude)
222 (lambda (occupant _ prev)
225 ;; Empty list and #f are non-exclusion
228 ;; A list of addresses... since our address object is (annoyingly)
229 ;; currently a simple cons cell...
230 ((exclude-1 ... exclude-rest)
231 (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
232 ;; Must be an individual address!
233 (_ (equal? occupant exclude))))
236 (cons occupant prev)))
238 (slot-ref gameobj 'occupants)))
240 (define-mhandler (gameobj-get-occupants actor message)
241 "Get all present occupants of the room."
242 (define exclude (message-ref message 'exclude #f))
244 (gameobj-occupants actor #:exclude exclude))
246 (<-reply actor message
247 #:occupants occupants))
249 (define-mhandler (gameobj-act-get-loc actor message)
250 (<-reply actor message
251 #:val (slot-ref actor 'loc)))
253 (define (gameobj-set-loc! gameobj loc)
254 "Set the location of this object."
255 (define old-loc (gameobj-loc gameobj))
256 (format #t "DEBUG: Location set to ~s for ~s\n"
257 loc (actor-id-actor gameobj))
259 (slot-set! gameobj 'loc loc)
260 ;; Change registation of where we currently are
262 (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
264 (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
266 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
267 (define-mhandler (gameobj-act-set-loc! actor message loc)
268 "Action routine to set the location."
269 (gameobj-set-loc! actor loc))
271 (define (slot-ref-maybe-runcheck gameobj slot whos-asking)
272 "Do a slot-ref on gameobj, evaluating it including ourselves
273 and whos-asking, and see if we should just return it or run it."
274 (match (slot-ref gameobj slot)
275 ((? procedure? slot-val-proc)
276 (slot-val-proc gameobj whos-asking))
277 (anything-else anything-else)))
279 (define gameobj-get-name (simple-slot-getter 'name))
281 (define-mhandler (gameobj-act-set-name! actor message val)
282 (slot-set! actor 'name val))
284 (define-mhandler (gameobj-get-desc actor message whos-looking)
286 (match (slot-ref actor 'desc)
287 ((? procedure? desc-proc)
288 (desc-proc actor whos-looking))
290 (<-reply actor message #:val desc-text))
292 (define (gameobj-visible-to-player? gameobj whos-looking)
293 "Check to see whether we're visible to the player or not.
294 By default, this is whether or not the generally-visible flag is set."
295 (slot-ref gameobj 'generally-visible))
297 (define-mhandler (gameobj-visible-name actor message whos-looking)
299 (define we-are-visible
300 ((slot-ref actor 'visible-to-player?) actor whos-looking))
302 (define name-to-return
305 (match (slot-ref actor 'name)
306 ((? procedure? name-proc)
307 (name-proc actor whos-looking))
312 (<-reply actor message #:text name-to-return))
314 (define (gameobj-self-destruct gameobj)
315 "General gameobj self destruction routine"
316 ;; Unregister from being in any particular room
317 (gameobj-set-loc! gameobj #f)
319 (self-destruct gameobj))
321 (define-mhandler (gameobj-act-self-destruct gameobj message)
322 "Action routine for self destruction"
323 (gameobj-self-destruct gameobj))
325 ;; Unless an actor has a tell message, we just ignore it
326 (define gameobj-tell-no-op
329 (define (gameobj-replace-data-occupants actor)
330 "The general purpose list of replacement data"
331 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
332 (slot-ref actor 'occupants))))
334 (define (gameobj-replace-data* actor)
335 ;; For now, just call gameobj-replace-data-occupants.
336 ;; But there may be more in the future!
337 (gameobj-replace-data-occupants actor))
339 ;; So sad that objects must assist in their replacement ;_;
340 ;; But that's life in a live hacked game!
341 (define (gameobj-act-assist-replace actor message)
342 "Vanilla method for assisting in self-replacement for live hacking"
343 (apply <-reply actor message
344 (gameobj-replace-data* actor)))
347 ;;; Utilities every gameobj has
348 ;;; ---------------------------
350 (define (dyn-ref gameobj special-symbol)
351 "Dynamically look up a special object from the gm"
352 (match special-symbol
353 ;; if it's a symbol, look it up dynamically
356 (<-wait gameobj (slot-ref gameobj 'gm) 'lookup-special
357 #:symbol special-symbol)
359 ;; if it's false, return nothing
361 ;; otherwise it's probably an address, return it as-is