Piston collapses spaces properly

This commit is contained in:
Simon Robertshaw
2013-01-26 17:14:44 +00:00
parent 7475a49e0b
commit 3e60181dbe

View File

@@ -49,12 +49,14 @@ Element_PSTN::Element_PSTN()
//#TPT-Directive ElementHeader Element_PSTN static int tempParts[128]; //#TPT-Directive ElementHeader Element_PSTN static int tempParts[128];
int Element_PSTN::tempParts[128]; int Element_PSTN::tempParts[128];
//#TPT-Directive ElementHeader Element_PSTN static int tempPartAmount[128];
int Element_PSTN::tempPartAmount[128];
#define PISTON_INACTIVE 0x00 #define PISTON_INACTIVE 0x00
#define PISTON_RETRACT 0x01 #define PISTON_RETRACT 0x01
#define PISTON_EXTEND 0x02 #define PISTON_EXTEND 0x02
#define MAX_FRAME 0x0F #define MAX_FRAME 0x0F
#define DEFAULT_LIMIT 0x1F #define DEFAULT_LIMIT 0x06
//#TPT-Directive ElementHeader Element_PSTN static int update(UPDATE_FUNC_ARGS) //#TPT-Directive ElementHeader Element_PSTN static int update(UPDATE_FUNC_ARGS)
int Element_PSTN::update(UPDATE_FUNC_ARGS) int Element_PSTN::update(UPDATE_FUNC_ARGS)
@@ -155,7 +157,7 @@ int Element_PSTN::update(UPDATE_FUNC_ARGS)
//#TPT-Directive ElementHeader Element_PSTN static int MoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int size, int amount, bool retract, int callDepth = 0) //#TPT-Directive ElementHeader Element_PSTN static int MoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int size, int amount, bool retract, int callDepth = 0)
int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int size, int amount, bool retract, int callDepth) int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int size, int amount, bool retract, int callDepth)
{ {
bool foundEnd = false, foundParts = false; bool foundParts = false;
int posX, posY, r, spaces = 0, currentPos = 0; int posX, posY, r, spaces = 0, currentPos = 0;
r = sim->pmap[stackY][stackX]; r = sim->pmap[stackY][stackX];
if(!callDepth && (r&0xFF) == PT_FRME) { if(!callDepth && (r&0xFF) == PT_FRME) {
@@ -191,6 +193,7 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
return biggestMove; return biggestMove;
} }
if(retract){ if(retract){
bool foundEnd = false;
//Warning: retraction does not scan to see if it has space //Warning: retraction does not scan to see if it has space
for(posX = stackX, posY = stackY; currentPos < size; posX += directionX, posY += directionY) { for(posX = stackX, posY = stackY; currentPos < size; posX += directionX, posY += directionY) {
if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) { if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) {
@@ -218,36 +221,42 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
if(!foundParts && foundEnd) if(!foundParts && foundEnd)
return amount; return amount;
} else { } else {
for(posX = stackX, posY = stackY; currentPos < size; posX += directionX, posY += directionY) { for(posX = stackX, posY = stackY; currentPos < size + amount; posX += directionX, posY += directionY) {
if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) { if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) {
break; break;
} }
r = sim->pmap[posY][posX]; r = sim->pmap[posY][posX];
if(!r) { if(!r) {
spaces++; spaces++;
foundEnd = true; tempParts[currentPos++] = 0;
if(spaces >= amount)
break;
} else { } else {
foundParts = true; foundParts = true;
if(currentPos < size)
tempParts[currentPos++] = r>>8; tempParts[currentPos++] = r>>8;
else
break;
} }
} }
if(amount > spaces) if(foundParts && spaces){
amount = spaces;
if(foundParts && foundEnd) {
//Move particles //Move particles
for(int j = 0; j < currentPos; j++) { int possibleMovement = 0;
for(int j = currentPos-1; j >= 0; j--) {
int jP = tempParts[j]; int jP = tempParts[j];
if(!jP) {
possibleMovement++;
continue;
}
if(!possibleMovement)
continue;
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = 0; 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].x += (float)(directionX*possibleMovement);
sim->parts[jP].y += (float)(directionY*amount); 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); sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = sim->parts[jP].type|(jP<<8);
} }
return amount; return possibleMovement;
} }
if(!foundParts && foundEnd) if(!foundParts && spaces)
return amount; return spaces;
} }
return 0; return 0;
} }