about summary refs log tree commit diff stats
path: root/game.scm
blob: f3d67dbbce71c157ea3a79bb64126fee51ae33e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
#| -*- scheme -*-
exec csi -R r7rs -ss "$0" "$@"
game
|#

(import (scheme base)
        (scheme write)
        (chicken io)
        (chicken port)
        (yolk common)
        (yolk xterm)
        (yolk erase)
        (yolk cursor)
        (matchable)
        (stty))

(define (log . xs)
  (with-output-to-port (current-error-port)
    (lambda ()
      (for-each (lambda (x) (display x) (display " ") x)
                xs)
      (newline))))

(define (main args)
  (parameterize ((world (make-world 80 25)))
    (dynamic-wind game-setup
                  game-loop
                  game-cleanup))
  #t)

(define (game-setup)
  ;; Prepare terminal
  (stty '(raw (not echo)))
  (set-buffering-mode! (current-input-port) #:none 1)
  (set-buffering-mode! (current-output-port) #:none 1)
  (draw alt-buffer-enable
        erase-screen
        (cursor-save)
        invisible-cursor)
  ;; Set up world
  (world-init (world))
  (world-draw (world)))

(define (game-cleanup)
  ;; Restore terminal
  (stty '(cooked echo))
  (draw cursor-home
        (cursor-restore)
        alt-buffer-disable
        visible-cursor))

(define (game-loop)
  (let loop ((c (readch)))
    (if (eq? c 'done)
        #t
        (let ((done? #f))
          (match c
            ((or #\q #\)
             (set! done? #t))
            ;; Escape characters
            (#\escape
             (match (readch)
               (#\[ (match (readch)
                      (#\A (up!))
                      (#\B (down!))
                      (#\C (right!))
                      (#\D (left!))
                      (else)))
               (else)))
            (else))
          (world-draw (world))
          (loop (if done? 'done (readch)))))))

(define (readch)
  (let ((c (integer->char (read-byte))))
    c))

(define (draw . instructions)
  (for-each (lambda (i)
              (cond
               ((string? i) (display i))
               ((list? i) (apply draw i))
               (else (error "Don't know how to draw" i))))
            instructions))

(define-record-type world
  (%make-world width height map)
  world?
  (width world-width world-width-set!)
  (height world-height world-height-set!)
  (map world-map world-map-set!))

(define (in-bounds? world x y)
  (or (< x (world-width world))
      (> x 0)
      (< y (world-height world))
      (> y 0)))

(define (coords->index world x y)
  (if (in-bounds? world x y)
      (+ x (* (world-width world) y))
      (error "Out of bounds"
             (list (world-width world) (world-height world))
             (list x y))))

(define (world-get world x y)
  (vector-ref (world-map world) (coords->index world x y)))

(define (world-set! world x y obj)
  (vector-set! (world-map world)
               (coords->index world x y)
               obj))

(define (make-world width height)
  (%make-world width height (make-vector (* width height) #f)))

(define world (make-parameter #f))

(define (for-world proc world)
  (do ((y 1 (+ y 1)))
      ((= y (world-height world)) #t)
    (do ((x 1 (+ x 1)))
        ((= x (world-width world)) #t)
      (proc world x y))))

(define (world-draw world)
  (draw cursor-home)
  (for-world (lambda (w x y)
               (cond
                ((thing? (world-get w x y))
                 (draw (cursor-move x y)
                       (thing-look (world-get w x y))))
                (else
                 (draw (cursor-move x y)
                       " "))))
             world))

(define (world-init world)
  (for-world (lambda (w x y)
               (cond
                ((or (= x 1)
                     (= y 1)
                     (= x (- (world-width world) 1))
                     (= y (- (world-height world) 1)))
                 (thing-place! w (wall x y)))
                (else)))
             world)
  (thing-place! world (me)))

(define-record-type thing
  (make-thing name look x y z attrs)
  thing?
  (name thing-name)
  (look thing-look thing-look-set!)
  (x thing-x thing-x-set!)
  (y thing-y thing-y-set!)
  (z thing-z thing-z-set!)
  (attrs thing-attrs thing-attrs-set!))

(define (thing-place! world thing)
  (world-set! world (thing-x thing) (thing-y thing) thing))

(define (thing-move! world thing x y)
  (world-set! world (thing-x thing) (thing-y thing) #f)
  (thing-x-set! thing x)
  (thing-y-set! thing y)
  (world-set! world x y thing))

(define (thing-move-relative! world thing dx dy)
  (let* ((new-x (+ (thing-x thing) dx))
         (new-y (+ (thing-y thing) dy))
         (other (world-get world new-x new-y)))
    (cond
     ((and (thing? other)
           (> (thing-z other) (thing-z thing))))
     (else
      (cond ((< new-x 1)
             (set! new-x 1))
            ((> new-x (- (world-width world) 1))
             (set! new-x (- (world-width world) 1))))
      (cond ((< new-y 1)
             (set! new-y 1))
            ((> new-y (- (world-height world) 1))
             (set! new-y (- (world-height world) 1))))
      (thing-move! world thing new-x new-y)))))

(define (wall x y)
  (make-thing 'wall "#" x y 100 '()))

(define me
  (make-parameter
   (make-thing 'me "@" 40 10 2 '())))

(define (up!)
  (thing-move-relative! (world) (me) 0 -1))

(define (down!)
  (thing-move-relative! (world) (me) 0 1))

(define (right!)
  (thing-move-relative! (world) (me) 1 0))

(define (left!)
  (thing-move-relative! (world) (me) -1 0))