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