/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "tileset/TileInstanceGridDiff.h" #include #include TileInstanceGridDiff TileInstanceGridDiff_create(TileInstanceGrid * newTileGrid, TileInstanceGrid * oldTileGrid, bool compareTileIDs, bool compareMetadata) { TileInstanceGridDiff diff; diff.xorTileIDs = NULL; diff.metadataChangeCount = 0; diff.metadataChanges = NULL; if (oldTileGrid->size.x != newTileGrid->size.x || oldTileGrid->size.y != newTileGrid->size.y) { diff.oldTileGrid = TileInstanceGrid_copy(oldTileGrid); diff.newTileGrid = TileInstanceGrid_copy(newTileGrid); } else { diff.oldTileGrid.size = diff.newTileGrid.size = newTileGrid->size; diff.oldTileGrid.tileInstances = NULL; diff.newTileGrid.tileInstances = NULL; unsigned int width = newTileGrid->size.x; unsigned int height = newTileGrid->size.y; TileInstance * oldTileInstances = oldTileGrid->tileInstances; TileInstance * newTileInstances = newTileGrid->tileInstances; if (compareTileIDs) { diff.deltaMinY = UINT_MAX; for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { if (oldTileInstances[rowIndex * width + columnIndex].tileID != newTileInstances[rowIndex * width + columnIndex].tileID) { diff.deltaMinY = rowIndex; goto minYFound; } } } minYFound: if (diff.deltaMinY != UINT_MAX) { diff.deltaMaxY = UINT_MAX; for (unsigned int rowIndex = 0; rowIndex < height - diff.deltaMinY; rowIndex++) { unsigned int effectiveRowIndex = height - 1 - rowIndex; for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { if (oldTileInstances[effectiveRowIndex * width + columnIndex].tileID != newTileInstances[effectiveRowIndex * width + columnIndex].tileID) { diff.deltaMaxY = height - rowIndex; goto maxYFound; } } } maxYFound: assert(diff.deltaMaxY != UINT_MAX); diff.deltaMinX = UINT_MAX; for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { for (unsigned int rowIndex = diff.deltaMinY; rowIndex < diff.deltaMaxY; rowIndex++) { if (oldTileInstances[rowIndex * width + columnIndex].tileID != newTileInstances[rowIndex * width + columnIndex].tileID) { diff.deltaMinX = columnIndex; goto minXFound; } } } minXFound: assert(diff.deltaMinX != UINT_MAX); diff.deltaMaxX = UINT_MAX; for (unsigned int columnIndex = 0; columnIndex < width - diff.deltaMinX; columnIndex++) { for (unsigned int rowIndex = diff.deltaMinY; rowIndex < diff.deltaMaxY; rowIndex++) { if (oldTileInstances[rowIndex * width + (width - 1 - columnIndex)].tileID != newTileInstances[rowIndex * width + (width - 1 - columnIndex)].tileID) { diff.deltaMaxX = width - columnIndex; goto maxXFound; } } } maxXFound: assert(diff.deltaMaxX != UINT_MAX); unsigned int deltaWidth = diff.deltaMaxX - diff.deltaMinX; unsigned int deltaHeight = diff.deltaMaxY - diff.deltaMinY; diff.xorTileIDs = malloc(sizeof(diff.xorTileIDs[0]) * deltaWidth * deltaHeight); for (unsigned int rowIndex = 0; rowIndex < deltaHeight; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < deltaWidth; columnIndex++) { diff.xorTileIDs[rowIndex * deltaWidth + columnIndex] = oldTileInstances[(rowIndex + diff.deltaMinY) * width + diff.deltaMinX + columnIndex].tileID ^ newTileInstances[(rowIndex + diff.deltaMinY) * width + diff.deltaMinX + columnIndex].tileID; } } } } if (compareMetadata) { unsigned int metadataChangeAllocatedCount = 0; for (unsigned int tileIndex = 0; tileIndex < width * height; tileIndex++) { if (!isDataHashTableEqual(oldTileInstances[tileIndex].metadata, newTileInstances[tileIndex].metadata)) { if (diff.metadataChangeCount >= metadataChangeAllocatedCount) { metadataChangeAllocatedCount += metadataChangeAllocatedCount + (metadataChangeAllocatedCount == 0) * 16; diff.metadataChanges = realloc(diff.metadataChanges, metadataChangeAllocatedCount * sizeof(*diff.metadataChanges)); } diff.metadataChanges[diff.metadataChangeCount].index = tileIndex; if (oldTileInstances[tileIndex].metadata == NULL) { diff.metadataChanges[diff.metadataChangeCount].oldMetadata = NULL; } else { diff.metadataChanges[diff.metadataChangeCount].oldMetadata = hashCopy(oldTileInstances[tileIndex].metadata); } if (newTileInstances[tileIndex].metadata == NULL) { diff.metadataChanges[diff.metadataChangeCount].newMetadata = NULL; } else { diff.metadataChanges[diff.metadataChangeCount].newMetadata = hashCopy(newTileInstances[tileIndex].metadata); } diff.metadataChangeCount++; } } } } return diff; } void TileInstanceGridDiff_dispose(TileInstanceGridDiff * diff) { if (diff->oldTileGrid.tileInstances != NULL) { TileInstanceGrid_dispose(&diff->oldTileGrid); TileInstanceGrid_dispose(&diff->newTileGrid); } free(diff->xorTileIDs); for (unsigned int changeIndex = 0; changeIndex < diff->metadataChangeCount; changeIndex++) { if (diff->metadataChanges[changeIndex].oldMetadata != NULL) { hashDispose(diff->metadataChanges[changeIndex].oldMetadata); } if (diff->metadataChanges[changeIndex].newMetadata != NULL) { hashDispose(diff->metadataChanges[changeIndex].newMetadata); } } free(diff->metadataChanges); } #define applyImplementation(age_to) \ if (diff->oldTileGrid.size.x != diff->newTileGrid.size.x || diff->oldTileGrid.size.y != diff->newTileGrid.size.y) { \ TileInstanceGrid_dispose(target); \ *target = TileInstanceGrid_copy(&diff->age_to##TileGrid); \ \ } else { \ if (diff->xorTileIDs != NULL) { \ unsigned int deltaMinX = diff->deltaMinX; \ unsigned int deltaMinY = diff->deltaMinY; \ unsigned int deltaWidth = diff->deltaMaxX - deltaMinX; \ unsigned int deltaHeight = diff->deltaMaxY - deltaMinY; \ unsigned int width = target->size.x; \ for (unsigned int rowIndex = 0; rowIndex < deltaHeight; rowIndex++) { \ for (unsigned int columnIndex = 0; columnIndex < deltaWidth; columnIndex++) { \ target->tileInstances[(rowIndex + deltaMinY) * width + deltaMinX + columnIndex].tileID ^= diff->xorTileIDs[rowIndex * deltaWidth + columnIndex]; \ } \ } \ } \ for (unsigned int changeIndex = 0; changeIndex < diff->metadataChangeCount; changeIndex++) { \ if (target->tileInstances[diff->metadataChanges[changeIndex].index].metadata != NULL) { \ hashDispose(target->tileInstances[diff->metadataChanges[changeIndex].index].metadata); \ } \ if (diff->metadataChanges[changeIndex].age_to##Metadata == NULL) { \ target->tileInstances[diff->metadataChanges[changeIndex].index].metadata = NULL; \ } else { \ target->tileInstances[diff->metadataChanges[changeIndex].index].metadata = hashCopy(diff->metadataChanges[changeIndex].age_to##Metadata); \ } \ } \ } void TileInstanceGridDiff_revert(TileInstanceGridDiff * diff, TileInstanceGrid * target) { applyImplementation(old); } void TileInstanceGridDiff_apply(TileInstanceGridDiff * diff, TileInstanceGrid * target) { applyImplementation(new); }