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