aboutsummaryrefslogtreecommitdiff
path: root/src/cellular-automaton.c
blob: 183684abc38d7222c5febfbb94ca7dbd556cea47 (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
#include <stdio.h>
#include "cellular-automaton.h"
#include "utils.h"

void
cellular_automaton_init (struct cellular_automaton *cellular_automaton,
                         uint32_t function, uint32_t rows, uint32_t cols)
{
  board_init (&cellular_automaton->board, rows, cols);
  cellular_automaton->back_board_bitarray =
    malloc (cellular_automaton->board.bitarray.size);
  cellular_automaton->function = function;
}

void
cellular_automaton_progress (struct cellular_automaton *cellular_automaton)
{
  // uint32_t alive = 0;
  for (int32_t row = 0; row < cellular_automaton->board.rows; ++row) {
    for (int32_t col = 0; col < cellular_automaton->board.cols; ++col) {
      struct board *board = &cellular_automaton->board;
      uint8_t state =
        board_get (board, row - 1, col + 0) << 3 |
        board_get (board, row + 0, col - 1) << 1 |
        board_get (board, row + 0, col + 0) << 0 |
        board_get (board, row + 0, col + 1) << 2 |
        board_get (board, row + 1, col + 0) << 4;

      // if (board_get (board, row, col)) {
      //   // printf ("%d/%d, ", row, col);
      //   ++alive;
      // }

      setbit (cellular_automaton->back_board_bitarray,
              row*board->cols + col,
              cellular_automaton->function >> state & 1);
    }
  } // printf ("%u\n", alive);

  SWAP (cellular_automaton->back_board_bitarray,
        cellular_automaton->board.bitarray.bytes);
}

void
cellular_automaton_free (struct cellular_automaton *cellular_automaton)
{
  board_free (&cellular_automaton->board);
  free (cellular_automaton->back_board_bitarray);
}