added dynamic linking approach to exits. live hacking rooms works! :D :D
[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 (pk 'replace (message-ref message 'replace #f)))
132   (when replaces
133     (let ((replace-reply
134            (pk 'replace-reply (<-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   (display "gameobj init!\n")
146   (run-replacement actor message gameobj-replace-steps*))
147
148 (define (gameobj-goes-by gameobj)
149   "Find the name we go by.  Defaults to #:name if nothing else provided."
150   (cond ((slot-ref gameobj 'goes-by) =>
151          identity)
152         ((slot-ref gameobj 'name) =>
153          (lambda (name)
154            (list name)))
155         (else '())))
156
157 (define (gameobj-act-goes-by actor message)
158   "Reply to a message requesting what we go by."
159   (<-reply actor message
160            #:goes-by (gameobj-goes-by actor)))
161
162 (define (val-or-run val-or-proc)
163   "Evaluate if a procedure, or just return otherwise"
164   (if (procedure? val-or-proc)
165       (val-or-proc)
166       val-or-proc))
167
168 (define (filter-commands commands verb)
169   (filter
170    (lambda (cmd)
171      (equal? (command-verbs cmd)
172              verb))
173    commands))
174
175 (define-mhandler (gameobj-get-commands actor message verb)
176   "Get commands a co-occupant of the room might execute for VERB"
177   (define filtered-commands
178     (filter-commands (val-or-run (slot-ref actor 'commands))
179                      verb))
180   (<-reply actor message
181            #:commands filtered-commands
182            #:goes-by (gameobj-goes-by actor)))
183
184 (define-mhandler (gameobj-get-container-commands actor message verb)
185   "Get commands as the container / room of message's sender"
186   (define filtered-commands
187     (filter-commands (val-or-run (slot-ref actor 'container-commands))
188                      verb))
189   (<-reply actor message #:commands filtered-commands))
190
191 (define-mhandler (gameobj-add-occupant! actor message who)
192   "Add an actor to our list of present occupants"
193   (hash-set! (slot-ref actor 'occupants)
194              who #t))
195
196 (define-mhandler (gameobj-remove-occupant! actor message who)
197   "Remove an occupant from the room."
198   (hash-remove! (slot-ref actor 'occupants) who))
199
200 (define* (gameobj-occupants gameobj #:key exclude)
201   (hash-fold
202    (lambda (occupant _ prev)
203      (define exclude-it?
204        (match exclude
205          ;; Empty list and #f are non-exclusion
206          (() #f)
207          (#f #f)
208          ;; A list of addresses... since our address object is (annoyingly)
209          ;; currently a simple cons cell...
210          ((exclude-1 ... exclude-rest)
211           (pk 'failboat (member occupant (pk 'exclude-lst exclude))))
212          ;; Must be an individual address!
213          (_ (equal? occupant exclude))))
214      (if exclude-it?
215          prev
216          (cons occupant prev)))
217    '()
218    (slot-ref gameobj 'occupants)))
219
220 (define-mhandler (gameobj-get-occupants actor message)
221   "Get all present occupants of the room."
222   (define exclude (message-ref message 'exclude #f))
223   (define occupants
224     (gameobj-occupants actor #:exclude exclude))
225
226   (<-reply actor message
227            #:occupants occupants))
228
229 (define (gameobj-set-loc! gameobj loc)
230   "Set the location of this object."
231   (define old-loc (gameobj-loc gameobj))
232   (format #t "DEBUG: Location set to ~s for ~s\n"
233           loc (actor-id-actor gameobj))
234
235   (slot-set! gameobj 'loc loc)
236   ;; Change registation of where we currently are
237   (if loc
238       (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj)))
239   (if old-loc
240       (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj))))
241
242 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
243 (define-mhandler (gameobj-act-set-loc! actor message loc)
244   "Action routine to set the location."
245   (gameobj-set-loc! actor loc))
246
247 (define gameobj-get-name (simple-slot-getter 'name))
248
249 (define-mhandler (gameobj-act-set-name! actor message val)
250   (slot-set! actor 'name val))
251
252 (define-mhandler (gameobj-get-desc actor message whos-looking)
253   (define desc-text
254     (match (slot-ref actor 'desc)
255       ((? procedure? desc-proc)
256        (desc-proc actor whos-looking))
257       (desc desc)))
258   (<-reply actor message #:val desc-text))
259
260 (define (gameobj-visible-to-player? gameobj whos-looking)
261   "Check to see whether we're visible to the player or not.
262 By default, this is whether or not the generally-visible flag is set."
263   (slot-ref gameobj 'generally-visible))
264
265 (define-mhandler (gameobj-visible-name actor message whos-looking)
266   ;; Are we visible?
267   (define we-are-visible
268     ((slot-ref actor 'visible-to-player?) actor whos-looking))
269
270   (define name-to-return
271     (if we-are-visible
272         ;; Return our name
273         (match (slot-ref actor 'name)
274           ((? procedure? name-proc)
275            (name-proc actor whos-looking))
276           ((? string? name)
277            name)
278           (#f #f))
279         #f))
280   (<-reply actor message #:text name-to-return))
281
282 (define (gameobj-self-destruct gameobj)
283   "General gameobj self destruction routine"
284   ;; Unregister from being in any particular room
285   (gameobj-set-loc! gameobj #f)
286   ;; Boom!
287   (self-destruct gameobj))
288
289 (define-mhandler (gameobj-act-self-destruct gameobj message)
290   "Action routine for self destruction"
291   (gameobj-self-destruct gameobj))
292
293 ;; Unless an actor has a tell message, we just ignore it
294 (define gameobj-tell-no-op
295   (const 'no-op))
296
297 (define (gameobj-replace-data-occupants actor)
298   "The general purpose list of replacement data"
299   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
300                                     (slot-ref actor 'occupants))))
301
302 (define (gameobj-replace-data* actor)
303   ;; For now, just call gameobj-replace-data-occupants.
304   ;; But there may be more in the future!
305   (gameobj-replace-data-occupants actor))
306
307 ;; So sad that objects must assist in their replacement ;_;
308 ;; But that's life in a live hacked game!
309 (define (gameobj-act-assist-replace actor message)
310   "Vanilla method for assisting in self-replacement for live hacking"
311   (apply <-reply actor message
312          (gameobj-replace-data* actor)))
313
314 \f
315 ;;; Utilities every gameobj has
316 ;;; ---------------------------
317
318 (define (dyn-ref gameobj special-symbol)
319   "Dynamically look up a special object from the gm"
320   (match special-symbol
321     ;; if it's a symbol, look it up dynamically
322     ((? symbol? _)
323      (message-ref
324       (<-wait gameobj (slot-ref gameobj 'gm) 'lookup-special
325               #:symbol special-symbol)
326       'val))
327     ;; if it's false, return nothing
328     ((#f #f))
329     ;; otherwise it's probably an address, return it as-is
330     (_ special-symbol)))