remove stupid pre/post message stuff
[mudsync.git] / mudsync / player.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 player)
20   #:use-module (mudsync command)
21   #:use-module (mudsync gameobj)
22   #:use-module (mudsync game-master)
23   #:use-module (mudsync parser)
24   #:use-module (8sync systems actors)
25   #:use-module (8sync agenda)
26   #:use-module (ice-9 control)
27   #:use-module (ice-9 format)
28   #:use-module (ice-9 match)
29   #:use-module (oop goops)
30   #:use-module (srfi srfi-1)
31   #:use-module (srfi srfi-9)
32   #:export (<player>
33             player-self-commands))
34
35 ;;; Players
36 ;;; =======
37
38 (define player-actions
39   (build-actions
40    (init (wrap-apply player-init))
41    (handle-input (wrap-apply player-handle-input))
42    (tell (wrap-apply player-tell))))
43
44 (define player-actions*
45   (append player-actions
46           gameobj-actions))
47
48 (define player-dispatcher
49   (simple-dispatcher player-actions*))
50
51 (define-class <player> (<gameobj>)
52   (username #:init-keyword #:username
53             #:getter player-username)
54
55   (self-commands
56    #:init-value '()
57    #:getter player-self-commands)
58
59   (message-handler
60    #:init-value
61    ;; @@: We're gonna need action inheritance real awful soon, huh?
62    (wrap-apply player-dispatcher)))
63
64
65 ;;; player message handlers
66
67 (define-mhandler (player-init player message)
68   ;; Look around the room we're in
69   (<- player (gameobj-loc player) 'look-room))
70
71
72 (define-mhandler (player-handle-input player message input)
73   (define split-input (split-verb-and-rest input))
74   (define input-verb (car split-input))
75   (define input-rest (cdr split-input))
76
77   (define command-candidates
78     (player-gather-command-handlers player input-verb))
79
80   (define winner
81     (find-command-winner command-candidates input-rest))
82
83   (match winner
84     ((cmd-action winner-id message-args)
85      (apply send-message player winner-id cmd-action message-args))
86     (#f
87      (<- player (gameobj-gm player) 'write-home
88          #:text "Huh?\n"))))
89
90 (define-mhandler (player-tell player message text)
91   (<- player (gameobj-gm player) 'write-home
92       #:text text))
93
94
95 ;;; Command handling
96 ;;; ================
97
98 ;; @@: Hard to know whether this should be in player.scm or here...
99 ;; @@: This could be more efficient as a stream...!?
100 (define (player-gather-command-handlers player verb)
101   (define player-loc
102     (let ((result (gameobj-loc player)))
103       (if result
104           result
105           (throw 'player-has-no-location
106                  "Player ~a has no location!  How'd that happen?\n"
107                  #:player-id (actor-id player)))))
108
109   ;; Ask the room for its commands
110   (define room-commands
111     ;; TODO: Map room id and sort
112     (message-ref
113      (<-wait player player-loc
114              'get-container-commands
115              #:verb verb)
116      'commands))
117
118   ;; All the co-occupants of the room (not including ourself)
119   (define co-occupants
120     (remove
121      (lambda (x) (equal? x (actor-id player)))
122      (message-ref
123       (<-wait player player-loc 'get-occupants)
124       'occupants)))
125
126   ;; @@: There's a race condition here if someone leaves the room
127   ;;   during this, heh...
128   ;;   I'm not sure it can be solved, but "lag" on the race can be
129   ;;   reduced maybe?
130
131   ;; Get all the co-occupants' commands
132   (define co-occupant-commands
133     ;; TODO: Switch this to a fold.  Ignore a result if it
134     ;;   returns false for in the command response
135     (fold
136      (lambda (co-occupant prev)
137        (let* ((result (<-wait player co-occupant 'get-commands
138                               #:verb verb))
139               (commands (message-ref result 'commands))
140               (goes-by (message-ref result 'goes-by)))
141          (append
142           (map (lambda (command)
143                  (list command goes-by co-occupant))
144                commands)
145           prev)))
146      '()
147      co-occupants))
148
149   ;; Append our own command handlers
150   (define our-commands
151     (player-self-commands player))
152
153   ;; TODO: Append our inventory's relevant command handlers
154
155   ;; Now return a big ol sorted list of ((actor-id . command))
156   (append
157    (sort-commands-append-actor room-commands
158                                player-loc '()) ; room doesn't go by anything
159    (sort-commands-multi-actors co-occupant-commands)
160    (sort-commands-append-actor our-commands
161                                (actor-id player) '()))) ; nor does player
162
163 (define (sort-commands-append-actor commands actor-id goes-by)
164   (sort-commands-multi-actors
165    (map (lambda (command) (list command goes-by actor-id)) commands)))
166
167 (define (sort-commands-multi-actors actors-and-commands)
168   (sort
169    actors-and-commands
170    (lambda (x y)
171      (> (command-priority (car x))
172         (command-priority (car y))))))
173
174
175 (define (find-command-winner sorted-candidates line)
176   "Find a command winner from a sorted list of candidates"
177   ;; A cache of results from matchers we've already seen
178   ;; TODO: fill this in
179   (define matcher-cache '())
180   (call/ec
181    (lambda (return)
182      (for-each
183       (match-lambda
184         ((command actor-goes-by actor-id)
185          (let* ((matcher (command-matcher command))
186                 (matched (matcher line)))
187            (if (and matched
188                     ;; Great, it matched, but does it also pass
189                     ;; should-handle?
190                     (apply (command-should-handle command)
191                            actor-goes-by
192                            matched))  ; matched is kwargs if truthy
193                (return (list (command-action command)
194                              actor-id matched))
195                #f))))
196       sorted-candidates)
197      #f)))