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