-
Notifications
You must be signed in to change notification settings - Fork 766
/
PSTN.cpp
328 lines (309 loc) · 10.1 KB
/
PSTN.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
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
#include "simulation/Elements.h"
//#TPT-Directive ElementClass Element_PSTN PT_PSTN 168
Element_PSTN::Element_PSTN()
{
Identifier = "DEFAULT_PT_PSTN";
Name = "PSTN";
Colour = PIXPACK(0xAA9999);
MenuVisible = 1;
MenuSection = SC_FORCE;
Enabled = 1;
Advection = 0.0f;
AirDrag = 0.00f * CFDS;
AirLoss = 0.90f;
Loss = 0.00f;
Collision = 0.0f;
Gravity = 0.0f;
Diffusion = 0.00f;
HotAir = 0.000f * CFDS;
Falldown = 0;
Flammable = 0;
Explosive = 0;
Meltable = 0;
Hardness = 0;
Weight = 100;
Temperature = 283.15f;
HeatConduct = 0;
Description = "Piston, extends and pushes particles.";
State = ST_SOLID;
Properties = TYPE_SOLID;
LowPressure = IPL;
LowPressureTransition = NT;
HighPressure = IPH;
HighPressureTransition = NT;
LowTemperature = ITL;
LowTemperatureTransition = NT;
HighTemperature = ITH;
HighTemperatureTransition = NT;
Update = &Element_PSTN::update;
Graphics = &Element_PSTN::graphics;
}
//#TPT-Directive ElementHeader Element_PSTN static int tempParts[XRES]
int Element_PSTN::tempParts[XRES];
#define PISTON_INACTIVE 0x00
#define PISTON_RETRACT 0x01
#define PISTON_EXTEND 0x02
#define MAX_FRAME 0x0F
#define DEFAULT_LIMIT 0x1F
#define DEFAULT_ARM_LIMIT 0xFF
//#TPT-Directive ElementHeader Element_PSTN static int update(UPDATE_FUNC_ARGS)
int Element_PSTN::update(UPDATE_FUNC_ARGS)
{
if(parts[i].life)
return 0;
int maxSize = parts[i].tmp ? parts[i].tmp : DEFAULT_LIMIT;
int armLimit = parts[i].tmp2 ? parts[i].tmp2 : DEFAULT_ARM_LIMIT;
int state = 0;
int r, nxx, nyy, nxi, nyi, rx, ry;
int directionX = 0, directionY = 0;
if (state == PISTON_INACTIVE) {
for (rx=-2; rx<3; rx++)
for (ry=-2; ry<3; ry++)
if (BOUNDS_CHECK && (rx || ry) && (!rx || !ry))
{
r = pmap[y+ry][x+rx];
if (!r)
continue;
if ((r&0xFF)==PT_SPRK && parts[r>>8].life==3) {
if(parts[r>>8].ctype == PT_PSCN)
state = PISTON_EXTEND;
else
state = PISTON_RETRACT;
}
}
}
if(state == PISTON_EXTEND || state == PISTON_RETRACT) {
for (rx=-1; rx<2; rx++)
for (ry=-1; ry<2; ry++)
if (BOUNDS_CHECK && (rx || ry) && (!rx || !ry))
{
r = pmap[y+ry][x+rx];
if (!r)
continue;
if ((r&0xFF) == PT_PSTN)
{
bool movedPiston = false;
bool foundEnd = false;
int pistonEndX, pistonEndY;
int pistonCount = -1;// number of PSTN particles minus 1
int newSpace = 0;
int armCount = 0;
directionX = rx;
directionY = ry;
for (nxx = 0, nyy = 0, nxi = directionX, nyi = directionY; ; nyy += nyi, nxx += nxi) {
if (!(x+nxx<XRES && y+nyy<YRES && x+nxx >= 0 && y+nyy >= 0)) {
break;
}
r = pmap[y+nyy][x+nxx];
if((r&0xFF)==PT_PSTN) {
if(parts[r>>8].life)
armCount++;
else if (armCount)
{
pistonEndX = x+nxx;
pistonEndY = y+nyy;
foundEnd = true;
break;
}
else
{
if (parts[r>>8].temp>283.15)
pistonCount += (int)((parts[r>>8].temp-268.15)/10);// how many tens of degrees above 0 C, rounded to nearest 10
else
pistonCount++;
}
} else {
pistonEndX = x+nxx;
pistonEndY = y+nyy;
foundEnd = true;
break;
}
}
if(foundEnd) {
if(state == PISTON_EXTEND) {
if(armCount+pistonCount > armLimit)
pistonCount = armLimit-armCount;
if(pistonCount > 0) {
newSpace = MoveStack(sim, pistonEndX, pistonEndY, directionX, directionY, maxSize, pistonCount, false, parts[i].ctype, true);
if(newSpace) {
//Create new piston section
for(int j = 0; j < newSpace; j++) {
int nr = sim->create_part(-3, pistonEndX+(nxi*j), pistonEndY+(nyi*j), PT_PSTN);
if (nr > -1) {
parts[nr].life = 1;
if (parts[i].dcolour)
{
int colour=parts[i].dcolour;
parts[nr].dcolour=(colour&0xFF000000)|std::max(colour&0xFF0000-0x3C0000,0)|std::max(colour&0xFF00-0x3C00,0)|std::max(colour&0xFF-0x3C,0);
}
}
}
movedPiston = true;
}
}
} else if(state == PISTON_RETRACT) {
if(pistonCount > armCount)
pistonCount = armCount;
if(armCount) {
MoveStack(sim, pistonEndX, pistonEndY, directionX, directionY, maxSize, pistonCount, true, parts[i].ctype, true);
movedPiston = true;
}
}
}
if (movedPiston)
return 0;
}
}
}
return 0;
}
//#TPT-Directive ElementHeader Element_PSTN static int CanMoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int maxSize, int amount, bool retract, int block)
int Element_PSTN::CanMoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int maxSize, int amount, bool retract, int block)
{
int posX, posY, r, spaces = 0, currentPos = 0;
if (amount <= 0)
return 0;
for(posX = stackX, posY = stackY; currentPos < maxSize + amount && currentPos < XRES-1; posX += directionX, posY += directionY) {
if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) {
break;
}
r = sim->pmap[posY][posX];
if (sim->IsWallBlocking(posX, posY, 0) || (block && (r&0xFF) == block))
return spaces;
if(!r) {
spaces++;
tempParts[currentPos++] = -1;
if(spaces >= amount)
break;
} else {
if(spaces < maxSize && currentPos < maxSize && (!retract || ((r&0xFF) == PT_FRME) && posX == stackX && posY == stackY))
tempParts[currentPos++] = r>>8;
else
return spaces;
}
}
if (spaces)
return currentPos;
else
return 0;
}
//#TPT-Directive ElementHeader Element_PSTN static int MoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int maxSize, int amount, bool retract, int block, bool sticky, int callDepth = 0)
int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int maxSize, int amount, bool retract, int block, bool sticky, int callDepth)
{
bool foundParts = false;
int posX, posY, r, spaces = 0, currentPos = 0;
r = sim->pmap[stackY][stackX];
if(!callDepth && (r&0xFF) == PT_FRME) {
int newY = !!directionX, newX = !!directionY;
int realDirectionX = retract?-directionX:directionX;
int realDirectionY = retract?-directionY:directionY;
int maxRight = MAX_FRAME, maxLeft = MAX_FRAME;
//check if we can push all the FRME
for(int c = retract; c < MAX_FRAME; c++) {
posY = stackY + (c*newY);
posX = stackX + (c*newX);
if (posX < XRES && posY < YRES && posX >= 0 && posY >= 0 && (sim->pmap[posY][posX]&0xFF) == PT_FRME) {
int val = CanMoveStack(sim, posX, posY, realDirectionX, realDirectionY, maxSize, amount, retract, block);
if(val < amount)
amount = val;
} else {
maxRight = c;
break;
}
}
for(int c = 1; c < MAX_FRAME; c++) {
posY = stackY - (c*newY);
posX = stackX - (c*newX);
if (posX < XRES && posY < YRES && posX >= 0 && posY >= 0 && (sim->pmap[posY][posX]&0xFF) == PT_FRME) {
int val = CanMoveStack(sim, posX, posY, realDirectionX, realDirectionY, maxSize, amount, retract, block);
if(val < amount)
amount = val;
} else {
maxLeft = c;
break;
}
}
//If the piston is pushing frame, iterate out from the centre to the edge and push everything resting on frame
for(int c = 1; c < maxRight; c++) {
posY = stackY + (c*newY);
posX = stackX + (c*newX);
MoveStack(sim, posX, posY, directionX, directionY, maxSize, amount, retract, block, !sim->parts[sim->pmap[posY][posX]>>8].tmp, 1);
}
for(int c = 1; c < maxLeft; c++) {
posY = stackY - (c*newY);
posX = stackX - (c*newX);
MoveStack(sim, posX, posY, directionX, directionY, maxSize, amount, retract, block, !sim->parts[sim->pmap[posY][posX]>>8].tmp, 1);
}
//Remove arm section if retracting with FRME
if (retract)
for(int j = 1; j <= amount; j++)
sim->kill_part(sim->pmap[stackY+(directionY*-j)][stackX+(directionX*-j)]>>8);
return MoveStack(sim, stackX, stackY, directionX, directionY, maxSize, amount, retract, block, !sim->parts[sim->pmap[stackY][stackX]>>8].tmp, 1);
}
if(retract){
//Remove arm section if retracting without FRME
if (!callDepth)
for(int j = 1; j <= amount; j++)
sim->kill_part(sim->pmap[stackY+(directionY*-j)][stackX+(directionX*-j)]>>8);
bool foundEnd = false;
for(posX = stackX, posY = stackY; currentPos < maxSize && currentPos < XRES-1; posX += directionX, posY += directionY) {
if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) {
break;
}
r = sim->pmap[posY][posX];
if(!r || (r&0xFF) == block || (!sticky && (r&0xFF) != PT_FRME)) {
break;
} else {
foundParts = true;
tempParts[currentPos++] = r>>8;
}
}
if(foundParts) {
//Move particles
for(int j = 0; j < currentPos; j++) {
int jP = tempParts[j];
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = 0;
sim->parts[jP].x += (float)((-directionX)*amount);
sim->parts[jP].y += (float)((-directionY)*amount);
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = sim->parts[jP].type|(jP<<8);
}
return amount;
}
if(!foundParts && foundEnd)
return amount;
} else {
currentPos = CanMoveStack(sim, stackX, stackY, directionX, directionY, maxSize, amount, retract, block);
if(currentPos){
//Move particles
int possibleMovement = 0;
for(int j = currentPos-1; j >= 0; j--) {
int jP = tempParts[j];
if(jP < 0) {
possibleMovement++;
continue;
}
if(!possibleMovement)
continue;
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = 0;
sim->parts[jP].x += (float)(directionX*possibleMovement);
sim->parts[jP].y += (float)(directionY*possibleMovement);
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = sim->parts[jP].type|(jP<<8);
}
return possibleMovement;
}
if(!foundParts && spaces)
return spaces;
}
return 0;
}
//#TPT-Directive ElementHeader Element_PSTN static int graphics(GRAPHICS_FUNC_ARGS)
int Element_PSTN::graphics(GRAPHICS_FUNC_ARGS)
{
if(cpart->life)
{
*colr -= 60;
*colg -= 60;
}
return 0;
}
Element_PSTN::~Element_PSTN() {}