Skip to content

Commit

Permalink
translate wall grids when shifting stamps by more than 4 pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Oct 5, 2017
1 parent 4ff0a9f commit 97c4123
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
43 changes: 33 additions & 10 deletions src/client/GameSave.cpp
Expand Up @@ -162,6 +162,7 @@ void GameSave::InitVars()
gravityMode = 0;
airMode = 0;
edgeMode = 0;
translated.x = translated.y = 0;
}

bool GameSave::Collapsed()
Expand Down Expand Up @@ -266,6 +267,7 @@ vector2d GameSave::Translate(vector2d translate)
Expand();
int nx, ny;
vector2d pos;
vector2d translateReal = translate;
float minx = 0, miny = 0, maxx = 0, maxy = 0;
// determine minimum and maximum position of all particles / signs
for (size_t i = 0; i < signs.size(); i++)
Expand Down Expand Up @@ -320,24 +322,25 @@ vector2d GameSave::Translate(vector2d translate)

// call Transform to do the transformation we wanted when calling this function
translate = v2d_add(translate, v2d_multiply_float(backCorrection, CELL));
Transform(m2d_identity, translate,
(blockWidth + backCorrection.x + frontCorrection.x) * CELL,
(blockHeight + backCorrection.y + frontCorrection.y) * CELL
Transform(m2d_identity, translate, translateReal,
(blockWidth + backCorrection.x + frontCorrection.x) * CELL,
(blockHeight + backCorrection.y + frontCorrection.y) * CELL
);

// return how much we corrected. This is used to offset the position of the current stamp
// otherwise it would attempt to recenter it with the current height
// otherwise it would attempt to recenter it with the current size
return v2d_add(v2d_multiply_float(backCorrection, -CELL), v2d_multiply_float(frontCorrection, CELL));
}

void GameSave::Transform(matrix2d transform, vector2d translate)
{
if(Collapsed())
if (Collapsed())
Expand();

int width = blockWidth*CELL, height = blockHeight*CELL, newWidth, newHeight;
vector2d tmp, ctl, cbr;
vector2d cornerso[4];
vector2d translateReal = translate;
// undo any translation caused by rotation
cornerso[0] = v2d_new(0,0);
cornerso[1] = v2d_new(width-1,0);
Expand All @@ -357,12 +360,15 @@ void GameSave::Transform(matrix2d transform, vector2d translate)
translate = v2d_sub(translate,tmp);
newWidth = floor(cbr.x+0.5f)-floor(ctl.x+0.5f)+1;
newHeight = floor(cbr.y+0.5f)-floor(ctl.y+0.5f)+1;
Transform(transform, translate, newWidth, newHeight);
Transform(transform, translate, translateReal, newWidth, newHeight);
}

void GameSave::Transform(matrix2d transform, vector2d translate, int newWidth, int newHeight)
// transform is a matrix describing how we want to rotate this save
// translate can vary depending on whether the save is bring rotated, or if a normal translate caused it to expand
// translateReal is the original amount we tried to translate, used to calculate wall shifting
void GameSave::Transform(matrix2d transform, vector2d translate, vector2d translateReal, int newWidth, int newHeight)
{
if(Collapsed())
if (Collapsed())
Expand();

if (newWidth>XRES) newWidth = XRES;
Expand Down Expand Up @@ -416,14 +422,30 @@ void GameSave::Transform(matrix2d transform, vector2d translate, int newWidth, i
particles[i].vx = vel.x;
particles[i].vy = vel.y;
}

// translate walls and other grid items when the stamp is shifted more than 4 pixels in any direction
int translateX = 0, translateY = 0;
if (translateReal.x > 0 && ((int)translated.x%CELL == 3
|| (translated.x < 0 && (int)translated.x%CELL == 0)))
translateX = CELL;
else if (translateReal.x < 0 && ((int)translated.x%CELL == -3
|| (translated.x > 0 && (int)translated.x%CELL == 0)))
translateX = -CELL;
if (translateReal.y > 0 && ((int)translated.y%CELL == 3
|| (translated.y < 0 && (int)translated.y%CELL == 0)))
translateY = CELL;
else if (translateReal.y < 0 && ((int)translated.y%CELL == -3
|| (translated.y > 0 && (int)translated.y%CELL == 0)))
translateY = -CELL;

for (y=0; y<blockHeight; y++)
for (x=0; x<blockWidth; x++)
{
pos = v2d_new(x*CELL+CELL*0.4f, y*CELL+CELL*0.4f);
pos = v2d_new(x*CELL+CELL*0.4f+translateX, y*CELL+CELL*0.4f+translateY);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = pos.x/CELL;
ny = pos.y/CELL;
if (nx<0 || nx>=newBlockWidth || ny<0 || ny>=newBlockHeight)
if (pos.x<0 || nx>=newBlockWidth || pos.y<0 || ny>=newBlockHeight)
continue;
if (blockMap[y][x])
{
Expand All @@ -441,6 +463,7 @@ void GameSave::Transform(matrix2d transform, vector2d translate, int newWidth, i
velocityYNew[ny][nx] = velocityY[y][x];
ambientHeatNew[ny][nx] = ambientHeat[y][x];
}
translated = v2d_add(m2d_multiply_v2d(transform, translated), translateReal);

for (int j = 0; j < blockHeight; j++)
{
Expand Down
4 changes: 3 additions & 1 deletion src/client/GameSave.h
Expand Up @@ -78,7 +78,7 @@ class GameSave
std::vector<char> Serialise();
vector2d Translate(vector2d translate);
void Transform(matrix2d transform, vector2d translate);
void Transform(matrix2d transform, vector2d translate, int newWidth, int newHeight);
void Transform(matrix2d transform, vector2d translate, vector2d translateReal, int newWidth, int newHeight);

void Expand();
void Collapse();
Expand All @@ -103,6 +103,8 @@ class GameSave
private:
bool expanded;
bool hasOriginalData;
// number of pixels translated. When translating CELL pixels, shift all CELL grids
vector2d translated;

std::vector<char> originalData;

Expand Down

0 comments on commit 97c4123

Please sign in to comment.