-
Notifications
You must be signed in to change notification settings - Fork 5
/
ascsee.c
288 lines (232 loc) · 6.89 KB
/
ascsee.c
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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <wand/MagickWand.h>
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
// grab an argv.
#define ARG(x, y) (argc > (x) ? atoi(argv[x]) : (y))
// (death) larva stage -> pupa -> maturity
#define MAP " .`,'\"^~-_+=*:/<ruvi!1lYVWMOX@&#"
unsigned char
*g_life,
*g_pixel;
int
g_width,
g_height;
unsigned char* pLife(int x, int y) { return &g_life[x * g_width + y]; }
unsigned char Life(int x, int y) { return g_life[x * g_width + y]; }
unsigned char* pPixel(int x, int y) { return &g_pixel[x * g_width + y]; }
unsigned char Pixel(int x, int y) { return g_pixel[x * g_width + y]; }
int main(int argc, char*argv[]) {
fd_set fd_r;
srand(time(0));
struct timeval ts;
MagickBooleanType stat;
MagickWand *wand;
int
cutoff = 225,
contrastRounds = rand() % 3 + 1,
moveradius = -1,
MATURE = 16,
// chance of reproducing
reproduce = ARG(2, rand() % 50 + 10),
// number of children bigger means fewer children
litterSize = ARG(3, rand() % 90 + 8),
// chance of dying
dieoff = ARG(4, rand() % 20 + 2),
// chance of trying to move around
move = ARG(5, rand() % 80 + 2),
// chance of growth
growth = ARG(6, rand() % 40 + 2),
// How many rounds to do before displaying to the screen
viewEvery = ARG(7, 15),
// frames per second
fps = 45;
FILE *fdesc;
// critical population size
// If the population falls below this amount,
// it gets seeded up to it.
float criticalFrac = 0.001 / 100;
int
fd,
iw,
ix,
iy,
iz,
c_height,
c_width,
critical,
population = 0,
landSize,
i,
j,
maturity,
turn = 0,
limI,
limJ;
if(argc > 1) {
fd = open(argv[1], O_RDONLY);
if(!fd) {
printf("Can't open file: %s\n", argv[1]);
exit(-1);
}
} else {
printf("Please tell me the image file to use.\n");
exit(-2);
}
ts.tv_sec = 0;
// Determine the height and width of the users' current terminal
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
c_height = g_height = w.ws_row - 2;
c_width = g_width = w.ws_col;
landSize = g_height * g_width;
// our critical population is a function of our landsize
critical = MAX((int)( criticalFrac * (float)landSize ), 1);
// the move radius is a function of the image height
moveradius = MAX(0.06 * g_height, 1);
// Load the file {
MagickWandGenesis();
wand = NewMagickWand();
fdesc = fdopen(fd, "rb");
stat = MagickReadImageFile(wand, fdesc);
if (stat == MagickFalse) {
printf("Image Magick can't read %s. It can usually read just about anything!\nIs this an image file? Are you sure?\n", argv[1]);
return 0;
}
// }}
g_life = (unsigned char*)malloc(sizeof(unsigned char) * landSize);
g_pixel = (unsigned char*)malloc(sizeof(unsigned char) * landSize);
memset(g_life, 0, landSize);
MagickResetIterator(wand);
while (MagickNextImage(wand) != MagickFalse);
// increase the contrast of the image a few times
for(ix = 0; ix < contrastRounds; ix++) {
MagickContrastImage(wand, MagickTrue);
}
//MagickWriteImage(wand, "/tmp/file.png");
MagickResizeImage(
wand,
g_width, g_height,
LanczosFilter,
1.0
);
MagickExportImagePixels(wand,
0, 0,
g_width, g_height,
"I",
CharPixel,
g_pixel
);
printf("reproduce:%d litter:%d die:%d move:%d (rad:%d) grow:%d (crit:%d)\n",
reproduce,
litterSize,
dieoff,
move,
moveradius,
growth,
critical
);
// We don't generate on screen at every step because that would be very tedious.
for(;;turn++) {
if(turn % viewEvery == 0) {
// seed if necessary
for(ix = population; ix < critical; ix++) {
for(;;) {
i = rand() % g_height;
j = rand() % g_width;
if(Life(i, j) == 0) {
*pLife(i, j) = MATURE;
break;
}
}
}
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
// This makes sure that if the user resizes less then
// the initial size, it won't screw up the generation into some
// garbled mess.
c_height = MIN(g_height, w.ws_row - 2);
c_width = MIN(g_width, w.ws_col);
}
population = 0;
for(ix = 0; ix < g_height; ix++) {
for(iy = 0; iy < g_width; iy++) {
if(turn % viewEvery == 0 && iy < c_width && ix < c_height) {
putchar(MAP[abs((int)Life(ix, iy) - 0 * (int)Pixel(ix, iy)) >> 3]);
}
maturity = Life(ix, iy);
if(maturity > 0) {
population ++;
// The chance of death is
//
// random number % the brightness value of the pixel being < the dieoff
// number.
if(1.05 * maturity >= Pixel(ix,iy) && (rand() % (Pixel(ix,iy) + 1)) < dieoff ) {
g_life[g_width * ix + iy] = 0;
// reproduces
} else {
if(maturity > MATURE && rand() % reproduce == 0) {
limI = MIN(ix + 2, g_height);
limJ = MIN(iy + 2, g_width);
for(i = MAX(ix - 2, 0); i < limI; i++){
for(j = MAX(iy - 2, 0); j < limJ; j++){
if(Life(i, j) == 0 && (rand() % litterSize == 0)) {
*pLife(i, j) = 1;
}
}
}
}
// randomly move to a new empty spot
if(rand() % move == 0) {
limI = MIN(ix + moveradius, g_height);
limJ = MIN(iy + moveradius, g_width);
for(iz = 0; iz < 7; iz++) {
i = ix + rand() % (moveradius * 2) - moveradius;
j = iy + rand() % (moveradius * 2) - moveradius;
i = MAX(i, 0);
j = MAX(j, 0);
i = MIN(i, g_height);
j = MIN(j, g_width);
if(Life(i, j) == 0) {
if(Pixel(i, j) > Pixel(ix, iy)) {
*pLife(i, j) = Life(ix, iy);
*pLife(ix, iy) = 0;
break;
}
}
}
}
// growing up
if(rand() % growth == 0) {
if(Life(ix, iy) < cutoff) {
pLife(ix, iy)[0]++;
}
}
}
}
}
if(turn % viewEvery == 0 && ix < c_height) {
putchar('\n');
}
}
// This returns everything to the top for another draw.
if(turn % viewEvery == 0) {
// This makes sure we don't peg the processor.
FD_ZERO(&fd_r);
FD_SET(0, &fd_r);
// This is our sleep value - at least 300
ts.tv_usec = 1000000 / fps;
select(1, &fd_r, NULL, NULL, &ts);
if(FD_ISSET(0, &fd_r)) {
return(0);
}
printf("\033[%dA", c_height);
}
}
free(g_life);
free(g_pixel);
return 0;
}