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