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 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-self-commands
40 (empty-command "inventory" 'cmd-inventory)
42 ;; @@: Should use an "alias" system for common aliases?
43 (empty-command "inv" 'cmd-inventory)
44 (empty-command "i" 'cmd-inventory)))
46 (define-class <player> (<gameobj>)
47 (username #:init-keyword #:username
48 #:getter player-username)
50 (self-commands #:init-value player-self-commands)
52 (actions #:allocation #:each-subclass
56 (handle-input player-handle-input)
58 (disconnect-self-destruct player-disconnect-self-destruct)
59 (cmd-inventory player-cmd-inventory))))
62 ;;; player message handlers
64 (define (player-init player message)
65 ;; Look around the room we're in
66 (<- (gameobj-loc player) 'look-room))
69 (define* (player-handle-input player message #:key input)
70 (define split-input (split-verb-and-rest input))
71 (define input-verb (car split-input))
72 (define input-rest (cdr split-input))
74 (define command-candidates
75 (player-gather-command-handlers player input-verb))
78 (find-command-winner command-candidates input-rest))
81 ((cmd-action winner-id message-args)
82 (apply <- winner-id cmd-action message-args))
84 (<- (gameobj-gm player) 'write-home
87 (define* (player-tell player message #:key text)
88 (<- (gameobj-gm player) 'write-home
91 (define (player-disconnect-self-destruct player message)
92 "Action routine for being told to disconnect and self destruct."
93 (define loc (gameobj-loc player))
96 #:exclude (actor-id player)
97 #:text (format #f "~a disappears in a puff of entropy!\n"
98 (slot-ref player 'name))))
99 (gameobj-self-destruct player))
101 (define (player-cmd-inventory player message)
102 "Display the inventory for the player"
106 (mbody-val (<-wait inv-item 'get-name)))
107 (gameobj-occupants player)))
109 (if (eq? inv-names '())
110 "You aren't carrying anything.\n"
112 "You are carrying:\n"
113 (map (lambda (item-name)
114 (string-append " * " item-name "\n"))
116 (<- (actor-id player) 'tell #:text text-to-show))
122 ;; @@: Hard to know whether this should be in player.scm or here...
123 ;; @@: This could be more efficient as a stream...!?
124 (define (player-gather-command-handlers player verb)
126 (let ((result (gameobj-loc player)))
129 (throw 'player-has-no-location
130 "Player ~a has no location! How'd that happen?\n"
131 #:player-id (actor-id player)))))
133 ;; Ask the room for its commands
134 (define room-commands
135 ;; TODO: Map room id and sort
136 (mbody-receive (_ #:key commands)
137 (<-wait player-loc 'get-container-commands
141 ;; All the co-occupants of the room (not including ourself)
144 (lambda (x) (equal? x (actor-id player)))
145 (mbody-receive (_ #:key occupants)
146 (<-wait player-loc 'get-occupants)
149 ;; @@: There's a race condition here if someone leaves the room
150 ;; during this, heh...
151 ;; I'm not sure it can be solved, but "lag" on the race can be
154 ;; Get all the co-occupants' commands
155 (define co-occupant-commands
157 (lambda (co-occupant prev)
158 (mbody-receive (_ #:key commands goes-by)
159 (<-wait co-occupant 'get-commands
162 (map (lambda (command)
163 (list command goes-by co-occupant))
169 ;; Append our own command handlers
173 (equal? (command-verbs cmd) verb))
175 (slot-ref player 'self-commands))))
177 ;; Append our inventory's relevant command handlers
179 (gameobj-occupants player))
180 (define inv-item-commands
182 (lambda (inv-item prev)
183 (mbody-receive (_ #:key commands goes-by)
184 (<-wait inv-item 'get-contained-commands
187 (map (lambda (command)
188 (list command goes-by inv-item))
194 ;; Now return a big ol sorted list of ((actor-id . command))
196 (sort-commands-append-actor room-commands
197 player-loc '()) ; room doesn't go by anything
198 (sort-commands-multi-actors co-occupant-commands)
199 (sort-commands-append-actor our-commands
200 (actor-id player) '()) ; nor does player
201 (sort-commands-multi-actors inv-item-commands)))
203 (define (sort-commands-append-actor commands actor-id goes-by)
204 (sort-commands-multi-actors
205 (map (lambda (command) (list command goes-by actor-id)) commands)))
207 (define (sort-commands-multi-actors actors-and-commands)
211 (> (command-priority (car x))
212 (command-priority (car y))))))
215 (define (find-command-winner sorted-candidates line)
216 "Find a command winner from a sorted list of candidates"
217 ;; A cache of results from matchers we've already seen
218 ;; TODO: fill in this cache. This is a *critical* optimization!
219 (define matcher-cache '())
224 ((command actor-goes-by actor-id)
225 (let* ((matcher (command-matcher command))
226 (matched (matcher line)))
228 ;; Great, it matched, but does it also pass
230 (apply (command-should-handle command)
232 matched)) ; matched is kwargs if truthy
233 (return (list (command-action command)