finishing unfinished comment
[mudsync.git] / mudsync / container.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 ;;; Containers
20 ;;; ==========
21 ;;;
22 ;;; While all gameobjs are containers, some gameobjs are more
23 ;;; containers than others.
24
25 (define-module (mudsync container)
26   #:use-module (8sync)
27   #:use-module (oop goops)
28   #:use-module (mudsync gameobj)
29   #:use-module (mudsync utils)
30   #:use-module (ice-9 control)
31   #:export (<container>
32             cmd-take-from cmd-put-in))
33
34 (define-actor <container> (<gameobj>)
35   ((cmd-take-from cmd-take-from)
36    (cmd-put-in cmd-put-in))
37   ;; Can be a boolean or a procedure accepting
38   ;; (gameobj whos-acting take-what)
39   (take-from-me? #:init-value #t
40                  #:init-keyword #:take-from-me?)
41   ;; Can be a boolean or a procedure accepting
42   ;; (gameobj whos-acting put-what)
43   (put-in-me? #:init-value #t
44               #:init-keyword #:put-in-me?))
45
46 ;; @@: Moving this to a container subclass/mixin could allow a lot more
47 ;;   customization of take out / put in phrases
48 (define* (cmd-take-from gameobj message
49                         #:key direct-obj indir-obj preposition
50                         (player (message-from message)))
51   (define player-name
52     (mbody-val (<-wait player 'get-name)))
53   (define player-loc
54     (mbody-val (<-wait player 'get-loc)))
55   (define our-name (slot-ref gameobj 'name))
56   ;; We need to check if we even have such a thing
57   (define this-thing
58     (call/ec
59      (lambda (return)
60        (for-each (lambda (occupant)
61                    (define goes-by (mbody-val (<-wait occupant 'goes-by)))
62                    (when (ci-member direct-obj goes-by)
63                      (return occupant)))
64                  (gameobj-occupants gameobj))
65        ;; nothing found
66        #f)))
67   (define (this-thing-name)
68     (mbody-val (<-wait this-thing 'get-name)))
69   (define (should-take-from-me)
70     (and this-thing
71          (slot-ref-maybe-runcheck gameobj 'take-from-me? player this-thing)))
72   (define (default-objection)
73     `("Unfortunately, it doesn't seem like you can take "
74       ,(this-thing-name) " " ,preposition " " ,our-name "."))
75
76   (define (this-thing-objection)
77     (mbody-receive (_ taken-ok? #:key why-not) ; does the object object to being removed?
78         (<-wait this-thing 'ok-to-be-taken-from? player) ; @@ no need to supply from where
79       (and (not taken-ok?)
80            ;; Either give the specified reason, or give a boilerplate one
81            (or why-not
82                (default-objection)))))
83   (cond
84    ;; Unfortunately this does leak information about what is contained
85    ;; by us.  Maybe not what's wanted in all circumstances.
86    ((not this-thing)
87     (<- player 'tell
88         #:text `("You don't see any such " ,direct-obj " to take "
89                  ,preposition " " ,our-name ".")))
90    ;; A particular objection to taking this thing.
91    ;; We should allow customizing the reason here, which could be
92    ;; provided by the 'ok-to-be-taken-from? slot.
93    ((not (should-take-from-me))
94     (<- player 'tell
95         #:text (default-objection)))
96    ;; the thing we wsant to take itself has objected...
97    ((this-thing-objection) =>
98     (lambda (objection)
99       (<- player 'tell
100           #:text objection)))
101    ;; looks like we can take it
102    (else
103     ;; Wait to announce to the player just in case settting the location
104     ;; errors out or something.  Maybe it's overthinking things, I dunno.
105     (<-wait this-thing 'set-loc! #:loc player)
106     (<- player 'tell
107         #:text `("You take " ,(this-thing-name) " from "
108                  ,our-name "."))
109     (<- player-loc 'tell-room
110         #:text `(,player-name " takes " ,(this-thing-name) " from "
111                               ,our-name ".")
112         #:exclude player))))
113
114 (define* (cmd-put-in gameobj message
115                      #:key direct-obj indir-obj preposition
116                      (player (message-from message)))
117   (define player-name
118     (mbody-val (<-wait player 'get-name)))
119   (define player-loc
120     (mbody-val (<-wait player 'get-loc)))
121   (define our-name (slot-ref gameobj 'name))
122   ;; We need to check if we even have such a thing
123   (define this-thing
124     (call/ec
125      (lambda (return)
126        (for-each (lambda (occupant)
127                    (define goes-by (mbody-val (<-wait occupant 'goes-by)))
128                    (when (ci-member direct-obj goes-by)
129                      (return occupant)))
130                  (mbody-val (<-wait player 'get-occupants)))
131        ;; nothing found
132        #f)))
133   (define (this-thing-name)
134     (mbody-val (<-wait this-thing 'get-name)))
135   (define (should-put-in-me)
136     (and this-thing
137          (slot-ref-maybe-runcheck gameobj 'put-in-me? player this-thing)))
138   (define (default-objection)
139     `("As much as you'd like to, it doesn't seem like you can put "
140       ,(this-thing-name) " " ,preposition " " ,our-name "."))
141   (define (this-thing-objection)
142     (mbody-receive (_ put-in-ok? #:key why-not) ; does the object object to being moved?
143         (<-wait this-thing 'ok-to-be-put-in? player (actor-id gameobj))
144       (and (not put-in-ok?)
145            ;; Either give the specified reason, or give a boilerplate one
146            (or why-not (default-objection)))))
147   (cond
148    ;; Is it not there, or maybe we won't allow it to be taken?
149    ((not this-thing)
150     (<- player 'tell
151         #:text `("You don't seem to have any such " ,direct-obj " to put "
152                  ,preposition " " ,our-name ".")))
153
154    ((or (not (should-put-in-me)))
155     (<- player 'tell
156         #:text (default-objection)))
157    ;; the thing we wsant to take itself has objected...
158    ((this-thing-objection) =>
159     (lambda (objection)
160       (<- player 'tell
161           #:text objection)))
162    ;; looks like we can take it
163    (else
164     ;; Wait to announce to the player just in case settting the location
165     ;; errors out or something.  Maybe it's overthinking things, I dunno.
166     (<-wait this-thing 'set-loc! #:loc (actor-id gameobj))
167     (<- player 'tell
168         #:text `("You put " ,(this-thing-name) " " ,preposition " "
169                  ,our-name "."))
170     (<- player-loc 'tell-room
171         #:text `(,player-name " puts " ,(this-thing-name) " " ,preposition " "
172                               ,our-name ".")
173         #:exclude player))))