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-no-op cmd-put-in-no-op))
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?)
117 ;; TODO: Remove this and use actor-alive? instead.
118 ;; Set this on self-destruct
119 ;; (checked by some "long running" game routines)
120 (destructed #:init-value #f)
122 (actions #:allocation #:each-subclass
123 ;;; Actions supported by all gameobj
126 (init gameobj-act-init)
127 ;; Commands for co-occupants
128 (get-commands gameobj-get-commands)
129 ;; Commands for participants in a room
130 (get-container-commands gameobj-get-container-commands)
131 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
132 (get-contained-commands gameobj-get-contained-commands)
134 (get-occupants gameobj-get-occupants)
135 (add-occupant! gameobj-add-occupant!)
136 (remove-occupant! gameobj-remove-occupant!)
137 (get-loc gameobj-act-get-loc)
138 (set-loc! gameobj-act-set-loc!)
139 (get-name gameobj-get-name)
140 (set-name! gameobj-act-set-name!)
141 (get-desc gameobj-get-desc)
142 (goes-by gameobj-act-goes-by)
143 (visible-name gameobj-visible-name)
144 (self-destruct gameobj-act-self-destruct)
145 (tell gameobj-tell-no-op)
146 (assist-replace gameobj-act-assist-replace)
147 (ok-to-drop-here? (lambda (gameobj message . _)
148 (<-reply message #t))) ; ok to drop by default
149 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
150 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
155 (cmd-take-from cmd-take-from-no-op)
156 (cmd-put-in cmd-put-in-no-op))))
159 ;;; gameobj message handlers
160 ;;; ========================
162 ;; Kind of a useful utility, maybe?
163 (define (simple-slot-getter slot)
164 (lambda (actor message)
165 (<-reply message (slot-ref actor slot))))
167 (define (gameobj-replace-step-occupants actor occupants)
168 ;; Snarf all the occupants!
169 (display "replacing occupant\n")
173 (<-wait occupant 'set-loc!
174 #:loc (actor-id actor)))
177 (define gameobj-replace-steps*
178 (list gameobj-replace-step-occupants))
180 (define (run-replacement actor replaces replace-steps)
182 (mbody-receive (_ #:key occupants)
183 (<-wait replaces 'assist-replace)
185 (lambda (replace-step)
186 (replace-step actor occupants))
189 ;; @@: This could be kind of a messy way of doing gameobj-act-init
190 ;; stuff. If only we had generic methods :(
191 (define* (gameobj-act-init actor message #:key replace)
192 "Your most basic game object init procedure.
193 Assists in its replacement of occupants if necessary and nothing else."
194 (run-replacement actor replace gameobj-replace-steps*))
196 (define (gameobj-goes-by gameobj)
197 "Find the name we go by. Defaults to #:name if nothing else provided."
198 (cond ((slot-ref gameobj 'goes-by) =>
200 ((slot-ref gameobj 'name) =>
205 (define (gameobj-act-goes-by actor message)
206 "Reply to a message requesting what we go by."
207 (<-reply message (gameobj-goes-by actor)))
209 (define (val-or-run val-or-proc)
210 "Evaluate if a procedure, or just return otherwise"
211 (if (procedure? val-or-proc)
215 (define (get-candidate-commands actor rmeta-sym verb)
216 (class-rmeta-ref (class-of actor) rmeta-sym verb
219 (define* (gameobj-get-commands actor message #:key verb)
220 "Get commands a co-occupant of the room might execute for VERB"
221 (define candidate-commands
222 (get-candidate-commands actor 'commands verb))
224 #:commands candidate-commands
225 #:goes-by (gameobj-goes-by actor)))
227 (define* (gameobj-get-container-commands actor message #:key verb)
228 "Get commands as the container / room of message's sender"
229 (define candidate-commands
230 (get-candidate-commands actor 'container-commands verb))
231 (<-reply message #:commands candidate-commands))
233 (define* (gameobj-get-contained-commands actor message #:key verb)
234 "Get commands as being contained (eg inventory) of commanding gameobj"
235 (define candidate-commands
236 (get-candidate-commands actor 'contained-commands verb))
238 #:commands candidate-commands
239 #:goes-by (gameobj-goes-by actor)))
241 (define* (gameobj-add-occupant! actor message #:key who)
242 "Add an actor to our list of present occupants"
243 (hash-set! (slot-ref actor 'occupants)
246 (define* (gameobj-remove-occupant! actor message #:key who)
247 "Remove an occupant from the room."
248 (hash-remove! (slot-ref actor 'occupants) who))
250 (define* (gameobj-occupants gameobj #:key exclude)
252 (lambda (occupant _ prev)
255 ;; Empty list and #f are non-exclusion
258 ;; A list of addresses... since our address object is (annoyingly)
259 ;; currently a simple cons cell...
260 ((exclude-1 ... exclude-rest)
261 (member occupant exclude))
262 ;; Must be an individual address!
263 (_ (equal? occupant exclude))))
266 (cons occupant prev)))
268 (slot-ref gameobj 'occupants)))
270 (define* (gameobj-get-occupants actor message #:key exclude)
271 "Get all present occupants of the room."
273 (gameobj-occupants actor #:exclude exclude))
274 (<-reply message occupants))
276 (define (gameobj-act-get-loc actor message)
277 (<-reply message (slot-ref actor 'loc)))
279 (define (gameobj-set-loc! gameobj loc)
280 "Set the location of this object."
281 (define old-loc (gameobj-loc gameobj))
282 (format #t "DEBUG: Location set to ~s for ~s\n"
283 loc (actor-id-actor gameobj))
285 (when (not (equal? old-loc loc))
286 (slot-set! gameobj 'loc loc)
287 ;; Change registation of where we currently are
289 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
291 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
293 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
294 (define* (gameobj-act-set-loc! actor message #:key loc)
295 "Action routine to set the location."
296 (gameobj-set-loc! actor loc))
298 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
299 "Do a slot-ref on gameobj, evaluating it including ourselves
300 and whos-asking, and see if we should just return it or run it."
301 (match (slot-ref gameobj slot)
302 ((? procedure? slot-val-proc)
303 (apply slot-val-proc gameobj whos-asking other-args))
304 (anything-else anything-else)))
306 (define gameobj-get-name (simple-slot-getter 'name))
308 (define* (gameobj-act-set-name! actor message val)
309 (slot-set! actor 'name val))
311 (define* (gameobj-get-desc actor message #:key whos-looking)
313 (match (slot-ref actor 'desc)
314 ((? procedure? desc-proc)
315 (desc-proc actor whos-looking))
317 (<-reply message desc-text))
319 (define (gameobj-visible-to-player? gameobj whos-looking)
320 "Check to see whether we're visible to the player or not.
321 By default, this is whether or not the generally-visible flag is set."
322 (not (slot-ref gameobj 'invisible?)))
324 (define* (gameobj-visible-name actor message #:key whos-looking)
326 (define we-are-visible
327 ((slot-ref actor 'visible-to-player?) actor whos-looking))
329 (define name-to-return
332 (match (slot-ref actor 'name)
333 ((? procedure? name-proc)
334 (name-proc actor whos-looking))
339 (<-reply message #:text name-to-return))
341 (define (gameobj-self-destruct gameobj)
342 "General gameobj self destruction routine"
343 ;; Unregister from being in any particular room
344 (gameobj-set-loc! gameobj #f)
345 (slot-set! gameobj 'destructed #t)
347 (self-destruct gameobj))
349 (define* (gameobj-act-self-destruct gameobj message #:key why)
350 "Action routine for self destruction"
351 (gameobj-self-destruct gameobj))
353 ;; Unless an actor has a tell message, we just ignore it
354 (define gameobj-tell-no-op
357 (define (gameobj-replace-data-occupants gameobj)
358 "The general purpose list of replacement data"
359 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
360 (slot-ref gameobj 'occupants))))
362 (define (gameobj-replace-data* gameobj)
363 ;; For now, just call gameobj-replace-data-occupants.
364 ;; But there may be more in the future!
365 (gameobj-replace-data-occupants gameobj))
367 ;; So sad that objects must assist in their replacement ;_;
368 ;; But that's life in a live hacked game!
369 (define (gameobj-act-assist-replace gameobj message)
370 "Vanilla method for assisting in self-replacement for live hacking"
371 (apply <-reply message
372 (gameobj-replace-data* gameobj)))
374 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
375 (call-with-values (lambda ()
376 (slot-ref-maybe-runcheck gameobj 'take-me?
377 whos-acting #:from #t))
378 ;; This allows this to reply with #:why-not if appropriate
380 (apply <-reply message args))))
382 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
383 (call-with-values (lambda ()
384 (slot-ref-maybe-runcheck gameobj 'drop-me?
386 ;; This allows this to reply with #:why-not if appropriate
388 (apply <-reply message args))))
391 ;;; Utilities every gameobj has
392 ;;; ---------------------------
394 (define (dyn-ref gameobj special-symbol)
395 "Dynamically look up a special object from the gm"
396 (match special-symbol
397 ;; if it's a symbol, look it up dynamically
399 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
400 #:symbol special-symbol)))
401 ;; if it's false, return nothing
403 ;; otherwise it's probably an address, return it as-is
411 (define* (cmd-take gameobj message
413 (player (message-from message)))
415 (mbody-val (<-wait player 'get-name)))
417 (mbody-val (<-wait player 'get-loc)))
418 (define our-name (slot-ref gameobj 'name))
419 (define self-should-take
420 (slot-ref-maybe-runcheck gameobj 'take-me? player))
421 ;; @@: Is there any reason to allow the room to object in the way
422 ;; that there is for dropping? It doesn't seem like it.
423 (call-with-values (lambda ()
424 (slot-ref-maybe-runcheck gameobj 'take-me? player))
425 (lambda* (self-should-take #:key (why-not
426 `("It doesn't seem like you can take "
429 ;; Set the location to whoever's picking us up
431 (gameobj-set-loc! gameobj player)
433 #:text (format #f "You pick up ~a.\n"
435 (<- player-loc 'tell-room
436 #:text (format #f "~a picks up ~a.\n"
440 (<- player 'tell #:text why-not)))))
442 (define* (cmd-drop gameobj message
444 (player (message-from message)))
446 (mbody-val (<-wait player 'get-name)))
448 (mbody-val (<-wait player 'get-loc)))
449 (define our-name (slot-ref gameobj 'name))
451 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
452 (define (room-objection-to-drop)
453 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
454 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
456 ;; Either give the specified reason, or give a boilerplate one
458 `("You'd love to drop " ,our-name
459 " but for some reason it doesn't seem like you can"
460 " do that here.")))))
464 #:text `("It doesn't seem like you can drop " ,our-name
465 " here, because you don't seem to be anywhere?!?")))
466 ;; TODO: Let ourselves supply a reason why not.
469 #:text (format #f "It doesn't seem like you can drop ~a.\n"
471 ((room-objection-to-drop)
473 #:text room-objection-to-drop))
475 (gameobj-set-loc! gameobj player-loc)
476 ;; TODO: Allow more flavortext here.
478 #:text (format #f "You drop ~a.\n"
480 (<- player-loc 'tell-room
481 #:text (format #f "~a drops ~a.\n"
486 (define* (cmd-take-from-no-op gameobj message
487 #:key direct-obj indir-obj preposition
488 (player (message-from message)))
490 #:text `("It doesn't seem like you can take anything "
492 ,(slot-ref gameobj 'name) ".")))
494 (define* (cmd-put-in-no-op gameobj message
495 #:key direct-obj indir-obj preposition
496 (player (message-from message)))
498 #:text `("It doesn't seem like you can put anything "
500 ,(slot-ref gameobj 'name) ".")))