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