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