-
Notifications
You must be signed in to change notification settings - Fork 2
/
multicolor.cpp
271 lines (248 loc) · 6.27 KB
/
multicolor.cpp
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
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <sstream>
#include "clife.hpp"
#include "util.hpp"
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <cassert>
#include <cmath>
#include <ctime>
#include <sys/ioctl.h>
/** Written by Sjors Gielen, eth0 winter 2014
* Feel free to use this for anything you like
*/
constexpr bool to_ledscreen = false;
constexpr bool concise = true;
std::function<int()> get_width;
// Uncomment to get terminal-based size
#define FIXED_SIZE
#define FIXED_WIDTH 80
#define FIXED_HEIGHT 8
struct MulticolorValue {
bool value;
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t age;
MulticolorValue() : value(false), red(0), green(0), blue(0), age(0){}
MulticolorValue(std::vector<MulticolorValue> vec) : value(true), age(0) {
std::vector<int> hues;
for(auto const &cell : vec) {
if(cell.value) {
float r = float(cell.red) / UINT8_MAX;
float g = float(cell.green) / UINT8_MAX;
float b = float(cell.blue) / UINT8_MAX;
float min = std::min(r, std::min(g, b));
float max = std::max(r, std::max(g, b));
// hue from 0 to 360
int hue = max == 0 ? 0 :
r == max ? (60 * (0 + (g-b)/(max-min))) :
g == max ? (60 * (2 + (b-r)/(max-min))) :
(60 * (4 + (r-g)/(max-min)));
if(hue < 0) {
hue += 360;
}
hues.push_back(hue);
}
}
assert(hues.size() == 3);
std::sort(hues.begin(), hues.end());
int range1 = hues[2] - hues[0];
int range2 = (hues[0] + 360) - hues[1];
int range3 = (hues[1] + 360) - hues[2];
int avghue;
if(std::min(range1, std::min(range2, range3)) == range1) {
avghue = (hues[0] + hues[1] + hues[2]) / 3;
} else if(std::min(range1, std::min(range2, range3)) == range2) {
avghue = (hues[0] + hues[1] + hues[2] + 360) / 3;
} else {
avghue = (hues[0] + hues[1] + hues[2] + 720) / 3;
}
avghue %= 360;
float sat = 1;
float value = 1;
int h_i = std::floor(avghue / 60);
float f = (avghue / 60.) - h_i;
float p = value * (1 - sat);
float q = value * (1 - f * sat);
float t = value * (1 - (1 - f) * sat);
value *= 255;
p *= 255;
q *= 255;
t *= 255;
switch(h_i) {
case 0:
red = value;
green = t;
blue = p;
break;
case 1:
red = q;
green = value;
blue = p;
break;
case 2:
red = p;
green = value;
blue = t;
break;
case 3:
red = p;
green = q;
blue = value;
break;
case 4:
red = t;
green = p;
blue = value;
break;
case 5:
red = value;
green = p;
blue = q;
break;
default:
abort();
};
}
MulticolorValue(bool value) : value(value), red(0), green(0), blue(0), age(0) {
if(value) {
char color = (rand() % 6) + 1;
red = (color & 1) ? 255 : 0;
green = (color & 2) ? 255 : 0;
blue = (color & 4) ? 255 : 0;
}
}
void age_once() {
age = (age + 1) % 50;
}
operator bool() const { return value; }
std::string hash() const {
if(value) {
return std::string("1") + char(red) + char(green) + char(blue);
} else {
return "0\x00\x00\x00";
}
}
char getChar() const {
return (value ? 'o' : ' ');
}
void begin_screen(std::ostream &os) const {
if(!to_ledscreen && !concise) {
std::cout << "+";
for(int i = 0; i < get_width(); ++i) {
std::cout << "-";
}
std::cout << "+\n";
}
}
void end_screen(std::ostream &os) const {
begin_screen(os);
std::cout << "\x1b[1;1H" << std::flush;
}
void begin_line(std::ostream &os) const {
if(!to_ledscreen && !concise) {
std::cout << "|";
}
}
void end_line(std::ostream &os) const {
if(!to_ledscreen && !concise) {
std::cout << "|\n";
}
}
void print(std::ostream &os) const {
if(value && to_ledscreen) {
os << red << green << blue;
} else if(value) {
double brightness = std::max(0.3, 1 - age / 20.);
uint8_t redval = (red * brightness) / 43;
uint8_t greenval = (green * brightness) / 43;
uint8_t blueval = (blue * brightness) / 43;
uint8_t code = 16 + 36 * redval + 6 * greenval + blueval;
os << "\x1b[38;5;" << int(code) << "mo\x1b[m";
} else if(to_ledscreen) {
os << '\x00' << '\x00' << '\x00';
} else {
os << ' ';
}
}
};
template <typename FieldType>
void check_stop_condition(FieldType field, std::vector<uint64_t> &earlier_hashes, bool &done, int &repeats_to_do) {
uint64_t hash = field.field_hash();
for(size_t i = 0; i < earlier_hashes.size(); ++i) {
if(earlier_hashes[i] == hash) {
done = true;
repeats_to_do = 50;
break;
}
}
earlier_hashes.push_back(hash);
}
int main(int argc, char *argv[]) {
int microsleeptime = 100'000;
if(argc == 2) {
std::stringstream ss;
ss << argv[1];
ss >> microsleeptime;
if(!ss) {
std::cerr << "Usage: " << argv[0] << " [microsleeptime]" << std::endl;
}
} else if(argc > 2) {
std::cerr << "Usage: " << argv[0] << " [microsleeptime]" << std::endl;
}
init_random();
winsize current_winsize;
#if defined(FIXED_SIZE)
current_winsize.ws_col = FIXED_WIDTH;
current_winsize.ws_row = FIXED_HEIGHT;
#else
ioctl(STDOUT_FILENO, TIOCGWINSZ, ¤t_winsize);
#endif
std::vector<uint64_t> earlier_hashes;
GameOfLifeField<MulticolorValue> field(current_winsize.ws_col, current_winsize.ws_row);
get_width = [&field]() {
return field.get_width();
};
field.generateRandom(35);
bool field_done = false;
int repeats_to_do = 0;
pthread_mutex_t mtx;
pthread_mutex_init(&mtx, nullptr);
pthread_cond_t cond;
pthread_cond_init(&cond, nullptr);
struct timespec ts = {};
ts.tv_sec = std::time(nullptr);
pthread_mutex_lock(&mtx);
while(!field_done || repeats_to_do > 0) {
#if !defined(FIXED_SIZE)
winsize new_winsize;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &new_winsize);
if(current_winsize.ws_col != new_winsize.ws_col
|| current_winsize.ws_row != new_winsize.ws_row) {
field.resize(new_winsize.ws_col, new_winsize.ws_row);
}
current_winsize = new_winsize;
#endif
if(repeats_to_do > 0) {
--repeats_to_do;
}
field.nextState();
field.print_simple(std::cout);
if(!field_done) {
check_stop_condition(field, earlier_hashes, field_done, repeats_to_do);
}
ts.tv_nsec += microsleeptime * 1'000;
ts.tv_sec += ts.tv_nsec / 1'000'000'000;
ts.tv_nsec %= 1'000'000'000;
pthread_cond_timedwait(&cond, &mtx, &ts);
}
pthread_mutex_unlock(&mtx);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mtx);
}