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 ;; dominant version (goes before everything)
92 (container-dom-commands #:allocation #:each-subclass
93 #:init-thunk (build-commands))
94 ;; subordinate version (goes after everything)
95 (container-sub-commands #:allocation #:each-subclass
96 #:init-thunk (build-commands))
98 ;; Commands we can handle by being contained by something else
99 (contained-commands #:allocation #:each-subclass
102 ("drop" ((direct-command cmd-drop #:obvious? #f)))))
104 ;; Most objects are generally visible by default
105 (invisible? #:init-value #f
106 #:init-keyword #:invisible?)
107 ;; TODO: Fold this into a procedure in invisible? similar
108 ;; to take-me? and etc
110 #:init-value (wrap-apply gameobj-visible-to-player?))
112 ;; Can be a boolean or a procedure accepting
113 ;; (gameobj whos-acting #:key from)
114 (take-me? #:init-value #f
115 #:init-keyword #:take-me?)
116 ;; Can be a boolean or a procedure accepting
117 ;; (gameobj whos-acting where)
118 (drop-me? #:init-value #t
119 #:init-keyword #:drop-me?)
121 ;; TODO: Remove this and use actor-alive? instead.
122 ;; Set this on self-destruct
123 ;; (checked by some "long running" game routines)
124 (destructed #:init-value #f)
126 (actions #:allocation #:each-subclass
127 ;;; Actions supported by all gameobj
130 (init gameobj-act-init)
131 ;; Commands for co-occupants
132 (get-commands gameobj-get-commands)
133 ;; Commands for participants in a room
134 (get-container-dom-commands gameobj-get-container-dom-commands)
135 (get-container-sub-commands gameobj-get-container-sub-commands)
136 ;; Commands for inventory items, etc (occupants of the gameobj commanding)
137 (get-contained-commands gameobj-get-contained-commands)
139 (get-occupants gameobj-get-occupants)
140 (add-occupant! gameobj-add-occupant!)
141 (remove-occupant! gameobj-remove-occupant!)
142 (get-loc gameobj-act-get-loc)
143 (set-loc! gameobj-act-set-loc!)
144 (get-name gameobj-get-name)
145 (set-name! gameobj-act-set-name!)
146 (get-desc gameobj-get-desc)
147 (goes-by gameobj-act-goes-by)
148 (visible-name gameobj-visible-name)
149 (self-destruct gameobj-act-self-destruct)
150 (tell gameobj-tell-no-op)
151 (assist-replace gameobj-act-assist-replace)
152 (ok-to-drop-here? (lambda (gameobj message . _)
153 (<-reply message #t))) ; ok to drop by default
154 (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
155 (ok-to-be-put-in? gameobj-ok-to-be-put-in)
160 (cmd-take-from cmd-take-from-no-op)
161 (cmd-put-in cmd-put-in-no-op))))
164 ;;; gameobj message handlers
165 ;;; ========================
167 ;; Kind of a useful utility, maybe?
168 (define (simple-slot-getter slot)
169 (lambda (actor message)
170 (<-reply message (slot-ref actor slot))))
172 (define (gameobj-replace-step-occupants actor occupants)
173 ;; Snarf all the occupants!
174 (display "replacing occupant\n")
178 (<-wait occupant 'set-loc!
179 #:loc (actor-id actor)))
182 (define gameobj-replace-steps*
183 (list gameobj-replace-step-occupants))
185 (define (run-replacement actor replaces replace-steps)
187 (mbody-receive (_ #:key occupants)
188 (<-wait replaces 'assist-replace)
190 (lambda (replace-step)
191 (replace-step actor occupants))
194 ;; @@: This could be kind of a messy way of doing gameobj-act-init
195 ;; stuff. If only we had generic methods :(
196 (define* (gameobj-act-init actor message #:key replace)
197 "Your most basic game object init procedure.
198 Assists in its replacement of occupants if necessary and nothing else."
199 (run-replacement actor replace gameobj-replace-steps*))
201 (define (gameobj-goes-by gameobj)
202 "Find the name we go by. Defaults to #:name if nothing else provided."
203 (cond ((slot-ref gameobj 'goes-by) =>
205 ((slot-ref gameobj 'name) =>
210 (define (gameobj-act-goes-by actor message)
211 "Reply to a message requesting what we go by."
212 (<-reply message (gameobj-goes-by actor)))
214 (define (val-or-run val-or-proc)
215 "Evaluate if a procedure, or just return otherwise"
216 (if (procedure? val-or-proc)
220 (define (get-candidate-commands actor rmeta-sym verb)
221 (class-rmeta-ref (class-of actor) rmeta-sym verb
224 (define* (gameobj-get-commands actor message #:key verb)
225 "Get commands a co-occupant of the room might execute for VERB"
226 (define candidate-commands
227 (get-candidate-commands actor 'commands verb))
229 #:commands candidate-commands
230 #:goes-by (gameobj-goes-by actor)))
232 (define* (gameobj-get-container-dom-commands actor message #:key verb)
233 "Get (dominant) commands as the container / room of message's sender"
234 (define candidate-commands
235 (get-candidate-commands actor 'container-dom-commands verb))
236 (<-reply message #:commands candidate-commands))
238 (define* (gameobj-get-container-sub-commands actor message #:key verb)
239 "Get (subordinate) commands as the container / room of message's sender"
240 (define candidate-commands
241 (get-candidate-commands actor 'container-sub-commands verb))
242 (<-reply message #:commands candidate-commands))
244 (define* (gameobj-get-contained-commands actor message #:key verb)
245 "Get commands as being contained (eg inventory) of commanding gameobj"
246 (define candidate-commands
247 (get-candidate-commands actor 'contained-commands verb))
249 #:commands candidate-commands
250 #:goes-by (gameobj-goes-by actor)))
252 (define* (gameobj-add-occupant! actor message #:key who)
253 "Add an actor to our list of present occupants"
254 (hash-set! (slot-ref actor 'occupants)
257 (define* (gameobj-remove-occupant! actor message #:key who)
258 "Remove an occupant from the room."
259 (hash-remove! (slot-ref actor 'occupants) who))
261 (define* (gameobj-occupants gameobj #:key exclude)
263 (lambda (occupant _ prev)
266 ;; Empty list and #f are non-exclusion
269 ;; A list of addresses... since our address object is (annoyingly)
270 ;; currently a simple cons cell...
271 ((exclude-1 ... exclude-rest)
272 (member occupant exclude))
273 ;; Must be an individual address!
274 (_ (equal? occupant exclude))))
277 (cons occupant prev)))
279 (slot-ref gameobj 'occupants)))
281 (define* (gameobj-get-occupants actor message #:key exclude)
282 "Get all present occupants of the room."
284 (gameobj-occupants actor #:exclude exclude))
285 (<-reply message occupants))
287 (define (gameobj-act-get-loc actor message)
288 (<-reply message (slot-ref actor 'loc)))
290 (define (gameobj-set-loc! gameobj loc)
291 "Set the location of this object."
292 (define old-loc (gameobj-loc gameobj))
293 (format #t "DEBUG: Location set to ~s for ~s\n"
294 loc (actor-id-actor gameobj))
296 (when (not (equal? old-loc loc))
297 (slot-set! gameobj 'loc loc)
298 ;; Change registation of where we currently are
300 (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
302 (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
304 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
305 (define* (gameobj-act-set-loc! actor message #:key loc)
306 "Action routine to set the location."
307 (gameobj-set-loc! actor loc))
309 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
310 "Do a slot-ref on gameobj, evaluating it including ourselves
311 and whos-asking, and see if we should just return it or run it."
312 (match (slot-ref gameobj slot)
313 ((? procedure? slot-val-proc)
314 (apply slot-val-proc gameobj whos-asking other-args))
315 (anything-else anything-else)))
317 (define gameobj-get-name (simple-slot-getter 'name))
319 (define* (gameobj-act-set-name! actor message val)
320 (slot-set! actor 'name val))
322 (define* (gameobj-get-desc actor message #:key whos-looking)
324 (match (slot-ref actor 'desc)
325 ((? procedure? desc-proc)
326 (desc-proc actor whos-looking))
328 (<-reply message desc-text))
330 (define (gameobj-visible-to-player? gameobj whos-looking)
331 "Check to see whether we're visible to the player or not.
332 By default, this is whether or not the generally-visible flag is set."
333 (not (slot-ref gameobj 'invisible?)))
335 (define* (gameobj-visible-name actor message #:key whos-looking)
337 (define we-are-visible
338 ((slot-ref actor 'visible-to-player?) actor whos-looking))
340 (define name-to-return
343 (match (slot-ref actor 'name)
344 ((? procedure? name-proc)
345 (name-proc actor whos-looking))
350 (<-reply message #:text name-to-return))
352 (define (gameobj-self-destruct gameobj)
353 "General gameobj self destruction routine"
354 ;; Unregister from being in any particular room
355 (gameobj-set-loc! gameobj #f)
356 (slot-set! gameobj 'destructed #t)
358 (self-destruct gameobj))
360 (define* (gameobj-act-self-destruct gameobj message #:key why)
361 "Action routine for self destruction"
362 (gameobj-self-destruct gameobj))
364 ;; Unless an actor has a tell message, we just ignore it
365 (define gameobj-tell-no-op
368 (define (gameobj-replace-data-occupants gameobj)
369 "The general purpose list of replacement data"
370 (list #:occupants (hash-map->list (lambda (occupant _) occupant)
371 (slot-ref gameobj 'occupants))))
373 (define (gameobj-replace-data* gameobj)
374 ;; For now, just call gameobj-replace-data-occupants.
375 ;; But there may be more in the future!
376 (gameobj-replace-data-occupants gameobj))
378 ;; So sad that objects must assist in their replacement ;_;
379 ;; But that's life in a live hacked game!
380 (define (gameobj-act-assist-replace gameobj message)
381 "Vanilla method for assisting in self-replacement for live hacking"
382 (apply <-reply message
383 (gameobj-replace-data* gameobj)))
385 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
386 (call-with-values (lambda ()
387 (slot-ref-maybe-runcheck gameobj 'take-me?
388 whos-acting #:from #t))
389 ;; This allows this to reply with #:why-not if appropriate
391 (apply <-reply message args))))
393 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
394 (call-with-values (lambda ()
395 (slot-ref-maybe-runcheck gameobj 'drop-me?
397 ;; This allows this to reply with #:why-not if appropriate
399 (apply <-reply message args))))
402 ;;; Utilities every gameobj has
403 ;;; ---------------------------
405 (define (dyn-ref gameobj special-symbol)
406 "Dynamically look up a special object from the gm"
407 (match special-symbol
408 ;; if it's a symbol, look it up dynamically
410 (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
411 #:symbol special-symbol)))
412 ;; if it's false, return nothing
414 ;; otherwise it's probably an address, return it as-is
422 (define* (cmd-take gameobj message
424 (player (message-from message)))
426 (mbody-val (<-wait player 'get-name)))
428 (mbody-val (<-wait player 'get-loc)))
429 (define our-name (slot-ref gameobj 'name))
430 (define self-should-take
431 (slot-ref-maybe-runcheck gameobj 'take-me? player))
432 ;; @@: Is there any reason to allow the room to object in the way
433 ;; that there is for dropping? It doesn't seem like it.
434 (call-with-values (lambda ()
435 (slot-ref-maybe-runcheck gameobj 'take-me? player))
436 (lambda* (self-should-take #:key (why-not
437 `("It doesn't seem like you can take "
440 ;; Set the location to whoever's picking us up
442 (gameobj-set-loc! gameobj player)
444 #:text (format #f "You pick up ~a.\n"
446 (<- player-loc 'tell-room
447 #:text (format #f "~a picks up ~a.\n"
451 (<- player 'tell #:text why-not)))))
453 (define* (cmd-drop gameobj message
455 (player (message-from message)))
457 (mbody-val (<-wait player 'get-name)))
459 (mbody-val (<-wait player 'get-loc)))
460 (define our-name (slot-ref gameobj 'name))
462 (slot-ref-maybe-runcheck gameobj 'drop-me? player))
463 (define (room-objection-to-drop)
464 (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
465 (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
467 ;; Either give the specified reason, or give a boilerplate one
469 `("You'd love to drop " ,our-name
470 " but for some reason it doesn't seem like you can"
471 " do that here.")))))
475 #:text `("It doesn't seem like you can drop " ,our-name
476 " here, because you don't seem to be anywhere?!?")))
477 ;; TODO: Let ourselves supply a reason why not.
480 #:text (format #f "It doesn't seem like you can drop ~a.\n"
482 ((room-objection-to-drop)
484 #:text room-objection-to-drop))
486 (gameobj-set-loc! gameobj player-loc)
487 ;; TODO: Allow more flavortext here.
489 #:text (format #f "You drop ~a.\n"
491 (<- player-loc 'tell-room
492 #:text (format #f "~a drops ~a.\n"
497 (define* (cmd-take-from-no-op gameobj message
498 #:key direct-obj indir-obj preposition
499 (player (message-from message)))
501 #:text `("It doesn't seem like you can take anything "
503 ,(slot-ref gameobj 'name) ".")))
505 (define* (cmd-put-in-no-op gameobj message
506 #:key direct-obj indir-obj preposition
507 (player (message-from message)))
509 #:text `("It doesn't seem like you can put anything "
511 ,(slot-ref gameobj 'name) ".")))