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