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