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