a3ae6b003e3d706407252ca931e9f1fbd4d50a56
[mudsync.git] / mudsync / thing.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 ;;; Common "things" and stuff you can do with things.
20
21 (define-module (mudsync thing)
22   #:use-module (mudsync command)
23   #:use-module (mudsync gameobj)
24   #:use-module (8sync systems actors)
25   #:use-module (8sync agenda)
26   #:use-module (oop goops)
27   #:use-module (ice-9 match)
28   #:use-module (ice-9 format)
29   #:export (<thing>
30             thing-commands
31             thing-commands*
32             thing-contained-commands
33             thing-contained-commands*
34             thing-actions
35             thing-actions*))
36
37 (define thing-commands
38   (list
39    (direct-command "take" 'cmd-take)))
40
41 ;; Doesn't inherit anything (gameobj has no commands)
42 ;; so it's an alias.
43 (define thing-commands* thing-commands)
44
45 (define thing-contained-commands
46   (list
47    (direct-command "drop" 'cmd-drop)))
48
49 ;; Doesn't inherit anything (gameobj has no contained-commands)
50 ;; so it's an alias.
51 (define thing-contained-commands* thing-contained-commands)
52
53 (define thing-actions
54   (build-actions
55    (cmd-take (wrap-apply thing-cmd-take))
56    (cmd-drop (wrap-apply thing-cmd-drop))))
57
58 (define thing-actions*
59   (append thing-actions
60           gameobj-actions))
61
62 (define thing-dispatcher
63   (simple-dispatcher thing-actions*))
64
65 (define-class <thing> (<gameobj>)
66   ;; Can be a boolean or a procedure accepting two arguments
67   ;; (thing-actor whos-acting)
68   (takeable #:init-value #f
69             #:init-keyword #:takeable)
70   ;; Can be a boolean or a procedure accepting two arguments
71   ;; (thing-actor whos-dropping)
72   (dropable #:init-value #t
73             #:init-keyword #:dropable)
74   (commands
75    #:init-value (wrap thing-commands))
76   (contained-commands
77    #:init-value (wrap thing-contained-commands))
78   (message-handler
79    #:init-value
80    (wrap-apply thing-dispatcher)))
81
82 (define-mhandler (thing-cmd-take thing message direct-obj)
83   (define player (message-from message))
84   (define player-name
85     (message-ref
86      (<-wait thing player 'get-name)
87      'val))
88   (define player-loc
89     (message-ref
90      (<-wait thing player 'get-loc)
91      'val))
92   (define thing-name (slot-ref thing 'name))
93   (define should-take
94     (slot-ref-maybe-runcheck thing 'takeable player))
95   (if should-take
96       ;; Set the location to whoever's picking us up
97       (begin
98         (gameobj-set-loc! thing player)
99         (<- thing player 'tell
100             #:text (format #f "You pick up ~a.\n"
101                            thing-name))
102         (<- thing player-loc 'tell-room
103             #:text (format #f "~a picks up ~a.\n"
104                            player-name
105                            thing-name)
106             #:exclude player))
107       (<- thing player 'tell
108           #:text (format #f "It doesn't seem like you can pick up ~a.\n"
109                          thing-name))))
110
111 (define-mhandler (thing-cmd-drop thing message direct-obj)
112   (define player (message-from message))
113   (define player-name
114     (message-ref
115      (<-wait thing player 'get-name)
116      'val))
117   (define player-loc
118     (message-ref
119      (<-wait thing player 'get-loc)
120      'val))
121   (define thing-name (slot-ref thing 'name))
122   (define should-drop
123     (slot-ref-maybe-runcheck thing 'dropable player))
124   (if player-loc
125       ;; Set the location to whoever's picking us up's location
126       (begin
127         (gameobj-set-loc! thing player-loc)
128         (<- thing player 'tell
129             #:text (format #f "You drop ~a.\n"
130                            thing-name))
131         (<- thing player-loc 'tell-room
132             #:text (format #f "~a drops ~a.\n"
133                            player-name
134                            thing-name)
135             #:exclude player))
136       (<- thing player 'tell
137           #:text (format #f "It doesn't seem like you can drop ~a.\n"
138                          thing-name))))