Skip to content

Commit

Permalink
fix FRME breaking when partially blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
krawthekrow authored and jacob1 committed Apr 18, 2017
1 parent 446d441 commit c2642f3
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/simulation/elements/PSTN.cpp
Expand Up @@ -45,6 +45,19 @@ Element_PSTN::Element_PSTN()
Graphics = &Element_PSTN::graphics;
}

//#TPT-Directive ElementHeader Element_PSTN struct StackData
struct Element_PSTN::StackData
{
int pushed;
int spaces;

StackData(int pushed, int spaces):
pushed(pushed),
spaces(spaces)
{
}
};

//#TPT-Directive ElementHeader Element_PSTN static int tempParts[XRES]
int Element_PSTN::tempParts[XRES];

Expand Down Expand Up @@ -175,20 +188,20 @@ int Element_PSTN::update(UPDATE_FUNC_ARGS)
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)
//#TPT-Directive ElementHeader Element_PSTN static StackData CanMoveStack(Simulation * sim, int stackX, int stackY, int directionX, int directionY, int maxSize, int amount, bool retract, int block)
Element_PSTN::StackData 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;
return StackData(0, 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;
return StackData(currentPos - spaces, spaces);
if (!r)
{
spaces++;
Expand All @@ -201,20 +214,17 @@ int Element_PSTN::CanMoveStack(Simulation * sim, int stackX, int stackY, int dir
if (currentPos - spaces < maxSize && (!retract || ((r&0xFF) == PT_FRME && posX == stackX && posY == stackY)))
tempParts[currentPos++] = r>>8;
else
return currentPos;
return StackData(currentPos - spaces, spaces);
}
}
if (spaces)
return currentPos;
else
return 0;
return StackData(currentPos - spaces, spaces);
}

//#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;
int posX, posY, r;
r = sim->pmap[stackY][stackX];
if(!callDepth && (r&0xFF) == PT_FRME) {
int newY = !!directionX, newX = !!directionY;
Expand All @@ -227,9 +237,9 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
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;
int spaces = CanMoveStack(sim, posX, posY, realDirectionX, realDirectionY, maxSize, amount, retract, block).spaces;
if(spaces < amount)
amount = spaces;
} else {
maxRight = c;
break;
Expand All @@ -239,9 +249,9 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
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;
int spaces = CanMoveStack(sim, posX, posY, realDirectionX, realDirectionY, maxSize, amount, retract, block).spaces;
if(spaces < amount)
amount = spaces;
} else {
maxLeft = c;
break;
Expand Down Expand Up @@ -272,6 +282,7 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
for(int j = 1; j <= amount; j++)
sim->kill_part(sim->pmap[stackY+(directionY*-j)][stackX+(directionX*-j)]>>8);
bool foundEnd = false;
int currentPos = 0;
for(posX = stackX, posY = stackY; currentPos < maxSize && currentPos < XRES-1; posX += directionX, posY += directionY) {
if (!(posX < XRES && posY < YRES && posX >= 0 && posY >= 0)) {
break;
Expand Down Expand Up @@ -300,7 +311,8 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
if(!foundParts && foundEnd)
return amount;
} else {
currentPos = CanMoveStack(sim, stackX, stackY, directionX, directionY, maxSize, amount, retract, block);
StackData stackData = CanMoveStack(sim, stackX, stackY, directionX, directionY, maxSize, amount, retract, block);
int currentPos = stackData.pushed + stackData.spaces;
if(currentPos){
//Move particles
int possibleMovement = 0;
Expand All @@ -321,8 +333,6 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
}
return possibleMovement;
}
if(!foundParts && spaces)
return spaces;
}
return 0;
}
Expand Down

0 comments on commit c2642f3

Please sign in to comment.