Permalink
Browse files

save ambient heat in saves, if it is enabled

  • Loading branch information...
jacob1 committed Apr 25, 2017
1 parent 184777e commit c98f63bd89d8ecdae3cd3ee38f0f0a72d214622e
Showing with 62 additions and 13 deletions.
  1. +56 −10 src/client/GameSave.cpp
  2. +2 −0 src/client/GameSave.h
  3. +0 −3 src/gui/game/GameController.cpp
  4. +4 −0 src/simulation/Simulation.cpp
@@ -44,6 +44,7 @@ originalData(save.originalData)
std::copy(save.pressure[j], save.pressure[j]+blockWidth, pressure[j]);
std::copy(save.velocityX[j], save.velocityX[j]+blockWidth, velocityX[j]);
std::copy(save.velocityY[j], save.velocityY[j]+blockWidth, velocityY[j]);
std::copy(save.ambientHeat[j], save.ambientHeat[j]+blockWidth, ambientHeat[j]);
}
}
else
@@ -150,7 +151,9 @@ void GameSave::InitData()
pressure = NULL;
velocityX = NULL;
velocityY = NULL;
ambientHeat = NULL;
fromNewerVersion = false;
hasAmbientHeat = false;
}
void GameSave::InitVars()
@@ -248,6 +251,7 @@ void GameSave::setSize(int newWidth, int newHeight)
pressure = Allocate2DArray<float>(blockWidth, blockHeight, 0.0f);
velocityX = Allocate2DArray<float>(blockWidth, blockHeight, 0.0f);
velocityY = Allocate2DArray<float>(blockWidth, blockHeight, 0.0f);
ambientHeat = Allocate2DArray<float>(blockWidth, blockHeight, 0.0f);
}
std::vector<char> GameSave::Serialise()
@@ -296,18 +300,15 @@ void GameSave::Transform(matrix2d transform, vector2d translate)
newBlockHeight = newHeight/CELL;
unsigned char ** blockMapNew;
float ** fanVelXNew;
float ** fanVelYNew;
float ** pressureNew;
float ** velocityXNew;
float ** velocityYNew;
float **fanVelXNew, **fanVelYNew, **pressureNew, **velocityXNew, **velocityYNew, **ambientHeatNew;
blockMapNew = Allocate2DArray<unsigned char>(newBlockWidth, newBlockHeight, 0);
fanVelXNew = Allocate2DArray<float>(newBlockWidth, newBlockHeight, 0.0f);
fanVelYNew = Allocate2DArray<float>(newBlockWidth, newBlockHeight, 0.0f);
pressureNew = Allocate2DArray<float>(newBlockWidth, newBlockHeight, 0.0f);
velocityXNew = Allocate2DArray<float>(newBlockWidth, newBlockHeight, 0.0f);
velocityYNew = Allocate2DArray<float>(newBlockWidth, newBlockHeight, 0.0f);
ambientHeatNew = Allocate2DArray<float>(newBlockWidth, newBlockHeight, 0.0f);
// rotate and translate signs, parts, walls
for (size_t i = 0; i < signs.size(); i++)
@@ -366,6 +367,7 @@ void GameSave::Transform(matrix2d transform, vector2d translate)
pressureNew[ny][nx] = pressure[y][x];
velocityXNew[ny][nx] = velocityX[y][x];
velocityYNew[ny][nx] = velocityY[y][x];
ambientHeatNew[ny][nx] = ambientHeat[y][x];
}
for (int j = 0; j < blockHeight; j++)
@@ -376,6 +378,7 @@ void GameSave::Transform(matrix2d transform, vector2d translate)
delete[] pressure[j];
delete[] velocityX[j];
delete[] velocityY[j];
delete[] ambientHeat[j];
}
blockWidth = newBlockWidth;
@@ -387,13 +390,15 @@ void GameSave::Transform(matrix2d transform, vector2d translate)
delete[] pressure;
delete[] velocityX;
delete[] velocityY;
delete[] ambientHeat;
blockMap = blockMapNew;
fanVelX = fanVelXNew;
fanVelY = fanVelYNew;
pressure = pressureNew;
velocityX = velocityXNew;
velocityY = velocityYNew;
ambientHeat = ambientHeatNew;
}
void bson_error_handler(const char *err)
@@ -404,9 +409,9 @@ void bson_error_handler(const char *err)
void GameSave::readOPS(char * data, int dataLength)
{
unsigned char *inputData = (unsigned char*)data, *bsonData = NULL, *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *soapLinkData = NULL;
unsigned char *pressData = NULL, *vxData = NULL, *vyData = NULL;//, *ambientData = NULL;
unsigned char *pressData = NULL, *vxData = NULL, *vyData = NULL, *ambientData = NULL;
unsigned int inputDataLen = dataLength, bsonDataLen = 0, partsDataLen, partsPosDataLen, fanDataLen, wallDataLen, soapLinkDataLen;
unsigned int pressDataLen, vxDataLen, vyDataLen;
unsigned int pressDataLen, vxDataLen, vyDataLen, ambientDataLen;
unsigned partsCount = 0, *partsSimIndex = NULL;
int *freeIndices = NULL;
unsigned int blockX, blockY, blockW, blockH, fullX, fullY, fullW, fullH;
@@ -591,6 +596,17 @@ void GameSave::readOPS(char * data, int dataLength)
fprintf(stderr, "Invalid datatype of vy data: %d[%d] %d[%d] %d[%d]\n", bson_iterator_type(&iter), bson_iterator_type(&iter)==BSON_BINDATA, (unsigned char)bson_iterator_bin_type(&iter), ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER, bson_iterator_bin_len(&iter), bson_iterator_bin_len(&iter)>0);
}
}
else if (!strcmp(bson_iterator_key(&iter), "ambientMap"))
{
if(bson_iterator_type(&iter)==BSON_BINDATA && ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER && (ambientDataLen = bson_iterator_bin_len(&iter)) > 0)
{
ambientData = (unsigned char*)bson_iterator_bin_data(&iter);
}
else
{
fprintf(stderr, "Invalid datatype of vy data: %d[%d] %d[%d] %d[%d]\n", bson_iterator_type(&iter), bson_iterator_type(&iter)==BSON_BINDATA, (unsigned char)bson_iterator_bin_type(&iter), ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER, bson_iterator_bin_len(&iter), bson_iterator_bin_len(&iter)>0);
}
}
else if(strcmp(bson_iterator_key(&iter), "fanMap")==0)
{
if(bson_iterator_type(&iter)==BSON_BINDATA && ((unsigned char)bson_iterator_bin_type(&iter))==BSON_BIN_USER && (fanDataLen = bson_iterator_bin_len(&iter)) > 0)
@@ -884,6 +900,27 @@ void GameSave::readOPS(char * data, int dataLength)
}
}
//Read ambient data
if (ambientData)
{
unsigned int i = 0, tempTemp;
if (blockW * blockH > ambientDataLen)
{
fprintf(stderr, "Not enough ambient data\n");
goto fail;
}
for (unsigned int x = 0; x < blockW; x++)
{
for (unsigned int y = 0; y < blockH; y++)
{
tempTemp = ambientData[i++];
tempTemp |= (((unsigned)ambientData[i++]) << 8);
ambientHeat[blockY+y][blockX+x] = tempTemp;
}
}
hasAmbientHeat = true;
}
//Read particle data
if (partsData && partsPosData)
{
@@ -1909,12 +1946,12 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
{
//Particle *particles = sim->parts;
unsigned char *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *finalData = NULL, *outputData = NULL, *soapLinkData = NULL;
unsigned char *pressData = NULL, *vxData = NULL, *vyData = NULL;//, *ambientData = NULL;
unsigned char *pressData = NULL, *vxData = NULL, *vyData = NULL, *ambientData = NULL;
unsigned *partsPosLink = NULL, *partsPosFirstMap = NULL, *partsPosCount = NULL, *partsPosLastMap = NULL;
unsigned partsCount = 0, *partsSaveIndex = NULL;
unsigned *elementCount = new unsigned[PT_NUM];
unsigned int partsDataLen, partsPosDataLen, fanDataLen, wallDataLen,finalDataLen, outputDataLen, soapLinkDataLen;
unsigned int pressDataLen = 0, vxDataLen = 0, vyDataLen = 0;//, ambientDataLen = 0;
unsigned int pressDataLen = 0, vxDataLen = 0, vyDataLen = 0, ambientDataLen = 0;
int blockX, blockY, blockW, blockH, fullX, fullY, fullW, fullH;
int x, y, i, wallDataFound = 0;
int posCount, signsCount;
@@ -1948,7 +1985,8 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
pressData = (unsigned char*)malloc((blockW*blockH)*2);
vxData = (unsigned char*)malloc((blockW*blockH)*2);
vyData = (unsigned char*)malloc((blockW*blockH)*2);
if (!wallData || !fanData || !pressData || !vxData || !vyData)
ambientData = (unsigned char*)malloc((blockW*blockH)*2);
if (!wallData || !fanData || !pressData || !vxData || !vyData || !ambientData)
{
puts("Save Error, out of memory\n");
outputData = NULL;
@@ -1973,6 +2011,10 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
vyData[vyDataLen++] = (unsigned char)((int)(velY*128)&0xFF);
vyData[vyDataLen++] = (unsigned char)((int)(velY*128)>>8);
int tempTemp = (int)(ambientHeat[y][x]+0.5f);
ambientData[ambientDataLen++] = tempTemp;
ambientData[ambientDataLen++] = tempTemp >> 8;
if(blockMap[y][x] && !wallDataFound)
wallDataFound = 1;
if(blockMap[y][x]==WL_FAN)
@@ -2317,6 +2359,8 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
bson_append_binary(&b, "vxMap", (char)BSON_BIN_USER, (const char*)vxData, vxDataLen);
if (vyData)
bson_append_binary(&b, "vyMap", (char)BSON_BIN_USER, (const char*)vyData, vyDataLen);
if (ambientData && this->aheatEnable)
bson_append_binary(&b, "ambientMap", (char)BSON_BIN_USER, (const char*)ambientData, ambientDataLen);
if (soapLinkData)
bson_append_binary(&b, "soapLinks", BSON_BIN_USER, (const char *)soapLinkData, soapLinkDataLen);
if (partsData && palette.size())
@@ -2397,6 +2441,7 @@ char * GameSave::serialiseOPS(unsigned int & dataLength)
free(pressData);
free(vxData);
free(vyData);
free(ambientData);
free(fanData);
delete[] elementCount;
free(partsSaveIndex);
@@ -2436,6 +2481,7 @@ void GameSave::dealloc()
Deallocate2DArray<float>(&pressure, blockHeight);
Deallocate2DArray<float>(&velocityX, blockHeight);
Deallocate2DArray<float>(&velocityY, blockHeight);
Deallocate2DArray<float>(&ambientHeat, blockHeight);
}
GameSave::~GameSave()
@@ -30,6 +30,7 @@ class GameSave
int blockWidth, blockHeight;
bool fromNewerVersion;
bool hasAmbientHeat;
//Simulation data
//int ** particleMap;
@@ -41,6 +42,7 @@ class GameSave
float ** pressure;
float ** velocityX;
float ** velocityY;
float ** ambientHeat;
//Simulation Options
bool waterEEnabled;
@@ -1175,7 +1175,6 @@ void GameController::OpenLocalSaveWindow(bool asCurrent)
}
else
{
sim->SaveSimOptions(gameSave);
gameSave->paused = gameModel->GetPaused();
SaveFile tempSave("");
@@ -1384,7 +1383,6 @@ void GameController::OpenSaveWindow()
}
else
{
sim->SaveSimOptions(gameSave);
gameSave->paused = gameModel->GetPaused();
if(gameModel->GetSave())
@@ -1433,7 +1431,6 @@ void GameController::SaveAsCurrent()
else
{
gameSave->paused = gameModel->GetPaused();
sim->SaveSimOptions(gameSave);
if(gameModel->GetSave())
{
@@ -202,6 +202,8 @@ int Simulation::Load(int fullX, int fullY, GameSave * save)
pv[saveBlockY+blockY][saveBlockX+blockX] = save->pressure[saveBlockY][saveBlockX];
vx[saveBlockY+blockY][saveBlockX+blockX] = save->velocityX[saveBlockY][saveBlockX];
vy[saveBlockY+blockY][saveBlockX+blockX] = save->velocityY[saveBlockY][saveBlockX];
if (save->hasAmbientHeat)
hv[saveBlockY+blockY][saveBlockX+blockX] = save->ambientHeat[saveBlockY][saveBlockX];
}
}
@@ -302,9 +304,11 @@ GameSave * Simulation::Save(int fullX, int fullY, int fullX2, int fullY2)
newSave->pressure[saveBlockY][saveBlockX] = pv[saveBlockY+blockY][saveBlockX+blockX];
newSave->velocityX[saveBlockY][saveBlockX] = vx[saveBlockY+blockY][saveBlockX+blockX];
newSave->velocityY[saveBlockY][saveBlockX] = vy[saveBlockY+blockY][saveBlockX+blockX];
newSave->ambientHeat[saveBlockY][saveBlockX] = hv[saveBlockY+blockY][saveBlockX+blockX];
}
}
SaveSimOptions(newSave);
return newSave;
}

0 comments on commit c98f63b

Please sign in to comment.