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)
38 gameobj-self-destruct))
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 (get-desc (wrap-apply gameobj-get-desc))
56 (goes-by (wrap-apply gameobj-act-goes-by))
57 (visible-name (wrap-apply gameobj-visible-name))
58 (self-destruct (wrap-apply gameobj-act-self-destruct))
59 (tell (wrap-apply gameobj-tell-no-op))))
61 ;;; *all* game components that talk to players should somehow
62 ;;; derive from this class.
63 ;;; And all of them need a GM!
65 (define-class <gameobj> (<actor>)
70 ;; Uses a hash table like a set (values ignored)
71 (occupants #:init-thunk make-hash-table)
74 (gm #:init-keyword #:gm
76 ;; a name to be known by
77 (name #:init-keyword #:name
79 (goes-by #:init-keyword #:goes-by
83 #:init-keyword #:desc)
85 ;; Commands we can handle
86 (commands #:init-value '())
88 ;; Commands we can handle by being something's container
89 (container-commands #:init-value '())
92 (simple-dispatcher gameobj-actions))
94 ;; Most objects are generally visible by default
95 (generally-visible #:init-value #t
96 #:init-keyword #:generally-visible)
97 ;; @@: Would be preferable to be using generic methods for this...
98 ;; Hopefully we can port this to Guile 2.2 soon...
100 #:init-value (wrap-apply gameobj-visible-to-player?)))
103 ;;; gameobj message handlers
104 ;;; ========================
106 ;; Kind of a useful utility, maybe?
107 (define (simple-slot-getter slot)
108 (lambda (actor message)
109 (reply-message actor message
110 #:val (slot-ref actor slot))))
113 ;; @@: This could be kind of a messy way of doing gameobj-init
114 ;; stuff. If only we had generic methods :(
115 (define-mhandler (gameobj-init actor message)
116 "Your most basic game object init procedure. Does nothing."
119 (define (gameobj-goes-by gameobj)
120 "Find the name we go by. Defaults to #:name if nothing else provided."
121 (cond ((slot-ref gameobj 'goes-by) =>
123 ((slot-ref gameobj 'name) =>
128 (define (gameobj-act-goes-by actor message)
129 "Reply to a message requesting what we go by."
130 (<-reply actor message
131 #:goes-by (gameobj-goes-by actor)))
133 (define (val-or-run val-or-proc)
134 "Evaluate if a procedure, or just return otherwise"
135 (if (procedure? val-or-proc)
139 (define (filter-commands commands verb)
142 (equal? (command-verbs cmd)
146 (define-mhandler (gameobj-get-commands actor message verb)
147 "Get commands a co-occupant of the room might execute for VERB"
148 (define filtered-commands
149 (filter-commands (val-or-run (slot-ref actor 'commands))
151 (<-reply actor message
152 #:commands filtered-commands
153 #:goes-by (gameobj-goes-by actor)))
155 (define-mhandler (gameobj-get-container-commands actor message verb)
156 "Get commands as the container / room of message's sender"
157 (define filtered-commands
158 (filter-commands (val-or-run (slot-ref actor 'container-commands))
160 (<-reply actor message #:commands filtered-commands))
162 (define-mhandler (gameobj-add-occupant! actor message who)
163 "Add an actor to our list of present occupants"
164 (hash-set! (slot-ref actor 'occupants)
167 (define-mhandler (gameobj-remove-occupant! actor message who)
168 "Remove an occupant from the room."
169 (hash-remove! (slot-ref actor 'occupants) who))
171 (define* (gameobj-occupants gameobj #:key exclude)
173 (lambda (occupant _ prev)
176 ;; Empty list and #f are non-exclusion
179 ;; A list of addresses... since our address object is (annoyingly)
180 ;; currently a simple cons cell...
181 ((exclude-1 ... exclude-rest)
182 (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
183 ;; Must be an individual address!
184 (_ (equal? occupant exclude))))
187 (cons occupant prev)))
189 (slot-ref gameobj 'occupants)))
191 (define-mhandler (gameobj-get-occupants actor message)
192 "Get all present occupants of the room."
193 (define exclude (message-ref message 'exclude #f))
195 (gameobj-occupants actor #:exclude exclude))
197 (<-reply actor message
198 #:occupants occupants))
200 (define (gameobj-set-loc! gameobj loc)
201 "Set the location of this object."
202 (define old-loc (gameobj-loc gameobj))
203 (format #t "DEBUG: Location set to ~s for ~s\n"
204 loc (actor-id-actor gameobj))
206 (slot-set! gameobj 'loc loc)
207 ;; Change registation of where we currently are
209 (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
211 (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
213 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
214 (define-mhandler (gameobj-act-set-loc! actor message loc)
215 "Action routine to set the location."
216 (gameobj-set-loc! actor loc))
218 (define gameobj-get-name (simple-slot-getter 'name))
220 (define-mhandler (gameobj-get-desc actor message whos-looking)
222 (match (slot-ref actor 'desc)
223 ((? procedure? desc-proc)
224 (desc-proc actor whos-looking))
226 (<-reply actor message #:val desc-text))
228 (define (gameobj-simple-name-f gameobj)
229 "Simplest version: return ourselves for our name."
230 (gameobj-name gameobj))
232 (define (gameobj-visible-to-player? gameobj whos-looking)
233 "Check to see whether we're visible to the player or not.
234 By default, this is whether or not the generally-visible flag is set."
235 (slot-ref gameobj 'generally-visible))
237 (define-mhandler (gameobj-visible-name actor message whos-looking)
239 (define we-are-visible
240 ((slot-ref actor 'visible-to-player?) actor whos-looking))
242 (define name-to-return
245 (match (slot-ref actor 'name)
246 ((? procedure? name-proc)
247 (name-proc actor whos-looking))
252 (<-reply actor message #:text name-to-return))
254 (define (gameobj-self-destruct gameobj)
255 "General gameobj self destruction routine"
256 ;; Unregister from being in any particular room
257 (gameobj-set-loc! gameobj #f)
259 (self-destruct gameobj))
261 (define-mhandler (gameobj-act-self-destruct gameobj message)
262 "Action routine for self destruction"
263 (gameobj-self-destruct gameobj))
265 ;; Unless an actor has a tell message, we just ignore it
266 (define gameobj-tell-no-op