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)
44 ;;; Actions supported by all gameobj
45 (define gameobj-actions
47 (init (wrap-apply gameobj-init))
48 (get-commands (wrap-apply gameobj-get-commands))
49 (get-container-commands (wrap-apply gameobj-get-container-commands))
50 (get-occupants (wrap-apply gameobj-get-occupants))
51 (add-occupant! (wrap-apply gameobj-add-occupant!))
52 (remove-occupant! (wrap-apply gameobj-remove-occupant!))
53 (set-loc! (wrap-apply gameobj-act-set-loc!))
54 (get-name (wrap-apply gameobj-get-name))
55 (set-name! (wrap-apply gameobj-act-set-name!))
56 (get-desc (wrap-apply gameobj-get-desc))
57 (goes-by (wrap-apply gameobj-act-goes-by))
58 (visible-name (wrap-apply gameobj-visible-name))
59 (self-destruct (wrap-apply gameobj-act-self-destruct))
60 (tell (wrap-apply gameobj-tell-no-op))
61 (assist-replace (wrap-apply gameobj-act-assist-replace))))
63 ;;; *all* game components that talk to players should somehow
64 ;;; derive from this class.
65 ;;; And all of them need a GM!
67 (define-class <gameobj> (<actor>)
72 ;; Uses a hash table like a set (values ignored)
73 (occupants #:init-thunk make-hash-table)
76 (gm #:init-keyword #:gm
78 ;; a name to be known by
79 (name #:init-keyword #:name
81 (goes-by #:init-keyword #:goes-by
85 #:init-keyword #:desc)
87 ;; Commands we can handle
88 (commands #:init-value '())
90 ;; Commands we can handle by being something's container
91 (container-commands #:init-value '())
94 (simple-dispatcher gameobj-actions))
96 ;; Most objects are generally visible by default
97 (generally-visible #:init-value #t
98 #:init-keyword #:generally-visible)
99 ;; @@: Would be preferable to be using generic methods for this...
100 ;; Hopefully we can port this to Guile 2.2 soon...
102 #:init-value (wrap-apply gameobj-visible-to-player?)))
105 ;;; gameobj message handlers
106 ;;; ========================
108 ;; Kind of a useful utility, maybe?
109 (define (simple-slot-getter slot)
110 (lambda (actor message)
111 (reply-message actor message
112 #:val (slot-ref actor slot))))
115 (define (gameobj-replace-step-occupants actor replace-reply)
117 (message-ref replace-reply 'occupants #f))
118 ;; Snarf all the occupants!
119 (display "replacing occupant\n")
123 (<-wait actor occupant 'set-loc!
124 #:loc (actor-id actor)))
127 (define gameobj-replace-steps*
128 (list gameobj-replace-step-occupants))
130 (define (run-replacement actor message replace-steps)
131 (define replaces (message-ref message 'replace #f))
134 (<-wait actor replaces 'assist-replace)))
136 (lambda (replace-step)
137 (replace-step actor replace-reply))
140 ;; @@: This could be kind of a messy way of doing gameobj-init
141 ;; stuff. If only we had generic methods :(
142 (define-mhandler (gameobj-init actor message)
143 "Your most basic game object init procedure.
144 Assists in its replacement of occupants if necessary and nothing else."
145 (run-replacement actor message gameobj-replace-steps*))
147 (define (gameobj-goes-by gameobj)
148 "Find the name we go by. Defaults to #:name if nothing else provided."
149 (cond ((slot-ref gameobj 'goes-by) =>
151 ((slot-ref gameobj 'name) =>
156 (define (gameobj-act-goes-by actor message)
157 "Reply to a message requesting what we go by."
158 (<-reply actor message
159 #:goes-by (gameobj-goes-by actor)))
161 (define (val-or-run val-or-proc)
162 "Evaluate if a procedure, or just return otherwise"
163 (if (procedure? val-or-proc)
167 (define (filter-commands commands verb)
170 (equal? (command-verbs cmd)
174 (define-mhandler (gameobj-get-commands actor message verb)
175 "Get commands a co-occupant of the room might execute for VERB"
176 (define filtered-commands
177 (filter-commands (val-or-run (slot-ref actor 'commands))
179 (<-reply actor message
180 #:commands filtered-commands
181 #:goes-by (gameobj-goes-by actor)))
183 (define-mhandler (gameobj-get-container-commands actor message verb)
184 "Get commands as the container / room of message's sender"
185 (define filtered-commands
186 (filter-commands (val-or-run (slot-ref actor 'container-commands))
188 (<-reply actor message #:commands filtered-commands))
190 (define-mhandler (gameobj-add-occupant! actor message who)
191 "Add an actor to our list of present occupants"
192 (hash-set! (slot-ref actor 'occupants)
195 (define-mhandler (gameobj-remove-occupant! actor message who)
196 "Remove an occupant from the room."
197 (hash-remove! (slot-ref actor 'occupants) who))
199 (define* (gameobj-occupants gameobj #:key exclude)
201 (lambda (occupant _ prev)
204 ;; Empty list and #f are non-exclusion
207 ;; A list of addresses... since our address object is (annoyingly)
208 ;; currently a simple cons cell...
209 ((exclude-1 ... exclude-rest)
210 (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
211 ;; Must be an individual address!
212 (_ (equal? occupant exclude))))
215 (cons occupant prev)))
217 (slot-ref gameobj 'occupants)))
219 (define-mhandler (gameobj-get-occupants actor message)
220 "Get all present occupants of the room."
221 (define exclude (message-ref message 'exclude #f))
223 (gameobj-occupants actor #:exclude exclude))
225 (<-reply actor message
226 #:occupants occupants))
228 (define (gameobj-set-loc! gameobj loc)
229 "Set the location of this object."
230 (define old-loc (gameobj-loc gameobj))
231 (format #t "DEBUG: Location set to ~s for ~s\n"
232 loc (actor-id-actor gameobj))
234 (slot-set! gameobj 'loc loc)
235 ;; Change registation of where we currently are
237 (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
239 (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
241 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
242 (define-mhandler (gameobj-act-set-loc! actor message loc)
243 "Action routine to set the location."
244 (gameobj-set-loc! actor loc))
246 (define gameobj-get-name (simple-slot-getter 'name))
248 (define-mhandler (gameobj-act-set-name! actor message val)
249 (slot-set! actor 'name val))
251 (define-mhandler (gameobj-get-desc actor message whos-looking)
253 (match (slot-ref actor 'desc)
254 ((? procedure? desc-proc)
255 (desc-proc actor whos-looking))
257 (<-reply actor message #:val desc-text))
259 (define (gameobj-visible-to-player? gameobj whos-looking)
260 "Check to see whether we're visible to the player or not.
261 By default, this is whether or not the generally-visible flag is set."
262 (slot-ref gameobj 'generally-visible))
264 (define-mhandler (gameobj-visible-name actor message whos-looking)
266 (define we-are-visible
267 ((slot-ref actor 'visible-to-player?) actor whos-looking))
269 (define name-to-return
272 (match (slot-ref actor 'name)
273 ((? procedure? name-proc)
274 (name-proc actor whos-looking))
279 (<-reply actor message #:text name-to-return))
281 (define (gameobj-self-destruct gameobj)
282 "General gameobj self destruction routine"
283 ;; Unregister from being in any particular room
284 (gameobj-set-loc! gameobj #f)
286 (self-destruct gameobj))
288 (define-mhandler (gameobj-act-self-destruct gameobj message)
289 "Action routine for self destruction"
290 (gameobj-self-destruct gameobj))
292 ;; Unless an actor has a tell message, we just ignore it
293 (define gameobj-tell-no-op
296 (define (gameobj-replace-data-occupants actor)
297 "The general purpose list of replacement data"
298 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
299 (slot-ref actor 'occupants))))
301 (define (gameobj-replace-data* actor)
302 ;; For now, just call gameobj-replace-data-occupants.
303 ;; But there may be more in the future!
304 (gameobj-replace-data-occupants actor))
306 ;; So sad that objects must assist in their replacement ;_;
307 ;; But that's life in a live hacked game!
308 (define (gameobj-act-assist-replace actor message)
309 "Vanilla method for assisting in self-replacement for live hacking"
310 (apply <-reply actor message
311 (gameobj-replace-data* actor)))
314 ;;; Utilities every gameobj has
315 ;;; ---------------------------
317 (define (dyn-ref gameobj special-symbol)
318 "Dynamically look up a special object from the gm"
319 (match special-symbol
320 ;; if it's a symbol, look it up dynamically
323 (<-wait gameobj (slot-ref gameobj 'gm) 'lookup-special
324 #:symbol special-symbol)
326 ;; if it's false, return nothing
328 ;; otherwise it's probably an address, return it as-is