a79f50d3e7f878ae24b9a1b6d8d5d98ebaeb0086
[mudsync.git] / mudsync / command.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 (define-module (mudsync command)
20   #:use-module (mudsync parser)
21   #:use-module (8sync actors)
22   #:use-module (8sync rmeta-slot)
23   #:use-module (srfi srfi-1)
24   #:use-module (srfi srfi-9)
25   #:use-module (ice-9 control)
26   #:use-module (ice-9 match)
27
28   #:export (command?
29             command-verbs
30             command-matcher
31             command-should-handle
32             command-action
33             command-priority
34
35             build-commands
36
37             direct-command
38             prep-indir-command
39             prep-direct-command
40             loose-direct-command
41             loose-prep-command
42             empty-command
43             direct-greedy-command
44             greedy-command
45             player-gather-command-handlers
46             find-command-winner))
47
48 ;;; Commands
49 ;;; ========
50
51 (define %low-priority 0)
52 (define %default-priority 1)
53 (define %high-priority 2)
54
55 ;; ;;; Avoiding some annoying issues crossing the continuation barrier
56 ;; ;;; and the "@@" special form
57 ;; (define (make-command verbs matcher should-handle action priority)
58 ;;   (list '*command* verbs matcher should-handle action priority))
59
60 ;; (define command-verbs second)
61 ;; (define command-matcher third)
62 ;; (define command-should-handle fourth)
63 ;; (define command-action fifth)
64 ;; (define command-priority sixth)
65
66 (define-record-type <command>
67   (make-command verbs matcher should-handle action priority)
68   command?
69   (verbs command-verbs)
70   (matcher command-matcher)
71   (should-handle command-should-handle)
72   (action command-action)
73   (priority command-priority))
74
75 (define-syntax %build-command
76   (syntax-rules ()
77     ((_ (verb ...) ((cmd-proc action-sym args ...) ...))
78      (list (cons verb
79                  (list (cmd-proc (list verb ...)
80                                  (quote action-sym)
81                                  args ...)
82                        ...))
83            ...))
84     ((_ verb ((cmd-proc action-sym args ...) ...))
85      (list (cons verb
86                  (list (cmd-proc (list verb)
87                                  (quote action-sym)
88                                  args ...)
89                        ...))))))
90
91 (define-syntax-rule (build-commands (verb-or-verbs cmd-defs ...) ...)
92   (wrap-rmeta-slot
93    (append (%build-command verb-or-verbs cmd-defs ...) ...)))
94
95
96 (define (direct-command verbs action)
97   (make-command verbs
98                 cmatch-direct-obj
99                 ;; @@: Should we allow fancier matching than this?
100                 ;;   Let the actor itself pass along this whole method?
101                 (lambda* (goes-by #:key direct-obj)
102                   (member direct-obj goes-by))
103                 action
104                 %default-priority))
105
106 (define (loose-direct-command verbs action)
107   (make-command verbs
108                 cmatch-direct-obj
109                 ;; @@: Should we allow fancier matching than this?
110                 ;;   Let the actor itself pass along this whole method?
111                 (const #t)
112                 action
113                 %default-priority))
114
115
116 (define* (prep-indir-command verbs action #:optional prepositions)
117   (make-command verbs
118                 cmatch-indir-obj
119                 (lambda* (goes-by #:key direct-obj indir-obj preposition)
120                   (if prepositions
121                       (and
122                        (member indir-obj goes-by)
123                        (member preposition prepositions))
124                       (member indir-obj goes-by)))
125                 action
126                 %high-priority))
127
128 (define* (prep-direct-command verbs action #:optional prepositions)
129   (make-command verbs
130                 cmatch-indir-obj
131                 (lambda* (goes-by #:key direct-obj indir-obj preposition)
132                   (if prepositions
133                       (and
134                        (member  direct-obj goes-by)
135                        (member preposition prepositions))
136                       (member direct-obj goes-by)))
137                 action
138                 %high-priority))
139
140 (define* (loose-prep-command verbs action #:optional prepositions)
141   (make-command verbs
142                 cmatch-indir-obj
143                 (const #t)
144                 action
145                 %high-priority))
146
147
148 (define (empty-command verbs action)
149   (make-command verbs
150                 cmatch-empty
151                 (const #t)
152                 action
153                 %low-priority))
154
155 (define (greedy-command verbs action)
156   (make-command verbs
157                 cmatch-greedy
158                 (const #t)
159                 action
160                 %low-priority))
161
162 (define (direct-greedy-command verbs action)
163   "greedy commands but which match the direct object"
164   (make-command verbs
165                 cmatch-direct-obj-greedy
166                 (lambda* (goes-by #:key direct-obj rest)
167                   (member direct-obj goes-by))
168                 action
169                 %low-priority))
170
171 ;; @@: We should probably ONLY allow these to go to users!
172 (define* (custom-command verbs matcher should-handle action
173                          #:optional (priority %default-priority))
174   "Full-grained customizable command."
175   (make-command verbs matcher should-handle action priority))