moving between rooms nearly works
[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             #:accessor player-username)
54   ;; Connection id
55   (client #:accessor player-client)
56
57   (self-commands
58    #:init-value '()
59    #:accessor player-self-commands)
60
61   (message-handler
62    #:init-value
63    ;; @@: We're gonna need action inheritance real awful soon, huh?
64    (wrap-apply player-dispatcher)))
65
66
67 ;;; player message handlers
68
69 (define-mhandler (player-init! player message)
70   (player-look-around player))
71
72
73 (define-mhandler (player-handle-input player message input)
74   (define split-input (split-verb-and-rest input))
75   (define input-verb (pk 'input-verb (car split-input)))
76   (define input-rest (pk 'input-rest (cdr split-input)))
77
78   (define command-candidates
79     (pk 'candidates
80         (player-gather-command-handlers player input-verb)))
81
82   (define winner
83     (pk 'winner (find-command-winner command-candidates input-rest)))
84
85   (match winner
86     ((cmd-action winner-id message-args)
87      (apply send-message player (pk 'winner-id winner-id) (pk 'cmd-action cmd-action) (pk 'message-args message-args)))
88     (#f
89      (<- player (gameobj-gm player) 'write-home
90          #:text "Huh?\n"))))
91
92 (define-mhandler (player-tell player message text)
93   (<- player (gameobj-gm player) 'write-home
94       #:text text))
95
96
97 ;;; player methods
98
99 (define (player-look-around player)
100   (define room-name
101     (message-ref
102      (<-wait player (gameobj-loc player) 'get-name)
103      'val))
104   (define room-desc
105     (message-ref
106      (<-wait player (gameobj-loc player) 'get-desc)
107      'val))
108   (define message-text
109     (format #f "**~a**\n~a\n" room-name room-desc))
110
111   (<- player (gameobj-gm player) 'write-home #:text message-text))
112
113
114 ;;; Command handling
115 ;;; ================
116
117 ;; @@: Hard to know whether this should be in player.scm or here...
118 ;; @@: This could be more efficient as a stream...!?
119 (define (player-gather-command-handlers player verb)
120   (define player-loc
121     (let ((result (gameobj-loc player)))
122       (if result
123           result
124           (throw 'player-has-no-location
125                  "Player ~a has no location!  How'd that happen?\n"
126                  #:player-id (actor-id player)))))
127
128   ;; Ask the room for its commands
129   (define room-commands
130     ;; TODO: Map room id and sort
131     (message-ref
132      (<-wait player player-loc
133              'get-container-commands
134              #:verb verb)
135      'commands))
136
137   ;; All the co-occupants of the room (not including ourself)
138   (define co-occupants
139     (remove
140      (lambda (x) (equal? x (actor-id player)))
141      (message-ref
142       (<-wait player player-loc 'get-occupants)
143       'occupants)))
144
145   ;; @@: There's a race condition here if someone leaves the room
146   ;;   during this, heh...
147   ;;   I'm not sure it can be solved, but "lag" on the race can be
148   ;;   reduced maybe?
149
150   ;; Get all the co-occupants' commands
151   (define co-occupant-commands
152     ;; TODO: Switch this to a fold.  Ignore a result if it
153     ;;   returns false for in the command response
154     (map
155      (lambda (co-occupant)
156        (let ((result (<-wait player co-occupant 'get-commands
157                              #:verb verb)))
158          (list
159           (message-ref result 'commands)
160           (message-ref result 'goes-by)
161           co-occupant)))
162      co-occupants))
163
164   ;; Append our own command handlers
165   (define our-commands
166     (player-self-commands player))
167
168   ;; TODO: Append our inventory's relevant command handlers
169
170   ;; Now return a big ol sorted list of ((actor-id . command))
171   (append
172    (sort-commands-append-actor (pk 'room-commands room-commands)
173                                player-loc '()) ; room doesn't go by anything
174    (sort-commands-multi-actors co-occupant-commands)
175    (sort-commands-append-actor our-commands
176                                (actor-id player) '()))) ; nor does player
177
178 (define (sort-commands-append-actor commands actor-id goes-by)
179   (sort-commands-multi-actors
180    (map (lambda (command) (list command goes-by actor-id)) commands)))
181
182 (define (sort-commands-multi-actors actors-and-commands)
183   (sort
184    actors-and-commands
185    (lambda (x y)
186      (> (command-priority (car (pk 'x x)))
187         (command-priority (car (pk 'y y)))))))
188
189
190 (define (find-command-winner sorted-candidates line)
191   "Find a command winner from a sorted list of candidates"
192   ;; A cache of results from matchers we've already seen
193   ;; TODO: fill this in
194   (define matcher-cache '())
195   (call/ec
196    (lambda (return)
197      (for-each
198       (match-lambda
199         ((command actor-goes-by actor-id)
200          (let* ((matcher (command-matcher command))
201                 (matched (matcher line)))
202            (if (and matched
203                     ;; Great, it matched, but does it also pass
204                     ;; should-handle?
205                     (apply (command-should-handle command)
206                            actor-goes-by
207                            matched))  ; matched is kwargs if truthy
208                (return (list (command-action command)
209                              (pk 'earlier-actor-id actor-id) matched))
210                #f))))
211       sorted-candidates)
212      #f)))