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