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)
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" "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 (gameobj-setup-props gameobj)
245 (define class (class-of gameobj))
246 (define props (slot-ref gameobj 'props))
247 (maybe-build-rmeta-slot-cache! class 'initial-props
248 eq? hashq-set! hashq-ref)
249 ;; Kind of a kludge... we read through the rmeta-slot-cache
250 ;; and use that to build up the table
253 (when (not (hashq-ref props key value)) ; don't override init'ed instance values
254 (hashq-set! props key value)))
255 (rmeta-slot-cache (class-slot-ref class 'initial-props))))
257 ;; TODO: Use the *init* action?
258 ;; We could also use a generic method if they didn't have
259 ;; what I'm pretty sure is O(n) dispatch in GOOPS...
260 (define* (gameobj-act-init actor message #:key replace)
261 "Your most basic game object init procedure."
262 (gameobj-setup-props actor)
263 (run-replacement actor replace gameobj-replace-steps*))
265 (define* (gameobj-get-prop gameobj key #:optional dflt)
266 (hashq-ref (slot-ref gameobj 'props) key dflt))
268 (define* (gameobj-set-prop! gameobj key val)
269 (hashq-set! (slot-ref gameobj 'props) key val))
271 (define* (gameobj-act-get-prop actor message key #:optional dflt)
272 (gameobj-get-prop actor key dflt))
274 (define (gameobj-goes-by gameobj)
275 "Find the name we go by. Defaults to #:name if nothing else provided."
276 (cond ((slot-ref gameobj 'goes-by) =>
278 ((slot-ref gameobj 'name) =>
283 (define (gameobj-act-goes-by actor message)
284 "Reply to a message requesting what we go by."
285 (<-reply message (gameobj-goes-by actor)))
287 (define (val-or-run val-or-proc)
288 "Evaluate if a procedure, or just return otherwise"
289 (if (procedure? val-or-proc)
293 (define (get-candidate-commands actor rmeta-sym verb)
294 (class-rmeta-ref (class-of actor) rmeta-sym verb
297 (define* (gameobj-get-commands actor message #:key verb)
298 "Get commands a co-occupant of the room might execute for VERB"
299 (define candidate-commands
300 (get-candidate-commands actor 'commands verb))
302 #:commands candidate-commands
303 #:goes-by (gameobj-goes-by actor)))
305 (define* (gameobj-get-container-dom-commands actor message #:key verb)
306 "Get (dominant) commands as the container / room of message's sender"
307 (define candidate-commands
308 (get-candidate-commands actor 'container-dom-commands verb))
309 (<-reply message #:commands candidate-commands))
311 (define* (gameobj-get-container-sub-commands actor message #:key verb)
312 "Get (subordinate) commands as the container / room of message's sender"
313 (define candidate-commands
314 (get-candidate-commands actor 'container-sub-commands verb))
315 (<-reply message #:commands candidate-commands))
317 (define* (gameobj-get-contained-commands actor message #:key verb)
318 "Get commands as being contained (eg inventory) of commanding gameobj"
319 (define candidate-commands
320 (get-candidate-commands actor 'contained-commands verb))
322 #:commands candidate-commands
323 #:goes-by (gameobj-goes-by actor)))
325 (define* (gameobj-add-occupant! actor message #:key who)
326 "Add an actor to our list of present occupants"
327 (hash-set! (slot-ref actor 'occupants)
330 (define* (gameobj-remove-occupant! actor message #:key who)
331 "Remove an occupant from the room."
332 (hash-remove! (slot-ref actor 'occupants) who))
334 (define* (gameobj-occupants gameobj #:key exclude)
336 (lambda (occupant _ prev)
339 ;; Empty list and #f are non-exclusion
342 ;; A list of addresses... since our address object is (annoyingly)
343 ;; currently a simple cons cell...
344 ((exclude-1 ... exclude-rest)
345 (member occupant exclude))
346 ;; Must be an individual address!
347 (_ (equal? occupant exclude))))
350 (cons occupant prev)))
352 (slot-ref gameobj 'occupants)))
354 (define* (gameobj-get-occupants actor message #:key exclude)
355 "Get all present occupants of the room."
357 (gameobj-occupants actor #:exclude exclude))
358 (<-reply message occupants))
360 (define (gameobj-act-get-loc actor message)
361 (<-reply message (slot-ref actor 'loc)))
363 (define (gameobj-set-loc! gameobj loc)
364 "Set the location of this object."
365 (define old-loc (gameobj-loc gameobj))
366 (format #t "DEBUG: Location set to ~s for ~s\n"
367 loc (actor-id-actor gameobj))
369 (when (not (equal? old-loc loc))
370 (slot-set! gameobj 'loc loc)
371 ;; Change registation of where we currently are
373 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
375 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
377 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
378 (define* (gameobj-act-set-loc! actor message #:key loc)
379 "Action routine to set the location."
380 (gameobj-set-loc! actor loc))
382 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
383 "Do a slot-ref on gameobj, evaluating it including ourselves
384 and whos-asking, and see if we should just return it or run it."
385 (match (slot-ref gameobj slot)
386 ((? procedure? slot-val-proc)
387 (apply slot-val-proc gameobj whos-asking other-args))
388 (anything-else anything-else)))
390 (define gameobj-get-name (simple-slot-getter 'name))
392 (define* (gameobj-act-set-name! actor message val)
393 (slot-set! actor 'name val))
395 (define* (gameobj-desc gameobj #:key whos-looking)
396 (match (slot-ref gameobj 'desc)
397 ((? procedure? desc-proc)
398 (desc-proc gameobj whos-looking))
401 (define* (gameobj-get-desc actor message #:key whos-looking)
402 "This is the action equivalent of the gameobj-desc getter"
403 (<-reply message (gameobj-desc actor #:whos-looking whos-looking)))
405 (define (gameobj-visible-to-player? gameobj whos-looking)
406 "Check to see whether we're visible to the player or not.
407 By default, this is whether or not the generally-visible flag is set."
408 (not (slot-ref gameobj 'invisible?)))
410 (define* (gameobj-visible-name actor message #:key whos-looking)
412 (define we-are-visible
413 ((slot-ref actor 'visible-to-player?) actor whos-looking))
415 (define name-to-return
418 (match (slot-ref actor 'name)
419 ((? procedure? name-proc)
420 (name-proc actor whos-looking))
425 (<-reply message #:text name-to-return))
427 (define (gameobj-self-destruct gameobj)
428 "General gameobj self destruction routine"
429 ;; Unregister from being in any particular room
430 (gameobj-set-loc! gameobj #f)
431 (slot-set! gameobj 'destructed #t)
433 (self-destruct gameobj))
435 (define* (gameobj-act-self-destruct gameobj message #:key why)
436 "Action routine for self destruction"
437 (gameobj-self-destruct gameobj))
439 ;; Unless an actor has a tell message, we just ignore it
440 (define gameobj-tell-no-op
443 (define (gameobj-replace-data-occupants gameobj)
444 "The general purpose list of replacement data"
445 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
446 (slot-ref gameobj 'occupants))))
448 (define (gameobj-replace-data* gameobj)
449 ;; For now, just call gameobj-replace-data-occupants.
450 ;; But there may be more in the future!
451 (gameobj-replace-data-occupants gameobj))
453 ;; So sad that objects must assist in their replacement ;_;
454 ;; But that's life in a live hacked game!
455 (define (gameobj-act-assist-replace gameobj message)
456 "Vanilla method for assisting in self-replacement for live hacking"
457 (apply <-reply message
458 (gameobj-replace-data* gameobj)))
460 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
461 (call-with-values (lambda ()
462 (slot-ref-maybe-runcheck gameobj 'take-me?
463 whos-acting #:from #t))
464 ;; This allows this to reply with #:why-not if appropriate
466 (apply <-reply message args))))
468 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
469 (call-with-values (lambda ()
470 (slot-ref-maybe-runcheck gameobj 'drop-me?
472 ;; This allows this to reply with #:why-not if appropriate
474 (apply <-reply message args))))
477 ;;; Utilities every gameobj has
478 ;;; ---------------------------
480 (define (dyn-ref gameobj special-symbol)
481 "Dynamically look up a special object from the gm"
482 (match special-symbol
483 ;; if it's a symbol, look it up dynamically
485 ;; TODO: If we get back an #f at this point, should we throw
486 ;; an error? Obviously #f is okay, but maybe not if
487 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
488 #:symbol special-symbol)))
489 ;; if it's false, return nothing
491 ;; otherwise it's probably an address, return it as-is
499 (define %formless-desc
500 "You don't see anything special.")
502 (define* (cmd-look-at gameobj message
504 (player (message-from message)))
506 (or (gameobj-desc gameobj #:whos-looking player)
508 (<- player 'tell #:text desc)))
510 (define* (cmd-take gameobj message
512 (player (message-from message)))
514 (mbody-val (<-wait player 'get-name)))
516 (mbody-val (<-wait player 'get-loc)))
517 (define our-name (slot-ref gameobj 'name))
518 (define self-should-take
519 (slot-ref-maybe-runcheck gameobj 'take-me? player))
520 ;; @@: Is there any reason to allow the room to object in the way
521 ;; that there is for dropping? It doesn't seem like it.
522 (call-with-values (lambda ()
523 (slot-ref-maybe-runcheck gameobj 'take-me? player))
524 (lambda* (self-should-take #:key (why-not
525 `("It doesn't seem like you can take "
528 ;; Set the location to whoever's picking us up
530 (gameobj-set-loc! gameobj player)
532 #:text (format #f "You pick up ~a.\n"
534 (<- player-loc 'tell-room
535 #:text (format #f "~a picks up ~a.\n"
539 (<- player 'tell #:text why-not)))))
541 (define* (cmd-drop gameobj message
543 (player (message-from message)))
545 (mbody-val (<-wait player 'get-name)))
547 (mbody-val (<-wait player 'get-loc)))
548 (define our-name (slot-ref gameobj 'name))
550 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
551 (define (room-objection-to-drop)
552 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
553 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
555 ;; Either give the specified reason, or give a boilerplate one
557 `("You'd love to drop " ,our-name
558 " but for some reason it doesn't seem like you can"
559 " do that here.")))))
563 #:text `("It doesn't seem like you can drop " ,our-name
564 " here, because you don't seem to be anywhere?!?")))
565 ;; TODO: Let ourselves supply a reason why not.
568 #:text (format #f "It doesn't seem like you can drop ~a.\n"
570 ((room-objection-to-drop)
572 #:text room-objection-to-drop))
574 (gameobj-set-loc! gameobj player-loc)
575 ;; TODO: Allow more flavortext here.
577 #:text (format #f "You drop ~a.\n"
579 (<- player-loc 'tell-room
580 #:text (format #f "~a drops ~a.\n"
585 (define* (cmd-take-from-no-op gameobj message
586 #:key direct-obj indir-obj preposition
587 (player (message-from message)))
589 #:text `("It doesn't seem like you can take anything "
591 ,(slot-ref gameobj 'name) ".")))
593 (define* (cmd-put-in-no-op gameobj message
594 #:key direct-obj indir-obj preposition
595 (player (message-from message)))
597 #:text `("It doesn't seem like you can put anything "
599 ,(slot-ref gameobj 'name) ".")))