← Back to davo.co
aboutsummaryrefslogtreecommitdiffstats
path: root/B9-detach.c
blob: d6ecfb61805639c885b83b777f6313347ea139dd (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
#include "life.h"
//#include <stdlib.h>
#include <stdio.h>
#include <stdatomic.h>
//#include <limits.h>
#include "termin.h"
//#include "c23-fallback.h"
//#include <unistd.h>

// The keys that are used for cursor movement.
// Other keys that are use are
// b, B, space for birth9
// -, + to slow down and accelerate
// q, Q to quit
#define GO_UP 'k'
#define GO_DOWN 'l'
#define GO_RIGHT ';'
#define GO_LEFT 'j'
#define GO_HOME 'h'

// We translate escape sequences that we may receive to these standard
// characters for further processing.

#define ESCAPE '\e'

char const*const termin_trans[UCHAR_MAX+1] = {
  [GO_UP] = ESC_UP,
  [GO_DOWN] = ESC_DOWN,
  [GO_RIGHT] = ESC_FRWD,
  [GO_LEFT] = ESC_BKWD,
  [GO_HOME] = ESC_HOME,
};

static int account_thread(void*);

static
int update_thread(void* Lv) {
  /* Nobody should ever wait for this thread. */
  thrd_detach(thrd_current());
  /* Delegates part of our job to an auxiliary thread */
  thrd_create(&(thrd_t){0}, account_thread, Lv);
  life*restrict L = Lv;
  size_t changed = 1;
  size_t birth9 = 0;
  while (!L->finished && changed) {
    // block until there is work
    mtx_lock(&L->mtx);
    while (!L->finished && (L->accounted < L->iteration))
      life_wait(&L->upda, &L->mtx);

    // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    if (birth9 != L->birth9) life_torus(L);
    life_count(L);
    changed = life_update(L);
    life_torus(L);
    birth9 = L->birth9;
    if (L->iteration != SIZE_MAX) L->iteration++;
    else L->finished = true;
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    cnd_signal(&L->acco);
    cnd_signal(&L->draw);
    mtx_unlock(&L->mtx);
    life_sleep(1.0/L->frames);
  }
  /* Tells everybody that the game is over */
  L->finished = true;
  ungetc('q', stdin);
  return 0;
}

static
int draw_thread(void* Lv) {
  /* Nobody should ever wait for this thread. */
  thrd_detach(thrd_current());
  life*restrict L = Lv;
  size_t x0 = 0;
  size_t x1 = 0;
  fputs(ESC_CLEAR ESC_CLRSCR, stdout);
  while (!L->finished) {
    // Blocks until there is work
    mtx_lock(&L->mtx);
    while (!L->finished
           && (L->iteration <= L->drawn)
           && (x0 == L->x0)
           && (x1 == L->x1))
      life_wait(&L->draw, &L->mtx);

    // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    if (L->n0 <= 30) life_draw(L);
    else life_draw4(L);
    L->drawn++;
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    mtx_unlock(&L->mtx);

    x0 = L->x0;
    x1 = L->x1;
    // no need to draw too quickly
    life_sleep(1.0/40);
  }
  return 0;
}

// This number of consecutive states that are already known (that is,
// hashed in) decides that this sequence of states is cyclic.
constexpr unsigned repetition = 10;

static
int account_thread(void* Lv) {
  /* Nobody should ever wait for this thread. */
  thrd_detach(thrd_current());
  life*restrict L = Lv;
  while (!L->finished) {
    // Blocks until there is work
    mtx_lock(&L->mtx);
    while (!L->finished && (L->accounted == L->iteration))
      life_wait(&L->acco, &L->mtx);

    // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    life_account(L);
    if ((L->last + repetition) < L->accounted) {
      L->finished = true;
    }
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    cnd_signal(&L->upda);
    mtx_unlock(&L->mtx);
  }
  return 0;
}

static
int input_thread(void* Lv) {
  /* Nobody should ever wait for this thread. */
  thrd_detach(thrd_current());
  termin_unbuffered();
  life*restrict L = Lv;
  constexpr size_t len = 32;
  char command[len];
  do {
    auto c = getchar();
    command[0] = c;
    switch(c) {
    case GO_LEFT : life_advance(L,  0, -1); break;
    case GO_RIGHT: life_advance(L,  0, +1); break;
    case GO_UP   : life_advance(L, -1,  0); break;
    case GO_DOWN : life_advance(L, +1,  0); break;
    case GO_HOME : L->x0 = 1; L->x1 = 1;    break;
    case ESCAPE  :
      ungetc(termin_translate(termin_read_esc(len, command)), stdin);
      continue;
    case '+':      if (L->frames < 128) L->frames++; continue;
    case '-':      if (L->frames > 1)   L->frames--; continue;
    case ' ':
    case 'b':
    case 'B':
      mtx_lock(&L->mtx);
      // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
      life_birth9(L);
      // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      mtx_unlock(&L->mtx);
      break;
    case 'q':
    case 'Q':
    case EOF:      goto FINISH;
    }
    cnd_signal(&L->draw);
  } while (!(L->finished || feof(stdin)));
 FINISH:
  L->finished = true;
  return 0;
}

enum { n = 60, m = 160 };

bool M[n][m] = {
 { },
 { },
 { 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0},
 { 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
 { 0, 0, 0, 0, 1, 0, 1, 0, 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 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 },
 { 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0 },
 { },
};

life L = LIFE_INITIALIZER;

void B9_atexit(void) {
  /* Puts the board in a nice final picture */
  L.iteration = L.last;
  life_draw(&L);
  life_destroy(&L);
}

int main(int argc, char* argv[argc+1]) {
  /* Uses command-line arguments for the size of the board */
  size_t n0 = 30;
  size_t n1 = 80;
  if (argc > 1) n0 = strtoull(argv[1], nullptr, 0);
  if (argc > 2) n1 = strtoull(argv[2], nullptr, 0);
  /* Create an object that holds the game's data. */
  life_init(&L, n0, n1, M);
  atexit(B9_atexit);
  /* Creates four threads that operate on the same object and
     discards their IDs */
  thrd_create(&(thrd_t){0}, update_thread,  &L);
  thrd_create(&(thrd_t){0}, draw_thread,    &L);
  thrd_create(&(thrd_t){0}, input_thread,   &L);
  /* Ends this thread nicely and lets the threads go on nicely */
  thrd_exit(0);
}