Most of the rest of support for live hacking!
[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   #:use-module (srfi srfi-26)
26   #:export (<game-master>
27             make-default-room-conn-handler))
28
29 ;;; The game master!  Runs the world.
30 ;;; =================================
31
32 (define-class <game-master> (<actor>)
33   ;; Directory of "special" objects.
34   (special-dir #:init-thunk make-hash-table
35                #:getter gm-special-dir)
36
37   ;; A mapping of client ids to in-game actors
38   ;; and a reverse ;p
39   (client-dir #:init-thunk make-hash-table
40               #:getter gm-client-dir)
41   (reverse-client-dir #:init-thunk make-hash-table
42                       #:getter gm-reverse-client-dir)
43
44   ;; Network manager
45   (network-manager #:getter gm-network-manager
46                    #:init-value #f)
47
48   ;; How we get a new connection acclimated to the system
49   (new-conn-handler #:getter gm-new-conn-handler
50                     #:init-keyword #:new-conn-handler)
51
52   (message-handler
53    #:init-value
54    (make-action-dispatch
55     (init-world (wrap-apply gm-init-world))
56     (client-input (wrap-apply gm-handle-client-input))
57     (lookup-special (wrap-apply gm-lookup-special))
58     (new-client (wrap-apply gm-new-client))
59     (write-home (wrap-apply gm-write-home))
60     (client-closed (wrap-apply gm-client-closed))
61     (inject-special! (wrap-apply gm-inject-special!)))))
62
63
64 ;;; .. begin world init stuff ..
65
66 (define (gm-init-world gm message)
67   ;; Load database
68   ;;  TODO
69
70   ;; Init basic rooms / structure
71   (gm-init-game-spec gm (message-ref message 'game-spec))
72
73   ;; Restore database-based actors
74   ;;  TODO
75
76   ;; Set up the network
77   (gm-setup-network gm))
78
79
80 ;; @@: If you change this code, update gm-inject-special! if appropriate.
81 (define (gm-init-game-spec gm game-spec)
82   "Initialize the prebuilt special objects"
83   (define set-locs '())
84   (define specials
85     (map
86      (match-lambda
87        ((symbol class loc args ...)
88         ;; initialize the special object
89         (let ((special-obj
90                (apply create-actor* gm class
91                       ;; set cookie to be the object's symbol
92                       (symbol->string symbol)
93                       #:gm (actor-id gm)
94                       args)))
95           ;; register the object
96           (hash-set! (gm-special-dir gm) symbol special-obj)
97           ;; Give ourselves an instruction to set the location
98           (set! set-locs (cons (cons special-obj loc) set-locs))
99           ;; pass it back to the map
100           special-obj)))
101      game-spec))
102
103   ;; Set all initial locations
104   (for-each
105    (match-lambda
106      ((special-obj . loc)
107       (if loc
108           (<-wait gm special-obj 'set-loc!
109                   #:loc (hash-ref (gm-special-dir gm) loc)))))
110    set-locs)
111
112   ;; now init all the objects
113   (for-each
114    (lambda (special-obj)
115      (format #t "Initializing ~s...\n" (address->string special-obj))
116      (<-wait gm special-obj 'init))
117    specials))
118
119
120 (define (gm-setup-network gm)
121   ;; Create a default network manager if none available
122   (slot-set! gm 'network-manager
123              (create-actor* gm <network-manager> "netman"
124                        #:send-input-to (actor-id gm)))
125
126   ;; TODO: Add host and port options
127   (<-wait gm (gm-network-manager gm) 'start-listening))
128
129 (define (gm-setup-database gm)
130   'TODO)
131
132 ;;; .. end world init stuff ...
133
134 (define-mhandler (gm-new-client actor message client)
135   ;; @@: Maybe more indirection than needed for this
136   ((gm-new-conn-handler actor) actor client))
137
138
139 (define (gm-handle-client-input actor message)
140   "Handle input from a client."
141   (define client-id (message-ref message 'client))
142   (define input (message-ref message 'data))
143   ;; Look up player
144   (define player (hash-ref (gm-client-dir actor) client-id))
145
146   ;; debugging
147   (format #t "DEBUG: From ~s: ~s\n" client-id input)
148
149   (<- actor player 'handle-input
150       #:input input))
151
152 (define-mhandler (gm-lookup-special actor message symbol)
153   (<-reply actor message
154            #:room-id (hash-ref (slot-ref actor 'special-dir) symbol)))
155
156 (define-mhandler (gm-write-home actor message text)
157   (define client-id (hash-ref (gm-reverse-client-dir actor)
158                               (message-from message)))
159   (<- actor (gm-network-manager actor) 'send-to-client
160       #:client client-id
161       #:data text))
162
163 (define-mhandler (gm-client-closed gm message client)
164   ;; Do we have this client registered to an actor?  Get the id if so.
165   (define actor-id (hash-ref (gm-client-dir gm) client))
166
167   ;; Have the actor appropriately disappear / be removed from its
168   ;; room, if we have one.
169   ;; (In some games, if the user never connected)
170   (when actor-id
171     (<-wait gm actor-id 'disconnect-self-destruct)
172     ;; Unregister from the client directories.
173     (gm-unregister-client! gm client)))
174
175
176 (define-mhandler (gm-inject-special! gm message
177                                      special-symbol gameobj-spec)
178   "Inject, possiibly replacing the original, special symbol
179 using the gameobj-spec."
180   (define existing-obj
181     (hash-ref (slot-ref gm 'special-dir) special-symbol))
182
183   ;; There's a lot of overlap here with gm-init-game-spec.
184   ;; We could try to union them?  It seemed hard last time I looked,
185   ;; because things need to run in a different order.
186   (match gameobj-spec
187     (((? (cut eq? <> special-symbol) symbol) class loc args ...)
188      ;; initialize the special object
189      (let ((special-obj
190             (apply create-actor* gm class
191                    ;; set cookie to be the object's symbol
192                    (symbol->string symbol)
193                    #:gm (actor-id gm)
194                    args)))
195        ;; Set the location
196        (<-wait gm special-obj 'set-loc!
197                   #:loc (hash-ref (gm-special-dir gm) loc))
198        ;; Initialize the object, and depending on if an object
199        ;; already exists with this info, ask it to coordinate
200        ;; replacing with the existing object.
201        (if existing-obj
202            (<-wait gm special-obj 'init #:replace existing-obj)
203            (<-wait gm special-obj 'init))
204        ;; Register the object
205        (hash-set! (gm-special-dir gm) symbol special-obj)
206        ;; Destroy the original, if it exists.
207        (if existing-obj
208            (<- gm existing-obj 'self-destruct #:why 'replaced))))))
209
210 ;;; GM utilities
211
212 (define (gm-register-client! gm client-id player)
213   (hash-set! (gm-client-dir gm) client-id player)
214   (hash-set! (gm-reverse-client-dir gm) player client-id))
215
216 (define* (gm-unregister-client! gm client-id #:optional destroy-player)
217   "Remove a connection/player combo and ask them to self destruct"
218   (match (hash-remove! (gm-client-dir gm) client-id)  ; Remove from our client dir
219     ((_ . player-id)
220      ;; Remove from reverse table too
221      (hash-remove! (gm-reverse-client-dir gm) client-id)
222      ;; Destroy player 
223      (if destroy-player
224          (<- gm player-id 'self-destruct)))
225     (#f (throw 'no-client-to-unregister
226                "Can't unregister a client that doesn't exist?"
227                client-id))))
228
229 ;;; An easy default
230
231 (define (make-default-room-conn-handler default-room)
232   "Make a handler for a GM that dumps people in a default room
233 with an anonymous persona"
234   (let ((count 0))
235     (lambda (gm client-id)
236       (set! count (+ count 1))
237       (let* ((guest-name (string-append "Guest-"
238                                         (number->string count)))
239              (room-id
240               (hash-ref (gm-special-dir gm) default-room))
241              ;; create and register the player
242              (player
243               (create-actor* gm (@@ (mudsync player) <player>) "player"
244                              #:name guest-name
245                              #:gm (actor-id gm)
246                              #:client client-id)))
247         ;; Register the player in our database of players -> connections
248         (gm-register-client! gm client-id player)
249         ;; Dump the player into the default room
250         (<-wait gm player 'set-loc! #:loc room-id)
251         ;; Initialize the player
252         (<-wait gm player 'init)
253         (<- gm room-id 'tell-room
254             #:text (format #f "You see ~a materialize out of thin air!\n"
255                            guest-name)
256             #:exclude player)))))
257