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
48 ;; Some of the more common commands
50 cmd-take-from cmd-put-in))
56 ;;; *all* game components that talk to players should somehow
57 ;;; derive from this class.
58 ;;; And all of them need a GM!
60 (define-class <gameobj> (<actor>)
65 ;; Uses a hash table like a set (values ignored)
66 (occupants #:init-thunk make-hash-table)
69 (gm #:init-keyword #:gm
72 ;; a name to be known by
73 (name #:init-keyword #:name
75 (goes-by #:init-keyword #:goes-by
79 #:init-keyword #:desc)
81 ;; Commands we can handle
82 (commands #:allocation #:each-subclass
83 #:init-thunk (build-commands
84 ("take" ((direct-command cmd-take)
85 (prep-indir-command cmd-take-from
87 ("put" ((prep-indir-command cmd-put-in
88 '("in" "inside" "on"))))))
90 ;; Commands we can handle by being something's container
91 (container-commands #:allocation #:each-subclass
92 #:init-thunk (build-commands))
94 ;; Commands we can handle by being contained by something else
95 (contained-commands #:allocation #:each-subclass
98 ("drop" ((direct-command cmd-drop #:obvious? #f)))))
100 ;; Most objects are generally visible by default
101 (invisible? #:init-value #f
102 #:init-keyword #:invisible?)
103 ;; TODO: Fold this into a procedure in invisible? similar
104 ;; to take-me? and etc
106 #:init-value (wrap-apply gameobj-visible-to-player?))
108 ;; Can be a boolean or a procedure accepting
109 ;; (gameobj whos-acting #:key from)
110 (take-me? #:init-value #f
111 #:init-keyword #:take-me?)
112 ;; Can be a boolean or a procedure accepting
113 ;; (gameobj whos-acting where)
114 (drop-me? #:init-value #t
115 #:init-keyword #:drop-me?)
116 ;; Can be a boolean or a procedure accepting
117 ;; (gameobj whos-acting take-what)
118 (take-from-me? #:init-value #f
119 #:init-keyword #:take-from-me?)
120 ;; Can be a boolean or a procedure accepting
121 ;; (gameobj whos-acting put-what)
122 (put-in-me? #:init-value #f
123 #:init-keyword #:put-in-me?)
125 ;; TODO: Remove this and use actor-alive? instead.
126 ;; Set this on self-destruct
127 ;; (checked by some "long running" game routines)
128 (destructed #:init-value #f)
130 (actions #:allocation #:each-subclass
131 ;;; Actions supported by all gameobj
134 (init gameobj-act-init)
135 ;; Commands for co-occupants
136 (get-commands gameobj-get-commands)
137 ;; Commands for participants in a room
138 (get-container-commands gameobj-get-container-commands)
139 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
140 (get-contained-commands gameobj-get-contained-commands)
142 (get-occupants gameobj-get-occupants)
143 (add-occupant! gameobj-add-occupant!)
144 (remove-occupant! gameobj-remove-occupant!)
145 (get-loc gameobj-act-get-loc)
146 (set-loc! gameobj-act-set-loc!)
147 (get-name gameobj-get-name)
148 (set-name! gameobj-act-set-name!)
149 (get-desc gameobj-get-desc)
150 (goes-by gameobj-act-goes-by)
151 (visible-name gameobj-visible-name)
152 (self-destruct gameobj-act-self-destruct)
153 (tell gameobj-tell-no-op)
154 (assist-replace gameobj-act-assist-replace)
155 (ok-to-drop-here? (lambda (gameobj message . _)
156 (<-reply message #t))) ; ok to drop by default
157 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
158 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
162 (cmd-take-from cmd-take-from)
163 (cmd-put-in cmd-put-in)
164 (cmd-drop cmd-drop))))
167 ;;; gameobj message handlers
168 ;;; ========================
170 ;; Kind of a useful utility, maybe?
171 (define (simple-slot-getter slot)
172 (lambda (actor message)
173 (<-reply message (slot-ref actor slot))))
175 (define (gameobj-replace-step-occupants actor occupants)
176 ;; Snarf all the occupants!
177 (display "replacing occupant\n")
181 (<-wait occupant 'set-loc!
182 #:loc (actor-id actor)))
185 (define gameobj-replace-steps*
186 (list gameobj-replace-step-occupants))
188 (define (run-replacement actor replaces replace-steps)
190 (mbody-receive (_ #:key occupants)
191 (<-wait replaces 'assist-replace)
193 (lambda (replace-step)
194 (replace-step actor occupants))
197 ;; @@: This could be kind of a messy way of doing gameobj-act-init
198 ;; stuff. If only we had generic methods :(
199 (define* (gameobj-act-init actor message #:key replace)
200 "Your most basic game object init procedure.
201 Assists in its replacement of occupants if necessary and nothing else."
202 (run-replacement actor replace gameobj-replace-steps*))
204 (define (gameobj-goes-by gameobj)
205 "Find the name we go by. Defaults to #:name if nothing else provided."
206 (cond ((slot-ref gameobj 'goes-by) =>
208 ((slot-ref gameobj 'name) =>
213 (define (gameobj-act-goes-by actor message)
214 "Reply to a message requesting what we go by."
215 (<-reply message (gameobj-goes-by actor)))
217 (define (val-or-run val-or-proc)
218 "Evaluate if a procedure, or just return otherwise"
219 (if (procedure? val-or-proc)
223 (define (get-candidate-commands actor rmeta-sym verb)
224 (class-rmeta-ref (class-of actor) rmeta-sym verb
227 (define* (gameobj-get-commands actor message #:key verb)
228 "Get commands a co-occupant of the room might execute for VERB"
229 (define candidate-commands
230 (get-candidate-commands actor 'commands verb))
232 #:commands candidate-commands
233 #:goes-by (gameobj-goes-by actor)))
235 (define* (gameobj-get-container-commands actor message #:key verb)
236 "Get commands as the container / room of message's sender"
237 (define candidate-commands
238 (get-candidate-commands actor 'container-commands verb))
239 (<-reply message #:commands candidate-commands))
241 (define* (gameobj-get-contained-commands actor message #:key verb)
242 "Get commands as being contained (eg inventory) of commanding gameobj"
243 (define candidate-commands
244 (get-candidate-commands actor 'contained-commands verb))
246 #:commands candidate-commands
247 #:goes-by (gameobj-goes-by actor)))
249 (define* (gameobj-add-occupant! actor message #:key who)
250 "Add an actor to our list of present occupants"
251 (hash-set! (slot-ref actor 'occupants)
254 (define* (gameobj-remove-occupant! actor message #:key who)
255 "Remove an occupant from the room."
256 (hash-remove! (slot-ref actor 'occupants) who))
258 (define* (gameobj-occupants gameobj #:key exclude)
260 (lambda (occupant _ prev)
263 ;; Empty list and #f are non-exclusion
266 ;; A list of addresses... since our address object is (annoyingly)
267 ;; currently a simple cons cell...
268 ((exclude-1 ... exclude-rest)
269 (member occupant exclude))
270 ;; Must be an individual address!
271 (_ (equal? occupant exclude))))
274 (cons occupant prev)))
276 (slot-ref gameobj 'occupants)))
278 (define* (gameobj-get-occupants actor message #:key exclude)
279 "Get all present occupants of the room."
281 (gameobj-occupants actor #:exclude exclude))
282 (<-reply message occupants))
284 (define (gameobj-act-get-loc actor message)
285 (<-reply message (slot-ref actor 'loc)))
287 (define (gameobj-set-loc! gameobj loc)
288 "Set the location of this object."
289 (define old-loc (gameobj-loc gameobj))
290 (format #t "DEBUG: Location set to ~s for ~s\n"
291 loc (actor-id-actor gameobj))
293 (when (not (equal? old-loc loc))
294 (slot-set! gameobj 'loc loc)
295 ;; Change registation of where we currently are
297 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
299 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
301 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
302 (define* (gameobj-act-set-loc! actor message #:key loc)
303 "Action routine to set the location."
304 (gameobj-set-loc! actor loc))
306 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
307 "Do a slot-ref on gameobj, evaluating it including ourselves
308 and whos-asking, and see if we should just return it or run it."
309 (match (slot-ref gameobj slot)
310 ((? procedure? slot-val-proc)
311 (apply slot-val-proc gameobj whos-asking other-args))
312 (anything-else anything-else)))
314 (define gameobj-get-name (simple-slot-getter 'name))
316 (define* (gameobj-act-set-name! actor message val)
317 (slot-set! actor 'name val))
319 (define* (gameobj-get-desc actor message #:key whos-looking)
321 (match (slot-ref actor 'desc)
322 ((? procedure? desc-proc)
323 (desc-proc actor whos-looking))
325 (<-reply message desc-text))
327 (define (gameobj-visible-to-player? gameobj whos-looking)
328 "Check to see whether we're visible to the player or not.
329 By default, this is whether or not the generally-visible flag is set."
330 (not (slot-ref gameobj 'invisible?)))
332 (define* (gameobj-visible-name actor message #:key whos-looking)
334 (define we-are-visible
335 ((slot-ref actor 'visible-to-player?) actor whos-looking))
337 (define name-to-return
340 (match (slot-ref actor 'name)
341 ((? procedure? name-proc)
342 (name-proc actor whos-looking))
347 (<-reply message #:text name-to-return))
349 (define (gameobj-self-destruct gameobj)
350 "General gameobj self destruction routine"
351 ;; Unregister from being in any particular room
352 (gameobj-set-loc! gameobj #f)
353 (slot-set! gameobj 'destructed #t)
355 (self-destruct gameobj))
357 (define* (gameobj-act-self-destruct gameobj message #:key why)
358 "Action routine for self destruction"
359 (gameobj-self-destruct gameobj))
361 ;; Unless an actor has a tell message, we just ignore it
362 (define gameobj-tell-no-op
365 (define (gameobj-replace-data-occupants gameobj)
366 "The general purpose list of replacement data"
367 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
368 (slot-ref gameobj 'occupants))))
370 (define (gameobj-replace-data* gameobj)
371 ;; For now, just call gameobj-replace-data-occupants.
372 ;; But there may be more in the future!
373 (gameobj-replace-data-occupants gameobj))
375 ;; So sad that objects must assist in their replacement ;_;
376 ;; But that's life in a live hacked game!
377 (define (gameobj-act-assist-replace gameobj message)
378 "Vanilla method for assisting in self-replacement for live hacking"
379 (apply <-reply message
380 (gameobj-replace-data* gameobj)))
382 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
383 (call-with-values (lambda ()
384 (slot-ref-maybe-runcheck gameobj 'take-me?
385 whos-acting #:from #t))
386 ;; This allows this to reply with #:why-not if appropriate
388 (apply <-reply message args))))
390 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
391 (call-with-values (lambda ()
392 (slot-ref-maybe-runcheck gameobj 'drop-me?
394 ;; This allows this to reply with #:why-not if appropriate
396 (apply <-reply message args))))
399 ;;; Utilities every gameobj has
400 ;;; ---------------------------
402 (define (dyn-ref gameobj special-symbol)
403 "Dynamically look up a special object from the gm"
404 (match special-symbol
405 ;; if it's a symbol, look it up dynamically
407 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
408 #:symbol special-symbol)))
409 ;; if it's false, return nothing
411 ;; otherwise it's probably an address, return it as-is
419 (define* (cmd-take gameobj message
421 (player (message-from message)))
423 (mbody-val (<-wait player 'get-name)))
425 (mbody-val (<-wait player 'get-loc)))
426 (define our-name (slot-ref gameobj 'name))
427 (define self-should-take
428 (slot-ref-maybe-runcheck gameobj 'take-me? player))
429 ;; @@: Is there any reason to allow the room to object in the way
430 ;; that there is for dropping? It doesn't seem like it.
431 (call-with-values (lambda ()
432 (slot-ref-maybe-runcheck gameobj 'take-me? player))
433 (lambda* (self-should-take #:key (why-not
434 `("It doesn't seem like you can take "
437 ;; Set the location to whoever's picking us up
439 (gameobj-set-loc! gameobj player)
441 #:text (format #f "You pick up ~a.\n"
443 (<- player-loc 'tell-room
444 #:text (format #f "~a picks up ~a.\n"
448 (<- player 'tell #:text why-not)))))
450 (define* (cmd-drop gameobj message
452 (player (message-from message)))
454 (mbody-val (<-wait player 'get-name)))
456 (mbody-val (<-wait player 'get-loc)))
457 (define our-name (slot-ref gameobj 'name))
459 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
460 (define (room-objection-to-drop)
461 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
462 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
464 ;; Either give the specified reason, or give a boilerplate one
466 `("You'd love to drop " ,our-name
467 " but for some reason it doesn't seem like you can"
468 " do that here.")))))
472 #:text `("It doesn't seem like you can drop " ,our-name
473 " here, because you don't seem to be anywhere?!?")))
474 ;; TODO: Let ourselves supply a reason why not.
477 #:text (format #f "It doesn't seem like you can drop ~a.\n"
479 ((room-objection-to-drop)
481 #:text room-objection-to-drop))
483 (gameobj-set-loc! gameobj player-loc)
484 ;; TODO: Allow more flavortext here.
486 #:text (format #f "You drop ~a.\n"
488 (<- player-loc 'tell-room
489 #:text (format #f "~a drops ~a.\n"
494 ;; @@: Moving this to a container subclass/mixin could allow a lot more
495 ;; customization of take out / put in phrases
496 (define* (cmd-take-from gameobj message
497 #:key direct-obj indir-obj preposition
498 (player (message-from message)))
500 (mbody-val (<-wait player 'get-name)))
502 (mbody-val (<-wait player 'get-loc)))
503 (define our-name (slot-ref gameobj 'name))
504 ;; We need to check if we even have such a thing
508 (for-each (lambda (occupant)
509 (define goes-by (mbody-val (<-wait occupant 'goes-by)))
510 (when (ci-member direct-obj goes-by)
512 (gameobj-occupants gameobj))
515 (define (this-thing-name)
516 (mbody-val (<-wait this-thing 'get-name)))
517 (define (should-take-from-me)
519 (slot-ref-maybe-runcheck gameobj 'take-from-me? player this-thing)))
520 (define (default-objection)
521 `("Unfortunately, it doesn't seem like you can take "
522 (this-thing-name) " " preposition " " our-name "."))
524 (define (this-thing-objection)
525 (mbody-receive (_ taken-ok? #:key why-not) ; does the object object to being removed?
526 (<-wait this-thing 'ok-to-be-taken-from? player) ; @@ no need to supply from where
528 ;; Either give the specified reason, or give a boilerplate one
530 (default-objection)))))
532 ;; Wait, aren't we going to check (should-take-from-me) later?
533 ;; Well yes, but this checks if there's a #f as the value, which
534 ;; is a much clearer indication that this doesn't take *anything*.
535 ((not (slot-ref gameobj 'take-from-me?))
537 #:text `("It's not really clear how to take something " ,preposition
540 ;; Unfortunately this does leak information about what is contained
541 ;; by us. Maybe not what's wanted in all circumstances.
544 #:text `("You don't see any such " ,direct-obj " to take from "
546 ;; A particular objection to taking this thing.
547 ;; We should allow customizing the reason here, which could be
548 ;; provided by the 'ok-to-be-taken-from? slot.
549 ((not (should-take-from-me))
551 #:text (default-objection)))
552 ;; the thing we wsant to take itself has objected...
553 ((this-thing-objection) =>
557 ;; looks like we can take it
559 ;; Wait to announce to the player just in case settting the location
560 ;; errors out or something. Maybe it's overthinking things, I dunno.
561 (<-wait this-thing 'set-loc! #:loc player)
563 #:text `("You take " ,(this-thing-name) " from "
565 (<- player-loc 'tell-room
566 #:text `(,player-name " takes " ,(this-thing-name) " from "
570 (define* (cmd-put-in gameobj message
571 #:key direct-obj indir-obj preposition
572 (player (message-from message)))
574 (mbody-val (<-wait player 'get-name)))
576 (mbody-val (<-wait player 'get-loc)))
577 (define our-name (slot-ref gameobj 'name))
578 ;; We need to check if we even have such a thing
582 (for-each (lambda (occupant)
583 (define goes-by (mbody-val (<-wait occupant 'goes-by)))
584 (when (ci-member direct-obj goes-by)
586 (mbody-val (<-wait player 'get-occupants)))
589 (define (this-thing-name)
590 (mbody-val (<-wait this-thing 'get-name)))
591 (define (should-put-in-me)
593 (slot-ref-maybe-runcheck gameobj 'put-in-me? player this-thing)))
594 (define (default-objection)
595 `("As much as you'd like to, it doesn't seem like you can put "
596 ,(this-thing-name) " " ,preposition " " ,our-name "."))
597 (define (this-thing-objection)
598 (mbody-receive (_ put-in-ok? #:key why-not) ; does the object object to being moved?
599 (<-wait this-thing 'ok-to-be-put-in? player (actor-id gameobj))
600 (and (not put-in-ok?)
601 ;; Either give the specified reason, or give a boilerplate one
602 (or why-not (default-objection)))))
604 ;; Is it not there, or maybe we won't allow it to be taken?
607 #:text `("You don't seem to have any such " ,direct-obj " to put "
608 ,preposition " " ,our-name ".")))
610 ((or (not (should-put-in-me)))
612 #:text (default-objection)))
613 ;; the thing we wsant to take itself has objected...
614 ((this-thing-objection) =>
618 ;; looks like we can take it
620 ;; Wait to announce to the player just in case settting the location
621 ;; errors out or something. Maybe it's overthinking things, I dunno.
622 (<-wait this-thing 'set-loc! #:loc (actor-id gameobj))
624 #:text `("You put " ,(this-thing-name) " " ,preposition " "
626 (<- player-loc 'tell-room
627 #:text `(,player-name " puts " ,(this-thing-name) " " ,preposition " "