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