Add wire cage
[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 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
31 (define-class <thing> (<gameobj>)
32   ;; Can be a boolean or a procedure accepting two arguments
33   ;; (thing-actor whos-acting)
34   (takeable #:init-value #f
35             #:init-keyword #:takeable)
36   ;; Can be a boolean or a procedure accepting two arguments
37   ;; (thing-actor whos-dropping)
38   (dropable #:init-value #t
39             #:init-keyword #:dropable)
40   (commands
41    #:allocation #:each-subclass
42    #:init-thunk (build-commands
43                  ("take" ((direct-command cmd-take)))))
44   (contained-commands
45    #:allocation #:each-subclass
46    #:init-value (build-commands
47                  ("drop" ((direct-command cmd-drop)))))
48   (actions #:allocation #:each-subclass
49            #:init-thunk
50            (build-actions
51             (cmd-take thing-cmd-take)
52             (cmd-drop thing-cmd-drop))))
53
54 (define* (thing-cmd-take thing message #:key direct-obj)
55   (define player (message-from message))
56   (define player-name
57     (mbody-val (<-wait player 'get-name)))
58   (define player-loc
59     (mbody-val (<-wait player 'get-loc)))
60   (define thing-name (slot-ref thing 'name))
61   (define should-take
62     (slot-ref-maybe-runcheck thing 'takeable player))
63   (if should-take
64       ;; Set the location to whoever's picking us up
65       (begin
66         (gameobj-set-loc! thing player)
67         (<- player 'tell
68             #:text (format #f "You pick up ~a.\n"
69                            thing-name))
70         (<- player-loc 'tell-room
71             #:text (format #f "~a picks up ~a.\n"
72                            player-name
73                            thing-name)
74             #:exclude player))
75       (<- player 'tell
76           #:text (format #f "It doesn't seem like you can pick up ~a.\n"
77                          thing-name))))
78
79 (define* (thing-cmd-drop thing message #:key direct-obj)
80   (define player (message-from message))
81   (define player-name
82     (mbody-val (<-wait player 'get-name)))
83   (define player-loc
84     (mbody-val (<-wait player 'get-loc)))
85   (define thing-name (slot-ref thing 'name))
86   (define should-drop
87     (slot-ref-maybe-runcheck thing 'dropable player))
88   (if player-loc
89       ;; Set the location to whoever's picking us up's location
90       (begin
91         (gameobj-set-loc! thing player-loc)
92         (<- player 'tell
93             #:text (format #f "You drop ~a.\n"
94                            thing-name))
95         (<- player-loc 'tell-room
96             #:text (format #f "~a drops ~a.\n"
97                            player-name
98                            thing-name)
99             #:exclude player))
100       (<- player 'tell
101           #:text (format #f "It doesn't seem like you can drop ~a.\n"
102                          thing-name))))