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 actors)
25 #:use-module (8sync agenda)
26 #:use-module (8sync rmeta-slot)
27 #:use-module (srfi srfi-1)
28 #:use-module (ice-9 control)
29 #:use-module (ice-9 format)
30 #:use-module (ice-9 match)
31 #:use-module (oop goops)
42 slot-ref-maybe-runcheck
51 ;;; *all* game components that talk to players should somehow
52 ;;; derive from this class.
53 ;;; And all of them need a GM!
55 (define-class <gameobj> (<actor>)
60 ;; Uses a hash table like a set (values ignored)
61 (occupants #:init-thunk make-hash-table)
64 (gm #:init-keyword #:gm
67 ;; a name to be known by
68 (name #:init-keyword #:name
70 (goes-by #:init-keyword #:goes-by
74 #:init-keyword #:desc)
76 ;; Commands we can handle
77 (commands #:allocation #:each-subclass
78 #:init-thunk (build-commands
79 ("take" ((direct-command cmd-take)
80 (prep-indir-command cmd-take-from
82 ("put" ((prep-indir-command cmd-put-in
83 '("in" "inside" "on"))))))
85 ;; Commands we can handle by being something's container
86 (container-commands #:allocation #:each-subclass
87 #:init-thunk (build-commands))
89 ;; Commands we can handle by being contained by something else
90 (contained-commands #:allocation #:each-subclass
93 ("drop" ((direct-command cmd-drop #:obvious? #f)))))
95 ;; Most objects are generally visible by default
96 (invisible? #:init-value #f
97 #:init-keyword #:invisible?)
98 ;; TODO: Fold this into a procedure in invisible? similar
99 ;; to take-me? and etc
101 #:init-value (wrap-apply gameobj-visible-to-player?))
103 ;; Can be a boolean or a procedure accepting
104 ;; (gameobj whos-acting #:key from)
105 (take-me? #:init-value #f
106 #:init-keyword #:take-me?)
107 ;; Can be a boolean or a procedure accepting
108 ;; (gameobj whos-acting where)
109 (drop-me? #:init-value #t
110 #:init-keyword #:drop-me?)
111 ;; Can be a boolean or a procedure accepting
112 ;; (gameobj whos-acting take-what)
113 (take-from-me? #:init-value #f
114 #:init-keyword #:take-from-me?)
115 ;; Can be a boolean or a procedure accepting
116 ;; (gameobj whos-acting put-what)
117 (put-in-me? #:init-value #f
118 #:init-keyword #:put-in-me?)
120 ;; TODO: Remove this and use actor-alive? instead.
121 ;; Set this on self-destruct
122 ;; (checked by some "long running" game routines)
123 (destructed #:init-value #f)
125 (actions #:allocation #:each-subclass
126 ;;; Actions supported by all gameobj
129 (init gameobj-act-init)
130 ;; Commands for co-occupants
131 (get-commands gameobj-get-commands)
132 ;; Commands for participants in a room
133 (get-container-commands gameobj-get-container-commands)
134 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
135 (get-contained-commands gameobj-get-contained-commands)
137 (get-occupants gameobj-get-occupants)
138 (add-occupant! gameobj-add-occupant!)
139 (remove-occupant! gameobj-remove-occupant!)
140 (get-loc gameobj-act-get-loc)
141 (set-loc! gameobj-act-set-loc!)
142 (get-name gameobj-get-name)
143 (set-name! gameobj-act-set-name!)
144 (get-desc gameobj-get-desc)
145 (goes-by gameobj-act-goes-by)
146 (visible-name gameobj-visible-name)
147 (self-destruct gameobj-act-self-destruct)
148 (tell gameobj-tell-no-op)
149 (assist-replace gameobj-act-assist-replace)
150 (ok-to-drop-here? (lambda (gameobj message . _)
151 (<-reply message #t))) ; ok to drop by default
152 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
153 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
157 (cmd-take-from cmd-take-from)
158 (cmd-put-in cmd-put-in)
159 (cmd-drop cmd-drop))))
162 ;;; gameobj message handlers
163 ;;; ========================
165 ;; Kind of a useful utility, maybe?
166 (define (simple-slot-getter slot)
167 (lambda (actor message)
168 (<-reply message (slot-ref actor slot))))
170 (define (gameobj-replace-step-occupants actor occupants)
171 ;; Snarf all the occupants!
172 (display "replacing occupant\n")
176 (<-wait occupant 'set-loc!
177 #:loc (actor-id actor)))
180 (define gameobj-replace-steps*
181 (list gameobj-replace-step-occupants))
183 (define (run-replacement actor replaces replace-steps)
185 (mbody-receive (_ #:key occupants)
186 (<-wait replaces 'assist-replace)
188 (lambda (replace-step)
189 (replace-step actor occupants))
192 ;; @@: This could be kind of a messy way of doing gameobj-act-init
193 ;; stuff. If only we had generic methods :(
194 (define* (gameobj-act-init actor message #:key replace)
195 "Your most basic game object init procedure.
196 Assists in its replacement of occupants if necessary and nothing else."
197 (run-replacement actor replace gameobj-replace-steps*))
199 (define (gameobj-goes-by gameobj)
200 "Find the name we go by. Defaults to #:name if nothing else provided."
201 (cond ((slot-ref gameobj 'goes-by) =>
203 ((slot-ref gameobj 'name) =>
208 (define (gameobj-act-goes-by actor message)
209 "Reply to a message requesting what we go by."
210 (<-reply message #:goes-by (gameobj-goes-by actor)))
212 (define (val-or-run val-or-proc)
213 "Evaluate if a procedure, or just return otherwise"
214 (if (procedure? val-or-proc)
218 (define (get-candidate-commands actor rmeta-sym verb)
219 (class-rmeta-ref (class-of actor) rmeta-sym verb
222 (define* (gameobj-get-commands actor message #:key verb)
223 "Get commands a co-occupant of the room might execute for VERB"
224 (define candidate-commands
225 (get-candidate-commands actor 'commands verb))
227 #:commands candidate-commands
228 #:goes-by (gameobj-goes-by actor)))
230 (define* (gameobj-get-container-commands actor message #:key verb)
231 "Get commands as the container / room of message's sender"
232 (define candidate-commands
233 (get-candidate-commands actor 'container-commands verb))
234 (<-reply message #:commands candidate-commands))
236 (define* (gameobj-get-contained-commands actor message #:key verb)
237 "Get commands as being contained (eg inventory) of commanding gameobj"
238 (define candidate-commands
239 (get-candidate-commands actor 'contained-commands verb))
241 #:commands candidate-commands
242 #:goes-by (gameobj-goes-by actor)))
244 (define* (gameobj-add-occupant! actor message #:key who)
245 "Add an actor to our list of present occupants"
246 (hash-set! (slot-ref actor 'occupants)
249 (define* (gameobj-remove-occupant! actor message #:key who)
250 "Remove an occupant from the room."
251 (hash-remove! (slot-ref actor 'occupants) who))
253 (define* (gameobj-occupants gameobj #:key exclude)
255 (lambda (occupant _ prev)
258 ;; Empty list and #f are non-exclusion
261 ;; A list of addresses... since our address object is (annoyingly)
262 ;; currently a simple cons cell...
263 ((exclude-1 ... exclude-rest)
264 (member occupant exclude))
265 ;; Must be an individual address!
266 (_ (equal? occupant exclude))))
269 (cons occupant prev)))
271 (slot-ref gameobj 'occupants)))
273 (define* (gameobj-get-occupants actor message #:key exclude)
274 "Get all present occupants of the room."
276 (gameobj-occupants actor #:exclude exclude))
277 (<-reply message occupants))
279 (define (gameobj-act-get-loc actor message)
280 (<-reply message (slot-ref actor 'loc)))
282 (define (gameobj-set-loc! gameobj loc)
283 "Set the location of this object."
284 (define old-loc (gameobj-loc gameobj))
285 (format #t "DEBUG: Location set to ~s for ~s\n"
286 loc (actor-id-actor gameobj))
288 (when (not (equal? old-loc loc))
289 (slot-set! gameobj 'loc loc)
290 ;; Change registation of where we currently are
292 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
294 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
296 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
297 (define* (gameobj-act-set-loc! actor message #:key loc)
298 "Action routine to set the location."
299 (gameobj-set-loc! actor loc))
301 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
302 "Do a slot-ref on gameobj, evaluating it including ourselves
303 and whos-asking, and see if we should just return it or run it."
304 (match (slot-ref gameobj slot)
305 ((? procedure? slot-val-proc)
306 (apply slot-val-proc gameobj whos-asking other-args))
307 (anything-else anything-else)))
309 (define gameobj-get-name (simple-slot-getter 'name))
311 (define* (gameobj-act-set-name! actor message val)
312 (slot-set! actor 'name val))
314 (define* (gameobj-get-desc actor message #:key whos-looking)
316 (match (slot-ref actor 'desc)
317 ((? procedure? desc-proc)
318 (desc-proc actor whos-looking))
320 (<-reply message desc-text))
322 (define (gameobj-visible-to-player? gameobj whos-looking)
323 "Check to see whether we're visible to the player or not.
324 By default, this is whether or not the generally-visible flag is set."
325 (not (slot-ref gameobj 'invisible?)))
327 (define* (gameobj-visible-name actor message #:key whos-looking)
329 (define we-are-visible
330 ((slot-ref actor 'visible-to-player?) actor whos-looking))
332 (define name-to-return
335 (match (slot-ref actor 'name)
336 ((? procedure? name-proc)
337 (name-proc actor whos-looking))
342 (<-reply message #:text name-to-return))
344 (define (gameobj-self-destruct gameobj)
345 "General gameobj self destruction routine"
346 ;; Unregister from being in any particular room
347 (gameobj-set-loc! gameobj #f)
348 (slot-set! gameobj 'destructed #t)
350 (self-destruct gameobj))
352 (define* (gameobj-act-self-destruct gameobj message #:key why)
353 "Action routine for self destruction"
354 (gameobj-self-destruct gameobj))
356 ;; Unless an actor has a tell message, we just ignore it
357 (define gameobj-tell-no-op
360 (define (gameobj-replace-data-occupants gameobj)
361 "The general purpose list of replacement data"
362 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
363 (slot-ref gameobj 'occupants))))
365 (define (gameobj-replace-data* gameobj)
366 ;; For now, just call gameobj-replace-data-occupants.
367 ;; But there may be more in the future!
368 (gameobj-replace-data-occupants gameobj))
370 ;; So sad that objects must assist in their replacement ;_;
371 ;; But that's life in a live hacked game!
372 (define (gameobj-act-assist-replace gameobj message)
373 "Vanilla method for assisting in self-replacement for live hacking"
374 (apply <-reply message
375 (gameobj-replace-data* gameobj)))
377 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
378 (<-reply message (slot-ref-maybe-runcheck gameobj 'take-me?
379 whos-acting #:from #t)))
381 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
382 (<-reply message (slot-ref-maybe-runcheck gameobj 'drop-me?
386 ;;; Utilities every gameobj has
387 ;;; ---------------------------
389 (define (dyn-ref gameobj special-symbol)
390 "Dynamically look up a special object from the gm"
391 (match special-symbol
392 ;; if it's a symbol, look it up dynamically
394 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
395 #:symbol special-symbol)))
396 ;; if it's false, return nothing
398 ;; otherwise it's probably an address, return it as-is
406 (define* (cmd-take gameobj message #:key direct-obj)
407 (define player (message-from message))
409 (mbody-val (<-wait player 'get-name)))
411 (mbody-val (<-wait player 'get-loc)))
412 (define our-name (slot-ref gameobj 'name))
413 (define self-should-take
414 (slot-ref-maybe-runcheck gameobj 'take-me? player))
415 ;; @@: Is there any reason to allow the room to object in the way
416 ;; that there is for dropping? It doesn't seem like it.
417 ;; TODO: Allow gameobj to customize
419 ;; Set the location to whoever's picking us up
421 (gameobj-set-loc! gameobj player)
423 #:text (format #f "You pick up ~a.\n"
425 (<- player-loc 'tell-room
426 #:text (format #f "~a picks up ~a.\n"
431 #:text (format #f "It doesn't seem like you can take ~a.\n"
434 (define* (cmd-drop gameobj message #:key direct-obj)
435 (define player (message-from message))
437 (mbody-val (<-wait player 'get-name)))
439 (mbody-val (<-wait player 'get-loc)))
440 (define our-name (slot-ref gameobj 'name))
442 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
443 (define (room-objection-to-drop)
444 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
445 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
447 ;; Either give the specified reason, or give a boilerplate one
449 `("You'd love to drop " ,our-name
450 " but for some reason it doesn't seem like you can"
451 " do that here.")))))
455 #:text `("It doesn't seem like you can drop " ,our-name
456 " here, because you don't seem to be anywhere?!?")))
457 ;; TODO: Let ourselves supply a reason why not.
460 #:text (format #f "It doesn't seem like you can drop ~a.\n"
462 ((room-objection-to-drop)
464 #:text room-objection-to-drop))
466 (gameobj-set-loc! gameobj player-loc)
467 ;; TODO: Allow more flavortext here.
469 #:text (format #f "You drop ~a.\n"
471 (<- player-loc 'tell-room
472 #:text (format #f "~a drops ~a.\n"
477 (define* (cmd-take-from gameobj message
478 #:key direct-obj indir-obj preposition)
479 (define player (message-from message))
481 (mbody-val (<-wait player 'get-name)))
483 (mbody-val (<-wait player 'get-loc)))
484 (define our-name (slot-ref gameobj 'name))
485 ;; We need to check if we even have such a thing
486 (define thing-to-take
489 (for-each (lambda (occupant)
490 (mbody-receive (_ #:key goes-by)
491 (<-wait occupant 'goes-by)
492 (when (member direct-obj goes-by)
494 (gameobj-occupants gameobj))
497 (define (should-take-from-me)
499 (slot-ref-maybe-runcheck gameobj 'take-from-me? player thing-to-take)))
500 ;; @@: Right now we give the same response to both something not being
501 ;; an occupant and to not being permitted to be removed. This isn't
502 ;; very rich and maybe not as helpful as it could be. Right now I'm
503 ;; trying to avoid "leaking" information about if an object isn't there.
504 ;; However maybe by making the different responses as slots which can be
505 ;; set, this wouldn't be a problem.
506 (define default-objection
507 "As much as you'd like to take it, it doesn't seem like you can.")
508 (define (thing-to-take-objection)
509 (mbody-receive (_ taken-ok? #:key why-not) ; does the object object to being removed?
510 (<-wait thing-to-take 'ok-to-be-taken-from? player) ; @@ no need to supply from where
512 ;; Either give the specified reason, or give a boilerplate one
513 (or why-not default-objection))))
515 ;; Is it not there, or maybe we won't allow it to be taken?
516 ((or (not thing-to-take) (not (should-take-from-me)))
517 (<- (message-from message) 'tell
518 #:text default-objection))
519 ;; the thing we wsant to take itself has objected...
520 ((thing-to-take-objection) =>
522 (<- (message-from message) 'tell
524 ;; looks like we can take it
526 (let ((thing-to-take-name
527 (mbody-val (<-wait thing-to-take 'get-name))))
528 ;; Wait to announce to the player just in case settting the location
529 ;; errors out or something. Maybe it's overthinking things, I dunno.
530 (<-wait thing-to-take 'set-loc! #:loc player)
532 #:text `("You take " ,thing-to-take-name " from "
534 (<- player-loc 'tell-room
535 #:text `(,player-name " takes " ,thing-to-take-name " from "
537 #:exclude player)))))
539 (define* (cmd-put-in gameobj message
540 #:key direct-obj indir-obj preposition)
541 (define player (message-from message))
543 (mbody-val (<-wait player 'get-name)))
545 (mbody-val (<-wait player 'get-loc)))
546 (define our-name (slot-ref gameobj 'name))
547 ;; We need to check if we even have such a thing
551 (for-each (lambda (occupant)
552 (mbody-receive (_ #:key goes-by)
553 (<-wait occupant 'goes-by)
554 (when (member direct-obj goes-by)
556 (mbody-val (<-wait player 'get-occupants)))
559 (define (should-put-in-me)
561 (slot-ref-maybe-runcheck gameobj 'put-in-me? player this-thing)))
562 ;; @@: Right now we give the same response to both something not being
563 ;; an occupant and to not being permitted to be removed. This isn't
564 ;; very rich and maybe not as helpful as it could be. Right now I'm
565 ;; trying to avoid "leaking" information about if an object isn't there.
566 ;; However maybe by making the different responses as slots which can be
567 ;; set, this wouldn't be a problem.
568 (define default-objection
569 "As much as you'd like to, it doesn't seem like you can put that in there.")
570 (define (this-thing-objection)
571 (mbody-receive (_ put-in-ok? #:key why-not) ; does the object object to being moved?
572 (<-wait this-thing 'ok-to-be-put-in? player (actor-id gameobj))
573 (and (not put-in-ok?)
574 ;; Either give the specified reason, or give a boilerplate one
575 (or why-not default-objection))))
577 ;; Is it not there, or maybe we won't allow it to be taken?
578 ((or (not this-thing) (not (should-put-in-me)))
579 (<- (message-from message) 'tell
580 #:text default-objection))
581 ;; the thing we wsant to take itself has objected...
582 ((this-thing-objection) =>
584 (<- (message-from message) 'tell
586 ;; looks like we can take it
588 (let ((this-thing-name
589 (mbody-val (<-wait this-thing 'get-name))))
590 ;; Wait to announce to the player just in case settting the location
591 ;; errors out or something. Maybe it's overthinking things, I dunno.
592 (<-wait this-thing 'set-loc! #:loc (actor-id gameobj))
594 #:text `("You put " ,this-thing-name " in "
596 (<- player-loc 'tell-room
597 #:text `(,player-name " puts " ,this-thing-name " in "
599 #:exclude player)))))