cb844d1e65ab01a983ba81da808bd194c2e08993
[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 (ice-9 match)
28   #:use-module (oop goops)
29   #:export (<gameobj>
30
31             gameobj-loc
32             gameobj-gm
33
34             gameobj-occupants
35             gameobj-actions
36             gameobj-self-destruct))
37
38 ;;; Gameobj
39 ;;; =======
40
41
42 ;;; Actions supported by all gameobj
43 (define gameobj-actions
44   (build-actions
45    (init (wrap-apply gameobj-init))
46    (get-commands (wrap-apply gameobj-get-commands))
47    (get-container-commands (wrap-apply gameobj-get-container-commands))
48    (get-occupants (wrap-apply gameobj-get-occupants))
49    (add-occupant! (wrap-apply gameobj-add-occupant!))
50    (remove-occupant! (wrap-apply gameobj-remove-occupant!))
51    (set-loc! (wrap-apply gameobj-act-set-loc!))
52    (get-name (wrap-apply gameobj-get-name))
53    (set-name! (wrap-apply gameobj-act-set-name!))
54    (get-desc (wrap-apply gameobj-get-desc))
55    (goes-by (wrap-apply gameobj-act-goes-by))
56    (visible-name (wrap-apply gameobj-visible-name))
57    (self-destruct (wrap-apply gameobj-act-self-destruct))
58    (tell (wrap-apply gameobj-tell-no-op))
59    (assist-replace (wrap-apply gameobj-act-assist-replace))))
60
61 ;;; *all* game components that talk to players should somehow
62 ;;; derive from this class.
63 ;;; And all of them need a GM!
64
65 (define-class <gameobj> (<actor>)
66   ;; location id
67   (loc #:init-value #f
68        #:getter gameobj-loc)
69   
70   ;; Uses a hash table like a set (values ignored)
71   (occupants #:init-thunk make-hash-table)
72
73   ;; game master id
74   (gm #:init-keyword #:gm
75       #:getter gameobj-gm)
76   ;; a name to be known by
77   (name #:init-keyword #:name
78         #:init-value #f)
79   (goes-by #:init-keyword #:goes-by
80            #:init-value #f)
81
82   (desc #:init-value #f
83         #:init-keyword #:desc)
84
85   ;; Commands we can handle
86   (commands #:init-value '())
87
88   ;; Commands we can handle by being something's container
89   (container-commands #:init-value '())
90   (message-handler
91    #:init-value
92    (simple-dispatcher gameobj-actions))
93
94   ;; Most objects are generally visible by default
95   (generally-visible #:init-value #t
96                      #:init-keyword #:generally-visible)
97   ;; @@: Would be preferable to be using generic methods for this...
98   ;;   Hopefully we can port this to Guile 2.2 soon...
99   (visible-to-player?
100    #:init-value (wrap-apply gameobj-visible-to-player?)))
101
102
103 ;;; gameobj message handlers
104 ;;; ========================
105
106 ;; Kind of a useful utility, maybe?
107 (define (simple-slot-getter slot)
108   (lambda (actor message)
109     (reply-message actor message
110                    #:val (slot-ref actor slot))))
111
112
113 (define (gameobj-replace-step-occupants actor replace-reply)
114   (define occupants
115     (message-ref replace-reply 'occupants #f))
116   ;; Snarf all the occupants!
117   (when occupants
118     (for-each
119      (lambda (occupant)
120        (<-wait actor occupant 'set-loc!
121                #:loc (actor-id actor)))
122      occupants)))
123
124 (define gameobj-replace-steps*
125   (list gameobj-replace-step-occupants))
126
127 (define (run-replacement actor message replace-steps)
128   (define replaces (message-ref message 'replaces #f))
129   (when replaces
130     (let ((replace-reply
131            (<-wait actor replaces 'assist-replace)))
132       (for-each
133        (lambda (replace-step)
134          (replace-step actor replace-reply))
135        replace-steps))))
136
137
138 ;; @@: This could be kind of a messy way of doing gameobj-init
139 ;;   stuff.  If only we had generic methods :(
140 (define-mhandler (gameobj-init actor message)
141   "Your most basic game object init procedure.
142 Assists in its replacement of occupants if necessary and nothing else."
143   (run-replacement actor message gameobj-replace-steps*))
144
145 (define (gameobj-goes-by gameobj)
146   "Find the name we go by.  Defaults to #:name if nothing else provided."
147   (cond ((slot-ref gameobj 'goes-by) =>
148          identity)
149         ((slot-ref gameobj 'name) =>
150          (lambda (name)
151            (list name)))
152         (else '())))
153
154 (define (gameobj-act-goes-by actor message)
155   "Reply to a message requesting what we go by."
156   (<-reply actor message
157            #:goes-by (gameobj-goes-by actor)))
158
159 (define (val-or-run val-or-proc)
160   "Evaluate if a procedure, or just return otherwise"
161   (if (procedure? val-or-proc)
162       (val-or-proc)
163       val-or-proc))
164
165 (define (filter-commands commands verb)
166   (filter
167    (lambda (cmd)
168      (equal? (command-verbs cmd)
169              verb))
170    commands))
171
172 (define-mhandler (gameobj-get-commands actor message verb)
173   "Get commands a co-occupant of the room might execute for VERB"
174   (define filtered-commands
175     (filter-commands (val-or-run (slot-ref actor 'commands))
176                      verb))
177   (<-reply actor message
178            #:commands filtered-commands
179            #:goes-by (gameobj-goes-by actor)))
180
181 (define-mhandler (gameobj-get-container-commands actor message verb)
182   "Get commands as the container / room of message's sender"
183   (define filtered-commands
184     (filter-commands (val-or-run (slot-ref actor 'container-commands))
185                      verb))
186   (<-reply actor message #:commands filtered-commands))
187
188 (define-mhandler (gameobj-add-occupant! actor message who)
189   "Add an actor to our list of present occupants"
190   (hash-set! (slot-ref actor 'occupants)
191              who #t))
192
193 (define-mhandler (gameobj-remove-occupant! actor message who)
194   "Remove an occupant from the room."
195   (hash-remove! (slot-ref actor 'occupants) who))
196
197 (define* (gameobj-occupants gameobj #:key exclude)
198   (hash-fold
199    (lambda (occupant _ prev)
200      (define exclude-it?
201        (match exclude
202          ;; Empty list and #f are non-exclusion
203          (() #f)
204          (#f #f)
205          ;; A list of addresses... since our address object is (annoyingly)
206          ;; currently a simple cons cell...
207          ((exclude-1 ... exclude-rest)
208           (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
209          ;; Must be an individual address!
210          (_ (equal? occupant exclude))))
211      (if exclude-it?
212          prev
213          (cons occupant prev)))
214    '()
215    (slot-ref gameobj 'occupants)))
216
217 (define-mhandler (gameobj-get-occupants actor message)
218   "Get all present occupants of the room."
219   (define exclude (message-ref message 'exclude #f))
220   (define occupants
221     (gameobj-occupants actor #:exclude exclude))
222
223   (<-reply actor message
224            #:occupants occupants))
225
226 (define (gameobj-set-loc! gameobj loc)
227   "Set the location of this object."
228   (define old-loc (gameobj-loc gameobj))
229   (format #t "DEBUG: Location set to ~s for ~s\n"
230           loc (actor-id-actor gameobj))
231
232   (slot-set! gameobj 'loc loc)
233   ;; Change registation of where we currently are
234   (if loc
235       (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
236   (if old-loc
237       (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
238
239 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
240 (define-mhandler (gameobj-act-set-loc! actor message loc)
241   "Action routine to set the location."
242   (gameobj-set-loc! actor loc))
243
244 (define gameobj-get-name (simple-slot-getter 'name))
245
246 (define-mhandler (gameobj-act-set-name! actor message val)
247   (slot-set! actor 'name val))
248
249 (define-mhandler (gameobj-get-desc actor message whos-looking)
250   (define desc-text
251     (match (slot-ref actor 'desc)
252       ((? procedure? desc-proc)
253        (desc-proc actor whos-looking))
254       (desc desc)))
255   (<-reply actor message #:val desc-text))
256
257 (define (gameobj-visible-to-player? gameobj whos-looking)
258   "Check to see whether we're visible to the player or not.
259 By default, this is whether or not the generally-visible flag is set."
260   (slot-ref gameobj 'generally-visible))
261
262 (define-mhandler (gameobj-visible-name actor message whos-looking)
263   ;; Are we visible?
264   (define we-are-visible
265     ((slot-ref actor 'visible-to-player?) actor whos-looking))
266
267   (define name-to-return
268     (if we-are-visible
269         ;; Return our name
270         (match (slot-ref actor 'name)
271           ((? procedure? name-proc)
272            (name-proc actor whos-looking))
273           ((? string? name)
274            name)
275           (#f #f))
276         #f))
277   (<-reply actor message #:text name-to-return))
278
279 (define (gameobj-self-destruct gameobj)
280   "General gameobj self destruction routine"
281   ;; Unregister from being in any particular room
282   (gameobj-set-loc! gameobj #f)
283   ;; Boom!
284   (self-destruct gameobj))
285
286 (define-mhandler (gameobj-act-self-destruct gameobj message)
287   "Action routine for self destruction"
288   (gameobj-self-destruct gameobj))
289
290 ;; Unless an actor has a tell message, we just ignore it
291 (define gameobj-tell-no-op
292   (const 'no-op))
293
294 (define (gameobj-replace-data-occupants actor)
295   "The general purpose list of replacement data"
296   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
297                                     (slot-ref actor 'occupants))))
298
299 (define (gameobj-replace-data* actor)
300   ;; For now, just call gameobj-replace-data-occupants.
301   ;; But there may be more in the future!
302   (gameobj-replace-data-occupants actor))
303
304 ;; So sad that objects must assist in their replacement ;_;
305 ;; But that's life in a live hacked game!
306 (define (gameobj-act-assist-replace actor message)
307   "Vanilla method for assisting in self-replacement for live hacking"
308   (apply <-reply actor message
309          (gameobj-replace-data* actor)))