-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
263 lines (222 loc) · 6.38 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qdbmp.h"
#include "dynamicArray.h"
#include "constants.h"
typedef struct {
int x, y, orientation;
} Ant;
Array2D field;
Ant ant;
int assEnabled = FALSE;
int bmpEnabled = FALSE;
int stdoutEnabled = FALSE;
unsigned long maxSteps = 0;
unsigned long steps = 0;
//region in/output
int drawFieldToBmp() {
// get file name
char fileName[60] = "";
snprintf(fileName, sizeof fileName, "./images/%lu.bmp", steps);
//printf("Printing to file %s...\n", fileName);
// create bitmap
BMP *bmp = BMP_Create(field.sizeX * BMP_SCALE, field.sizeY * BMP_SCALE, 8);
BMP_SetPaletteColor(bmp, PALLETTE_BLACK, 0, 0, 0);
BMP_SetPaletteColor(bmp, PALLETTE_WHITE, 255, 255, 255);
BMP_SetPaletteColor(bmp, PALLETTE_ANT, 255, 0, 0);
// set values of bitmap
for (unsigned int y = 0; y < (unsigned int) field.sizeY; y++) {
for (unsigned int x = 0; x < (unsigned int) field.sizeX; x++) {
for (unsigned int subpixel_x = 0; subpixel_x < BMP_SCALE; subpixel_x++) {
for (unsigned int subpixel_y = 0; subpixel_y < BMP_SCALE; subpixel_y++) {
BMP_SetPixelIndex(bmp, x * BMP_SCALE + subpixel_x, y * BMP_SCALE + subpixel_y,
(unsigned char) (field.array[x][y] == BLACK ? PALLETTE_BLACK : PALLETTE_WHITE));
}
}
}
}
// write to file
BMP_WriteFile(bmp, fileName);
BMP_CHECK_ERROR(stdout, -1);
// free resources
BMP_Free(bmp);
}
void printAnt() {
switch (ant.orientation) {
case NORTH:
printf("N");
break;
case EAST:
printf("E");
break;
case SOUTH:
printf("S");
break;
case WEST:
printf("W");
break;
default:
printf('A');
break;
}
}
void printField() {
printf("Step %lu: X=%d Y=%d", steps, ant.x, ant.y);
if (bmpEnabled) printf("; BMP print enabled");
if (assEnabled) printf("; ASS enabled");
printf("; Field: \n");
for (int y = 0; y < field.sizeY; y++) {
for (int x = 0; x < field.sizeX; x++) {
if (ant.x == x && ant.y == y) {
printAnt();
continue;
}
if (field.array[x][y] == WHITE) {
printf("░");
} else {
printf("▓");
}
}
printf("\n");
}
}
int queryIsYes(char *msg, int defaultValue) {
printf("%s", msg);
if (defaultValue == TRUE) {
printf(" (Y/n) : ");
} else {
printf(" (y/N) : ");
}
char input;
fflush(stdin);
scanf(" %c", &input);
printf("\n");
if (defaultValue == TRUE) {
return ((input != 'n') && (input != 'N'));
} else {
return ((input == 'y') || (input == 'Y'));
}
}
//endregion
//region Ant stuff
void ass() {
if (ant.x < 0) { // left
if (ant.y < 0) { // top
ant.x++;
ant.y++;
} else if (ant.y > field.sizeY - 1) { // bottom
ant.x++;
ant.y--;
}
} else if (ant.x > field.sizeX - 1) { // right
if (ant.y < 0) { // top
ant.x--;
ant.y++;
} else if (ant.y > field.sizeY - 1) { // bottom
ant.x--;
ant.y--;
}
}
}
void changeOrientation() {
if (field.array[ant.x][ant.y] == WHITE) {
ant.orientation = (ant.orientation + 1) % 4;
} else {
ant.orientation = (ant.orientation - 1) % 4;
ant.orientation = (ant.orientation >= 0) ? ant.orientation : (4 + ant.orientation);
}
}
void moveAnt() {
switch (ant.orientation) {
case NORTH:
ant.y -= 1;
break;
case EAST:
ant.x += 1;
break;
case SOUTH:
ant.y += 1;
break;
case WEST:
ant.x -= 1;
break;
default:
printf("The ant entered the 3rd dimension o_O\n");
}
}
void keepInBounds() {
if (ant.x < 0) ant.x = 0;
if (ant.x >= field.sizeX) ant.x = (int) field.sizeX - 1;
if (ant.y < 0) ant.y = 0;
if (ant.y >= field.sizeY) ant.y = (int) field.sizeY - 1;
}
void langtonsAnt() {
changeOrientation();
field.array[ant.x][ant.y] = (field.array[ant.x][ant.y] == WHITE) ? BLACK : WHITE;
moveAnt();
if (assEnabled) ass();
keepInBounds();
steps++;
}
//endregion
void setGlobalVars() {
// Input field
int x = 0, y = 0;
while (x < 1 || y < 1) {
printf("Groesse des Feldes in X-Richtung: ");
scanf("%d", &x);
printf("Groesse des Feldes in Y-Richtung: ");
scanf("%d", &y);
}
array2dInit(&field, x, y);
// input ant
x = -1, y = -1;
while (x < 0 || y < 0 || x > field.sizeX || y > field.sizeY) {
printf("X-Position der Ameise: ");
scanf("%d", &x);
printf("Y-Position der Ameise: ");
scanf("%d", &y);
}
ant = (Ant) {
.x = x,
.y = y,
.orientation = NORTH
};
// settings
assEnabled = queryIsYes("Do not want to enable ASS (Anti Stuck System)?", TRUE);
bmpEnabled = queryIsYes("Do you want to create an image for every step in ./images/?", TRUE);
stdoutEnabled = queryIsYes("Do you want to print to console?", FALSE);
}
int main() {
setGlobalVars();
// if no output -> how many generations?
if (!stdoutEnabled) {
printf("How many steps do you want to do? (0=infinite): ");
fflush(stdin);
scanf("%lu", &maxSteps);
}
// print ffmpeg command
if (bmpEnabled) {
int pixelsX = (int) (BMP_SCALE * field.sizeX), pixelsY = (int) (BMP_SCALE * field.sizeY);
printf("ffmpeg.exe -r 20 -f image2 -s %dx%d -i ./images/%s.bmp -vcodec libx264 -pix_fmt yuv420p ./out.mp4\n",
pixelsX, pixelsY, "%d");
}
int exit = FALSE;
printf("Starting loop...\n");
while (!exit) {
if (stdoutEnabled) printField();
if (bmpEnabled) drawFieldToBmp();
if (stdoutEnabled) {
exit = queryIsYes("Do you want to stop?", FALSE);
} else {
if (steps % 100 == 0) {
printf("Reached step %lu\n", steps);
fflush(stdout);
}
exit = (maxSteps != 0) && (steps >= maxSteps);
}
langtonsAnt();
}
return 0;
}