-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdot.c
333 lines (304 loc) · 7.65 KB
/
dot.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "common.h"
#include "vector.h"
#include "random.h"
#include "elements.h"
#include "bubble.h"
#include "dot.h"
#include "menu.h"
#include "input.h"
#include "block.h"
#include "save.h"
const int Dot_LIMITS[] = {20000, 30000, 40000, Dot_MAX};
//int Part_limit = Part_LIMITS[0];
static Dot parts_2[Dot_MAX+5];
Dot* const Dot_0 = parts_2+5;
Dot* const Dot_EMPTY = Dot_0-5;
Dot* const Dot_BGFAN = Dot_0-4;
Dot* const Dot_WHEEL = Dot_0-3;
Dot* const Dot_BALL = Dot_0-2;
Dot* const Dot_BLOCK = Dot_0-1;
AUTORUN {
for (int i=1; i<=5; i++)
Dot_0[-i] = (Dot){.type = -i};
}
Dot* Dot_next = Dot_0;
Dot* Dot_at[HEIGHT][WIDTH];
Dot** const Dot_grid0 = &Dot_at[0][0];
static int Dot_counts[Elem_MAX];
int* Dot_updateCounts(void) {
for (Elem i=0; i<Elem_MAX; i++)
Dot_counts[i] = 0;
Dot_FOR (p) {
if (p->type > 0)
Dot_counts[p->type]++;
}
return Dot_counts;
}
bool Dot_limit1000(void) {
return Dot_next+1000 < Dot_0+Dot_limit;
}
// todo: make a create that takes a vector
Dot* Dot_create(real x, real y, Elem element) {
if (Dot_next >= Dot_0+Dot_limit)
return NULL;
// must not be more than 1px outside visible area
if (x<7 || x>=WIDTH-7 || y<7 || y>=HEIGHT-7)
return NULL;
*Dot_next = (Dot){
.pos={x,y},
.vel={0,0},
.type=element,
.charge=0,
.held=false,
};
Dot_toGrid(Dot_next);
return Dot_next++;
}
void Dot_remove(Dot* part) {
*Dot_pos2(part->pos) = Dot_EMPTY;
Dot_next--;
if (Dot_next != part) {
*part = *Dot_next;
*Dot_pos2(part->pos) = part->type==Elem_FAN ? Dot_BGFAN : part;
}
}
void Dot_swap(Dot* part1, Dot* part2) {
Dot* temp3 = *Dot_pos2(part1->pos);
*Dot_pos2(part1->pos) = *Dot_pos2(part2->pos);
*Dot_pos2(part2->pos) = temp3;
Point temp = part1->pos;
part1->pos = part2->pos;
part2->pos = temp;
}
void Dot_blow(Dot* part, Point airvel) {
airvel.xy *= 3.8/(Vec_fastDist(airvel)+1);
if (*Dot_pos(part->pos.x+airvel.x, part->pos.y)<=Dot_BGFAN)
part->pos.x += airvel.x;
if (*Dot_pos(part->pos.x, part->pos.y+airvel.y)<=Dot_BGFAN)
part->pos.y += airvel.y;
*Dot_pos2(part->pos) = part;
}
void Dot_shuffle(void) {
Dot_FOR (p) {
Dot* c = &Dot_0[rand() % (Dot_next-Dot_0)];
Dot temp = *p;
*p = *c;
*c = temp;
*Dot_pos2(p->pos) = p->type==Elem_FAN ? Dot_BGFAN : p;
*Dot_pos2(c->pos) = c->type==Elem_FAN ? Dot_BGFAN : c;
}
}
extern int wa;
void Dot_update(void) {
Dot_FOR (p) {
if (!Menu_cursorInMenu && wa==0) {
if (!p->held) {
if (Menu_dragStart) {
if (p->type == Elem_FAN)
continue;
Point d = (Point){.xy=Menu_pen.xy - p->pos.xy};
if (Vec_fastDist(d) < 4*Menu_penSize)
p->held = true;
}
} else if (Menu_dragging) {
p->vel.xy += (Menu_pen.xy - p->pos.xy)*0.1;
} else {
p->held = false;
}
}
Block* c = Block_at(p->pos.x, p->pos.y);
if (p->type != Elem_FAN)
*Dot_pos2(p->pos) = Dot_EMPTY;
switch (p->type) {
// todo: maybe put this in a real separately compiled file
#define Dot_KILL(...) { Dot_remove(p--); goto Dot_continue; }
#define UPDATE_PART 1
#include "elements/All.c"
#undef UPDATE_PART
#undef Dot_KILL
}
Dot_continue:;
}
// check parts that go off screen
Dot_FOR (p) {
if (Menu_edgeMode==0) { // void edge
if (p->pos.x<8||p->pos.x>=W+8||p->pos.y<8||p->pos.y>=H+8) {
Dot_remove(p--);
}
} else { //loop edge
if (p->pos.x<8) {
Dot** o = &Dot_pos3(p->pos, W, 0);
if (*o<=Dot_BGFAN && p->pos.y>=8 && p->pos.y<H+8) {
*Dot_pos2(p->pos) = Dot_EMPTY;
p->pos.x += W;
*o = p;
} else
Dot_remove(p--);
} else if (p->pos.x>=W+8) {
Dot** o = &Dot_pos3(p->pos, -W, 0);
if (*o<=Dot_BGFAN && p->pos.y>=8 && p->pos.y<H+8) {
*Dot_pos2(p->pos) = Dot_EMPTY;
p->pos.x -= W;
*o = p;
} else
Dot_remove(p--);
} else if (p->pos.y<8) {
Dot** o = &Dot_pos3(p->pos, 0, H);
if (*o<=Dot_BGFAN) {
*Dot_pos2(p->pos) = Dot_EMPTY;
p->pos.y += H;
*o = p;
} else
Dot_remove(p--);
} else if (p->pos.y>=H+8) {
Dot** o = &Dot_pos3(p->pos, 0, -H);
if (*o<=Dot_BGFAN) {
*Dot_pos2(p->pos) = Dot_EMPTY;
p->pos.y -= H;
*o = p;
} else
Dot_remove(p--);
}
}
}
}
// this is a very common pattern used by elements for explosions
void Dot_doRadius(axis x, axis y, axis radius, void (*func)(axis, axis, axis, axis)) {
axis n = atLeast(x-radius, 4);
axis z = atLeast(y-radius, 4);
axis v = atMost(x+radius, WIDTH-4-1);
axis r = atMost(y+radius, HEIGHT-4-1);
for (axis b=z;b<=r;b++)
for (axis e=n;e<=v;e++)
if ((e-x)*(e-x)+(b-y)*(b-y)<=radius*radius)
func(e, b, x, y);
}
bool Dot_checkPump(Dot* part, Dot* pump, int dir) {
if (pump->type == Elem_PUMP && !pump->Cpump.type) {
pump->Cpump.dir = ~dir;
pump->Cpump.amount = 1;
pump->Cpump.type = part->type;
return true;
}
return false;
}
void Dot_liquidUpdate(Dot* p, Block* c, real adv, real x1, real x2, real xr1, real yr1, real yr2, real frc) {
p->vel.x += adv*c->vel.x;
p->vel.y += adv*c->vel.y;
if (Dot_pos3(p->pos,0,1) != Dot_EMPTY) {
if (Dot_pos3(p->pos,-1,0) == Dot_EMPTY)
p->vel.x -= Random_2(x1, x2);
if (Dot_pos3(p->pos,1,0) == Dot_EMPTY)
p->vel.x += Random_2(x1, x2);
}
p->vel.x += Random_2(-xr1, xr1);
p->vel.y += Random_2(yr1, yr2);
p->vel.x *= frc;
p->vel.y *= frc;
Point airvel = c->vel;
airvel.xy += p->vel.xy;
Dot_blow(p, airvel);
}
// flood fill
void Dot_paint(axis x, axis y, Elem replace, Elem type, int charge) {
axis x1 = x;
while (1) {
Dot* f = *Dot_pos(x1,y);
if (f>=Dot_0 && f->type==replace) {
f->type = type;
f->charge = charge;
x1--;
} else
break;
}
x1++;
axis x2 = x+1;
while (1) {
Dot* f = *Dot_pos(x2,y);
if (f>=Dot_0 && f->type==replace) {
f->type = type;
f->charge = charge;
x2++;
} else
break;
}
x2--;
for (x=x1; x<=x2; x++) {
Dot* p = *Dot_pos(x,y-1);
if (p>=Dot_0 && p->type==replace)
Dot_paint(x,y-1,replace,type,charge);
p = *Dot_pos(x,y+1);
if (p>=Dot_0 && p->type==replace)
Dot_paint(x,y+1,replace,type,charge);
}
}
// get the particle at a random location
// within a 5x5 region centered on `pos`
Dot* Dot_rndNear5(Point pos) {
int x = Random_int(5)-2;
int y = Random_int(5)-2;
return Dot_pos3(pos, x, y);
}
Dot* Dot_rndNear(Point pos, axis diam) {
int x = Random_int(diam)-diam/2;
int y = Random_int(diam)-diam/2;
return Dot_pos3(pos, x, y);
}
void Dot_print(Dot* p) {
printf("%s\npos: %f,%f\nvel: %f,%f\ncharge: %d\n", ELEMENTS[p->type].name, (double)p->pos.x, (double)p->pos.y, (double)p->vel.x, (double)p->vel.y, p->charge);
}
void Dot_toGrid(Dot* p) {
*Dot_pos2(p->pos) = p;
}
Dot* Dot_dirNear(Point pos, char dir) {
return Dot_pos2(pos)[(Offset[]){-WIDTH,-1,1,WIDTH}[dir]];
}
Offset Dot_ofs8(char dir) {
return (Offset[]){1,-1,WIDTH,-WIDTH,WIDTH+1,WIDTH-1,-WIDTH+1,-WIDTH-1}[dir];
}
static bool onscreen(int x, int y){
return x>=8 && x<W+8 && y>=8 && y<H+8;
}
void Dot_save(SavePixel save[H][W]) {
Dot_FOR (p) {
axis x = p->pos.x;
axis y = p->pos.y;
if (onscreen(x,y)) {
x -= 8;
y -= 8;
Elem type = p->type;
if (type==Elem_FAN)
save[y][x].charge = (int)(64*Vec_angle(p->vel)/TAU);// does this need to be wrapped?
else if (type==Elem_FIREWORKS)
save[y][x].charge = p->charge%100;
else if (type==Elem_THUNDER) {//my addition
if (p->charge>=7000)
type = Elem_GLASS;
else if (p->charge>=6000) {
if (p->Cthunder2.type==6000>>2)
type = Elem_METAL;
else
type = Elem_MERCURY;
}
}
save[y][x].type = type;
}
}
}
void Dot_reset(void) {
for (axis y=0; y<HEIGHT; y++) {
for (axis x=0; x<WIDTH; x++) {
if (Block_at(x,y)->block==Block_BLOCK)
*Dot_pos(x,y) = Dot_BLOCK;
else
*Dot_pos(x,y) = Dot_EMPTY;
}
}
Dot_FOR (p) { //probably unnecessary
p->held = false;
}
Dot_next = Dot_0;
}