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