]> git.jsancho.org Git - gacela.git/blob - src/examples/engine-conway-game.scm
Entity sets and engine api through systems
[gacela.git] / src / examples / engine-conway-game.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 (define-module (gacela examples engine-conway-game)
19   #:use-module (gacela system)
20   #:use-module (gacela engine)
21   #:use-module (ice-9 receive))
22
23
24 (define* (neighborhood cell #:key (size 1))
25   (let ((min (* -1 size)))
26     (let loop-x ((delta-x size) (res '()))
27       (cond ((< delta-x min) res)
28             (else
29              (loop-x (- delta-x 1)
30                      (let loop-y ((delta-y size) (res res))
31                        (cond ((< delta-y min) res)
32                              (else
33                               (loop-y (- delta-y 1)
34                                       (cond ((not (and (= delta-x 0) (= delta-y 0)))
35                                              (cons (list (+ delta-x (car cell)) (+ delta-y (cadr cell))) res))
36                                             (else res))))))))))))
37
38 (define* (frequencies cells #:optional (res '()))
39   (cond ((null? cells)
40          res)
41         (else
42          (let ((freq (or (assoc-ref res (car cells)) 0)))
43            (frequencies (cdr cells)
44                         (assoc-set! res (car cells) (+ freq 1)))))))
45
46 (define* (dead-loop cells freq #:optional (deads '()))
47   (cond ((null? cells)
48          (values freq deads))
49         (else
50          (let* ((key (get-key (car cells)))
51                 (coord (get-component 'coord (car cells)))
52                 (f (or (assoc-ref freq coord) 0))
53                 (new-freq (assoc-remove! freq coord)))
54            (cond ((not (or (= f 2) (= f 3)))
55                   (dead-loop (cdr cells)
56                              new-freq
57                              (cons (remove-entity key) deads)))
58                  (else
59                   (dead-loop (cdr cells) new-freq deads)))))))
60
61 (define* (live-loop freq #:optional (lives '()))
62   (cond ((null? freq)
63          lives)
64         (else
65          (cond ((= (cdar freq) 3)
66                 (live-loop (cdr freq)
67                            (cons (new-entity `(coord . ,(caar freq))) lives)))
68                (else
69                 (live-loop (cdr freq) lives))))))
70
71 (define-system lives-or-deads ((cells (coord)))
72   (let ((freq (frequencies (apply append (map (lambda (c) (neighborhood (get-component 'coord c))) cells)))))
73     (receive (freq2 deads) (dead-loop cells freq)
74       (entities-changes
75        (append deads
76                (live-loop freq2))))))
77
78 (define-system print-world ((cells (coord)))
79   (format #t "Live Cells: ~a~%" (length cells)))
80
81 (define-engine conway-game lives-or-deads print-world)
82
83 (with-engine conway-game ()
84   (let ((cells '((4 1) (4 2) (5 1) (5 2)
85                  (11 3) (11 4) (11 5) (12 2) (12 6) (13 1) (13 7) (14 1) (14 7)
86                  (15 4) (16 2) (16 6) (17 3) (17 4) (17 5) (18 4)
87                  (21 5) (21 6) (21 7) (22 5) (22 6) (22 7) (23 4) (23 8)
88                  (25 3) (25 4) (25 8) (25 9)
89                  (35 6) (35 7) (36 6) (36 7))))
90     (entities-changes
91      (map (lambda (c) (new-entity `(coord . ,c))) cells))))
92
93
94 (export neighborhood
95         frequencies
96         dead-loop
97         live-loop
98         conway-game)