1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
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.
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.
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/>.
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)
33 player-self-commands))
38 (define player-actions
40 (init (wrap-apply player-init))
41 (handle-input (wrap-apply player-handle-input))
42 (tell (wrap-apply player-tell))))
44 (define player-actions*
45 (append player-actions
48 (define player-dispatcher
49 (simple-dispatcher player-actions*))
51 (define-class <player> (<gameobj>)
52 (username #:init-keyword #:username
53 #:getter player-username)
57 #:getter player-self-commands)
61 ;; @@: We're gonna need action inheritance real awful soon, huh?
62 (wrap-apply player-dispatcher)))
65 ;;; player message handlers
67 (define-mhandler (player-init player message)
68 ;; Look around the room we're in
69 (<- player (gameobj-loc player) 'look-room))
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)))
77 (define command-candidates
79 (player-gather-command-handlers player input-verb)))
82 (pk 'winner (find-command-winner command-candidates input-rest)))
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)))
88 (<- player (gameobj-gm player) 'write-home
91 (define-mhandler (player-tell player message text)
92 (<- player (gameobj-gm player) 'write-home
99 ;; @@: Hard to know whether this should be in player.scm or here...
100 ;; @@: This could be more efficient as a stream...!?
101 (define (player-gather-command-handlers player verb)
103 (let ((result (gameobj-loc player)))
106 (throw 'player-has-no-location
107 "Player ~a has no location! How'd that happen?\n"
108 #:player-id (actor-id player)))))
110 ;; Ask the room for its commands
111 (define room-commands
112 ;; TODO: Map room id and sort
114 (<-wait player player-loc
115 'get-container-commands
119 ;; All the co-occupants of the room (not including ourself)
122 (lambda (x) (equal? x (actor-id player)))
124 (<-wait player player-loc 'get-occupants)
127 ;; @@: There's a race condition here if someone leaves the room
128 ;; during this, heh...
129 ;; I'm not sure it can be solved, but "lag" on the race can be
132 ;; Get all the co-occupants' commands
133 (define co-occupant-commands
134 ;; TODO: Switch this to a fold. Ignore a result if it
135 ;; returns false for in the command response
137 (lambda (co-occupant prev)
138 (display "pre-message\n")
139 (let* ((result (<-wait player co-occupant 'get-commands
141 (commands (message-ref result 'commands))
142 (goes-by (message-ref result 'goes-by)))
143 (display "post-message\n")
145 (map (lambda (command)
146 (list command goes-by co-occupant))
152 ;; Append our own command handlers
154 (player-self-commands player))
156 ;; TODO: Append our inventory's relevant command handlers
158 ;; Now return a big ol sorted list of ((actor-id . command))
160 (sort-commands-append-actor (pk 'room-commands room-commands)
161 player-loc '()) ; room doesn't go by anything
162 (sort-commands-multi-actors co-occupant-commands)
163 (sort-commands-append-actor our-commands
164 (actor-id player) '()))) ; nor does player
166 (define (sort-commands-append-actor commands actor-id goes-by)
167 (sort-commands-multi-actors
168 (map (lambda (command) (list command goes-by actor-id)) commands)))
170 (define (sort-commands-multi-actors actors-and-commands)
176 (> (command-priority (car x))
177 (command-priority (car y))))))
180 (define (find-command-winner sorted-candidates line)
181 "Find a command winner from a sorted list of candidates"
182 ;; A cache of results from matchers we've already seen
183 ;; TODO: fill this in
184 (define matcher-cache '())
189 ((command actor-goes-by actor-id)
190 (let* ((matcher (command-matcher command))
191 (matched (matcher line)))
193 ;; Great, it matched, but does it also pass
195 (apply (command-should-handle command)
197 matched)) ; matched is kwargs if truthy
198 (return (list (command-action command)
199 (pk 'earlier-actor-id actor-id) matched))