From 97c4123533e54059d2ab742b186e7fbfbdd260e6 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Wed, 4 Oct 2017 20:56:50 -0400 Subject: [PATCH] translate wall grids when shifting stamps by more than 4 pixels --- src/client/GameSave.cpp | 43 +++++++++++++++++++++++++++++++---------- src/client/GameSave.h | 4 +++- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 958fed17c..2bcc8ecf7 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -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=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++) { diff --git a/src/client/GameSave.h b/src/client/GameSave.h index bd719ceab..487111754 100644 --- a/src/client/GameSave.h +++ b/src/client/GameSave.h @@ -78,7 +78,7 @@ public: std::vector 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 @@ public: private: bool expanded; bool hasOriginalData; + // number of pixels translated. When translating CELL pixels, shift all CELL grids + vector2d translated; std::vector originalData;