about summary refs log tree commit diff stats
path: root/game.scm
blob: a799ba73d8321784e03fa3bc6936b32c1465e362 (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
#!/bin/sh
#| -*- scheme -*-
exec csi -R r7rs -ss "$0" "$@"
|#
#+chicken (import r7rs)

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

(define (draw . instructions)
  (for-each (lambda (i)
              (cond
               ((string? i)
                (display i))
               ((list? i)
                (apply draw i))))
            instructions))

(define me
  (string-append "@" (cursor-left 1)))

(define rock
  (string-append "o" (cursor-left 1)))

(define blank
  (string-append " " (cursor-left 1)))

(define WIDTH
  (make-parameter 80))

(define HEIGHT
  (make-parameter 24))

(define WORLD
  (make-vector (* (WIDTH) (HEIGHT)) 0))

(define (get-position x y)
  (if (or (> x (WIDTH))
          (< x 0)
          (> y (HEIGHT))
          (< y 0))
      #f
      (vector-ref WORLD (+ y (* (WIDTH) x)))))

(define (set-position! x y thing)
  (if (or (> x (WIDTH))
          (< x 0)
          (> y (HEIGHT))
          (< y 0))
      (error "Out of bounds" (list (WIDTH) (HEIGHT)) (list x y))
  (vector-set! WORLD (+ y (* (WIDTH) x)) thing)))

(define X
  (make-parameter (/ (WIDTH) 2)))

(define Y
  (make-parameter (/ (HEIGHT) 2)))

(define (move-up)
  (unless (<= (Y) 2)
    (draw blank (cursor-up 1) me)
    (Y (- (Y) 1))))

(define (move-left)
  (unless (<= (X) 2)
    (draw blank (cursor-left 1) me)
    (X (- (X) 1))))

(define (move-right)
  (unless (>= (X) (- (WIDTH) 1))
    (draw blank (cursor-right 1) me)
    (X (+ (X) 1))))

(define (move-down)
  (unless (>= (Y) (- (HEIGHT) 1))
    (draw blank (cursor-down 1) me)
    (Y (+ (Y) 1))))

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

  ;; Draw borders
  (draw cursor-home
        "/"
        (list->string
         (let loop ((c 1)
                    (acc '()))
           (if (>= c (- (WIDTH) 1))
               acc
               (loop (+ 1 c)
                     (cons #\- acc)))))
        "\\"
        (let loop ((r 2)
                   (acc '()))
          (if (>= r (HEIGHT))
              acc
              (loop (+ 1 r)
                    (append `(,(cursor-move 0 r)
                              "|"
                              ,(cursor-move (WIDTH) r)
                              "|")
                            acc))))
        (cursor-move 1 (HEIGHT))
        "\\"
        (list->string
         (let loop ((c 1)
                    (acc '()))
           (if (>= c (- (WIDTH) 1))
               acc
               (loop (+ 1 c)
                     (cons #\- acc)))))
        "/")

  ;; Draw character
  (draw (cursor-move (X) (Y))
        me))

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

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

(define (car* x)
  (and (pair? x)
       (car x)))

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

(define (main args)
  (dynamic-wind
    game-setup
    game-loop
    game-cleanup)
  #t)