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