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