Full screen css structure
[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
46 (define-class <player> (<gameobj>)
47   (username #:init-keyword #:username
48             #:getter player-username)
49
50   (self-commands #:init-value player-self-commands)
51
52   (actions #:allocation #:each-subclass
53            #:init-value
54            (build-actions
55             (init player-init)
56             (handle-input player-handle-input)
57             (tell player-tell)
58             (disconnect-self-destruct player-disconnect-self-destruct)
59             (cmd-inventory player-cmd-inventory))))
60
61
62 ;;; player message handlers
63
64 (define (player-init player message)
65   ;; Look around the room we're in
66   (<- (gameobj-loc player) 'look-room))
67
68
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))
73
74   (define command-candidates
75     (player-gather-command-handlers player input-verb))
76
77   (define winner
78     (find-command-winner command-candidates input-rest))
79
80   (match winner
81     ((cmd-action winner-id message-args)
82      (apply <- winner-id cmd-action message-args))
83     (#f
84      (<- (gameobj-gm player) 'write-home
85          #:text "Huh?\n"))))
86
87 (define* (player-tell player message #:key text)
88   (<- (gameobj-gm player) 'write-home
89       #:text text))
90
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))
94   (when loc
95     (<- loc 'tell-room
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))
100
101 (define (player-cmd-inventory player message)
102   "Display the inventory for the player"
103   (define inv-names
104     (map
105      (lambda (inv-item)
106        (mbody-val (<-wait inv-item 'get-name)))
107      (gameobj-occupants player)))
108   (define text-to-show
109     (if (eq? inv-names '())
110         "You aren't carrying anything.\n"
111         (apply string-append
112                "You are carrying:\n"
113                (map (lambda (item-name)
114                       (string-append "  * " item-name "\n"))
115                     inv-names))))
116   (<- (actor-id player) 'tell #:text text-to-show))
117
118
119 ;;; Command handling
120 ;;; ================
121
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)
125   (define player-loc
126     (let ((result (gameobj-loc player)))
127       (if result
128           result
129           (throw 'player-has-no-location
130                  "Player ~a has no location!  How'd that happen?\n"
131                  #:player-id (actor-id player)))))
132
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
138                 #:verb verb)
139       commands))
140
141   ;; All the co-occupants of the room (not including ourself)
142   (define co-occupants
143     (remove
144      (lambda (x) (equal? x (actor-id player)))
145      (mbody-receive (_ #:key occupants)
146          (<-wait player-loc 'get-occupants)
147        occupants)))
148
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
152   ;;   reduced maybe?
153
154   ;; Get all the co-occupants' commands
155   (define co-occupant-commands
156     (fold
157      (lambda (co-occupant prev)
158        (mbody-receive (_ #:key commands goes-by)
159            (<-wait co-occupant 'get-commands
160                    #:verb verb)
161          (append
162           (map (lambda (command)
163                  (list command goes-by co-occupant))
164                commands)
165           prev)))
166      '()
167      co-occupants))
168
169   ;; Append our own command handlers
170   (define our-commands
171     (filter
172      (lambda (cmd)
173        (equal? (command-verbs cmd) verb))
174      (val-or-run
175       (slot-ref player 'self-commands))))
176
177   ;; Append our inventory's relevant command handlers
178   (define inv-items
179     (gameobj-occupants player))
180   (define inv-item-commands
181     (fold
182      (lambda (inv-item prev)
183        (mbody-receive (_ #:key commands goes-by)
184            (<-wait inv-item 'get-contained-commands
185                    #:verb verb)
186          (append
187           (map (lambda (command)
188                  (list command goes-by inv-item))
189                commands)
190           prev)))
191      '()
192      inv-items))
193
194   ;; Now return a big ol sorted list of ((actor-id . command))
195   (append
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)))
202
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)))
206
207 (define (sort-commands-multi-actors actors-and-commands)
208   (sort
209    actors-and-commands
210    (lambda (x y)
211      (> (command-priority (car x))
212         (command-priority (car y))))))
213
214
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 '())
220   (call/ec
221    (lambda (return)
222      (for-each
223       (match-lambda
224         ((command actor-goes-by actor-id)
225          (let* ((matcher (command-matcher command))
226                 (matched (matcher line)))
227            (if (and matched
228                     ;; Great, it matched, but does it also pass
229                     ;; should-handle?
230                     (apply (command-should-handle command)
231                            actor-goes-by
232                            matched))  ; matched is kwargs if truthy
233                (return (list (command-action command)
234                              actor-id matched))
235                #f))))
236       sorted-candidates)
237      #f)))