command.scm 6.4 KB

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