Permalink
Browse files

translate wall grids when shifting stamps by more than 4 pixels

  • Loading branch information...
jacob1 committed Oct 5, 2017
1 parent 4ff0a9f commit 97c4123533e54059d2ab742b186e7fbfbdd260e6
Showing with 36 additions and 11 deletions.
  1. +33 −10 src/client/GameSave.cpp
  2. +3 −1 src/client/GameSave.h
@@ -162,6 +162,7 @@ void GameSave::InitVars()
gravityMode = 0;
airMode = 0;
edgeMode = 0;
translated.x = translated.y = 0;
}
bool GameSave::Collapsed()
@@ -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++)
@@ -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);
@@ -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;
@@ -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])
{
@@ -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++)
{
@@ -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();
@@ -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;

0 comments on commit 97c4123

Please sign in to comment.