summaryrefslogtreecommitdiff
path: root/examples/life.scm
blob: 4e747eccc40c65979d422c4de087e83d8fa18ed6 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
;;; Life, Sly edition
;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
;;; Copyright (C) 2014, 2015 David Thompson <davet@gnu.org>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Conway's Game of Life

(use-modules (srfi srfi-1)
             (srfi srfi-9)
             (ice-9 match)
             (ice-9 vlist)
             (sly game)
             (sly window)
             (sly utils)
             (sly signal)
             (sly repl)
             (sly math rect)
             (sly math transform)
             (sly math vector)
             (sly render)
             (sly render camera)
             (sly render sprite)
             (sly render sprite-batch)
             (sly render texture)
             (sly render tileset)
             (sly render color)
             (sly input mouse))

;;;
;;; Utils
;;;

(define (vlist-substitute vlist index item)
  "Return a new VLIST with element at INDEX replaced with ITEM"
  (vlist-append
   (vlist-take vlist index)
   (vlist-cons item
               (vlist-drop vlist (+ 1 index)))))

;; Pulled out of mines.scm
(define (enumerate-each proc lst)
  (let loop ((k 0) (lst lst))
    (match lst
      (() *unspecified*)
      ((head . tail)
       (proc head k)
       (loop (1+ k) tail)))))

;;;
;;; Sly stuff starts here
;;;

(load "common.scm")

(define tile-size 32)
(define window-res (vector2 448 480))

(define-signal tileset
  (on-start (load-tileset "mines/images/tiles.png" 32 32)))

(define (alive-texture tileset)
  (tileset-ref tileset 12))

(define (empty-texture tileset)
  (tileset-ref tileset 13))

(define-signal batch
  (on-start (make-sprite-batch (expt 14 2))))

;;;
;;; State
;;;

;; Size of the board
;; @@: Maybe shouldn't be a signal, since
;;     this can't really be dynamically resized at present
(define-signal board-size 14)

