6c739123d63e862b4537cb2206e004d82d3866b2
[mudsync.git] / mudsync / gameobj.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of Mudsync.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19 ;;; Game actor
20 ;;; ==========
21
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)
33   #:export (<gameobj>
34
35             gameobj-loc
36             gameobj-gm
37
38             gameobj-act-init
39             gameobj-set-loc!
40             gameobj-occupants
41             gameobj-self-destruct
42
43             slot-ref-maybe-runcheck
44             val-or-run
45
46             dyn-ref
47
48             ;; Some of the more common commands
49             cmd-take cmd-drop
50             cmd-take-from cmd-put-in))
51
52 ;;; Gameobj
53 ;;; =======
54
55
56 ;;; *all* game components that talk to players should somehow
57 ;;; derive from this class.
58 ;;; And all of them need a GM!
59
60 (define-class <gameobj> (<actor>)
61   ;; location id
62   (loc #:init-value #f
63        #:getter gameobj-loc)
64   
65   ;; Uses a hash table like a set (values ignored)
66   (occupants #:init-thunk make-hash-table)
67
68   ;; game master id
69   (gm #:init-keyword #:gm
70       #:getter gameobj-gm)
71
72   ;; a name to be known by
73   (name #:init-keyword #:name
74         #:init-value #f)
75   (goes-by #:init-keyword #:goes-by
76            #:init-value #f)
77
78   (desc #:init-value #f
79         #:init-keyword #:desc)
80
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
86                                                        '("from" "out of"))))
87                           ("put" ((prep-indir-command cmd-put-in
88                                                       '("in" "inside" "on"))))))
89
90   ;; Commands we can handle by being something's container
91   (container-commands #:allocation #:each-subclass
92                       #:init-thunk (build-commands))
93
94   ;; Commands we can handle by being contained by something else
95   (contained-commands #:allocation #:each-subclass
96                       #:init-thunk 
97                       (build-commands
98                        ("drop" ((direct-command cmd-drop #:obvious? #f)))))
99
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
105   (visible-to-player?
106    #:init-value (wrap-apply gameobj-visible-to-player?))
107
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?)
116   ;; Can be a boolean or a procedure accepting
117   ;; (gameobj whos-acting take-what)
118   (take-from-me? #:init-value #f
119                  #:init-keyword #:take-from-me?)
120   ;; Can be a boolean or a procedure accepting
121   ;; (gameobj whos-acting put-what)
122   (put-in-me? #:init-value #f
123               #:init-keyword #:put-in-me?)
124
125   ;; TODO: Remove this and use actor-alive? instead.
126   ;; Set this on self-destruct
127   ;; (checked by some "long running" game routines)
128   (destructed #:init-value #f)
129
130   (actions #:allocation #:each-subclass
131            ;;; Actions supported by all gameobj
132            #:init-thunk
133            (build-actions
134             (init gameobj-act-init)
135             ;; Commands for co-occupants
136             (get-commands gameobj-get-commands)
137             ;; Commands for participants in a room
138             (get-container-commands gameobj-get-container-commands)
139             ;; Commands for inventory items, etc (occupants of the gameobj commanding)
140             (get-contained-commands gameobj-get-contained-commands)
141
142             (get-occupants gameobj-get-occupants)
143             (add-occupant! gameobj-add-occupant!)
144             (remove-occupant! gameobj-remove-occupant!)
145             (get-loc gameobj-act-get-loc)
146             (set-loc! gameobj-act-set-loc!)
147             (get-name gameobj-get-name)
148             (set-name! gameobj-act-set-name!)
149             (get-desc gameobj-get-desc)
150             (goes-by gameobj-act-goes-by)
151             (visible-name gameobj-visible-name)
152             (self-destruct gameobj-act-self-destruct)
153             (tell gameobj-tell-no-op)
154             (assist-replace gameobj-act-assist-replace)
155             (ok-to-drop-here? (lambda (gameobj message . _)
156                                 (<-reply message #t))) ; ok to drop by default
157             (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
158             (ok-to-be-put-in? gameobj-ok-to-be-put-in)
159
160             ;; Common commands
161             (cmd-take cmd-take)
162             (cmd-take-from cmd-take-from)
163             (cmd-put-in cmd-put-in)
164             (cmd-drop cmd-drop))))
165
166
167 ;;; gameobj message handlers
168 ;;; ========================
169
170 ;; Kind of a useful utility, maybe?
171 (define (simple-slot-getter slot)
172   (lambda (actor message)
173     (<-reply message (slot-ref actor slot))))
174
175 (define (gameobj-replace-step-occupants actor occupants)
176   ;; Snarf all the occupants!
177   (display "replacing occupant\n")
178   (when occupants
179     (for-each
180      (lambda (occupant)
181        (<-wait occupant 'set-loc!
182                #:loc (actor-id actor)))
183      occupants)))
184
185 (define gameobj-replace-steps*
186   (list gameobj-replace-step-occupants))
187
188 (define (run-replacement actor replaces replace-steps)
189   (when replaces
190     (mbody-receive (_ #:key occupants)
191         (<-wait replaces 'assist-replace)
192       (for-each
193        (lambda (replace-step)
194          (replace-step actor occupants))
195        replace-steps))))
196
197 ;; @@: This could be kind of a messy way of doing gameobj-act-init
198 ;;   stuff.  If only we had generic methods :(
199 (define* (gameobj-act-init actor message #:key replace)
200   "Your most basic game object init procedure.
201 Assists in its replacement of occupants if necessary and nothing else."
202   (run-replacement actor replace gameobj-replace-steps*))
203
204 (define (gameobj-goes-by gameobj)
205   "Find the name we go by.  Defaults to #:name if nothing else provided."
206   (cond ((slot-ref gameobj 'goes-by) =>
207          identity)
208         ((slot-ref gameobj 'name) =>
209          (lambda (name)
210            (list name)))
211         (else '())))
212
213 (define (gameobj-act-goes-by actor message)
214   "Reply to a message requesting what we go by."
215   (<-reply message (gameobj-goes-by actor)))
216
217 (define (val-or-run val-or-proc)
218   "Evaluate if a procedure, or just return otherwise"
219   (if (procedure? val-or-proc)
220       (val-or-proc)
221       val-or-proc))
222
223 (define (get-candidate-commands actor rmeta-sym verb)
224   (class-rmeta-ref (class-of actor) rmeta-sym verb
225                    #:dflt '()))
226
227 (define* (gameobj-get-commands actor message #:key verb)
228   "Get commands a co-occupant of the room might execute for VERB"
229   (define candidate-commands
230     (get-candidate-commands actor 'commands verb))
231   (<-reply message
232            #:commands candidate-commands
233            #:goes-by (gameobj-goes-by actor)))
234
235 (define* (gameobj-get-container-commands actor message #:key verb)
236   "Get commands as the container / room of message's sender"
237   (define candidate-commands
238     (get-candidate-commands actor 'container-commands verb))
239   (<-reply message #:commands candidate-commands))
240
241 (define* (gameobj-get-contained-commands actor message #:key verb)
242   "Get commands as being contained (eg inventory) of commanding gameobj"
243   (define candidate-commands
244     (get-candidate-commands actor 'contained-commands verb))
245   (<-reply message
246            #:commands candidate-commands
247            #:goes-by (gameobj-goes-by actor)))
248
249 (define* (gameobj-add-occupant! actor message #:key who)
250   "Add an actor to our list of present occupants"
251   (hash-set! (slot-ref actor 'occupants)
252              who #t))
253
254 (define* (gameobj-remove-occupant! actor message #:key who)
255   "Remove an occupant from the room."
256   (hash-remove! (slot-ref actor 'occupants) who))
257
258 (define* (gameobj-occupants gameobj #:key exclude)
259   (hash-fold
260    (lambda (occupant _ prev)
261      (define exclude-it?
262        (match exclude
263          ;; Empty list and #f are non-exclusion
264          (() #f)
265          (#f #f)
266          ;; A list of addresses... since our address object is (annoyingly)
267          ;; currently a simple cons cell...
268          ((exclude-1 ... exclude-rest)
269           (member occupant exclude))
270          ;; Must be an individual address!
271          (_ (equal? occupant exclude))))
272      (if exclude-it?
273          prev
274          (cons occupant prev)))
275    '()
276    (slot-ref gameobj 'occupants)))
277
278 (define* (gameobj-get-occupants actor message #:key exclude)
279   "Get all present occupants of the room."
280   (define occupants
281     (gameobj-occupants actor #:exclude exclude))
282   (<-reply message occupants))
283
284 (define (gameobj-act-get-loc actor message)
285   (<-reply message (slot-ref actor 'loc)))
286
287 (define (gameobj-set-loc! gameobj loc)
288   "Set the location of this object."
289   (define old-loc (gameobj-loc gameobj))
290   (format #t "DEBUG: Location set to ~s for ~s\n"
291           loc (actor-id-actor gameobj))
292
293   (when (not (equal? old-loc loc))
294     (slot-set! gameobj 'loc loc)
295     ;; Change registation of where we currently are
296     (if old-loc
297         (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
298     (if loc
299         (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
300
301 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
302 (define* (gameobj-act-set-loc! actor message #:key loc)
303   "Action routine to set the location."
304   (gameobj-set-loc! actor loc))
305
306 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
307   "Do a slot-ref on gameobj, evaluating it including ourselves
308 and whos-asking, and see if we should just return it or run it."
309   (match (slot-ref gameobj slot)
310     ((? procedure? slot-val-proc)
311      (apply slot-val-proc gameobj whos-asking other-args))
312     (anything-else anything-else)))
313
314 (define gameobj-get-name (simple-slot-getter 'name))
315
316 (define* (gameobj-act-set-name! actor message val)
317   (slot-set! actor 'name val))
318
319 (define* (gameobj-get-desc actor message #:key whos-looking)
320   (define desc-text
321     (match (slot-ref actor 'desc)
322       ((? procedure? desc-proc)
323        (desc-proc actor whos-looking))
324       (desc desc)))
325   (<-reply message desc-text))
326
327 (define (gameobj-visible-to-player? gameobj whos-looking)
328   "Check to see whether we're visible to the player or not.
329 By default, this is whether or not the generally-visible flag is set."
330   (not (slot-ref gameobj 'invisible?)))
331
332 (define* (gameobj-visible-name actor message #:key whos-looking)
333   ;; Are we visible?
334   (define we-are-visible
335     ((slot-ref actor 'visible-to-player?) actor whos-looking))
336
337   (define name-to-return
338     (if we-are-visible
339         ;; Return our name
340         (match (slot-ref actor 'name)
341           ((? procedure? name-proc)
342            (name-proc actor whos-looking))
343           ((? string? name)
344            name)
345           (#f #f))
346         #f))
347   (<-reply message #:text name-to-return))
348
349 (define (gameobj-self-destruct gameobj)
350   "General gameobj self destruction routine"
351   ;; Unregister from being in any particular room
352   (gameobj-set-loc! gameobj #f)
353   (slot-set! gameobj 'destructed #t)
354   ;; Boom!
355   (self-destruct gameobj))
356
357 (define* (gameobj-act-self-destruct gameobj message #:key why)
358   "Action routine for self destruction"
359   (gameobj-self-destruct gameobj))
360
361 ;; Unless an actor has a tell message, we just ignore it
362 (define gameobj-tell-no-op
363   (const 'no-op))
364
365 (define (gameobj-replace-data-occupants gameobj)
366   "The general purpose list of replacement data"
367   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
368                                     (slot-ref gameobj 'occupants))))
369
370 (define (gameobj-replace-data* gameobj)
371   ;; For now, just call gameobj-replace-data-occupants.
372   ;; But there may be more in the future!
373   (gameobj-replace-data-occupants gameobj))
374
375 ;; So sad that objects must assist in their replacement ;_;
376 ;; But that's life in a live hacked game!
377 (define (gameobj-act-assist-replace gameobj message)
378   "Vanilla method for assisting in self-replacement for live hacking"
379   (apply <-reply message
380          (gameobj-replace-data* gameobj)))
381
382 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
383   (call-with-values (lambda ()
384                       (slot-ref-maybe-runcheck gameobj 'take-me?
385                                                whos-acting #:from #t))
386     ;; This allows this to reply with #:why-not if appropriate
387     (lambda args
388       (apply <-reply message args))))
389
390 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
391   (call-with-values (lambda ()
392                       (slot-ref-maybe-runcheck gameobj 'drop-me?
393                                                whos-acting where))
394     ;; This allows this to reply with #:why-not if appropriate
395     (lambda args
396       (apply <-reply message args))))
397
398 \f
399 ;;; Utilities every gameobj has
400 ;;; ---------------------------
401
402 (define (dyn-ref gameobj special-symbol)
403   "Dynamically look up a special object from the gm"
404   (match special-symbol
405     ;; if it's a symbol, look it up dynamically
406     ((? symbol? _)
407      (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
408                         #:symbol special-symbol)))
409     ;; if it's false, return nothing
410     (#f #f)
411     ;; otherwise it's probably an address, return it as-is
412     (_ special-symbol)))
413
414
415 \f
416 ;;; Basic actions
417 ;;; -------------
418
419 (define* (cmd-take gameobj message
420                    #:key direct-obj
421                    (player (message-from message)))
422   (define player-name
423     (mbody-val (<-wait player 'get-name)))
424   (define player-loc
425     (mbody-val (<-wait player 'get-loc)))
426   (define our-name (slot-ref gameobj 'name))
427   (define self-should-take
428     (slot-ref-maybe-runcheck gameobj 'take-me? player))
429   ;; @@: Is there any reason to allow the room to object in the way
430   ;;   that there is for dropping?  It doesn't seem like it.
431   (call-with-values (lambda ()
432                       (slot-ref-maybe-runcheck gameobj 'take-me? player))
433     (lambda* (self-should-take #:key (why-not
434                                       `("It doesn't seem like you can take "
435                                         ,our-name ".")))
436       (if self-should-take
437           ;; Set the location to whoever's picking us up
438           (begin
439             (gameobj-set-loc! gameobj player)
440             (<- player 'tell
441                 #:text (format #f "You pick up ~a.\n"
442                                our-name))
443             (<- player-loc 'tell-room
444                 #:text (format #f "~a picks up ~a.\n"
445                                player-name
446                                our-name)
447                 #:exclude player))
448           (<- player 'tell #:text why-not)))))
449
450 (define* (cmd-drop gameobj message
451                    #:key direct-obj
452                    (player (message-from message)))
453   (define player-name
454     (mbody-val (<-wait player 'get-name)))
455   (define player-loc
456     (mbody-val (<-wait player 'get-loc)))
457   (define our-name (slot-ref gameobj 'name))
458   (define should-drop
459     (slot-ref-maybe-runcheck gameobj 'drop-me? player))
460   (define (room-objection-to-drop)
461     (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
462         (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
463       (and (not drop-ok?)
464            ;; Either give the specified reason, or give a boilerplate one
465            (or why-not
466                `("You'd love to drop " ,our-name
467                  " but for some reason it doesn't seem like you can"
468                  " do that here.")))))
469   (cond
470    ((not player-loc)
471     (<- player 'tell
472         #:text `("It doesn't seem like you can drop " ,our-name 
473                 " here, because you don't seem to be anywhere?!?")))
474    ;; TODO: Let ourselves supply a reason why not.
475    ((not should-drop)
476     (<- player 'tell
477         #:text (format #f "It doesn't seem like you can drop ~a.\n"
478                        our-name)))
479    ((room-objection-to-drop)
480     (<- player 'tell
481         #:text room-objection-to-drop))
482    (else
483     (gameobj-set-loc! gameobj player-loc)
484     ;; TODO: Allow more flavortext here.
485     (<- player 'tell
486         #:text (format #f "You drop ~a.\n"
487                        our-name))
488     (<- player-loc 'tell-room
489         #:text (format #f "~a drops ~a.\n"
490                        player-name
491                        our-name)
492         #:exclude player))))
493
494 ;; @@: Moving this to a container subclass/mixin could allow a lot more
495 ;;   customization of take out / put in phrases
496 (define* (cmd-take-from gameobj message
497                         #:key direct-obj indir-obj preposition
498                         (player (message-from message)))
499   (define player-name
500     (mbody-val (<-wait player 'get-name)))
501   (define player-loc
502     (mbody-val (<-wait player 'get-loc)))
503   (define our-name (slot-ref gameobj 'name))
504   ;; We need to check if we even have such a thing
505   (define this-thing
506     (call/ec
507      (lambda (return)
508        (for-each (lambda (occupant)
509                    (define goes-by (mbody-val (<-wait occupant 'goes-by)))
510                    (when (ci-member direct-obj goes-by)
511                      (return occupant)))
512                  (gameobj-occupants gameobj))
513        ;; nothing found
514        #f)))
515   (define (this-thing-name)
516     (mbody-val (<-wait this-thing 'get-name)))
517   (define (should-take-from-me)
518     (and this-thing
519          (slot-ref-maybe-runcheck gameobj 'take-from-me? player this-thing)))
520   (define (default-objection)
521     `("Unfortunately, it doesn't seem like you can take "
522       (this-thing-name) " " preposition " " our-name "."))
523
524   (define (this-thing-objection)
525     (mbody-receive (_ taken-ok? #:key why-not) ; does the object object to being removed?
526         (<-wait this-thing 'ok-to-be-taken-from? player) ; @@ no need to supply from where
527       (and (not taken-ok?)
528            ;; Either give the specified reason, or give a boilerplate one
529            (or why-not
530                (default-objection)))))
531   (cond
532    ;; Wait, aren't we going to check (should-take-from-me) later?
533    ;; Well yes, but this checks if there's a #f as the value, which
534    ;; is a much clearer indication that this doesn't take *anything*.
535    ((not (slot-ref gameobj 'take-from-me?))
536     (<- player 'tell
537         #:text `("It's not really clear how to take something " ,preposition
538                  " " ,our-name ".")))
539
540    ;; Unfortunately this does leak information about what is contained
541    ;; by us.  Maybe not what's wanted in all circumstances.
542    ((not this-thing)
543     (<- player 'tell
544         #:text `("You don't see any such " ,direct-obj " to take from "
545                  ,our-name ".")))
546    ;; A particular objection to taking this thing.
547    ;; We should allow customizing the reason here, which could be
548    ;; provided by the 'ok-to-be-taken-from? slot.
549    ((not (should-take-from-me))
550     (<- player 'tell
551         #:text (default-objection)))
552    ;; the thing we wsant to take itself has objected...
553    ((this-thing-objection) =>
554     (lambda (objection)
555       (<- player 'tell
556           #:text objection)))
557    ;; looks like we can take it
558    (else
559     ;; Wait to announce to the player just in case settting the location
560     ;; errors out or something.  Maybe it's overthinking things, I dunno.
561     (<-wait this-thing 'set-loc! #:loc player)
562     (<- player 'tell
563         #:text `("You take " ,(this-thing-name) " from "
564                  ,our-name "."))
565     (<- player-loc 'tell-room
566         #:text `(,player-name " takes " ,(this-thing-name) " from "
567                               ,our-name ".")
568         #:exclude player))))
569
570 (define* (cmd-put-in gameobj message
571                      #:key direct-obj indir-obj preposition
572                      (player (message-from message)))
573   (define player-name
574     (mbody-val (<-wait player 'get-name)))
575   (define player-loc
576     (mbody-val (<-wait player 'get-loc)))
577   (define our-name (slot-ref gameobj 'name))
578   ;; We need to check if we even have such a thing
579   (define this-thing
580     (call/ec
581      (lambda (return)
582        (for-each (lambda (occupant)
583                    (define goes-by (mbody-val (<-wait occupant 'goes-by)))
584                    (when (ci-member direct-obj goes-by)
585                      (return occupant)))
586                  (mbody-val (<-wait player 'get-occupants)))
587        ;; nothing found
588        #f)))
589   (define (this-thing-name)
590     (mbody-val (<-wait this-thing 'get-name)))
591   (define (should-put-in-me)
592     (and this-thing
593          (slot-ref-maybe-runcheck gameobj 'put-in-me? player this-thing)))
594   (define (default-objection)
595     `("As much as you'd like to, it doesn't seem like you can put "
596       ,(this-thing-name) " " ,preposition " " ,our-name "."))
597   (define (this-thing-objection)
598     (mbody-receive (_ put-in-ok? #:key why-not) ; does the object object to being moved?
599         (<-wait this-thing 'ok-to-be-put-in? player (actor-id gameobj))
600       (and (not put-in-ok?)
601            ;; Either give the specified reason, or give a boilerplate one
602            (or why-not (default-objection)))))
603   (cond
604    ;; Is it not there, or maybe we won't allow it to be taken?
605    ((not this-thing)
606     (<- player 'tell
607         #:text `("You don't seem to have any such " ,direct-obj " to put "
608                  ,preposition " " ,our-name ".")))
609
610    ((or (not (should-put-in-me)))
611     (<- player 'tell
612         #:text (default-objection)))
613    ;; the thing we wsant to take itself has objected...
614    ((this-thing-objection) =>
615     (lambda (objection)
616       (<- player 'tell
617           #:text objection)))
618    ;; looks like we can take it
619    (else
620     ;; Wait to announce to the player just in case settting the location
621     ;; errors out or something.  Maybe it's overthinking things, I dunno.
622     (<-wait this-thing 'set-loc! #:loc (actor-id gameobj))
623     (<- player 'tell
624         #:text `("You put " ,(this-thing-name) " " ,preposition " "
625                  ,our-name "."))
626     (<- player-loc 'tell-room
627         #:text `(,player-name " puts " ,(this-thing-name) " " ,preposition " "
628                               ,our-name ".")
629         #:exclude player))))