X-Git-Url: https://jxself.org/git/?p=mudsync.git;a=blobdiff_plain;f=mudsync%2Fgame-master.scm;h=8bc239bdb9bebedf6eebbe047879f8e495b218ab;hp=595a9032ee391b9dc1b29dc6e4b361750490ca92;hb=50cd2aba8f13ec7aecb58a683aa55ae665cf83ab;hpb=8c357c87019a70aabdfadb4c71e3b063251cff87 diff --git a/mudsync/game-master.scm b/mudsync/game-master.scm index 595a903..8bc239b 100644 --- a/mudsync/game-master.scm +++ b/mudsync/game-master.scm @@ -22,6 +22,7 @@ #:use-module (8sync agenda) #:use-module (oop goops) #:use-module (ice-9 match) + #:use-module (srfi srfi-26) #:export ( make-default-room-conn-handler)) @@ -33,10 +34,6 @@ (special-dir #:init-thunk make-hash-table #:getter gm-special-dir) - ;; Room directory. Room symbols to locations. - (room-dir #:init-thunk make-hash-table - #:getter gm-room-dir) - ;; A mapping of client ids to in-game actors ;; and a reverse ;p (client-dir #:init-thunk make-hash-table @@ -45,11 +42,11 @@ #:getter gm-reverse-client-dir) ;; Network manager - (network-manager #:accessor gm-network-manager + (network-manager #:getter gm-network-manager #:init-value #f) ;; How we get a new connection acclimated to the system - (new-conn-handler #:accessor gm-new-conn-handler + (new-conn-handler #:getter gm-new-conn-handler #:init-keyword #:new-conn-handler) (message-handler @@ -57,9 +54,11 @@ (make-action-dispatch (init-world (wrap-apply gm-init-world)) (client-input (wrap-apply gm-handle-client-input)) - (lookup-room (wrap-apply gm-lookup-room)) + (lookup-special (wrap-apply gm-lookup-special)) (new-client (wrap-apply gm-new-client)) - (write-home (wrap-apply gm-write-home))))) + (write-home (wrap-apply gm-write-home)) + (client-closed (wrap-apply gm-client-closed)) + (inject-special! (wrap-apply gm-inject-special!))))) ;;; .. begin world init stuff .. @@ -69,7 +68,7 @@ ;; TODO ;; Init basic rooms / structure - (gm-init-rooms gm (message-ref message 'room-spec)) + (gm-init-game-spec gm (message-ref message 'game-spec)) ;; Restore database-based actors ;; TODO @@ -77,49 +76,51 @@ ;; Set up the network (gm-setup-network gm)) -(define (gm-init-rooms gm rooms-spec) - "Initialize the prebuilt rooms" - ;; @@: Would it be nicer to just allow passing in - ;; #:exits to the room spec itself? - (define (exit-from-spec exit-spec) - "Take room exits syntax from the spec, turn it into exits" - (match exit-spec - ((name to-symbol desc) - (make (@@ (mudsync room) ) - #:name name - #:to-symbol to-symbol - #:desc desc)))) - - (define rooms + +;; @@: If you change this code, update gm-inject-special! if appropriate. +(define (gm-init-game-spec gm game-spec) + "Initialize the prebuilt special objects" + (define set-locs '()) + (define specials (map (match-lambda - ((room-symbol room-class - room-args ... - (room-exits ...)) - ;; initialize the room - (let ((room - (apply create-actor* gm room-class "room" + ((symbol class loc args ...) + ;; initialize the special object + (let ((special-obj + (apply create-actor* gm class + ;; set cookie to be the object's symbol + (symbol->string symbol) #:gm (actor-id gm) - #:exits (map exit-from-spec (pk 'dem-exits room-exits)) - room-args))) - ;; register the room - (hash-set! (gm-room-dir gm) room-symbol room) + args))) + ;; register the object + (hash-set! (gm-special-dir gm) symbol special-obj) + ;; Give ourselves an instruction to set the location + (set! set-locs (cons (cons special-obj loc) set-locs)) ;; pass it back to the map - room))) - rooms-spec)) + special-obj))) + game-spec)) - ;; now wire up all the exits + ;; Set all initial locations (for-each - (lambda (room) - (format #t "Wiring up ~s...\n" (address->string room)) - (<-wait gm room 'wire-exits!)) - rooms)) + (match-lambda + ((special-obj . loc) + (if loc + (<-wait gm special-obj 'set-loc! + #:loc (hash-ref (gm-special-dir gm) loc))))) + set-locs) + + ;; now init all the objects + (for-each + (lambda (special-obj) + (format #t "Initializing ~s...\n" (address->string special-obj)) + (<-wait gm special-obj 'init)) + specials)) (define (gm-setup-network gm) ;; Create a default network manager if none available - (set! (gm-network-manager gm) - (create-actor* gm "netman" + (slot-set! gm 'network-manager + (create-actor* gm "netman" #:send-input-to (actor-id gm))) ;; TODO: Add host and port options @@ -148,10 +149,9 @@ (<- actor player 'handle-input #:input input)) -(define-mhandler (gm-lookup-room actor message symbol) - (define room-id - (slot-ref (gm-room-dir actor) symbol)) - (<-reply actor message room-id)) +(define-mhandler (gm-lookup-special actor message symbol) + (<-reply actor message + #:val (hash-ref (slot-ref actor 'special-dir) symbol))) (define-mhandler (gm-write-home actor message text) (define client-id (hash-ref (gm-reverse-client-dir actor) @@ -160,6 +160,52 @@ #:client client-id #:data text)) +(define-mhandler (gm-client-closed gm message client) + ;; Do we have this client registered to an actor? Get the id if so. + (define actor-id (hash-ref (gm-client-dir gm) client)) + + ;; Have the actor appropriately disappear / be removed from its + ;; room, if we have one. + ;; (In some games, if the user never connected) + (when actor-id + (<-wait gm actor-id 'disconnect-self-destruct) + ;; Unregister from the client directories. + (gm-unregister-client! gm client))) + + +(define-mhandler (gm-inject-special! gm message + special-symbol gameobj-spec) + "Inject, possiibly replacing the original, special symbol +using the gameobj-spec." + (define existing-obj + (hash-ref (slot-ref gm 'special-dir) special-symbol)) + + ;; There's a lot of overlap here with gm-init-game-spec. + ;; We could try to union them? It seemed hard last time I looked, + ;; because things need to run in a different order. + (match gameobj-spec + (((? (cut eq? <> special-symbol) symbol) class loc args ...) + ;; initialize the special object + (let ((special-obj + (apply create-actor* gm class + ;; set cookie to be the object's symbol + (symbol->string symbol) + #:gm (actor-id gm) + args))) + ;; Set the location + (<-wait gm special-obj 'set-loc! + #:loc (hash-ref (gm-special-dir gm) loc)) + ;; Initialize the object, and depending on if an object + ;; already exists with this info, ask it to coordinate + ;; replacing with the existing object. + (if existing-obj + (<-wait gm special-obj 'init #:replace existing-obj) + (<-wait gm special-obj 'init)) + ;; Register the object + (hash-set! (gm-special-dir gm) symbol special-obj) + ;; Destroy the original, if it exists. + (if existing-obj + (<- gm existing-obj 'self-destruct #:why 'replaced)))))) ;;; GM utilities @@ -167,14 +213,15 @@ (hash-set! (gm-client-dir gm) client-id player) (hash-set! (gm-reverse-client-dir gm) player client-id)) -(define (gm-unregister-client! gm client-id) +(define* (gm-unregister-client! gm client-id #:optional destroy-player) "Remove a connection/player combo and ask them to self destruct" (match (hash-remove! (gm-client-dir gm) client-id) ; Remove from our client dir ((_ . player-id) ;; Remove from reverse table too (hash-remove! (gm-reverse-client-dir gm) client-id) ;; Destroy player - (<- gm player-id 'destroy-self)) + (if destroy-player + (<- gm player-id 'self-destruct))) (#f (throw 'no-client-to-unregister "Can't unregister a client that doesn't exist?" client-id)))) @@ -190,11 +237,11 @@ with an anonymous persona" (let* ((guest-name (string-append "Guest-" (number->string count))) (room-id - (hash-ref (gm-room-dir gm) default-room)) + (hash-ref (gm-special-dir gm) default-room)) ;; create and register the player (player (create-actor* gm (@@ (mudsync player) ) "player" - #:username guest-name + #:name guest-name #:gm (actor-id gm) #:client client-id))) ;; Register the player in our database of players -> connections @@ -202,5 +249,8 @@ with an anonymous persona" ;; Dump the player into the default room (<-wait gm player 'set-loc! #:loc room-id) ;; Initialize the player - (<- gm player 'init))))) - + (<-wait gm player 'init) + (<- gm room-id 'tell-room + #:text (format #f "You see ~a materialize out of thin air!\n" + guest-name) + #:exclude player)))))