We can finally move around!
[mudsync.git] / mudsync / game-master.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 game-master)
20   #:use-module (mudsync networking)
21   #:use-module (8sync systems actors)
22   #:use-module (8sync agenda)
23   #:use-module (oop goops)
24   #:use-module (ice-9 match)
25   #:export (<game-master>
26             make-default-room-conn-handler))
27
28 ;;; The game master!  Runs the world.
29 ;;; =================================
30
31 (define-class <game-master> (<actor>)
32   ;; Directory of "special" objects.
33   (special-dir #:init-thunk make-hash-table
34                #:getter gm-special-dir)
35
36   ;; Room directory.  Room symbols to locations.
37   (room-dir #:init-thunk make-hash-table
38             #:getter gm-room-dir)
39
40   ;; A mapping of client ids to in-game actors
41   ;; and a reverse ;p
42   (client-dir #:init-thunk make-hash-table
43               #:getter gm-client-dir)
44   (reverse-client-dir #:init-thunk make-hash-table
45                       #:getter gm-reverse-client-dir)
46
47   ;; Network manager
48   (network-manager #:accessor gm-network-manager
49                    #:init-value #f)
50
51   ;; How we get a new connection acclimated to the system
52   (new-conn-handler #:accessor gm-new-conn-handler
53                     #:init-keyword #:new-conn-handler)
54
55   (message-handler
56    #:init-value
57    (make-action-dispatch
58     (init-world (wrap-apply gm-init-world))
59     (client-input (wrap-apply gm-handle-client-input))
60     (lookup-room (wrap-apply gm-lookup-room))
61     (new-client (wrap-apply gm-new-client))
62     (write-home (wrap-apply gm-write-home)))))
63
64
65 ;;; .. begin world init stuff ..
66
67 (define (gm-init-world gm message)
68   ;; Load database
69   ;;  TODO
70
71   ;; Init basic rooms / structure
72   (gm-init-rooms gm (message-ref message 'room-spec))
73
74   ;; Restore database-based actors
75   ;;  TODO
76
77   ;; Set up the network
78   (gm-setup-network gm))
79
80 (define (gm-init-rooms gm rooms-spec)
81   "Initialize the prebuilt rooms"
82   (define rooms
83     (map
84      (match-lambda
85        ((room-symbol room-class
86                      room-args ...)
87         ;; initialize the room
88         (let ((room
89                (apply create-actor* gm room-class "room"
90                       #:gm (actor-id gm)
91                       room-args)))
92           ;; register the room
93           (hash-set! (gm-room-dir gm) room-symbol room)
94           ;; pass it back to the map
95           room)))
96      rooms-spec))
97
98   ;; now wire up all the exits
99   (for-each
100    (lambda (room)
101      (format #t "Wiring up ~s...\n" (address->string room))
102      (<-wait gm room 'wire-exits!))
103    rooms))
104
105
106 (define (gm-setup-network gm)
107   ;; Create a default network manager if none available
108   (set! (gm-network-manager gm)
109         (create-actor* gm <network-manager> "netman"
110                        #:send-input-to (actor-id gm)))
111
112   ;; TODO: Add host and port options
113   (<-wait gm (gm-network-manager gm) 'start-listening))
114
115 (define (gm-setup-database gm)
116   'TODO)
117
118 ;;; .. end world init stuff ...
119
120 (define-mhandler (gm-new-client actor message client)
121   ;; @@: Maybe more indirection than needed for this
122   ((gm-new-conn-handler actor) actor client))
123
124
125 (define (gm-handle-client-input actor message)
126   "Handle input from a client."
127   (define client-id (message-ref message 'client))
128   (define input (message-ref message 'data))
129   ;; Look up player
130   (define player (hash-ref (gm-client-dir actor) client-id))
131
132   ;; debugging
133   (format #t "DEBUG: From ~s: ~s\n" client-id input)
134
135   (<- actor player 'handle-input
136       #:input input))
137
138 (define-mhandler (gm-lookup-room actor message symbol)
139   (<-reply actor message
140            #:room-id (hash-ref (slot-ref actor 'room-dir) symbol)))
141
142 (define-mhandler (gm-write-home actor message text)
143   (define client-id (hash-ref (gm-reverse-client-dir actor)
144                               (message-from message)))
145   (<- actor (gm-network-manager actor) 'send-to-client
146       #:client client-id
147       #:data text))
148
149
150 ;;; GM utilities
151
152 (define (gm-register-client! gm client-id player)
153   (hash-set! (gm-client-dir gm) client-id player)
154   (hash-set! (gm-reverse-client-dir gm) player client-id))
155
156 (define (gm-unregister-client! gm client-id)
157   "Remove a connection/player combo and ask them to self destruct"
158   (match (hash-remove! (gm-client-dir gm) client-id)  ; Remove from our client dir
159     ((_ . player-id)
160      ;; Remove from reverse table too
161      (hash-remove! (gm-reverse-client-dir gm) client-id)
162      ;; Destroy player 
163      (<- gm player-id 'destroy-self))
164     (#f (throw 'no-client-to-unregister
165                "Can't unregister a client that doesn't exist?"
166                client-id))))
167
168 ;;; An easy default
169
170 (define (make-default-room-conn-handler default-room)
171   "Make a handler for a GM that dumps people in a default room
172 with an anonymous persona"
173   (display "right before breakage?\n")
174   (let ((count 0))
175     (lambda (gm client-id)
176       (set! count (+ count 1))
177       (let* ((guest-name (string-append "Guest-"
178                                         (number->string count)))
179              (room-id
180               (hash-ref (gm-room-dir gm) default-room))
181              ;; create and register the player
182              (player
183               (create-actor* gm (@@ (mudsync player) <player>) "player"
184                              #:username guest-name
185                              #:gm (actor-id gm)
186                              #:client client-id)))
187         (display "Are we broke yet?\n")
188         ;; Register the player in our database of players -> connections
189         (gm-register-client! gm client-id player)
190         ;; Dump the player into the default room
191         (<-wait gm player 'set-loc! #:loc room-id)
192         ;; Initialize the player
193         (<- gm player 'init)))))
194