(define (make-fresh-board board-size)
  "Make a fresh board (all empty / #f cells)"
  (define (fold-board-size-times thunk)
    (fold
     (lambda (i prev)
       (vlist-cons
        (thunk)
        prev))
     vlist-null
     (iota board-size)))

  (fold-board-size-times
   (lambda ()
     (fold-board-size-times
      (lambda () #f)))))

;; Give a heartbeat indicating it's time to run an evolution on the board
;; (if the simulation is running)
(define-signal time-to-evolve
  (signal-constant 'evolve (signal-every 20)))

(define (tile-on-board? x y board-size)
  "Is the tile on the board?"
  (define (on-board? pos)
    (if (and (>= pos 0)
             (< pos board-size))
        #t #f))
  (and (on-board? x) (on-board? y)))

(define (tile-at-pos pos board-size tile-size)
  "Find which tile is at the given position"
  (let* ((board-res
          (* board-size tile-size))
         (window-center
          (v* window-res 1/2))
         (first-tile-starts-at
          (v- window-center
              (* board-res 1/2)))
         (pos-relative-to-board
          (v- pos first-tile-starts-at)))
    (cons
     (floor
      (/ (vx pos-relative-to-board)
             tile-size))
     (floor
      (/ (vy pos-relative-to-board)
             tile-size)))))

;; Position of which tile the mouse is currently hovering over
(define-signal mouse-current-tile
  (signal-let ((mouse-position mouse-position)
               (board-size board-size))
    (tile-at-pos mouse-position
                 board-size tile-size)))

;; Mouse left click on a tile
(define-signal toggle-clicks
  (chain mouse-last-up
    (signal-filter (lambda (x) (eq? x 'left)) #f)
    (signal-sample-on mouse-current-tile)))

;; Whether or not the simulation is currently running
(define-signal simulation-running?
  (signal-fold
   (lambda (click currently-running)
     (not currently-running))
   #f
   (signal-filter (lambda (x) (eq? x 'right)) #f
                  mouse-last-up)))

;; Commands that the board should update (tile clicked, new board)
(define-signal board-update
  (signal-merge
   (signal-map (lambda (x)
                 (list 'toggle x))
               toggle-clicks)
   time-to-evolve))

(define (board-cell-ref board board-size row col)
  "Get the value of a cell on a board (#t or #f for aliveness)"
  (cond
   ;; wrap around on rows
   ((< row 0)
    (board-cell-ref board board-size (+ row board-size) col))
   ((>= row board-size)
    (board-cell-ref board board-size (- row board-size) col))

   ;; wrap around on cols
   ((< col 0)
    (board-cell-ref board board-size row (+ col board-size)))
   ((>= col board-size)
    (board-cell-ref board board-size row (- col board-size)))

   (else
    (vlist-ref (vlist-ref board row) col))))

(define (get-neighbors board board-size row col)
  "Get surrounding 8 neighbors of a cell"
  (define (get-cell row col)
    (board-cell-ref board board-size row col))
  (list
   ;; up
   (get-cell (+ row 1) col)
   ;; upper-right
   (get-cell (+ row 1) (+ col 1))
   ;; right
   (get-cell row (+ col 1))
   ;; lower-right
   (get-cell (- row 1) (+ col 1))
   ;; lower
   (get-cell (- row 1) col)
   ;; lower-left
   (get-cell (- row 1) (- col 1))
   ;; left
   (get-cell row (- col 1))
   ;; upper-left
   (get-cell (+ row 1) (- col 1))))

(define (cell-next-val board board-size row col)
  "Get the next value for a cell in an evolution based on its neighbors

If there is no neighbor on an edge, the board wraps around"
  (let* ((neighbors (get-neighbors board board-size
                                   row col))
         (alive-neighbors
          ;; We can use identity, because #t is true!
          (count identity neighbors))
         (currently-alive
          (board-cell-ref board board-size row col)))
    (if currently-alive
        (cond
         ;; Dies by under-population
         ((< alive-neighbors 2) #f)
         ;; healthy population, live
         ((or (= alive-neighbors 2)
              (= alive-neighbors 3))
          #t)
         ;; die from overcrowding
         ((> alive-neighbors 3) #f))
        ;; Since not currently alive,
        ;; we'll spawn if we have three neighbors
        (if (= alive-neighbors 3)
            #t #f))))

(define (evolve-board current-board board-size)
  "Return new evolved board based on CURRENT-BOARD for BOARD-SIZE"
  ;; loop on rows
  (list->vlist
   (map
    ;; loop on cols
    (lambda (row)
      (list->vlist
       (map
        (lambda (col)
          (cell-next-val current-board board-size row col))
        (iota board-size))))
    (iota board-size))))

(define (update-board-by-tile-toggle current-board row col)
  "Update CURRENT-BOARD by returning new board with ROW COL tile toggled"
  (let* ((current-row
          (vlist-ref current-board row))
         (current-status
          (vlist-ref current-row col)))
    (vlist-substitute
     current-board row
     (vlist-substitute
      current-row col
      (not current-status)))))

;; The actual game board structure
(define-signal board
  (signal-fold
   (lambda (update board-size running? current-board)
     (match update
       ('evolve
        (if running?
            (evolve-board current-board board-size)
            current-board))
       (('toggle (col . row))
        (if (tile-on-board? col row board-size)
            (update-board-by-tile-toggle
             current-board row col)
            current-board))
       (anything-else
        (format #t "Unhandled: ~s\n" anything-else)
        current-board)))
   (make-fresh-board (signal-ref board-size))
   board-update board-size simulation-running?))

;; Determine a tile's position
(define (tile-pos row col board-size tile-size)
  (v-
   (v+
    (vector2 (* col tile-size)
             (* row tile-size))
    (v* window-res 1/2))
   (vector2
    (/ (* board-size tile-size) 2)
    (/ (* board-size tile-size) 2))))

(define sprite-rect
  (make-rect 0 0 32 32))

;; Model of the tile grid
(define-signal tiles-view
  (signal-let ((board board)
               (board-size board-size)
               (batch batch)
               (tileset tileset))
    (if tileset
        (lambda (gfx)
          (with-sprite-batch batch gfx
            (enumerate-each
             (lambda (row y)
               (enumerate-each
                (lambda (alive? x)
                  (let ((rect (rect-move sprite-rect
                                         (tile-pos y x board-size tile-size))))
                    (sprite-batch-add! batch
                                       gfx
                                       (if alive?
                                           (alive-texture tileset)
                                           (empty-texture tileset))
                                       rect)))
                (vlist->list row)))
             (vlist->list board))))
        render-nothing)))

(define-signal camera
  (signal-let ((running? simulation-running?))
    (2d-camera #:area (make-rect (vector2 0 0) window-res)
               #:clear-color (if running?
                                 tango-dark-chameleon
                                 tango-dark-scarlet-red))))

(define-signal scene
  (signal-map with-camera camera tiles-view))

;;;
;;; Initialization
;;;

(add-hook! window-close-hook stop-game-loop)

(with-window (make-window #:title "Life (right click to start/stop)"
                          #:resolution window-res)
  (run-game-loop scene))