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