commands finally dispatch
[mudsync.git] / mudsync / room.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 (define-module (mudsync room)
20   #:use-module (mudsync command)
21   #:use-module (mudsync gameobj)
22   #:use-module (8sync systems actors)
23   #:use-module (8sync agenda)
24   #:use-module (oop goops)
25   #:export (<room>
26             room-actions
27             room-actions*
28
29             <exit>))
30
31 ;;; Rooms
32 ;;; =====
33
34 (define-class <exit> ()
35   ;; Used for wiring
36   (to-symbol #:accessor exit-to-symbol
37              #:init-keyword #:to-symbol)
38   ;; The actual address we use
39   (to-address #:accessor exit-to-address
40               #:init-keyword #:address)
41   ;; Name of the room (@@: Should this be names?)
42   (name #:accessor exit-name
43         #:init-keyword #:name)
44   (desc #:accessor exit-desc
45                #:init-keyword #:desc)
46
47   ;; *Note*: These two methods have an extra layer of indirection, but
48   ;;   it's for a good reason.
49   (visible-check #:init-value (const #t)
50                  #:init-keyword #:visible-check)
51   ;; By default all exits can be traversed
52   (traverse-check #:init-value (const #t)
53                   #:init-keyword #:traverse-check))
54
55 (define* (exit-can-traverse? exit actor
56                              #:optional (target-actor (actor-id actor)))
57   ((slot-ref exit 'traverse-check) exit actor target-actor))
58
59 (define* (exit-is-visible? exit actor
60                            #:optional (target-actor (actor-id actor)))
61   ((slot-ref exit 'traverse-check) exit actor target-actor))
62
63 (define %room-contain-commands
64   (list
65    (loose-direct-command "look" 'cmd-look-at)
66    (empty-command "look" 'cmd-look-room)
67    (loose-direct-command "go" 'cmd-go)))
68
69 ;; TODO: Subclass from container?
70 (define-class <room> (<gameobj>)
71   ;; A list of <exit>
72   (exits #:init-value '()
73          #:getter room-exits)
74
75   (container-commands
76    #:init-value %room-contain-commands)
77
78   (message-handler
79    #:allocation #:each-subclass
80    ;; @@: Can remove this indirection once things settle
81    #:init-value (wrap-apply room-action-dispatch)))
82
83
84 (define room-actions
85   (build-actions
86    ;; desc == description
87    (wire-exits! (wrap-apply room-wire-exits!))))
88
89 (define room-actions*
90   (append room-actions gameobj-actions))
91
92 (define room-action-dispatch
93   (simple-dispatcher room-actions*))
94
95
96 (define (room-wire-exits! room message)
97   "Actually hook up the rooms' exit addresses to the rooms they
98 claim to point to."
99   (for-each
100    (lambda (exit)
101      (define new-exit
102        (<-wait room (gameobj-gm room) 'lookup-room
103                #:symbol (exit-to-symbol exit)))
104
105      (set! (exit-to-address exit) new-exit))
106
107    (room-exits room)))
108