We can finally move around!
[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   #:use-module (srfi srfi-1)
26   #:export (<room>
27             room-actions
28             room-actions*
29
30             <exit>))
31
32 \f
33 ;;; Exits
34 ;;; =====
35
36 (define-class <exit> ()
37   ;; Used for wiring
38   (to-symbol #:init-keyword #:to-symbol)
39   ;; The actual address we use
40   (to-address #:init-keyword #:address)
41   ;; Name of the room (@@: Should this be names?)
42   (name #:getter exit-name
43         #:init-keyword #:name)
44   (desc #:init-keyword #:desc
45         #:init-value #f)
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
64 \f
65 ;;; Rooms
66 ;;; =====
67
68 (define %room-contain-commands
69   (list
70    (loose-direct-command "look" 'cmd-look-at)
71    (empty-command "look" 'cmd-look-room)
72    (loose-direct-command "go" 'cmd-go)))
73
74 ;; TODO: Subclass from container?
75 (define-class <room> (<gameobj>)
76   ;; A list of <exit>
77   (exits #:init-value '()
78          #:init-keyword #:exits
79          #:getter room-exits)
80
81   (container-commands
82    #:init-value %room-contain-commands)
83
84   (message-handler
85    #:allocation #:each-subclass
86    ;; @@: Can remove this indirection once things settle
87    #:init-value (wrap-apply room-action-dispatch)))
88
89
90 (define room-actions
91   (build-actions
92    ;; desc == description
93    (wire-exits! (wrap-apply room-wire-exits!))
94    (cmd-go (wrap-apply room-cmd-go))))
95
96 (define room-actions*
97   (append room-actions gameobj-actions))
98
99 (define room-action-dispatch
100   (simple-dispatcher room-actions*))
101
102
103 (define (room-wire-exits! room message)
104   "Actually hook up the rooms' exit addresses to the rooms they
105 claim to point to."
106   (for-each
107    (lambda (exit)
108      (define new-exit
109        (message-ref
110         (<-wait room (gameobj-gm room) 'lookup-room
111                 #:symbol (slot-ref exit 'to-symbol))
112         'room-id))
113
114      (slot-set! exit 'to-address new-exit))
115
116    (room-exits room)))
117
118 (define-mhandler (room-cmd-go room message direct-obj)
119   (define exit
120     (find
121      (lambda (exit)
122        (equal? (exit-name exit) direct-obj))
123      (room-exits room)))
124   (cond
125    (exit
126     (<-wait room (message-from message) 'set-loc!
127             #:loc (slot-ref exit 'to-address))
128     (<- room (message-from message) 'look-room))
129    (else
130     (<- room (message-from message) 'tell
131         #:text "I don't know where that is?\n"))))