Move containers over to their own module.
[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 (mudsync utils)
25   #:use-module (8sync actors)
26   #:use-module (8sync agenda)
27   #:use-module (8sync rmeta-slot)
28   #:use-module (srfi srfi-1)
29   #:use-module (ice-9 control)
30   #:use-module (ice-9 format)
31   #:use-module (ice-9 match)
32   #:use-module (oop goops)
33   #:export (<gameobj>
34
35             gameobj-loc
36             gameobj-gm
37
38             gameobj-act-init
39             gameobj-set-loc!
40             gameobj-occupants
41             gameobj-self-destruct
42
43             slot-ref-maybe-runcheck
44             val-or-run
45
46             dyn-ref
47
48             ;; Some of the more common commands
49             cmd-take cmd-drop
50             cmd-take-from-no-op cmd-put-in-no-op))
51
52 ;;; Gameobj
53 ;;; =======
54
55
56 ;;; *all* game components that talk to players should somehow
57 ;;; derive from this class.
58 ;;; And all of them need a GM!
59
60 (define-class <gameobj> (<actor>)
61   ;; location id
62   (loc #:init-value #f
63        #:getter gameobj-loc)
64   
65   ;; Uses a hash table like a set (values ignored)
66   (occupants #:init-thunk make-hash-table)
67
68   ;; game master id
69   (gm #:init-keyword #:gm
70       #:getter gameobj-gm)
71
72   ;; a name to be known by
73   (name #:init-keyword #:name
74         #:init-value #f)
75   (goes-by #:init-keyword #:goes-by
76            #:init-value #f)
77
78   (desc #:init-value #f
79         #:init-keyword #:desc)
80
81   ;; Commands we can handle
82   (commands #:allocation #:each-subclass
83             #:init-thunk (build-commands
84                           ("take" ((direct-command cmd-take)
85                                    (prep-indir-command cmd-take-from
86                                                        '("from" "out of"))))
87                           ("put" ((prep-indir-command cmd-put-in
88                                                       '("in" "inside" "on"))))))
89
90   ;; Commands we can handle by being something's container
91   (container-commands #:allocation #:each-subclass
92                       #:init-thunk (build-commands))
93
94   ;; Commands we can handle by being contained by something else
95   (contained-commands #:allocation #:each-subclass
96                       #:init-thunk 
97                       (build-commands
98                        ("drop" ((direct-command cmd-drop #:obvious? #f)))))
99
100   ;; Most objects are generally visible by default
101   (invisible? #:init-value #f
102               #:init-keyword #:invisible?)
103   ;; TODO: Fold this into a procedure in invisible? similar
104   ;;   to take-me? and etc
105   (visible-to-player?
106    #:init-value (wrap-apply gameobj-visible-to-player?))
107
108   ;; Can be a boolean or a procedure accepting
109   ;; (gameobj whos-acting #:key from)
110   (take-me? #:init-value #f
111             #:init-keyword #:take-me?)
112   ;; Can be a boolean or a procedure accepting
113   ;; (gameobj whos-acting where)
114   (drop-me? #:init-value #t
115             #:init-keyword #:drop-me?)
116
117   ;; TODO: Remove this and use actor-alive? instead.
118   ;; Set this on self-destruct
119   ;; (checked by some "long running" game routines)
120   (destructed #:init-value #f)
121
122   (actions #:allocation #:each-subclass
123            ;;; Actions supported by all gameobj
124            #:init-thunk
125            (build-actions
126             (init gameobj-act-init)
127             ;; Commands for co-occupants
128             (get-commands gameobj-get-commands)
129             ;; Commands for participants in a room
130             (get-container-commands gameobj-get-container-commands)
131             ;; Commands for inventory items, etc (occupants of the gameobj commanding)
132             (get-contained-commands gameobj-get-contained-commands)
133
134             (get-occupants gameobj-get-occupants)
135             (add-occupant! gameobj-add-occupant!)
136             (remove-occupant! gameobj-remove-occupant!)
137             (get-loc gameobj-act-get-loc)
138             (set-loc! gameobj-act-set-loc!)
139             (get-name gameobj-get-name)
140             (set-name! gameobj-act-set-name!)
141             (get-desc gameobj-get-desc)
142             (goes-by gameobj-act-goes-by)
143             (visible-name gameobj-visible-name)
144             (self-destruct gameobj-act-self-destruct)
145             (tell gameobj-tell-no-op)
146             (assist-replace gameobj-act-assist-replace)
147             (ok-to-drop-here? (lambda (gameobj message . _)
148                                 (<-reply message #t))) ; ok to drop by default
149             (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
150             (ok-to-be-put-in? gameobj-ok-to-be-put-in)
151
152             ;; Common commands
153             (cmd-take cmd-take)
154             (cmd-drop cmd-drop)
155             (cmd-take-from cmd-take-from-no-op)
156             (cmd-put-in cmd-put-in-no-op))))
157
158
159 ;;; gameobj message handlers
160 ;;; ========================
161
162 ;; Kind of a useful utility, maybe?
163 (define (simple-slot-getter slot)
164   (lambda (actor message)
165     (<-reply message (slot-ref actor slot))))
166
167 (define (gameobj-replace-step-occupants actor occupants)
168   ;; Snarf all the occupants!
169   (display "replacing occupant\n")
170   (when occupants
171     (for-each
172      (lambda (occupant)
173        (<-wait occupant 'set-loc!
174                #:loc (actor-id actor)))
175      occupants)))
176
177 (define gameobj-replace-steps*
178   (list gameobj-replace-step-occupants))
179
180 (define (run-replacement actor replaces replace-steps)
181   (when replaces
182     (mbody-receive (_ #:key occupants)
183         (<-wait replaces 'assist-replace)
184       (for-each
185        (lambda (replace-step)
186          (replace-step actor occupants))
187        replace-steps))))
188
189 ;; @@: This could be kind of a messy way of doing gameobj-act-init
190 ;;   stuff.  If only we had generic methods :(
191 (define* (gameobj-act-init actor message #:key replace)
192   "Your most basic game object init procedure.
193 Assists in its replacement of occupants if necessary and nothing else."
194   (run-replacement actor replace gameobj-replace-steps*))
195
196 (define (gameobj-goes-by gameobj)
197   "Find the name we go by.  Defaults to #:name if nothing else provided."
198   (cond ((slot-ref gameobj 'goes-by) =>
199          identity)
200         ((slot-ref gameobj 'name) =>
201          (lambda (name)
202            (list name)))
203         (else '())))
204
205 (define (gameobj-act-goes-by actor message)
206   "Reply to a message requesting what we go by."
207   (<-reply message (gameobj-goes-by actor)))
208
209 (define (val-or-run val-or-proc)
210   "Evaluate if a procedure, or just return otherwise"
211   (if (procedure? val-or-proc)
212       (val-or-proc)
213       val-or-proc))
214
215 (define (get-candidate-commands actor rmeta-sym verb)
216   (class-rmeta-ref (class-of actor) rmeta-sym verb
217                    #:dflt '()))
218
219 (define* (gameobj-get-commands actor message #:key verb)
220   "Get commands a co-occupant of the room might execute for VERB"
221   (define candidate-commands
222     (get-candidate-commands actor 'commands verb))
223   (<-reply message
224            #:commands candidate-commands
225            #:goes-by (gameobj-goes-by actor)))
226
227 (define* (gameobj-get-container-commands actor message #:key verb)
228   "Get commands as the container / room of message's sender"
229   (define candidate-commands
230     (get-candidate-commands actor 'container-commands verb))
231   (<-reply message #:commands candidate-commands))
232
233 (define* (gameobj-get-contained-commands actor message #:key verb)
234   "Get commands as being contained (eg inventory) of commanding gameobj"
235   (define candidate-commands
236     (get-candidate-commands actor 'contained-commands verb))
237   (<-reply message
238            #:commands candidate-commands
239            #:goes-by (gameobj-goes-by actor)))
240
241 (define* (gameobj-add-occupant! actor message #:key who)
242   "Add an actor to our list of present occupants"
243   (hash-set! (slot-ref actor 'occupants)
244              who #t))
245
246 (define* (gameobj-remove-occupant! actor message #:key who)
247   "Remove an occupant from the room."
248   (hash-remove! (slot-ref actor 'occupants) who))
249
250 (define* (gameobj-occupants gameobj #:key exclude)
251   (hash-fold
252    (lambda (occupant _ prev)
253      (define exclude-it?
254        (match exclude
255          ;; Empty list and #f are non-exclusion
256          (() #f)
257          (#f #f)
258          ;; A list of addresses... since our address object is (annoyingly)
259          ;; currently a simple cons cell...
260          ((exclude-1 ... exclude-rest)
261           (member occupant exclude))
262          ;; Must be an individual address!
263          (_ (equal? occupant exclude))))
264      (if exclude-it?
265          prev
266          (cons occupant prev)))
267    '()
268    (slot-ref gameobj 'occupants)))
269
270 (define* (gameobj-get-occupants actor message #:key exclude)
271   "Get all present occupants of the room."
272   (define occupants
273     (gameobj-occupants actor #:exclude exclude))
274   (<-reply message occupants))
275
276 (define (gameobj-act-get-loc actor message)
277   (<-reply message (slot-ref actor 'loc)))
278
279 (define (gameobj-set-loc! gameobj loc)
280   "Set the location of this object."
281   (define old-loc (gameobj-loc gameobj))
282   (format #t "DEBUG: Location set to ~s for ~s\n"
283           loc (actor-id-actor gameobj))
284
285   (when (not (equal? old-loc loc))
286     (slot-set! gameobj 'loc loc)
287     ;; Change registation of where we currently are
288     (if old-loc
289         (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
290     (if loc
291         (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
292
293 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
294 (define* (gameobj-act-set-loc! actor message #:key loc)
295   "Action routine to set the location."
296   (gameobj-set-loc! actor loc))
297
298 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
299   "Do a slot-ref on gameobj, evaluating it including ourselves
300 and whos-asking, and see if we should just return it or run it."
301   (match (slot-ref gameobj slot)
302     ((? procedure? slot-val-proc)
303      (apply slot-val-proc gameobj whos-asking other-args))
304     (anything-else anything-else)))
305
306 (define gameobj-get-name (simple-slot-getter 'name))
307
308 (define* (gameobj-act-set-name! actor message val)
309   (slot-set! actor 'name val))
310
311 (define* (gameobj-get-desc actor message #:key whos-looking)
312   (define desc-text
313     (match (slot-ref actor 'desc)
314       ((? procedure? desc-proc)
315        (desc-proc actor whos-looking))
316       (desc desc)))
317   (<-reply message desc-text))
318
319 (define (gameobj-visible-to-player? gameobj whos-looking)
320   "Check to see whether we're visible to the player or not.
321 By default, this is whether or not the generally-visible flag is set."
322   (not (slot-ref gameobj 'invisible?)))
323
324 (define* (gameobj-visible-name actor message #:key whos-looking)
325   ;; Are we visible?
326   (define we-are-visible
327     ((slot-ref actor 'visible-to-player?) actor whos-looking))
328
329   (define name-to-return
330     (if we-are-visible
331         ;; Return our name
332         (match (slot-ref actor 'name)
333           ((? procedure? name-proc)
334            (name-proc actor whos-looking))
335           ((? string? name)
336            name)
337           (#f #f))
338         #f))
339   (<-reply message #:text name-to-return))
340
341 (define (gameobj-self-destruct gameobj)
342   "General gameobj self destruction routine"
343   ;; Unregister from being in any particular room
344   (gameobj-set-loc! gameobj #f)
345   (slot-set! gameobj 'destructed #t)
346   ;; Boom!
347   (self-destruct gameobj))
348
349 (define* (gameobj-act-self-destruct gameobj message #:key why)
350   "Action routine for self destruction"
351   (gameobj-self-destruct gameobj))
352
353 ;; Unless an actor has a tell message, we just ignore it
354 (define gameobj-tell-no-op
355   (const 'no-op))
356
357 (define (gameobj-replace-data-occupants gameobj)
358   "The general purpose list of replacement data"
359   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
360                                     (slot-ref gameobj 'occupants))))
361
362 (define (gameobj-replace-data* gameobj)
363   ;; For now, just call gameobj-replace-data-occupants.
364   ;; But there may be more in the future!
365   (gameobj-replace-data-occupants gameobj))
366
367 ;; So sad that objects must assist in their replacement ;_;
368 ;; But that's life in a live hacked game!
369 (define (gameobj-act-assist-replace gameobj message)
370   "Vanilla method for assisting in self-replacement for live hacking"
371   (apply <-reply message
372          (gameobj-replace-data* gameobj)))
373
374 (define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
375   (call-with-values (lambda ()
376                       (slot-ref-maybe-runcheck gameobj 'take-me?
377                                                whos-acting #:from #t))
378     ;; This allows this to reply with #:why-not if appropriate
379     (lambda args
380       (apply <-reply message args))))
381
382 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
383   (call-with-values (lambda ()
384                       (slot-ref-maybe-runcheck gameobj 'drop-me?
385                                                whos-acting where))
386     ;; This allows this to reply with #:why-not if appropriate
387     (lambda args
388       (apply <-reply message args))))
389
390 \f
391 ;;; Utilities every gameobj has
392 ;;; ---------------------------
393
394 (define (dyn-ref gameobj special-symbol)
395   "Dynamically look up a special object from the gm"
396   (match special-symbol
397     ;; if it's a symbol, look it up dynamically
398     ((? symbol? _)
399      (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
400                         #:symbol special-symbol)))
401     ;; if it's false, return nothing
402     (#f #f)
403     ;; otherwise it's probably an address, return it as-is
404     (_ special-symbol)))
405
406
407 \f
408 ;;; Basic actions
409 ;;; -------------
410
411 (define* (cmd-take gameobj message
412                    #:key direct-obj
413                    (player (message-from message)))
414   (define player-name
415     (mbody-val (<-wait player 'get-name)))
416   (define player-loc
417     (mbody-val (<-wait player 'get-loc)))
418   (define our-name (slot-ref gameobj 'name))
419   (define self-should-take
420     (slot-ref-maybe-runcheck gameobj 'take-me? player))
421   ;; @@: Is there any reason to allow the room to object in the way
422   ;;   that there is for dropping?  It doesn't seem like it.
423   (call-with-values (lambda ()
424                       (slot-ref-maybe-runcheck gameobj 'take-me? player))
425     (lambda* (self-should-take #:key (why-not
426                                       `("It doesn't seem like you can take "
427                                         ,our-name ".")))
428       (if self-should-take
429           ;; Set the location to whoever's picking us up
430           (begin
431             (gameobj-set-loc! gameobj player)
432             (<- player 'tell
433                 #:text (format #f "You pick up ~a.\n"
434                                our-name))
435             (<- player-loc 'tell-room
436                 #:text (format #f "~a picks up ~a.\n"
437                                player-name
438                                our-name)
439                 #:exclude player))
440           (<- player 'tell #:text why-not)))))
441
442 (define* (cmd-drop gameobj message
443                    #:key direct-obj
444                    (player (message-from message)))
445   (define player-name
446     (mbody-val (<-wait player 'get-name)))
447   (define player-loc
448     (mbody-val (<-wait player 'get-loc)))
449   (define our-name (slot-ref gameobj 'name))
450   (define should-drop
451     (slot-ref-maybe-runcheck gameobj 'drop-me? player))
452   (define (room-objection-to-drop)
453     (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
454         (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
455       (and (not drop-ok?)
456            ;; Either give the specified reason, or give a boilerplate one
457            (or why-not
458                `("You'd love to drop " ,our-name
459                  " but for some reason it doesn't seem like you can"
460                  " do that here.")))))
461   (cond
462    ((not player-loc)
463     (<- player 'tell
464         #:text `("It doesn't seem like you can drop " ,our-name 
465                 " here, because you don't seem to be anywhere?!?")))
466    ;; TODO: Let ourselves supply a reason why not.
467    ((not should-drop)
468     (<- player 'tell
469         #:text (format #f "It doesn't seem like you can drop ~a.\n"
470                        our-name)))
471    ((room-objection-to-drop)
472     (<- player 'tell
473         #:text room-objection-to-drop))
474    (else
475     (gameobj-set-loc! gameobj player-loc)
476     ;; TODO: Allow more flavortext here.
477     (<- player 'tell
478         #:text (format #f "You drop ~a.\n"
479                        our-name))
480     (<- player-loc 'tell-room
481         #:text (format #f "~a drops ~a.\n"
482                        player-name
483                        our-name)
484         #:exclude player))))
485
486 (define* (cmd-take-from-no-op gameobj message
487                               #:key direct-obj indir-obj preposition
488                               (player (message-from message)))
489   (<- player 'tell
490       #:text `("It doesn't seem like you can take anything "
491                ,preposition " "
492                ,(slot-ref gameobj 'name) ".")))
493
494 (define* (cmd-put-in-no-op gameobj message
495                            #:key direct-obj indir-obj preposition
496                            (player (message-from message)))
497   (<- player 'tell
498       #:text `("It doesn't seem like you can put anything "
499                ,preposition " "
500                ,(slot-ref gameobj 'name) ".")))