big refactor to players, rooms, gameobj stuff
[mudsync.git] / mudsync / gameobj.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 ;;; Game actor
20 ;;; ==========
21
22 (define-module (mudsync gameobj)
23   #:use-module (8sync systems actors)
24   #:use-module (8sync agenda)
25   #:use-module (oop goops)
26   #:export (<gameobj>
27             gameobj-simple-name-f
28
29             gameobj-loc
30             gameobj-gm
31             gameobj-name
32             gameobj-name-f
33
34             gameobj-actions))
35
36 ;;; Gameobj
37 ;;; =======
38
39
40 ;;; Actions supported by all gameobj
41 (define gameobj-actions
42   (build-actions
43    (get-commands (wrap-apply gameobj-get-commands))
44    (get-container-commands (wrap-apply gameobj-get-container-commands))
45    (get-children (wrap-apply gameobj-get-children))
46    (add-occupant! (wrap-apply gameobj-add-child!))
47    (remove-occupant! (wrap-apply gameobj-remove-child!))
48    (set-loc! (wrap-apply gameobj-set-loc!))))
49
50 ;;; *all* game components that talk to players should somehow
51 ;;; derive from this class.
52 ;;; And all of them need a GM!
53
54 (define-class <gameobj> (<actor>)
55   ;; location id
56   (loc #:init-value #f
57        #:accessor gameobj-loc)
58   
59   ;; Uses a hash table like a set (values ignored)
60   (occupants #:init-thunk make-hash-table
61              #:accessor gameobj-occupants)
62
63   ;; game master id
64   (gm #:init-keyword #:gm
65       #:getter gameobj-gm)
66   ;; a name to be known by
67   (name #:init-keyword #:name
68         #:accessor gameobj-name)
69
70   (desc #:init-value ""
71         #:init-keyword #:desc)
72
73   ;; how to print our name
74   (name-f #:init-keyword #:name-f
75           #:getter gameobj-name-f
76           #:init-value (wrap gameobj-simple-name-f))
77
78   ;; Name aliases
79   (aliases #:init-keyword #:aliases
80            #:init-value '())
81
82   ;; Commands we can handle
83   (commands #:init-value '())
84
85   ;; Commands we can handle by being something's container
86   (container-commands #:init-value '())
87   (message-handler
88    #:init-value
89    (simple-dispatcher gameobj-actions)))
90
91
92 ;;; gameobj message handlers
93 ;;; ========================
94
95 (define-mhandler (gameobj-get-commands actor message verb)
96   (<-reply actor message #:commands (slot-ref actor 'commands)))
97
98 (define-mhandler (gameobj-get-container-commands actor message verb)
99   (<-reply actor message #:commands (slot-ref actor 'container-commands)))
100
101 (define-mhandler (gameobj-get-children actor message)
102   (define children
103     (hash-map->list (lambda (key val) key)
104                     (gameobj-children actor)))
105
106   (<-reply actor message
107            #:children children))
108
109 (define-mhandler (gameobj-set-loc! player message id)
110   (format #t "DEBUG: Location set to ~s for player ~s\n"
111           id (actor-id-actor player))
112   (set! (gameobj-loc player) id))
113
114
115 (define (gameobj-simple-name-f gameobj)
116   "Simplest version: return ourselves for our name."
117   (gameobj-name gameobj))
118
119