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