We can finally move around!
[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 (mudsync command)
24   #:use-module (8sync systems actors)
25   #:use-module (8sync agenda)
26   #:use-module (srfi srfi-1)
27   #:use-module (oop goops)
28   #:export (<gameobj>
29             gameobj-simple-name-f
30
31             gameobj-loc
32             gameobj-gm
33             gameobj-name
34             gameobj-name-f
35
36             gameobj-actions))
37
38 ;;; Gameobj
39 ;;; =======
40
41
42 ;;; Actions supported by all gameobj
43 (define gameobj-actions
44   (build-actions
45    (get-commands (wrap-apply gameobj-get-commands))
46    (get-container-commands (wrap-apply gameobj-get-container-commands))
47    (get-occupants (wrap-apply gameobj-get-occupants))
48    (add-occupant! (wrap-apply gameobj-add-occupant!))
49    (remove-occupant! (wrap-apply gameobj-remove-occupant!))
50    (set-loc! (wrap-apply gameobj-set-loc!))
51    (get-name (wrap-apply gameobj-get-name))
52    (get-desc (wrap-apply gameobj-get-desc))
53    ))
54
55 ;;; *all* game components that talk to players should somehow
56 ;;; derive from this class.
57 ;;; And all of them need a GM!
58
59 (define-class <gameobj> (<actor>)
60   ;; location id
61   (loc #:init-value #f
62        #:accessor gameobj-loc)
63   
64   ;; Uses a hash table like a set (values ignored)
65   (occupants #:init-thunk make-hash-table
66              #:accessor gameobj-occupants)
67
68   ;; game master id
69   (gm #:init-keyword #:gm
70       #:getter gameobj-gm)
71   ;; a name to be known by
72   (name #:init-keyword #:name
73         #:accessor gameobj-name)
74
75   (desc #:init-value ""
76         #:init-keyword #:desc)
77
78   ;; how to print our name
79   (name-f #:init-keyword #:name-f
80           #:getter gameobj-name-f
81           #:init-value (wrap gameobj-simple-name-f))
82
83   ;; Name aliases
84   (aliases #:init-keyword #:aliases
85            #:init-value '())
86
87   ;; Commands we can handle
88   (commands #:init-value '())
89
90   ;; Commands we can handle by being something's container
91   (container-commands #:init-value '())
92   (message-handler
93    #:init-value
94    (simple-dispatcher gameobj-actions)))
95
96
97 ;;; gameobj message handlers
98 ;;; ========================
99
100 ;; Kind of a useful utility, maybe?
101 (define (simple-slot-getter slot)
102   (lambda (actor message)
103     (reply-message actor message
104                    #:val (slot-ref actor slot))))
105
106 (define (filter-commands commands verb)
107   (filter
108    (lambda (cmd)
109      (equal? (command-verbs cmd)
110              verb))
111    commands))
112
113 (define-mhandler (gameobj-get-commands actor message verb)
114   (define filtered-commands
115     (filter-commands (slot-ref actor 'commands)
116                      verb))
117   (<-reply actor message #:commands filtered-commands))
118
119 (define-mhandler (gameobj-get-container-commands actor message verb)
120   (define filtered-commands
121     (filter-commands (slot-ref actor 'container-commands)
122                      verb))
123   (<-reply actor message #:commands filtered-commands))
124
125 (define-mhandler (gameobj-add-occupant! actor message who)
126   (hash-set! (slot-ref actor 'occupants)
127              who #t))
128
129 (define-mhandler (gameobj-remove-occupant! actor message who)
130   (hash-remove! (slot-ref actor 'occupants) who))
131
132 (define-mhandler (gameobj-get-occupants actor message)
133   (define occupants
134     (hash-map->list (lambda (key val) key)
135                     (gameobj-occupants actor)))
136
137   (<-reply actor message
138            #:occupants occupants))
139
140 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
141 (define-mhandler (gameobj-set-loc! actor message loc)
142   (define old-loc (gameobj-loc actor))
143   (format #t "DEBUG: Location set to ~s for ~s\n"
144           loc (actor-id-actor actor))
145
146   (set! (gameobj-loc actor) loc)
147   ;; Change registation of where we currently are
148   (if loc
149       (<-wait actor loc 'add-occupant! #:who (actor-id actor)))
150   (if old-loc
151       (<-wait actor old-loc 'remove-occupant! #:who (actor-id actor))))
152
153 (define gameobj-get-name (simple-slot-getter 'name))
154 (define gameobj-get-desc (simple-slot-getter 'desc))
155
156 (define (gameobj-simple-name-f gameobj)
157   "Simplest version: return ourselves for our name."
158   (gameobj-name gameobj))
159
160