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 (pk 'replace (message-ref message 'replace #f)))
134 (pk 'replace-reply (<-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 (display "gameobj init!\n")
146 (run-replacement actor message gameobj-replace-steps*))
148 (define (gameobj-goes-by gameobj)
149 "Find the name we go by. Defaults to #:name if nothing else provided."
150 (cond ((slot-ref gameobj 'goes-by) =>
152 ((slot-ref gameobj 'name) =>
157 (define (gameobj-act-goes-by actor message)
158 "Reply to a message requesting what we go by."
159 (<-reply actor message
160 #:goes-by (gameobj-goes-by actor)))
162 (define (val-or-run val-or-proc)
163 "Evaluate if a procedure, or just return otherwise"
164 (if (procedure? val-or-proc)
168 (define (filter-commands commands verb)
171 (equal? (command-verbs cmd)
175 (define-mhandler (gameobj-get-commands actor message verb)
176 "Get commands a co-occupant of the room might execute for VERB"
177 (define filtered-commands
178 (filter-commands (val-or-run (slot-ref actor 'commands))
180 (<-reply actor message
181 #:commands filtered-commands
182 #:goes-by (gameobj-goes-by actor)))
184 (define-mhandler (gameobj-get-container-commands actor message verb)
185 "Get commands as the container / room of message's sender"
186 (define filtered-commands
187 (filter-commands (val-or-run (slot-ref actor 'container-commands))
189 (<-reply actor message #:commands filtered-commands))
191 (define-mhandler (gameobj-add-occupant! actor message who)
192 "Add an actor to our list of present occupants"
193 (hash-set! (slot-ref actor 'occupants)
196 (define-mhandler (gameobj-remove-occupant! actor message who)
197 "Remove an occupant from the room."
198 (hash-remove! (slot-ref actor 'occupants) who))
200 (define* (gameobj-occupants gameobj #:key exclude)
202 (lambda (occupant _ prev)
205 ;; Empty list and #f are non-exclusion
208 ;; A list of addresses... since our address object is (annoyingly)
209 ;; currently a simple cons cell...
210 ((exclude-1 ... exclude-rest)
211 (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
212 ;; Must be an individual address!
213 (_ (equal? occupant exclude))))
216 (cons occupant prev)))
218 (slot-ref gameobj 'occupants)))
220 (define-mhandler (gameobj-get-occupants actor message)
221 "Get all present occupants of the room."
222 (define exclude (message-ref message 'exclude #f))
224 (gameobj-occupants actor #:exclude exclude))
226 (<-reply actor message
227 #:occupants occupants))
229 (define (gameobj-set-loc! gameobj loc)
230 "Set the location of this object."
231 (define old-loc (gameobj-loc gameobj))
232 (format #t "DEBUG: Location set to ~s for ~s\n"
233 loc (actor-id-actor gameobj))
235 (slot-set! gameobj 'loc loc)
236 ;; Change registation of where we currently are
238 (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
240 (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
242 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
243 (define-mhandler (gameobj-act-set-loc! actor message loc)
244 "Action routine to set the location."
245 (gameobj-set-loc! actor loc))
247 (define gameobj-get-name (simple-slot-getter 'name))
249 (define-mhandler (gameobj-act-set-name! actor message val)
250 (slot-set! actor 'name val))
252 (define-mhandler (gameobj-get-desc actor message whos-looking)
254 (match (slot-ref actor 'desc)
255 ((? procedure? desc-proc)
256 (desc-proc actor whos-looking))
258 (<-reply actor message #:val desc-text))
260 (define (gameobj-visible-to-player? gameobj whos-looking)
261 "Check to see whether we're visible to the player or not.
262 By default, this is whether or not the generally-visible flag is set."
263 (slot-ref gameobj 'generally-visible))
265 (define-mhandler (gameobj-visible-name actor message whos-looking)
267 (define we-are-visible
268 ((slot-ref actor 'visible-to-player?) actor whos-looking))
270 (define name-to-return
273 (match (slot-ref actor 'name)
274 ((? procedure? name-proc)
275 (name-proc actor whos-looking))
280 (<-reply actor message #:text name-to-return))
282 (define (gameobj-self-destruct gameobj)
283 "General gameobj self destruction routine"
284 ;; Unregister from being in any particular room
285 (gameobj-set-loc! gameobj #f)
287 (self-destruct gameobj))
289 (define-mhandler (gameobj-act-self-destruct gameobj message)
290 "Action routine for self destruction"
291 (gameobj-self-destruct gameobj))
293 ;; Unless an actor has a tell message, we just ignore it
294 (define gameobj-tell-no-op
297 (define (gameobj-replace-data-occupants actor)
298 "The general purpose list of replacement data"
299 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
300 (slot-ref actor 'occupants))))
302 (define (gameobj-replace-data* actor)
303 ;; For now, just call gameobj-replace-data-occupants.
304 ;; But there may be more in the future!
305 (gameobj-replace-data-occupants actor))
307 ;; So sad that objects must assist in their replacement ;_;
308 ;; But that's life in a live hacked game!
309 (define (gameobj-act-assist-replace actor message)
310 "Vanilla method for assisting in self-replacement for live hacking"
311 (apply <-reply actor message
312 (gameobj-replace-data* actor)))
315 ;;; Utilities every gameobj has
316 ;;; ---------------------------
318 (define (dyn-ref gameobj special-symbol)
319 "Dynamically look up a special object from the gm"
320 (match special-symbol
321 ;; if it's a symbol, look it up dynamically
324 (<-wait gameobj (slot-ref gameobj 'gm) 'lookup-special
325 #:symbol special-symbol)
327 ;; if it's false, return nothing
329 ;; otherwise it's probably an address, return it as-is