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