784db7c6f9a08691d919ad1a66a26693bbe24e5d
[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 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-self-commands
39   (list
40    (empty-command "inventory" 'cmd-inventory)
41    ;; aliases...
42    ;; @@: Should use an "alias" system for common aliases?
43    (empty-command "inv" 'cmd-inventory)
44    (empty-command "i" 'cmd-inventory)
45    (empty-command "help" 'cmd-help)))
46
47 (define-class <player> (<gameobj>)
48   (username #:init-keyword #:username
49             #:getter player-username)
50
51   (self-commands #:init-value (wrap player-self-commands))
52
53   (actions #:allocation #:each-subclass
54            #:init-value
55            (build-actions
56             (init player-init)
57             (handle-input player-handle-input)
58             (tell player-tell)
59             (disconnect-self-destruct player-disconnect-self-destruct)
60             (cmd-inventory player-cmd-inventory)
61             (cmd-help player-cmd-help))))
62
63
64 ;;; player message handlers
65
66 (define (player-init player message)
67   ;; Look around the room we're in
68   (<- (gameobj-loc player) 'look-room))
69
70
71 (define* (player-handle-input player message #:key input)
72   (define split-input (split-verb-and-rest input))
73   (define input-verb (car split-input))
74   (define input-rest (cdr split-input))
75
76   (define command-candidates
77     (player-gather-command-handlers player input-verb))
78
79   (define winner
80     (find-command-winner command-candidates input-rest))
81
82   (match winner
83     ((cmd-action winner-id message-args)
84      (apply <- winner-id cmd-action message-args))
85     (#f
86      (<- (gameobj-gm player) 'write-home
87          #:text "Huh? (type \"help\" for common commands)\n"))))
88
89 (define* (player-tell player message #:key text)
90   (<- (gameobj-gm player) 'write-home
91       #:text text))
92
93 (define (player-disconnect-self-destruct player message)
94   "Action routine for being told to disconnect and self destruct."
95   (define loc (gameobj-loc player))
96   (when loc
97     (<- loc 'tell-room
98         #:exclude (actor-id player)
99         #:text (format #f "~a disappears in a puff of entropy!\n"
100                        (slot-ref player 'name))))
101   (gameobj-self-destruct player))
102
103 (define (player-cmd-inventory player message)
104   "Display the inventory for the player"
105   (define inv-names
106     (map
107      (lambda (inv-item)
108        (mbody-val (<-wait inv-item 'get-name)))
109      (gameobj-occupants player)))
110   (define text-to-show
111     (if (eq? inv-names '())
112         "You aren't carrying anything.\n"
113         (apply string-append
114                "You are carrying:\n"
115                (map (lambda (item-name)
116                       (string-append "  * " item-name "\n"))
117                     inv-names))))
118   (<- (actor-id player) 'tell #:text text-to-show))
119
120 (define (player-cmd-help player message)
121   (<- (actor-id player) 'tell
122       #:text '((strong "** Mudsync Help **")(br)
123                (p "You're playing Mudsync, a multiplayer text-adventure. "
124                   "Type different commands to interact with your surroundings "
125                   "and other players.")
126                (p "Some common commands:"
127                   (ul (li (strong "say <message>") " -- "
128                           "Chat with other players in the same room. "
129                           "(Also aliased to the " (b "\"") " character.)")
130                       (li (strong "look") " -- "
131                           "Look around the room you're in.")
132                       (li (strong "look [at] <object>") " -- "
133                           "Examine a particular object.")
134                       (li (strong "go <exit>") " -- "
135                           "Move to another room in <exit> direction.")))
136                (p "Different objects can be interacted with in different ways. "
137                   "For example, if there's a bell in the same room as you, "
138                   "you might try typing " (em "ring bell")
139                   " and see what happens."))))
140
141
142 ;;; Command handling
143 ;;; ================
144
145 ;; @@: Hard to know whether this should be in player.scm or here...
146 ;; @@: This could be more efficient as a stream...!?
147 (define (player-gather-command-handlers player verb)
148   (define player-loc
149     (let ((result (gameobj-loc player)))
150       (if result
151           result
152           (throw 'player-has-no-location
153                  "Player ~a has no location!  How'd that happen?\n"
154                  #:player-id (actor-id player)))))
155
156   ;; Ask the room for its commands
157   (define room-commands
158     ;; TODO: Map room id and sort
159     (mbody-receive (_ #:key commands)
160         (<-wait player-loc 'get-container-commands
161                 #:verb verb)
162       commands))
163
164   ;; All the co-occupants of the room (not including ourself)
165   (define co-occupants
166     (remove
167      (lambda (x) (equal? x (actor-id player)))
168      (mbody-receive (_ #:key occupants)
169          (<-wait player-loc 'get-occupants)
170        occupants)))
171
172   ;; @@: There's a race condition here if someone leaves the room
173   ;;   during this, heh...
174   ;;   I'm not sure it can be solved, but "lag" on the race can be
175   ;;   reduced maybe?
176
177   ;; Get all the co-occupants' commands
178   (define co-occupant-commands
179     (fold
180      (lambda (co-occupant prev)
181        (mbody-receive (_ #:key commands goes-by)
182            (<-wait co-occupant 'get-commands
183                    #:verb verb)
184          (append
185           (map (lambda (command)
186                  (list command goes-by co-occupant))
187                commands)
188           prev)))
189      '()
190      co-occupants))
191
192   ;; Append our own command handlers
193   (define our-commands
194     (filter
195      (lambda (cmd)
196        (equal? (command-verbs cmd) verb))
197      (val-or-run
198       (slot-ref player 'self-commands))))
199
200   ;; Append our inventory's relevant command handlers
201   (define inv-items
202     (gameobj-occupants player))
203   (define inv-item-commands
204     (fold
205      (lambda (inv-item prev)
206        (mbody-receive (_ #:key commands goes-by)
207            (<-wait inv-item 'get-contained-commands
208                    #:verb verb)
209          (append
210           (map (lambda (command)
211                  (list command goes-by inv-item))
212                commands)
213           prev)))
214      '()
215      inv-items))
216
217   ;; Now return a big ol sorted list of ((actor-id . command))
218   (append
219    (sort-commands-append-actor room-commands
220                                player-loc '()) ; room doesn't go by anything
221    (sort-commands-multi-actors co-occupant-commands)
222    (sort-commands-append-actor our-commands
223                                (actor-id player) '()) ; nor does player
224    (sort-commands-multi-actors inv-item-commands)))
225
226 (define (sort-commands-append-actor commands actor-id goes-by)
227   (sort-commands-multi-actors
228    (map (lambda (command) (list command goes-by actor-id)) commands)))
229
230 (define (sort-commands-multi-actors actors-and-commands)
231   (sort
232    actors-and-commands
233    (lambda (x y)
234      (> (command-priority (car x))
235         (command-priority (car y))))))
236
237
238 (define (find-command-winner sorted-candidates line)
239   "Find a command winner from a sorted list of candidates"
240   ;; A cache of results from matchers we've already seen
241   ;; TODO: fill in this cache.  This is a *critical* optimization!
242   (define matcher-cache '())
243   (call/ec
244    (lambda (return)
245      (for-each
246       (match-lambda
247         ((command actor-goes-by actor-id)
248          (let* ((matcher (command-matcher command))
249                 (matched (matcher line)))
250            (if (and matched
251                     ;; Great, it matched, but does it also pass
252                     ;; should-handle?
253                     (apply (command-should-handle command)
254                            actor-goes-by
255                            matched))  ; matched is kwargs if truthy
256                (return (list (command-action command)
257                              actor-id matched))
258                #f))))
259       sorted-candidates)
260      #f)))