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 (mudsync utils)
25 #:use-module (8sync actors)
26 #:use-module (8sync agenda)
27 #:use-module (8sync rmeta-slot)
28 #:use-module (srfi srfi-1)
29 #:use-module (ice-9 control)
30 #:use-module (ice-9 format)
31 #:use-module (ice-9 match)
32 #:use-module (oop goops)
43 slot-ref-maybe-runcheck
49 ;; Some of the more common commands
51 cmd-take-from-no-op cmd-put-in-no-op))
56 (define build-props build-rmeta-slot)
58 ;;; *all* game components that talk to players should somehow
59 ;;; derive from this class.
60 ;;; And all of them need a GM!
62 (define-class <gameobj> (<actor>)
67 ;; Uses a hash table like a set (values ignored)
68 (occupants #:init-thunk make-hash-table)
71 (gm #:init-keyword #:gm
74 ;; a name to be known by
75 (name #:init-keyword #:name
77 (goes-by #:init-keyword #:goes-by
81 #:init-keyword #:desc)
83 ;; @@: Maybe commands should be renamed to verbs, I dunno
84 ;; Commands we can handle
85 (commands #:allocation #:each-subclass
86 #:init-thunk (build-commands
87 ("take" ((direct-command cmd-take)
88 (prep-indir-command cmd-take-from
90 ("put" ((prep-indir-command cmd-put-in
91 '("in" "inside" "on"))))))
93 ;; Commands we can handle by being something's container
94 ;; dominant version (goes before everything)
95 (container-dom-commands #:allocation #:each-subclass
96 #:init-thunk (build-commands))
97 ;; subordinate version (goes after everything)
98 (container-sub-commands #:allocation #:each-subclass
99 #:init-thunk (build-commands))
101 ;; Commands we can handle by being contained by something else
102 (contained-commands #:allocation #:each-subclass
105 ("drop" ((direct-command cmd-drop #:obvious? #f)))))
107 ;; The extremely squishy concept of "props"... properties!
108 ;; These are flags, etc etc of various types. This is a hashq table.
109 ;; These have upsides and downsides, but the big upside is that you can
110 ;; query a "prop" of a prospective gameobj without knowing what type of
111 ;; gameobj that is, and not fear some kind of breakage.
113 ;; props by default only have a 'get-prop read-only action handler;
114 ;; any coordination of setting a prop between actors must be
115 ;; added to that actor, to keep things from getting out of control.
116 (props #:init-thunk make-hash-table
117 #:init-keyword #:props)
118 ;; gameobjs may inherit an initial list of these via the
119 ;; initial-props slot, which must always have its
120 ;; #:allocation #:each-subclass and use (build-props) for the
122 ;; The vanilla gameobj has no props, on purpose.
123 (initial-props #:allocation #:each-subclass
124 #:init-thunk (build-props '()))
126 ;; Most objects are generally visible by default
127 (invisible? #:init-value #f
128 #:init-keyword #:invisible?)
129 ;; TODO: Fold this into a procedure in invisible? similar
130 ;; to take-me? and etc
132 #:init-value (wrap-apply gameobj-visible-to-player?))
134 ;; Can be a boolean or a procedure accepting
135 ;; (gameobj whos-acting #:key from)
136 (take-me? #:init-value #f
137 #:init-keyword #:take-me?)
138 ;; Can be a boolean or a procedure accepting
139 ;; (gameobj whos-acting where)
140 (drop-me? #:init-value #t
141 #:init-keyword #:drop-me?)
143 ;; TODO: Remove this and use actor-alive? instead.
144 ;; Set this on self-destruct
145 ;; (checked by some "long running" game routines)
146 (destructed #:init-value #f)
148 (actions #:allocation #:each-subclass
149 ;;; Actions supported by all gameobj
152 (init gameobj-act-init)
153 ;; Commands for co-occupants
154 (get-commands gameobj-get-commands)
155 ;; Commands for participants in a room
156 (get-container-dom-commands gameobj-get-container-dom-commands)
157 (get-container-sub-commands gameobj-get-container-sub-commands)
158 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
159 (get-contained-commands gameobj-get-contained-commands)
161 (get-occupants gameobj-get-occupants)
162 (add-occupant! gameobj-add-occupant!)
163 (remove-occupant! gameobj-remove-occupant!)
164 (get-loc gameobj-act-get-loc)
165 (set-loc! gameobj-act-set-loc!)
166 (get-name gameobj-get-name)
167 (set-name! gameobj-act-set-name!)
168 (get-desc gameobj-get-desc)
169 (get-prop gameobj-act-get-prop)
170 (goes-by gameobj-act-goes-by)
171 (visible-name gameobj-visible-name)
172 (self-destruct gameobj-act-self-destruct)
173 (tell gameobj-tell-no-op)
174 (assist-replace gameobj-act-assist-replace)
175 (ok-to-drop-here? (lambda (gameobj message . _)
176 (<-reply message #t))) ; ok to drop by default
177 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
178 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
183 (cmd-take-from cmd-take-from-no-op)
184 (cmd-put-in cmd-put-in-no-op))))
187 ;;; gameobj message handlers
188 ;;; ========================
190 ;; Kind of a useful utility, maybe?
191 (define (simple-slot-getter slot)
192 (lambda (actor message)
193 (<-reply message (slot-ref actor slot))))
195 (define (gameobj-replace-step-occupants actor occupants)
196 ;; Snarf all the occupants!
197 (display "replacing occupant\n")
201 (<-wait occupant 'set-loc!
202 #:loc (actor-id actor)))
205 (define gameobj-replace-steps*
206 (list gameobj-replace-step-occupants))
208 (define (run-replacement actor replaces replace-steps)
210 (mbody-receive (_ #:key occupants)
211 (<-wait replaces 'assist-replace)
213 (lambda (replace-step)
214 (replace-step actor occupants))
217 (define (gameobj-setup-props gameobj)
218 (define class (class-of gameobj))
219 (define props (slot-ref gameobj 'props))
220 (maybe-build-rmeta-slot-cache! class 'initial-props
221 eq? hashq-set! hashq-ref)
222 ;; Kind of a kludge... we read through the rmeta-slot-cache
223 ;; and use that to build up the table
226 (when (not (hashq-ref props key value)) ; don't override init'ed instance values
227 (hashq-set! props key value)))
228 (rmeta-slot-cache (class-slot-ref class 'initial-props))))
230 ;; TODO: Use the *init* action?
231 ;; We could also use a generic method if they didn't have
232 ;; what I'm pretty sure is O(n) dispatch in GOOPS...
233 (define* (gameobj-act-init actor message #:key replace)
234 "Your most basic game object init procedure."
235 (gameobj-setup-props actor)
236 (run-replacement actor replace gameobj-replace-steps*))
238 (define* (gameobj-get-prop gameobj key #:optional dflt)
239 (hashq-ref (slot-ref gameobj 'props) key dflt))
241 (define* (gameobj-set-prop! gameobj key val)
242 (hashq-set! (slot-ref gameobj 'props) key val))
244 (define* (gameobj-act-get-prop actor message key #:optional dflt)
245 (gameobj-get-prop actor key dflt))
247 (define (gameobj-goes-by gameobj)
248 "Find the name we go by. Defaults to #:name if nothing else provided."
249 (cond ((slot-ref gameobj 'goes-by) =>
251 ((slot-ref gameobj 'name) =>
256 (define (gameobj-act-goes-by actor message)
257 "Reply to a message requesting what we go by."
258 (<-reply message (gameobj-goes-by actor)))
260 (define (val-or-run val-or-proc)
261 "Evaluate if a procedure, or just return otherwise"
262 (if (procedure? val-or-proc)
266 (define (get-candidate-commands actor rmeta-sym verb)
267 (class-rmeta-ref (class-of actor) rmeta-sym verb
270 (define* (gameobj-get-commands actor message #:key verb)
271 "Get commands a co-occupant of the room might execute for VERB"
272 (define candidate-commands
273 (get-candidate-commands actor 'commands verb))
275 #:commands candidate-commands
276 #:goes-by (gameobj-goes-by actor)))
278 (define* (gameobj-get-container-dom-commands actor message #:key verb)
279 "Get (dominant) commands as the container / room of message's sender"
280 (define candidate-commands
281 (get-candidate-commands actor 'container-dom-commands verb))
282 (<-reply message #:commands candidate-commands))
284 (define* (gameobj-get-container-sub-commands actor message #:key verb)
285 "Get (subordinate) commands as the container / room of message's sender"
286 (define candidate-commands
287 (get-candidate-commands actor 'container-sub-commands verb))
288 (<-reply message #:commands candidate-commands))
290 (define* (gameobj-get-contained-commands actor message #:key verb)
291 "Get commands as being contained (eg inventory) of commanding gameobj"
292 (define candidate-commands
293 (get-candidate-commands actor 'contained-commands verb))
295 #:commands candidate-commands
296 #:goes-by (gameobj-goes-by actor)))
298 (define* (gameobj-add-occupant! actor message #:key who)
299 "Add an actor to our list of present occupants"
300 (hash-set! (slot-ref actor 'occupants)
303 (define* (gameobj-remove-occupant! actor message #:key who)
304 "Remove an occupant from the room."
305 (hash-remove! (slot-ref actor 'occupants) who))
307 (define* (gameobj-occupants gameobj #:key exclude)
309 (lambda (occupant _ prev)
312 ;; Empty list and #f are non-exclusion
315 ;; A list of addresses... since our address object is (annoyingly)
316 ;; currently a simple cons cell...
317 ((exclude-1 ... exclude-rest)
318 (member occupant exclude))
319 ;; Must be an individual address!
320 (_ (equal? occupant exclude))))
323 (cons occupant prev)))
325 (slot-ref gameobj 'occupants)))
327 (define* (gameobj-get-occupants actor message #:key exclude)
328 "Get all present occupants of the room."
330 (gameobj-occupants actor #:exclude exclude))
331 (<-reply message occupants))
333 (define (gameobj-act-get-loc actor message)
334 (<-reply message (slot-ref actor 'loc)))
336 (define (gameobj-set-loc! gameobj loc)
337 "Set the location of this object."
338 (define old-loc (gameobj-loc gameobj))
339 (format #t "DEBUG: Location set to ~s for ~s\n"
340 loc (actor-id-actor gameobj))
342 (when (not (equal? old-loc loc))
343 (slot-set! gameobj 'loc loc)
344 ;; Change registation of where we currently are
346 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
348 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
350 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
351 (define* (gameobj-act-set-loc! actor message #:key loc)
352 "Action routine to set the location."
353 (gameobj-set-loc! actor loc))
355 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
356 "Do a slot-ref on gameobj, evaluating it including ourselves
357 and whos-asking, and see if we should just return it or run it."
358 (match (slot-ref gameobj slot)
359 ((? procedure? slot-val-proc)
360 (apply slot-val-proc gameobj whos-asking other-args))
361 (anything-else anything-else)))
363 (define gameobj-get-name (simple-slot-getter 'name))
365 (define* (gameobj-act-set-name! actor message val)
366 (slot-set! actor 'name val))
368 (define* (gameobj-desc actor #:key whos-looking)
369 (match (slot-ref actor 'desc)
370 ((? procedure? desc-proc)
371 (desc-proc actor whos-looking))
374 (define* (gameobj-get-desc actor message #:key whos-looking)
375 "This is the action equivalent of the gameobj-desc getter"
376 (<-reply message (gameobj-desc actor #:whos-looking whos-looking)))
378 (define (gameobj-visible-to-player? gameobj whos-looking)
379 "Check to see whether we're visible to the player or not.
380 By default, this is whether or not the generally-visible flag is set."
381 (not (slot-ref gameobj 'invisible?)))
383 (define* (gameobj-visible-name actor message #:key whos-looking)
385 (define we-are-visible
386 ((slot-ref actor 'visible-to-player?) actor whos-looking))
388 (define name-to-return
391 (match (slot-ref actor 'name)
392 ((? procedure? name-proc)
393 (name-proc actor whos-looking))
398 (<-reply message #:text name-to-return))
400 (define (gameobj-self-destruct gameobj)
401 "General gameobj self destruction routine"
402 ;; Unregister from being in any particular room
403 (gameobj-set-loc! gameobj #f)
404 (slot-set! gameobj 'destructed #t)
406 (self-destruct gameobj))
408 (define* (gameobj-act-self-destruct gameobj message #:key why)
409 "Action routine for self destruction"
410 (gameobj-self-destruct gameobj))
412 ;; Unless an actor has a tell message, we just ignore it
413 (define gameobj-tell-no-op
416 (define (gameobj-replace-data-occupants gameobj)
417 "The general purpose list of replacement data"
418 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
419 (slot-ref gameobj 'occupants))))
421 (define (gameobj-replace-data* gameobj)
422 ;; For now, just call gameobj-replace-data-occupants.
423 ;; But there may be more in the future!
424 (gameobj-replace-data-occupants gameobj))
426 ;; So sad that objects must assist in their replacement ;_;
427 ;; But that's life in a live hacked game!
428 (define (gameobj-act-assist-replace gameobj message)
429 "Vanilla method for assisting in self-replacement for live hacking"
430 (apply <-reply message
431 (gameobj-replace-data* gameobj)))
433 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
434 (call-with-values (lambda ()
435 (slot-ref-maybe-runcheck gameobj 'take-me?
436 whos-acting #:from #t))
437 ;; This allows this to reply with #:why-not if appropriate
439 (apply <-reply message args))))
441 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
442 (call-with-values (lambda ()
443 (slot-ref-maybe-runcheck gameobj 'drop-me?
445 ;; This allows this to reply with #:why-not if appropriate
447 (apply <-reply message args))))
450 ;;; Utilities every gameobj has
451 ;;; ---------------------------
453 (define (dyn-ref gameobj special-symbol)
454 "Dynamically look up a special object from the gm"
455 (match special-symbol
456 ;; if it's a symbol, look it up dynamically
458 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
459 #:symbol special-symbol)))
460 ;; if it's false, return nothing
462 ;; otherwise it's probably an address, return it as-is
470 (define* (cmd-take gameobj message
472 (player (message-from message)))
474 (mbody-val (<-wait player 'get-name)))
476 (mbody-val (<-wait player 'get-loc)))
477 (define our-name (slot-ref gameobj 'name))
478 (define self-should-take
479 (slot-ref-maybe-runcheck gameobj 'take-me? player))
480 ;; @@: Is there any reason to allow the room to object in the way
481 ;; that there is for dropping? It doesn't seem like it.
482 (call-with-values (lambda ()
483 (slot-ref-maybe-runcheck gameobj 'take-me? player))
484 (lambda* (self-should-take #:key (why-not
485 `("It doesn't seem like you can take "
488 ;; Set the location to whoever's picking us up
490 (gameobj-set-loc! gameobj player)
492 #:text (format #f "You pick up ~a.\n"
494 (<- player-loc 'tell-room
495 #:text (format #f "~a picks up ~a.\n"
499 (<- player 'tell #:text why-not)))))
501 (define* (cmd-drop gameobj message
503 (player (message-from message)))
505 (mbody-val (<-wait player 'get-name)))
507 (mbody-val (<-wait player 'get-loc)))
508 (define our-name (slot-ref gameobj 'name))
510 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
511 (define (room-objection-to-drop)
512 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
513 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
515 ;; Either give the specified reason, or give a boilerplate one
517 `("You'd love to drop " ,our-name
518 " but for some reason it doesn't seem like you can"
519 " do that here.")))))
523 #:text `("It doesn't seem like you can drop " ,our-name
524 " here, because you don't seem to be anywhere?!?")))
525 ;; TODO: Let ourselves supply a reason why not.
528 #:text (format #f "It doesn't seem like you can drop ~a.\n"
530 ((room-objection-to-drop)
532 #:text room-objection-to-drop))
534 (gameobj-set-loc! gameobj player-loc)
535 ;; TODO: Allow more flavortext here.
537 #:text (format #f "You drop ~a.\n"
539 (<- player-loc 'tell-room
540 #:text (format #f "~a drops ~a.\n"
545 (define* (cmd-take-from-no-op gameobj message
546 #:key direct-obj indir-obj preposition
547 (player (message-from message)))
549 #:text `("It doesn't seem like you can take anything "
551 ,(slot-ref gameobj 'name) ".")))
553 (define* (cmd-put-in-no-op gameobj message
554 #:key direct-obj indir-obj preposition
555 (player (message-from message)))
557 #:text `("It doesn't seem like you can put anything "
559 ,(slot-ref gameobj 'name) ".")))