fece716dbe415d45f0073dedb8e6cfa70eeaa384
[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 format)
27   #:use-module (oop goops)
28   #:use-module (srfi srfi-1)
29   #:export (<player>
30             player-self-commands))
31
32 ;;; Players
33 ;;; =======
34
35 (define-class <player> (<gameobj>)
36   (username #:init-keyword #:username
37             #:accessor player-username)
38   ;; Connection id
39   (client #:accessor player-client)
40
41   (self-commands
42    #:init-value '()
43    #:accessor player-self-commands)
44
45   (message-handler
46    #:init-value
47    ;; @@: We're gonna need action inheritance real awful soon, huh?
48    (make-action-dispatch
49     (set-loc! (wrap-apply player-set-loc!))
50     (init (wrap-apply player-init!))
51     (handle-input (wrap-apply player-handle-input)))))
52
53 ;;; player message handlers
54
55 (define-mhandler (player-set-loc! player message id)
56   (format #t "DEBUG: Location set to ~s for player ~s\n"
57           id (actor-id-actor player))
58   (set! (gameobj-loc player) id))
59
60 (define-mhandler (player-init! player message)
61   (player-look-around player))
62
63
64 (define-mhandler (player-handle-input player message input)
65   (define split-input (split-verb-and-rest input))
66   (define input-verb (pk 'input-verb (car split-input)))
67   (define input-rest (pk 'input-rest (cdr split-input)))
68
69   (define command-candidates
70     (pk 'candidates
71         (player-gather-command-handlers player input-verb)))
72
73   (define winner
74     (pk 'winner (find-command-winner command-candidates input-rest)))
75
76   (<- player (gameobj-gm player) 'write-home
77       #:text
78       (format #f "<~a>: ~s\n"
79               (player-username player)
80               input)))
81
82
83 ;;; player methods
84
85 (define (player-look-around player)
86   (define room-name
87     (message-ref
88      (<-wait player (gameobj-loc player) 'get-name)
89      'val))
90   (define room-desc
91     (message-ref
92      (<-wait player (gameobj-loc player) 'get-desc)
93      'val))
94   (define message-text
95     (format #f "**~a**\n~a\n" room-name room-desc))
96
97   (<- player (gameobj-gm player) 'write-home #:text message-text))
98
99
100