Update mudsync code to use easier to use action inheritance system
[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
35 (define thing-commands
36   (list
37    (direct-command "take" 'cmd-take)))
38
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    (direct-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-class <thing> (<gameobj>)
52   ;; Can be a boolean or a procedure accepting two arguments
53   ;; (thing-actor whos-acting)
54   (takeable #:init-value #f
55             #:init-keyword #:takeable)
56   ;; Can be a boolean or a procedure accepting two arguments
57   ;; (thing-actor whos-dropping)
58   (dropable #:init-value #t
59             #:init-keyword #:dropable)
60   (commands
61    #:init-value (wrap thing-commands))
62   (contained-commands
63    #:init-value (wrap thing-contained-commands))
64   (actions #:allocation #:each-subclass
65            #:init-value
66            (mhandlers
67             (cmd-take thing-cmd-take)
68             (cmd-drop thing-cmd-drop))))
69
70 (define* (thing-cmd-take thing message #:key direct-obj)
71   (define player (message-from message))
72   (define player-name
73     (msg-val (<-wait thing player 'get-name)))
74   (define player-loc
75     (msg-val (<-wait thing player 'get-loc)))
76   (define thing-name (slot-ref thing 'name))
77   (define should-take
78     (slot-ref-maybe-runcheck thing 'takeable player))
79   (if should-take
80       ;; Set the location to whoever's picking us up
81       (begin
82         (gameobj-set-loc! thing player)
83         (<- thing player 'tell
84             #:text (format #f "You pick up ~a.\n"
85                            thing-name))
86         (<- thing player-loc 'tell-room
87             #:text (format #f "~a picks up ~a.\n"
88                            player-name
89                            thing-name)
90             #:exclude player))
91       (<- thing player 'tell
92           #:text (format #f "It doesn't seem like you can pick up ~a.\n"
93                          thing-name))))
94
95 (define* (thing-cmd-drop thing message #:key direct-obj)
96   (define player (message-from message))
97   (define player-name
98     (msg-val (<-wait thing player 'get-name)))
99   (define player-loc
100     (msg-val (<-wait thing player 'get-loc)))
101   (define thing-name (slot-ref thing 'name))
102   (define should-drop
103     (slot-ref-maybe-runcheck thing 'dropable player))
104   (if player-loc
105       ;; Set the location to whoever's picking us up's location
106       (begin
107         (gameobj-set-loc! thing player-loc)
108         (<- thing player 'tell
109             #:text (format #f "You drop ~a.\n"
110                            thing-name))
111         (<- thing player-loc 'tell-room
112             #:text (format #f "~a drops ~a.\n"
113                            player-name
114                            thing-name)
115             #:exclude player))
116       (<- thing player 'tell
117           #:text (format #f "It doesn't seem like you can drop ~a.\n"
118                          thing-name))))