Adjust disc-shield description
[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" "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 (gameobj-setup-props gameobj)
245   (define class (class-of gameobj))
246   (define props (slot-ref gameobj 'props))
247   (maybe-build-rmeta-slot-cache! class 'initial-props
248                                  eq? hashq-set! hashq-ref)
249   ;; Kind of a kludge... we read through the rmeta-slot-cache
250   ;; and use that to build up the table
251   (hash-for-each
252    (lambda (key value)
253      (when (not (hashq-ref props key value)) ; don't override init'ed instance values
254        (hashq-set! props key value)))
255    (rmeta-slot-cache (class-slot-ref class 'initial-props))))
256
257 ;; TODO: Use the *init* action?
258 ;;   We could also use a generic method if they didn't have
259 ;;   what I'm pretty sure is O(n) dispatch in GOOPS...
260 (define* (gameobj-act-init actor message #:key replace)
261   "Your most basic game object init procedure."
262   (gameobj-setup-props actor)
263   (run-replacement actor replace gameobj-replace-steps*))
264
265 (define* (gameobj-get-prop gameobj key #:optional dflt)
266   (hashq-ref (slot-ref gameobj 'props) key dflt))
267
268 (define* (gameobj-set-prop! gameobj key val)
269   (hashq-set! (slot-ref gameobj 'props) key val))
270
271 (define* (gameobj-act-get-prop actor message key #:optional dflt)
272   (gameobj-get-prop actor key dflt))
273
274 (define (gameobj-goes-by gameobj)
275   "Find the name we go by.  Defaults to #:name if nothing else provided."
276   (cond ((slot-ref gameobj 'goes-by) =>
277          identity)
278         ((slot-ref gameobj 'name) =>
279          (lambda (name)
280            (list name)))
281         (else '())))
282
283 (define (gameobj-act-goes-by actor message)
284   "Reply to a message requesting what we go by."
285   (<-reply message (gameobj-goes-by actor)))
286
287 (define (val-or-run val-or-proc)
288   "Evaluate if a procedure, or just return otherwise"
289   (if (procedure? val-or-proc)
290       (val-or-proc)
291       val-or-proc))
292
293 (define (get-candidate-commands actor rmeta-sym verb)
294   (class-rmeta-ref (class-of actor) rmeta-sym verb
295                    #:dflt '()))
296
297 (define* (gameobj-get-commands actor message #:key verb)
298   "Get commands a co-occupant of the room might execute for VERB"
299   (define candidate-commands
300     (get-candidate-commands actor 'commands verb))
301   (<-reply message
302            #:commands candidate-commands
303            #:goes-by (gameobj-goes-by actor)))
304
305 (define* (gameobj-get-container-dom-commands actor message #:key verb)
306   "Get (dominant) commands as the container / room of message's sender"
307   (define candidate-commands
308     (get-candidate-commands actor 'container-dom-commands verb))
309   (<-reply message #:commands candidate-commands))
310
311 (define* (gameobj-get-container-sub-commands actor message #:key verb)
312   "Get (subordinate) commands as the container / room of message's sender"
313   (define candidate-commands
314     (get-candidate-commands actor 'container-sub-commands verb))
315   (<-reply message #:commands candidate-commands))
316
317 (define* (gameobj-get-contained-commands actor message #:key verb)
318   "Get commands as being contained (eg inventory) of commanding gameobj"
319   (define candidate-commands
320     (get-candidate-commands actor 'contained-commands verb))
321   (<-reply message
322            #:commands candidate-commands
323            #:goes-by (gameobj-goes-by actor)))
324
325 (define* (gameobj-add-occupant! actor message #:key who)
326   "Add an actor to our list of present occupants"
327   (hash-set! (slot-ref actor 'occupants)
328              who #t))
329
330 (define* (gameobj-remove-occupant! actor message #:key who)
331   "Remove an occupant from the room."
332   (hash-remove! (slot-ref actor 'occupants) who))
333
334 (define* (gameobj-occupants gameobj #:key exclude)
335   (hash-fold
336    (lambda (occupant _ prev)
337      (define exclude-it?
338        (match exclude
339          ;; Empty list and #f are non-exclusion
340          (() #f)
341          (#f #f)
342          ;; A list of addresses... since our address object is (annoyingly)
343          ;; currently a simple cons cell...
344          ((exclude-1 ... exclude-rest)
345           (member occupant exclude))
346          ;; Must be an individual address!
347          (_ (equal? occupant exclude))))
348      (if exclude-it?
349          prev
350          (cons occupant prev)))
351    '()
352    (slot-ref gameobj 'occupants)))
353
354 (define* (gameobj-get-occupants actor message #:key exclude)
355   "Get all present occupants of the room."
356   (define occupants
357     (gameobj-occupants actor #:exclude exclude))
358   (<-reply message occupants))
359
360 (define (gameobj-act-get-loc actor message)
361   (<-reply message (slot-ref actor 'loc)))
362
363 (define (gameobj-set-loc! gameobj loc)
364   "Set the location of this object."
365   (define old-loc (gameobj-loc gameobj))
366   (format #t "DEBUG: Location set to ~s for ~s\n"
367           loc (actor-id-actor gameobj))
368
369   (when (not (equal? old-loc loc))
370     (slot-set! gameobj 'loc loc)
371     ;; Change registation of where we currently are
372     (if old-loc
373         (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
374     (if loc
375         (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))
376
377 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
378 (define* (gameobj-act-set-loc! actor message #:key loc)
379   "Action routine to set the location."
380   (gameobj-set-loc! actor loc))
381
382 (define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
383   "Do a slot-ref on gameobj, evaluating it including ourselves
384 and whos-asking, and see if we should just return it or run it."
385   (match (slot-ref gameobj slot)
386     ((? procedure? slot-val-proc)
387      (apply slot-val-proc gameobj whos-asking other-args))
388     (anything-else anything-else)))
389
390 (define gameobj-get-name (simple-slot-getter 'name))
391
392 (define* (gameobj-act-set-name! actor message val)
393   (slot-set! actor 'name val))
394
395 (define* (gameobj-desc gameobj #:key whos-looking)
396   (match (slot-ref gameobj 'desc)
397     ((? procedure? desc-proc)
398      (desc-proc gameobj whos-looking))
399     (desc desc)))
400
401 (define* (gameobj-get-desc actor message #:key whos-looking)
402   "This is the action equivalent of the gameobj-desc getter"
403   (<-reply message (gameobj-desc actor #:whos-looking whos-looking)))
404
405 (define (gameobj-visible-to-player? gameobj whos-looking)
406   "Check to see whether we're visible to the player or not.
407 By default, this is whether or not the generally-visible flag is set."
408   (not (slot-ref gameobj 'invisible?)))
409
410 (define* (gameobj-visible-name actor message #:key whos-looking)
411   ;; Are we visible?
412   (define we-are-visible
413     ((slot-ref actor 'visible-to-player?) actor whos-looking))
414
415   (define name-to-return
416     (if we-are-visible
417         ;; Return our name
418         (match (slot-ref actor 'name)
419           ((? procedure? name-proc)
420            (name-proc actor whos-looking))
421           ((? string? name)
422            name)
423           (#f #f))
424         #f))
425   (<-reply message #:text name-to-return))
426
427 (define (gameobj-self-destruct gameobj)
428   "General gameobj self destruction routine"
429   ;; Unregister from being in any particular room
430   (gameobj-set-loc! gameobj #f)
431   (slot-set! gameobj 'destructed #t)
432   ;; Boom!
433   (self-destruct gameobj))
434
435 (define* (gameobj-act-self-destruct gameobj message #:key why)
436   "Action routine for self destruction"
437   (gameobj-self-destruct gameobj))
438
439 ;; Unless an actor has a tell message, we just ignore it
440 (define gameobj-tell-no-op
441   (const 'no-op))
442
443 (define (gameobj-replace-data-occupants gameobj)
444   "The general purpose list of replacement data"
445   (list #:occupants (hash-map->list (lambda (occupant _) occupant)
446                                     (slot-ref gameobj 'occupants))))
447
448 (define (gameobj-replace-data* gameobj)
449   ;; For now, just call gameobj-replace-data-occupants.
450   ;; But there may be more in the future!
451   (gameobj-replace-data-occupants gameobj))
452
453 ;; So sad that objects must assist in their replacement ;_;
454 ;; But that's life in a live hacked game!
455 (define (gameobj-act-assist-replace gameobj message)
456   "Vanilla method for assisting in self-replacement for live hacking"
457   (apply <-reply message
458          (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
466       (apply <-reply message args))))
467
468 (define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
469   (call-with-values (lambda ()
470                       (slot-ref-maybe-runcheck gameobj 'drop-me?
471                                                whos-acting where))
472     ;; This allows this to reply with #:why-not if appropriate
473     (lambda args
474       (apply <-reply message args))))
475
476 \f
477 ;;; Utilities every gameobj has
478 ;;; ---------------------------
479
480 (define (dyn-ref gameobj special-symbol)
481   "Dynamically look up a special object from the gm"
482   (match special-symbol
483     ;; if it's a symbol, look it up dynamically
484     ((? symbol? _)
485      ;; TODO: If we get back an #f at this point, should we throw
486      ;;   an error?  Obviously #f is okay, but maybe not if 
487      (mbody-val (<-wait (slot-ref gameobj 'gm) 'lookup-special
488                         #:symbol special-symbol)))
489     ;; if it's false, return nothing
490     (#f #f)
491     ;; otherwise it's probably an address, return it as-is
492     (_ special-symbol)))
493
494
495 \f
496 ;;; Basic actions
497 ;;; -------------
498
499 (define %formless-desc
500   "You don't see anything special.")
501
502 (define* (cmd-look-at gameobj message
503                       #:key direct-obj
504                       (player (message-from message)))
505   (let ((desc
506          (or (gameobj-desc gameobj #:whos-looking player)
507              %formless-desc)))
508     (<- player 'tell #:text desc)))
509
510 (define* (cmd-take gameobj message
511                    #:key direct-obj
512                    (player (message-from message)))
513   (define player-name
514     (mbody-val (<-wait player 'get-name)))
515   (define player-loc
516     (mbody-val (<-wait player 'get-loc)))
517   (define our-name (slot-ref gameobj 'name))
518   (define self-should-take
519     (slot-ref-maybe-runcheck gameobj 'take-me? player))
520   ;; @@: Is there any reason to allow the room to object in the way
521   ;;   that there is for dropping?  It doesn't seem like it.
522   (call-with-values (lambda ()
523                       (slot-ref-maybe-runcheck gameobj 'take-me? player))
524     (lambda* (self-should-take #:key (why-not
525                                       `("It doesn't seem like you can take "
526                                         ,our-name ".")))
527       (if self-should-take
528           ;; Set the location to whoever's picking us up
529           (begin
530             (gameobj-set-loc! gameobj player)
531             (<- player 'tell
532                 #:text (format #f "You pick up ~a.\n"
533                                our-name))
534             (<- player-loc 'tell-room
535                 #:text (format #f "~a picks up ~a.\n"
536                                player-name
537                                our-name)
538                 #:exclude player))
539           (<- player 'tell #:text why-not)))))
540
541 (define* (cmd-drop gameobj message
542                    #:key direct-obj
543                    (player (message-from message)))
544   (define player-name
545     (mbody-val (<-wait player 'get-name)))
546   (define player-loc
547     (mbody-val (<-wait player 'get-loc)))
548   (define our-name (slot-ref gameobj 'name))
549   (define should-drop
550     (slot-ref-maybe-runcheck gameobj 'drop-me? player))
551   (define (room-objection-to-drop)
552     (mbody-receive (_ drop-ok? #:key why-not) ; does the room object to dropping?
553         (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
554       (and (not drop-ok?)
555            ;; Either give the specified reason, or give a boilerplate one
556            (or why-not
557                `("You'd love to drop " ,our-name
558                  " but for some reason it doesn't seem like you can"
559                  " do that here.")))))
560   (cond
561    ((not player-loc)
562     (<- player 'tell
563         #:text `("It doesn't seem like you can drop " ,our-name 
564                 " here, because you don't seem to be anywhere?!?")))
565    ;; TODO: Let ourselves supply a reason why not.
566    ((not should-drop)
567     (<- player 'tell
568         #:text (format #f "It doesn't seem like you can drop ~a.\n"
569                        our-name)))
570    ((room-objection-to-drop)
571     (<- player 'tell
572         #:text room-objection-to-drop))
573    (else
574     (gameobj-set-loc! gameobj player-loc)
575     ;; TODO: Allow more flavortext here.
576     (<- player 'tell
577         #:text (format #f "You drop ~a.\n"
578                        our-name))
579     (<- player-loc 'tell-room
580         #:text (format #f "~a drops ~a.\n"
581                        player-name
582                        our-name)
583         #:exclude player))))
584
585 (define* (cmd-take-from-no-op gameobj message
586                               #:key direct-obj indir-obj preposition
587                               (player (message-from message)))
588   (<- player 'tell
589       #:text `("It doesn't seem like you can take anything "
590                ,preposition " "
591                ,(slot-ref gameobj 'name) ".")))
592
593 (define* (cmd-put-in-no-op gameobj message
594                            #:key direct-obj indir-obj preposition
595                            (player (message-from message)))
596   (<- player 'tell
597       #:text `("It doesn't seem like you can put anything "
598                ,preposition " "
599                ,(slot-ref gameobj 'name) ".")))