-
Notifications
You must be signed in to change notification settings - Fork 1
/
world.c
264 lines (248 loc) · 5.69 KB
/
world.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
#include "world.h"
#include "null.h"
void SetCellUnwrapped(World *const w, int cellX, int cellY, bool setTo)
{
bool* buf;
if(w->writeBuf == RED)
{
buf = w->redBuffer;
}
else if(w->writeBuf == BLUE)
{
buf = w->blueBuffer;
}
if(((cellX >= 0) && (cellX < w->width))
&& ((cellY >= 0) && (cellY < w->height)))
{
//Packed array element retrieval.
buf[(cellY*w->width)+cellX] = setTo;
}
}
int WrapX(World *const w, int cellX)
{
/*Coordinate shift for world wrapping functionality.*/
if(cellX < 0)
{
//Go over left border
cellX = w->width + cellX;
}
else if(cellX >= w->width)
{
//Go over right border
cellX = cellX - w->width;
}
return cellX;
}
int WrapY(World *const w, int cellY)
{
if(cellY < 0)
{
//Go over top border.
cellY = w->height + cellY;
}
else if(cellY >= w->height)
{
//Go over right border
cellY = cellY - w->height;
}
return cellY;
};
void SetCell(World *const w, int cellX, int cellY, bool setTo)
{
SetCellUnwrapped(w, WrapX(w, cellX), WrapY(w, cellY), setTo);
}
void UpdateCell(World *const w, unsigned int cellX, unsigned int cellY)
{
unsigned char count = 0;
//Set up the bounds for our search, ensuring that they are
//within the array.
int fromX = cellX - 1;
//Commented-out functionality is pre world-wrapping
//if(fromX < 0) {
// fromX = 0;
//}
int toX = cellX + 1;
//if(toX >= w->width) {
// toX = w->width-1;
//}
int fromY = cellY - 1;
//if(fromY < 0) {
// fromY = 0;
//}
int toY = cellY + 1;
//if(toY >= w->height) {
// toY = w->height-1;
//}
bool ourCell = false; //TODO: Should it default false?
//Search for adjacent tiles.
for(int itY = fromY; itY <= toY; ++itY)
{
for(int itX = fromX; itX <= toX; ++itX)
{
//Is this just our cell?
if((itX == cellX) && (itY == cellY))
{
ourCell = GetCell(w, itX, itY);
}
else if(GetCell(w, itX, itY) == true)
{
++count;
}
}
}
if((count < 2) && (ourCell == true))
{
//Death by underpopulation
SetCell(w, cellX, cellY, false);
}
else if((count > 3) && (ourCell == true))
{
//Death by overcrowding
SetCell(w, cellX, cellY, false);
}
//Two neighbors on a live cell does nothing.
//Three on an empty cell creates a live cell (reproduction):
else if((count == 3) && (ourCell == false))
{
SetCell(w, cellX, cellY, true);
}
};
void FlipBuffers(World *const w)
{
if(w->writeBuf == RED)
{
w->writeBuf = BLUE;
memcpy(w->blueBuffer, w->redBuffer,
(w->width * w->height) * sizeof(bool));
}
if(w->writeBuf == BLUE)
{
w->writeBuf = RED;
memcpy(w->redBuffer, w->blueBuffer,
(w->width * w->height) * sizeof(bool));
}
}
void Update(World *const w)
{
for(int y = 0; y < w->height; ++y)
{
for(int x = 0; x < w->width; ++x)
{
UpdateCell(w, x, y);
}
}
FlipBuffers(w);
}
/*
* First int pair is new size, second is
* offset by which we blit the old map
* onto the new one.
*/
void ResizeWorld(World *const w, unsigned int width, unsigned int height,
int offX, int offY)
{
bool* newBlue = calloc( width * height, sizeof(bool) );
bool* newRed = calloc( width * height, sizeof(bool) );
if((newBlue != 0) && (newRed != 0))
{
//Iterate over every element in the old world
for(int x = 0; x < w->width; ++x)
{
for(int y = 0; y < w->height; ++y)
{
int insertX = x + offX;
int insertY = y + offY;
//Copy over the old world to the new
if(((insertX < width) && (insertX >= 0))
&& ((insertY < height) && (insertY >= 0 )))
{
//printf("X: %d \n", insertX);
//printf("Y: %d \n", insertY);
newBlue[(insertY*width)+insertX] =
w->blueBuffer[(y * (w->width))+x];
newRed[(insertY*width)+insertX] =
w->redBuffer[(y * (w->width))+x];
}
}
}
//Set up the world's properties, free old buffers.
free(w->redBuffer);
free(w->blueBuffer);
w->redBuffer = newRed;
w->blueBuffer = newBlue;
w->width = width;
w->height = height;
++(w->resizeRevision);
}
}
void FlipCell(World *const w, int cellX, int cellY)
{
SetCell(w, cellX, cellY, !(GetCell(w, cellX, cellY)));
}
void InitializeWorld(World *const w, unsigned int width, unsigned int height)
{
w->writeBuf = RED;
w->redBuffer = calloc(width*height, sizeof(bool));
w->blueBuffer = calloc(width*height, sizeof(bool));
w->width = width;
w->height = height;
w->resizeRevision = 0;
}
//Basically a convenience function over InitializeWorld.
World* MakeWorld(unsigned int width, unsigned int height)
{
World* w = malloc(sizeof(World));
InitializeWorld(w, width, height);
return w;
}
//Buffer, width, height, positionx, positiony
bool GetCellFromBuffer(bool* buf, unsigned int width, unsigned int height,
int posX, int posY)
{
if(((posX >= 0) && (posX < width))
&& ((posY >= 0) && (posY < height)))
{
//Packed array element retrieval.
return buf[(posY*width)+posX];
}
return -1;
};
/* For rendering and other such output functions.
* Wraps/hides buffering and such.
* Gets from the non-writing buffer.
* */
bool GetCellUnwrapped(World *const w, int x, int y)
{
//Bounds checking
if((x < 0) || (x >= w->width))
{
return -1;
}
if((y < 0) || (y >= w->height))
{
return -1;
}
if(w->writeBuf == RED)
{
return GetCellFromBuffer(w->blueBuffer,
w->width, w->height, x, y);
}
else if(w->writeBuf == BLUE)
{
return GetCellFromBuffer(w->redBuffer,
w->width, w->height, x, y);
}
else
{
return -1;
}
};
bool GetCell(World *const w, int x, int y)
{
return GetCellUnwrapped(w, WrapX(w, x), WrapY(w, y));
}
void DestroyWorld(World *const w)
{
free(w->redBuffer);
free(w->blueBuffer);
}