1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christine Lemmer-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)
45 slot-ref-maybe-runcheck
51 ;; Some of the more common commands
54 cmd-take-from-no-op cmd-put-in-no-op))
59 (define build-props build-rmeta-slot)
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
77 ;; a name to be known by
78 (name #:init-keyword #:name
80 (goes-by #:init-keyword #:goes-by
84 #:init-keyword #:desc)
86 ;; @@: Maybe commands should be renamed to verbs, I dunno
87 ;; Commands we can handle
88 (commands #:allocation #:each-subclass
89 #:init-thunk (build-commands
90 (("l" "look") ((direct-command cmd-look-at)))
91 ("take" ((direct-command cmd-take)
92 (prep-indir-command cmd-take-from
94 ("put" ((prep-indir-command cmd-put-in
95 '("in" "inside" "into" "on"))))))
97 ;; Commands we can handle by being something's container
98 ;; dominant version (goes before everything)
99 (container-dom-commands #:allocation #:each-subclass
100 #:init-thunk (build-commands))
101 ;; subordinate version (goes after everything)
102 (container-sub-commands #:allocation #:each-subclass
103 #:init-thunk (build-commands))
105 ;; Commands we can handle by being contained by something else
106 (contained-commands #:allocation #:each-subclass
109 (("l" "look") ((direct-command cmd-look-at)))
110 ("drop" ((direct-command cmd-drop #:obvious? #f)))))
112 ;; The extremely squishy concept of "props"... properties!
113 ;; These are flags, etc etc of various types. This is a hashq table.
114 ;; These have upsides and downsides, but the big upside is that you can
115 ;; query a "prop" of a prospective gameobj without knowing what type of
116 ;; gameobj that is, and not fear some kind of breakage.
118 ;; props by default only have a 'get-prop read-only action handler;
119 ;; any coordination of setting a prop between actors must be
120 ;; added to that actor, to keep things from getting out of control.
121 (props #:init-thunk make-hash-table
122 #:init-keyword #:props)
123 ;; gameobjs may inherit an initial list of these via the
124 ;; initial-props slot, which must always have its
125 ;; #:allocation #:each-subclass and use (build-props) for the
127 ;; The vanilla gameobj has no props, on purpose.
128 (initial-props #:allocation #:each-subclass
129 #:init-thunk (build-props '()))
131 ;; Most objects are generally visible by default
132 (invisible? #:init-value #f
133 #:init-keyword #:invisible?)
134 ;; TODO: Fold this into a procedure in invisible? similar
135 ;; to take-me? and etc
137 #:init-value (wrap-apply gameobj-visible-to-player?))
139 ;; Can be a boolean or a procedure accepting
140 ;; (gameobj whos-acting #:key from)
141 (take-me? #:init-value #f
142 #:init-keyword #:take-me?)
143 ;; Can be a boolean or a procedure accepting
144 ;; (gameobj whos-acting where)
145 (drop-me? #:init-value #t
146 #:init-keyword #:drop-me?)
148 ;; TODO: Remove this and use actor-alive? instead.
149 ;; Set this on self-destruct
150 ;; (checked by some "long running" game routines)
151 (destructed #:init-value #f)
153 (actions #:allocation #:each-subclass
154 ;;; Actions supported by all gameobj
157 (init gameobj-act-init)
158 ;; Commands for co-occupants
159 (get-commands gameobj-get-commands)
160 ;; Commands for participants in a room
161 (get-container-dom-commands gameobj-get-container-dom-commands)
162 (get-container-sub-commands gameobj-get-container-sub-commands)
163 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
164 (get-contained-commands gameobj-get-contained-commands)
166 (get-occupants gameobj-get-occupants)
167 (add-occupant! gameobj-add-occupant!)
168 (remove-occupant! gameobj-remove-occupant!)
169 (get-loc gameobj-act-get-loc)
170 (set-loc! gameobj-act-set-loc!)
171 (get-name gameobj-get-name)
172 (set-name! gameobj-act-set-name!)
173 (get-desc gameobj-get-desc)
174 (get-prop gameobj-act-get-prop)
175 (goes-by gameobj-act-goes-by)
176 (visible-name gameobj-visible-name)
177 (self-destruct gameobj-act-self-destruct)
178 (tell gameobj-tell-no-op)
179 (assist-replace gameobj-act-assist-replace)
180 (ok-to-drop-here? (lambda (gameobj message . _)
181 (<-reply message #t))) ; ok to drop by default
182 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
183 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
186 (cmd-look-at cmd-look-at)
189 (cmd-take-from cmd-take-from-no-op)
190 (cmd-put-in cmd-put-in-no-op))))
193 ;;; gameobj message handlers
194 ;;; ========================
196 ;; TODO: This init stuff is a mess, and should be redone now that
197 ;; we have the *init* action stuff. We've really spread out the
198 ;; logic for creating a gameobj in several places, eg gm-inject-special!
199 (define (create-gameobj class gm loc . args)
200 "Create a gameobj of CLASS with GM and set to location LOC, applying rest of ARGS.
201 Note that this doesn't do any special dyn-ref of the location."
202 (let ((new-gameobj (apply create-actor (%current-actor) class
205 (<-wait new-gameobj 'set-loc! #:loc loc)
206 ;; Initialize the object
207 (<-wait new-gameobj 'init)))
209 ;; ;; @@: Should we also dyn-ref the loc here? We can do that, unlike with
210 ;; ;; create-gameobj.
211 ;; ;; Another route could be to have set-loc! itself know how to use the
213 ;; (define (gameobj-create-gameobj gameobj class loc . args)
214 ;; "Like create-gameobj but saves the step of passing in the gm."
215 ;; (apply create-gameobj class (gameobj-gm gameobj) loc args))
217 ;; Kind of a useful utility, maybe?
218 (define (simple-slot-getter slot)
219 (lambda (actor message)
220 (<-reply message (slot-ref actor slot))))
222 (define (gameobj-replace-step-occupants actor occupants)
223 ;; Snarf all the occupants!
224 (display "replacing occupant\n")
228 (<-wait occupant 'set-loc!
229 #:loc (actor-id actor)))
232 (define gameobj-replace-steps*
233 (list gameobj-replace-step-occupants))
235 (define (run-replacement actor replaces replace-steps)
237 (mbody-receive (_ #:key occupants)
238 (<-wait replaces 'assist-replace)
240 (lambda (replace-step)
241 (replace-step actor occupants))
244 (define %nothing (cons '*the* '*nothing*))
245 (define (gameobj-setup-props gameobj)
246 (define class (class-of gameobj))
247 (define props (slot-ref gameobj 'props))
248 (maybe-build-rmeta-slot-cache! class 'initial-props
249 eq? hashq-set! hashq-ref)
250 ;; Kind of a kludge... we read through the rmeta-slot-cache
251 ;; and use that to build up the table
254 (when (eq? (hashq-ref props key %nothing) ; don't override init'ed instance values
256 (hashq-set! props key value)))
257 (rmeta-slot-cache (class-slot-ref class 'initial-props))))
259 ;; TODO: Use the *init* action?
260 ;; We could also use a generic method if they didn't have
261 ;; what I'm pretty sure is O(n) dispatch in GOOPS...
262 (define* (gameobj-act-init actor message #:key replace)
263 "Your most basic game object init procedure."
264 (gameobj-setup-props actor)
265 (run-replacement actor replace gameobj-replace-steps*))
267 (define* (gameobj-get-prop gameobj key #:optional dflt)
268 (hashq-ref (slot-ref gameobj 'props) key dflt))
270 (define* (gameobj-set-prop! gameobj key val)
271 (hashq-set! (slot-ref gameobj 'props) key val))
273 (define* (gameobj-act-get-prop actor message key #:optional dflt)
274 (<-reply message (gameobj-get-prop actor key dflt)))
276 (define (gameobj-goes-by gameobj)
277 "Find the name we go by. Defaults to #:name if nothing else provided."
278 (cond ((slot-ref gameobj 'goes-by) =>
280 ((slot-ref gameobj 'name) =>
285 (define (gameobj-act-goes-by actor message)
286 "Reply to a message requesting what we go by."
287 (<-reply message (gameobj-goes-by actor)))
289 (define (val-or-run val-or-proc)
290 "Evaluate if a procedure, or just return otherwise"
291 (if (procedure? val-or-proc)
295 (define (get-candidate-commands actor rmeta-sym verb)
296 (class-rmeta-ref (class-of actor) rmeta-sym verb
299 (define* (gameobj-get-commands actor message #:key verb)
300 "Get commands a co-occupant of the room might execute for VERB"
301 (define candidate-commands
302 (get-candidate-commands actor 'commands verb))
304 #:commands candidate-commands
305 #:goes-by (gameobj-goes-by actor)))
307 (define* (gameobj-get-container-dom-commands actor message #:key verb)
308 "Get (dominant) commands as the container / room of message's sender"
309 (define candidate-commands
310 (get-candidate-commands actor 'container-dom-commands verb))
311 (<-reply message #:commands candidate-commands))
313 (define* (gameobj-get-container-sub-commands actor message #:key verb)
314 "Get (subordinate) commands as the container / room of message's sender"
315 (define candidate-commands
316 (get-candidate-commands actor 'container-sub-commands verb))
317 (<-reply message #:commands candidate-commands))
319 (define* (gameobj-get-contained-commands actor message #:key verb)
320 "Get commands as being contained (eg inventory) of commanding gameobj"
321 (define candidate-commands
322 (get-candidate-commands actor 'contained-commands verb))
324 #:commands candidate-commands
325 #:goes-by (gameobj-goes-by actor)))
327 (define* (gameobj-add-occupant! actor message #:key who)
328 "Add an actor to our list of present occupants"
329 (hash-set! (slot-ref actor 'occupants)
332 (define* (gameobj-remove-occupant! actor message #:key who)
333 "Remove an occupant from the room."
334 (hash-remove! (slot-ref actor 'occupants) who))
336 (define* (gameobj-occupants gameobj #:key exclude)
338 (lambda (occupant _ prev)
341 ;; Empty list and #f are non-exclusion
344 ;; A list of addresses... since our address object is (annoyingly)
345 ;; currently a simple cons cell...
346 ((exclude-1 ... exclude-rest)
347 (member occupant exclude))
348 ;; Must be an individual address!
349 (_ (equal? occupant exclude))))
352 (cons occupant prev)))
354 (slot-ref gameobj 'occupants)))
356 (define* (gameobj-get-occupants actor message #:key exclude)
357 "Get all present occupants of the room."
359 (gameobj-occupants actor #:exclude exclude))
360 (<-reply message occupants))
362 (define (gameobj-act-get-loc actor message)
363 (<-reply message (slot-ref actor 'loc)))
365 (define (gameobj-set-loc! gameobj loc)
366 "Set the location of this object."
367 (define old-loc (gameobj-loc gameobj))
368 (format #t "DEBUG: Location set to ~s for ~s\n"
369 loc (actor-id-actor gameobj))
371 (when (not (equal? old-loc loc))
372 (slot-set! gameobj 'loc loc)
373 ;; Change registation of where we currently are
375 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
377 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
379 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
380 (define* (gameobj-act-set-loc! actor message #:key loc)
381 "Action routine to set the location."
382 (gameobj-set-loc! actor loc))
384 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
385 "Do a slot-ref on gameobj, evaluating it including ourselves
386 and whos-asking, and see if we should just return it or run it."
387 (match (slot-ref gameobj slot)
388 ((? procedure? slot-val-proc)
389 (apply slot-val-proc gameobj whos-asking other-args))
390 (anything-else anything-else)))
392 (define gameobj-get-name (simple-slot-getter 'name))
394 (define* (gameobj-act-set-name! actor message val)
395 (slot-set! actor 'name val))
397 (define* (gameobj-desc gameobj #:key whos-looking)
398 (match (slot-ref gameobj 'desc)
399 ((? procedure? desc-proc)
400 (desc-proc gameobj whos-looking))
403 (define* (gameobj-get-desc actor message #:key whos-looking)
404 "This is the action equivalent of the gameobj-desc getter"
405 (<-reply message (gameobj-desc actor #:whos-looking whos-looking)))
407 (define (gameobj-visible-to-player? gameobj whos-looking)
408 "Check to see whether we're visible to the player or not.
409 By default, this is whether or not the generally-visible flag is set."
410 (not (slot-ref gameobj 'invisible?)))
412 (define* (gameobj-visible-name actor message #:key whos-looking)
414 (define we-are-visible
415 ((slot-ref actor 'visible-to-player?) actor whos-looking))
417 (define name-to-return
420 (match (slot-ref actor 'name)
421 ((? procedure? name-proc)
422 (name-proc actor whos-looking))
427 (<-reply message #:text name-to-return))
429 (define (gameobj-self-destruct gameobj)
430 "General gameobj self destruction routine"
431 ;; Unregister from being in any particular room
432 (gameobj-set-loc! gameobj #f)
433 (slot-set! gameobj 'destructed #t)
435 (self-destruct gameobj))
437 (define* (gameobj-act-self-destruct gameobj message #:key why)
438 "Action routine for self destruction"
439 (gameobj-self-destruct gameobj))
441 ;; Unless an actor has a tell message, we just ignore it
442 (define gameobj-tell-no-op
445 (define (gameobj-replace-data-occupants gameobj)
446 "The general purpose list of replacement data"
447 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
448 (slot-ref gameobj 'occupants))))
450 (define (gameobj-replace-data* gameobj)
451 ;; For now, just call gameobj-replace-data-occupants.
452 ;; But there may be more in the future!
453 (gameobj-replace-data-occupants gameobj))
455 ;; So sad that objects must assist in their replacement ;_;
456 ;; But that's life in a live hacked game!
457 (define (gameobj-act-assist-replace gameobj message)
458 "Vanilla method for assisting in self-replacement for live hacking"
459 (apply <-reply message
460 (gameobj-replace-data* gameobj)))
462 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
463 (call-with-values (lambda ()
464 (slot-ref-maybe-runcheck gameobj 'take-me?
465 whos-acting #:from #t))
466 ;; This allows this to reply with #:why-not if appropriate
468 (apply <-reply message args))))
470 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
471 (call-with-values (lambda ()
472 (slot-ref-maybe-runcheck gameobj 'drop-me?
474 ;; This allows this to reply with #:why-not if appropriate
476 (apply <-reply message args))))
479 ;;; Utilities every gameobj has
480 ;;; ---------------------------
482 (define (dyn-ref gameobj special-symbol)
483 "Dynamically look up a special object from the gm"
484 (match special-symbol
485 ;; if it's a symbol, look it up dynamically
487 ;; TODO: If we get back an #f at this point, should we throw
488 ;; an error? Obviously #f is okay, but maybe not if
489 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
490 #:symbol special-symbol)))
491 ;; if it's false, return nothing
493 ;; otherwise it's probably an address, return it as-is
501 (define %formless-desc
502 "You don't see anything special.")
504 (define* (cmd-look-at gameobj message
506 (player (message-from message)))
508 (or (gameobj-desc gameobj #:whos-looking player)
510 (<- player 'tell #:text desc)))
512 (define* (cmd-take gameobj message
514 (player (message-from message)))
516 (mbody-val (<-wait player 'get-name)))
518 (mbody-val (<-wait player 'get-loc)))
519 (define our-name (slot-ref gameobj 'name))
520 (define self-should-take
521 (slot-ref-maybe-runcheck gameobj 'take-me? player))
522 ;; @@: Is there any reason to allow the room to object in the way
523 ;; that there is for dropping? It doesn't seem like it.
524 (call-with-values (lambda ()
525 (slot-ref-maybe-runcheck gameobj 'take-me? player))
526 (lambda* (self-should-take #:key (why-not
527 `("It doesn't seem like you can take "
530 ;; Set the location to whoever's picking us up
532 (gameobj-set-loc! gameobj player)
534 #:text (format #f "You pick up ~a.\n"
536 (<- player-loc 'tell-room
537 #:text (format #f "~a picks up ~a.\n"
541 (<- player 'tell #:text why-not)))))
543 (define* (cmd-drop gameobj message
545 (player (message-from message)))
547 (mbody-val (<-wait player 'get-name)))
549 (mbody-val (<-wait player 'get-loc)))
550 (define our-name (slot-ref gameobj 'name))
552 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
553 (define (room-objection-to-drop)
554 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
555 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
557 ;; Either give the specified reason, or give a boilerplate one
559 `("You'd love to drop " ,our-name
560 " but for some reason it doesn't seem like you can"
561 " do that here.")))))
565 #:text `("It doesn't seem like you can drop " ,our-name
566 " here, because you don't seem to be anywhere?!?")))
567 ;; TODO: Let ourselves supply a reason why not.
570 #:text (format #f "It doesn't seem like you can drop ~a.\n"
572 ((room-objection-to-drop)
574 #:text room-objection-to-drop))
576 (gameobj-set-loc! gameobj player-loc)
577 ;; TODO: Allow more flavortext here.
579 #:text (format #f "You drop ~a.\n"
581 (<- player-loc 'tell-room
582 #:text (format #f "~a drops ~a.\n"
587 (define* (cmd-take-from-no-op gameobj message
588 #:key direct-obj indir-obj preposition
589 (player (message-from message)))
591 #:text `("It doesn't seem like you can take anything "
593 ,(slot-ref gameobj 'name) ".")))
595 (define* (cmd-put-in-no-op gameobj message
596 #:key direct-obj indir-obj preposition
597 (player (message-from message)))
599 #:text `("It doesn't seem like you can put anything "
601 ,(slot-ref gameobj 'name) ".")))