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 (mudsync receive-star)
26 #:use-module (8sync actors)
27 #:use-module (8sync agenda)
28 #:use-module (8sync rmeta-slot)
29 #:use-module (srfi srfi-1)
30 #:use-module (ice-9 control)
31 #:use-module (ice-9 format)
32 #:use-module (ice-9 match)
33 #:use-module (oop goops)
46 slot-ref-maybe-runcheck
52 ;; Some of the more common commands
55 cmd-take-from-no-op cmd-put-in-no-op))
60 (define build-props build-rmeta-slot)
62 ;;; *all* game components that talk to players should somehow
63 ;;; derive from this class.
64 ;;; And all of them need a GM!
66 (define-class <gameobj> (<actor>)
71 ;; Uses a hash table like a set (values ignored)
72 (occupants #:init-thunk make-hash-table)
75 (gm #:init-keyword #:gm
78 ;; a name to be known by
79 (name #:init-keyword #:name
81 (goes-by #:init-keyword #:goes-by
85 #:init-keyword #:desc)
87 ;; @@: Maybe commands should be renamed to verbs, I dunno
88 ;; Commands we can handle
89 (commands #:allocation #:each-subclass
90 #:init-thunk (build-commands
91 (("l" "look") ((direct-command cmd-look-at)))
92 ("take" ((direct-command cmd-take)
93 (prep-indir-command cmd-take-from
95 ("put" ((prep-indir-command cmd-put-in
96 '("in" "inside" "into" "on"))))))
98 ;; Commands we can handle by being something's container
99 ;; dominant version (goes before everything)
100 (container-dom-commands #:allocation #:each-subclass
101 #:init-thunk (build-commands))
102 ;; subordinate version (goes after everything)
103 (container-sub-commands #:allocation #:each-subclass
104 #:init-thunk (build-commands))
106 ;; Commands we can handle by being contained by something else
107 (contained-commands #:allocation #:each-subclass
110 (("l" "look") ((direct-command cmd-look-at)))
111 ("drop" ((direct-command cmd-drop #:obvious? #f)))))
113 ;; The extremely squishy concept of "props"... properties!
114 ;; These are flags, etc etc of various types. This is a hashq table.
115 ;; These have upsides and downsides, but the big upside is that you can
116 ;; query a "prop" of a prospective gameobj without knowing what type of
117 ;; gameobj that is, and not fear some kind of breakage.
119 ;; props by default only have a 'get-prop read-only action handler;
120 ;; any coordination of setting a prop between actors must be
121 ;; added to that actor, to keep things from getting out of control.
122 (props #:init-thunk make-hash-table
123 #:init-keyword #:props)
124 ;; gameobjs may inherit an initial list of these via the
125 ;; initial-props slot, which must always have its
126 ;; #:allocation #:each-subclass and use (build-props) for the
128 ;; The vanilla gameobj has no props, on purpose.
129 (initial-props #:allocation #:each-subclass
130 #:init-thunk (build-props '()))
132 ;; Most objects are generally visible by default
133 (invisible? #:init-value #f
134 #:init-keyword #:invisible?)
135 ;; TODO: Fold this into a procedure in invisible? similar
136 ;; to take-me? and etc
138 #:init-value (wrap-apply gameobj-visible-to-player?))
140 ;; Can be a boolean or a procedure accepting
141 ;; (gameobj whos-acting #:key from)
142 (take-me? #:init-value #f
143 #:init-keyword #:take-me?)
144 ;; Can be a boolean or a procedure accepting
145 ;; (gameobj whos-acting where)
146 (drop-me? #:init-value #t
147 #:init-keyword #:drop-me?)
149 ;; TODO: Remove this and use actor-alive? instead.
150 ;; Set this on self-destruct
151 ;; (checked by some "long running" game routines)
152 (destructed #:init-value #f)
154 (actions #:allocation #:each-subclass
155 ;;; Actions supported by all gameobj
158 (init gameobj-act-init)
159 ;; Commands for co-occupants
160 (get-commands gameobj-get-commands)
161 ;; Commands for participants in a room
162 (get-container-dom-commands gameobj-get-container-dom-commands)
163 (get-container-sub-commands gameobj-get-container-sub-commands)
164 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
165 (get-contained-commands gameobj-get-contained-commands)
167 (get-occupants gameobj-get-occupants)
168 (add-occupant! gameobj-add-occupant!)
169 (remove-occupant! gameobj-remove-occupant!)
170 (get-loc gameobj-act-get-loc)
171 (set-loc! gameobj-act-set-loc!)
172 (get-name gameobj-get-name)
173 (set-name! gameobj-act-set-name!)
174 (get-desc gameobj-get-desc)
175 (get-prop gameobj-act-get-prop)
176 (goes-by gameobj-act-goes-by)
177 (visible-name gameobj-visible-name)
178 (self-destruct gameobj-act-self-destruct)
179 (tell gameobj-tell-no-op)
180 (assist-replace gameobj-act-assist-replace)
181 (ok-to-drop-here? (lambda (gameobj message . _)
182 #t)) ; ok to drop by default
183 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
184 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
187 (cmd-look-at cmd-look-at)
190 (cmd-take-from cmd-take-from-no-op)
191 (cmd-put-in cmd-put-in-no-op))))
194 ;;; gameobj message handlers
195 ;;; ========================
197 ;; TODO: This init stuff is a mess, and should be redone now that
198 ;; we have the *init* action stuff. We've really spread out the
199 ;; logic for creating a gameobj in several places, eg gm-inject-special!
200 (define (create-gameobj class gm loc . args)
201 "Create a gameobj of CLASS with GM and set to location LOC, applying rest of ARGS.
202 Note that this doesn't do any special dyn-ref of the location."
203 (let ((new-gameobj (apply create-actor class
206 (<-wait new-gameobj 'set-loc! #:loc loc)
207 ;; Initialize the object
208 (<-wait new-gameobj 'init)))
210 ;; ;; @@: Should we also dyn-ref the loc here? We can do that, unlike with
211 ;; ;; create-gameobj.
212 ;; ;; Another route could be to have set-loc! itself know how to use the
214 ;; (define (gameobj-create-gameobj gameobj class loc . args)
215 ;; "Like create-gameobj but saves the step of passing in the gm."
216 ;; (apply create-gameobj class (gameobj-gm gameobj) loc args))
218 ;; Kind of a useful utility, maybe?
219 (define (simple-slot-getter slot)
220 (lambda (actor message)
221 (slot-ref actor slot)))
223 (define (gameobj-replace-step-occupants actor occupants)
224 ;; Snarf all the occupants!
225 (display "replacing occupant\n")
229 (<-wait occupant 'set-loc!
230 #:loc (actor-id actor)))
233 (define gameobj-replace-steps*
234 (list gameobj-replace-step-occupants))
236 (define (run-replacement actor replaces replace-steps)
240 (<-wait replaces 'assist-replace))
241 (lambda* (#:key occupants)
243 (lambda (replace-step)
244 (replace-step actor occupants))
247 (define %nothing (cons '*the* '*nothing*))
248 (define (gameobj-setup-props gameobj)
249 (define class (class-of gameobj))
250 (define props (slot-ref gameobj 'props))
251 (maybe-build-rmeta-slot-cache! class 'initial-props
252 eq? hashq-set! hashq-ref)
253 ;; Kind of a kludge... we read through the rmeta-slot-cache
254 ;; and use that to build up the table
257 (when (eq? (hashq-ref props key %nothing) ; don't override init'ed instance values
259 (hashq-set! props key value)))
260 (rmeta-slot-cache (class-slot-ref class 'initial-props))))
262 ;; TODO: Use the *init* action?
263 ;; We could also use a generic method if they didn't have
264 ;; what I'm pretty sure is O(n) dispatch in GOOPS...
265 (define* (gameobj-act-init actor message #:key replace)
266 "Your most basic game object init procedure."
267 (gameobj-setup-props actor)
268 (run-replacement actor replace gameobj-replace-steps*))
270 (define* (gameobj-get-prop gameobj key #:optional dflt)
271 (hashq-ref (slot-ref gameobj 'props) key dflt))
273 (define* (gameobj-set-prop! gameobj key val)
274 (hashq-set! (slot-ref gameobj 'props) key val))
276 (define* (gameobj-act-get-prop actor message key #:optional dflt)
277 (gameobj-get-prop actor key dflt))
279 (define (gameobj-goes-by gameobj)
280 "Find the name we go by. Defaults to #:name if nothing else provided."
281 (cond ((slot-ref gameobj 'goes-by) =>
283 ((slot-ref gameobj 'name) =>
288 (define (gameobj-act-goes-by actor message)
289 "Reply to a message requesting what we go by."
290 (gameobj-goes-by actor))
292 (define (val-or-run val-or-proc)
293 "Evaluate if a procedure, or just return otherwise"
294 (if (procedure? val-or-proc)
298 (define (get-candidate-commands actor rmeta-sym verb)
299 (class-rmeta-ref (class-of actor) rmeta-sym verb
302 (define* (gameobj-get-commands actor message #:key verb)
303 "Get commands a co-occupant of the room might execute for VERB"
304 (define candidate-commands
305 (get-candidate-commands actor 'commands verb))
306 (values #:commands candidate-commands
307 #:goes-by (gameobj-goes-by actor)))
309 (define* (gameobj-get-container-dom-commands actor message #:key verb)
310 "Get (dominant) commands as the container / room of message's sender"
311 (define candidate-commands
312 (get-candidate-commands actor 'container-dom-commands verb))
313 (values #:commands candidate-commands))
315 (define* (gameobj-get-container-sub-commands actor message #:key verb)
316 "Get (subordinate) commands as the container / room of message's sender"
317 (define candidate-commands
318 (get-candidate-commands actor 'container-sub-commands verb))
319 (values #:commands candidate-commands))
321 (define* (gameobj-get-contained-commands actor message #:key verb)
322 "Get commands as being contained (eg inventory) of commanding gameobj"
323 (define candidate-commands
324 (get-candidate-commands actor 'contained-commands verb))
325 (values #:commands candidate-commands
326 #:goes-by (gameobj-goes-by actor)))
328 (define* (gameobj-add-occupant! actor message #:key who)
329 "Add an actor to our list of present occupants"
330 (hash-set! (slot-ref actor 'occupants)
333 (define* (gameobj-remove-occupant! actor message #:key who)
334 "Remove an occupant from the room."
335 (hash-remove! (slot-ref actor 'occupants) who))
337 (define* (gameobj-occupants gameobj #:key exclude)
339 (lambda (occupant _ prev)
342 ;; Empty list and #f are non-exclusion
345 ;; A list of addresses... since our address object is (annoyingly)
346 ;; currently a simple cons cell...
347 ((exclude-1 ... exclude-rest)
348 (member occupant exclude))
349 ;; Must be an individual address!
350 (_ (equal? occupant exclude))))
353 (cons occupant prev)))
355 (slot-ref gameobj 'occupants)))
357 (define* (gameobj-get-occupants actor message #:key exclude)
358 "Get all present occupants of the room."
359 (gameobj-occupants actor #:exclude exclude))
361 (define (gameobj-act-get-loc actor message)
362 (slot-ref actor 'loc))
364 (define (gameobj-set-loc! gameobj loc)
365 "Set the location of this object."
366 (define old-loc (gameobj-loc gameobj))
367 (format #t "DEBUG: Location set to ~s for ~s\n"
368 loc (actor-id-actor gameobj))
370 (when (not (equal? old-loc loc))
371 (slot-set! gameobj 'loc loc)
372 ;; Change registation of where we currently are
374 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
376 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
378 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
379 (define* (gameobj-act-set-loc! actor message #:key loc)
380 "Action routine to set the location."
381 (gameobj-set-loc! actor loc))
383 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
384 "Do a slot-ref on gameobj, evaluating it including ourselves
385 and whos-asking, and see if we should just return it or run it."
386 (match (slot-ref gameobj slot)
387 ((? procedure? slot-val-proc)
388 (apply slot-val-proc gameobj whos-asking other-args))
389 (anything-else anything-else)))
391 (define gameobj-get-name (simple-slot-getter 'name))
393 (define* (gameobj-act-set-name! actor message val)
394 (slot-set! actor 'name val))
396 (define* (gameobj-desc gameobj #:key whos-looking)
397 (match (slot-ref gameobj 'desc)
398 ((? procedure? desc-proc)
399 (desc-proc gameobj whos-looking))
402 (define* (gameobj-get-desc actor message #:key whos-looking)
403 "This is the action equivalent of the gameobj-desc getter"
404 (gameobj-desc actor #:whos-looking whos-looking))
406 (define (gameobj-visible-to-player? gameobj whos-looking)
407 "Check to see whether we're visible to the player or not.
408 By default, this is whether or not the generally-visible flag is set."
409 (not (slot-ref gameobj 'invisible?)))
411 (define* (gameobj-visible-name actor message #:key whos-looking)
413 (define we-are-visible
414 ((slot-ref actor 'visible-to-player?) actor whos-looking))
416 (define name-to-return
419 (match (slot-ref actor 'name)
420 ((? procedure? name-proc)
421 (name-proc actor whos-looking))
426 (values #:text name-to-return))
428 (define (gameobj-self-destruct gameobj)
429 "General gameobj self destruction routine"
430 ;; Unregister from being in any particular room
431 (gameobj-set-loc! gameobj #f)
432 (slot-set! gameobj 'destructed #t)
434 (self-destruct gameobj))
436 (define* (gameobj-act-self-destruct gameobj message #:key why)
437 "Action routine for self destruction"
438 (gameobj-self-destruct gameobj))
440 ;; Unless an actor has a tell message, we just ignore it
441 (define gameobj-tell-no-op
444 (define (gameobj-replace-data-occupants gameobj)
445 "The general purpose list of replacement data"
446 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
447 (slot-ref gameobj 'occupants))))
449 (define (gameobj-replace-data* gameobj)
450 ;; For now, just call gameobj-replace-data-occupants.
451 ;; But there may be more in the future!
452 (gameobj-replace-data-occupants gameobj))
454 ;; So sad that objects must assist in their replacement ;_;
455 ;; But that's life in a live hacked game!
456 (define (gameobj-act-assist-replace gameobj message)
457 "Vanilla method for assisting in self-replacement for live hacking"
458 (apply values (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
467 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
468 (call-with-values (lambda ()
469 (slot-ref-maybe-runcheck gameobj 'drop-me?
471 ;; This allows this to reply with #:why-not if appropriate
475 ;;; Utilities every gameobj has
476 ;;; ---------------------------
478 (define (dyn-ref gameobj special-symbol)
479 "Dynamically look up a special object from the gm"
480 (match special-symbol
481 ;; if it's a symbol, look it up dynamically
483 ;; TODO: If we get back an #f at this point, should we throw
484 ;; an error? Obviously #f is okay, but maybe not if
485 (<-wait (slot-ref gameobj 'gm) 'lookup-special
486 #:symbol special-symbol))
487 ;; if it's false, return nothing
489 ;; otherwise it's probably an address, return it as-is
497 (define %formless-desc
498 "You don't see anything special.")
500 (define* (cmd-look-at gameobj message
502 (player (message-from message)))
504 (or (gameobj-desc gameobj #:whos-looking player)
506 (<- player 'tell #:text desc)))
508 (define* (cmd-take gameobj message
510 (player (message-from message)))
512 (<-wait player 'get-name))
514 (<-wait player 'get-loc))
515 (define our-name (slot-ref gameobj 'name))
516 (define self-should-take
517 (slot-ref-maybe-runcheck gameobj 'take-me? player))
518 ;; @@: Is there any reason to allow the room to object in the way
519 ;; that there is for dropping? It doesn't seem like it.
520 (call-with-values (lambda ()
521 (slot-ref-maybe-runcheck gameobj 'take-me? player))
522 (lambda* (self-should-take #:key (why-not
523 `("It doesn't seem like you can take "
526 ;; Set the location to whoever's picking us up
528 (gameobj-set-loc! gameobj player)
530 #:text (format #f "You pick up ~a.\n"
532 (<- player-loc 'tell-room
533 #:text (format #f "~a picks up ~a.\n"
537 (<- player 'tell #:text why-not)))))
539 (define* (cmd-drop gameobj message
541 (player (message-from message)))
543 (<-wait player 'get-name))
545 (<-wait player 'get-loc))
546 (define our-name (slot-ref gameobj 'name))
548 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
549 (define (room-objection-to-drop)
551 (receive* (drop-ok? #:key why-not) ; does the room object to dropping?
552 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
554 ;; Either give the specified reason, or give a boilerplate one
556 `("You'd love to drop " ,our-name
557 " but for some reason it doesn't seem like you can"
558 " do that here.")))))
562 #:text `("It doesn't seem like you can drop " ,our-name
563 " here, because you don't seem to be anywhere?!?")))
564 ;; TODO: Let ourselves supply a reason why not.
567 #:text (format #f "It doesn't seem like you can drop ~a.\n"
569 ((room-objection-to-drop)
571 #:text room-objection-to-drop))
573 (gameobj-set-loc! gameobj player-loc)
574 ;; TODO: Allow more flavortext here.
576 #:text (format #f "You drop ~a.\n"
578 (<- player-loc 'tell-room
579 #:text (format #f "~a drops ~a.\n"
584 (define* (cmd-take-from-no-op gameobj message
585 #:key direct-obj indir-obj preposition
586 (player (message-from message)))
588 #:text `("It doesn't seem like you can take anything "
590 ,(slot-ref gameobj 'name) ".")))
592 (define* (cmd-put-in-no-op gameobj message
593 #:key direct-obj indir-obj preposition
594 (player (message-from message)))
596 #:text `("It doesn't seem like you can put anything "
598 ,(slot-ref gameobj 'name) ".")))