/* Copyright (c) 2024 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/TileAdjacencyBehaviorSet.h" #include "utilities/IOUtilities.h" #include struct TileAdjacencyBehaviorLookupEntry { unsigned int behaviorCount; TileAdjacencyBehavior ** behaviors; }; TileAdjacencyBehaviorSet * TileAdjacencyBehaviorSet_create(TileAdjacencyBehaviorSetID identifier, const char * name, unsigned int behaviorCount, TileAdjacencyBehavior * behaviors, bool copy) { TileAdjacencyBehaviorSet * behaviorSet = malloc(sizeof(*behaviorSet)); behaviorSet->identifier = identifier; behaviorSet->name = strdup_nullSafe(name); behaviorSet->behaviorCount = behaviorCount; if (copy) { behaviorSet->behaviors = malloc(behaviorCount * sizeof(*behaviors)); for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorCount; behaviorIndex++) { behaviorSet->behaviors[behaviorIndex] = TileAdjacencyBehavior_copy(&behaviors[behaviorIndex]); } } else { behaviorSet->behaviors = memdup(behaviors, behaviorCount * sizeof(*behaviorSet->behaviors)); } behaviorSet->behaviorLookup = HashTable_create(sizeof(struct TileAdjacencyBehaviorLookupEntry)); behaviorSet->lookupDirty = true; return behaviorSet; } TileAdjacencyBehaviorSet * TileAdjacencyBehaviorSet_copy(TileAdjacencyBehaviorSet * behaviorSet) { return TileAdjacencyBehaviorSet_create(behaviorSet->identifier, behaviorSet->name, behaviorSet->behaviorCount, behaviorSet->behaviors, true); } static bool freeBehaviorLookup(HashTable * hashTable, HashTable_key key, void * value, void * context) { struct TileAdjacencyBehaviorLookupEntry * entry = value; free(entry->behaviors); return true; } void TileAdjacencyBehaviorSet_dispose(TileAdjacencyBehaviorSet * behaviorSet) { free(behaviorSet->name); for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorSet->behaviorCount; behaviorIndex++) { TileAdjacencyBehavior_dispose(&behaviorSet->behaviors[behaviorIndex]); } free(behaviorSet->behaviors); HashTable_foreach(behaviorSet->behaviorLookup, freeBehaviorLookup, NULL); HashTable_dispose(behaviorSet->behaviorLookup); free(behaviorSet); } void TileAdjacencyBehaviorSet_serializeMinimal(TileAdjacencyBehaviorSet * behaviorSet, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(writeUInt32, context, "id", behaviorSet->identifier); call_virtual(writeString, context, "name", behaviorSet->name); call_virtual(writeUInt16, context, "behavior_format_version", TILE_ADJACENCY_FORMAT_VERSION); call_virtual(beginArray, context, "behaviors"); for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorSet->behaviorCount; behaviorIndex++) { call_virtual(beginStructure, context, NULL); TileAdjacencyBehavior_serializeMinimal(&behaviorSet->behaviors[behaviorIndex], context); call_virtual(endStructure, context); } call_virtual(endArray, context); } void TileAdjacencyBehaviorSet_serialize(TileAdjacencyBehaviorSet * behaviorSet, compat_type(SerializationContext *) serializationContext) { serialize_implementation_v2(TileAdjacencyBehaviorSet, behaviorSet, TILE_ADJACENCY_BEHAVIOR_SET); } TileAdjacencyBehaviorSet * TileAdjacencyBehaviorSet_deserializeMinimal(compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { DeserializationContext * context = deserializationContext; if (context->status != SERIALIZATION_ERROR_OK || formatVersion > TILE_ADJACENCY_BEHAVIOR_SET_FORMAT_VERSION) { return NULL; } TileAdjacencyBehaviorSet * behaviorSet = TileAdjacencyBehaviorSet_create(0, NULL, 0, NULL, false); behaviorSet->identifier = call_virtual(readUInt32, context, "id"); behaviorSet->name = strdup_nullSafe(call_virtual(readString, context, "name")); uint16_t behaviorFormatVersion = call_virtual(readUInt16, context, "behavior_format_version"); behaviorSet->behaviors = NULL; behaviorSet->behaviorCount = 0; unsigned int behaviorCount = call_virtual(beginArray, context, "behaviors"); if (behaviorCount > 0) { if (behaviorCount > TILE_ADJACENCY_BEHAVIOR_SET_BEHAVIOR_COUNT_MAX) { TileAdjacencyBehaviorSet_dispose(behaviorSet); return NULL; } behaviorSet->behaviors = malloc(behaviorCount * sizeof(*behaviorSet->behaviors)); for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorCount; behaviorIndex++) { call_virtual(beginStructure, context, NULL); if (!TileAdjacencyBehavior_deserializeMinimal(&behaviorSet->behaviors[behaviorIndex], context, behaviorFormatVersion)) { break; } behaviorSet->behaviorCount++; call_virtual(endStructure, context); } } call_virtual(endArray, context); if (context->status != SERIALIZATION_ERROR_OK) { TileAdjacencyBehaviorSet_dispose(behaviorSet); return NULL; } return behaviorSet; } TileAdjacencyBehaviorSet * TileAdjacencyBehaviorSet_deserialize(compat_type(DeserializationContext *) deserializationContext) { deserialize_implementation_v2(TileAdjacencyBehaviorSet, TILE_ADJACENCY_BEHAVIOR_SET); } void TileAdjacencyBehaviorSet_addBehavior(TileAdjacencyBehaviorSet * behaviorSet, TileAdjacencyBehavior behavior) { TileAdjacencyBehaviorSet_insertBehavior(behaviorSet, behaviorSet->behaviorCount, behavior); } void TileAdjacencyBehaviorSet_insertBehavior(TileAdjacencyBehaviorSet * behaviorSet, unsigned int behaviorIndex, TileAdjacencyBehavior behavior) { assert(behaviorIndex <= behaviorSet->behaviorCount); behaviorSet->behaviors = realloc(behaviorSet->behaviors, (behaviorSet->behaviorCount + 1) * sizeof(*behaviorSet->behaviors)); for (unsigned int behaviorIndex2 = behaviorSet->behaviorCount; behaviorIndex2 > behaviorIndex; behaviorIndex2--) { behaviorSet->behaviors[behaviorIndex2] = behaviorSet->behaviors[behaviorIndex2 - 1]; } behaviorSet->behaviors[behaviorIndex] = behavior; behaviorSet->behaviorCount++; behaviorSet->lookupDirty = true; } void TileAdjacencyBehaviorSet_removeBehaviorAtIndex(TileAdjacencyBehaviorSet * behaviorSet, unsigned int behaviorIndex) { if (behaviorIndex < behaviorSet->behaviorCount) { TileAdjacencyBehavior_dispose(&behaviorSet->behaviors[behaviorIndex]); behaviorSet->behaviorCount--; for (; behaviorIndex < behaviorSet->behaviorCount; behaviorIndex++) { behaviorSet->behaviors[behaviorIndex] = behaviorSet->behaviors[behaviorIndex + 1]; } behaviorSet->lookupDirty = true; } } void TileAdjacencyBehaviorSet_reorderBehavior(TileAdjacencyBehaviorSet * behaviorSet, unsigned int fromBehaviorIndex, unsigned int toBehaviorIndex) { assert(fromBehaviorIndex < behaviorSet->behaviorCount); assert(toBehaviorIndex < behaviorSet->behaviorCount); TileAdjacencyBehavior swap = behaviorSet->behaviors[fromBehaviorIndex]; int direction = (toBehaviorIndex > fromBehaviorIndex) * 2 - 1; for (unsigned int swapIndex = fromBehaviorIndex; swapIndex != toBehaviorIndex; swapIndex += direction) { behaviorSet->behaviors[swapIndex] = behaviorSet->behaviors[swapIndex + direction]; } behaviorSet->behaviors[toBehaviorIndex] = swap; behaviorSet->lookupDirty = true; } static void updateBehaviorLookup(TileAdjacencyBehaviorSet * behaviorSet) { HashTable_foreach(behaviorSet->behaviorLookup, freeBehaviorLookup, NULL); HashTable_deleteAll(behaviorSet->behaviorLookup); for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorSet->behaviorCount; behaviorIndex++) { struct TileAdjacencyBehaviorLookupEntry * entry = HashTable_get(behaviorSet->behaviorLookup, HashTable_uint32Key(behaviorSet->behaviors[behaviorIndex].ownTileID)); if (entry == NULL) { struct TileAdjacencyBehaviorLookupEntry newEntry = {1, malloc(sizeof(*newEntry.behaviors))}; newEntry.behaviors[0] = &behaviorSet->behaviors[behaviorIndex]; HashTable_set(behaviorSet->behaviorLookup, HashTable_uint32Key(behaviorSet->behaviors[behaviorIndex].ownTileID), &newEntry); } else { entry->behaviors = realloc(entry->behaviors, (entry->behaviorCount + 1) * sizeof(*entry->behaviors)); entry->behaviors[entry->behaviorCount++] = &behaviorSet->behaviors[behaviorIndex]; } } behaviorSet->lookupDirty = false; } void TileAdjacencyBehaviorSet_resolveImageIndexes(TileAdjacencyBehaviorSet * behaviorSet, ImageCollection * imageCollection) { for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorSet->behaviorCount; behaviorIndex++) { for (unsigned int overlayIndex = 0; overlayIndex < behaviorSet->behaviors[behaviorIndex].overlayCount; overlayIndex++) { behaviorSet->behaviors[behaviorIndex].overlays[overlayIndex].imageIndex = ImageCollection_getImageIndex(imageCollection, behaviorSet->behaviors[behaviorIndex].overlays[overlayIndex].imageID); } } } void TileAdjacencyBehaviorSet_calculateTotalBounds(TileAdjacencyBehaviorSet * behaviorSet, ImageCollection * imageCollection) { for (unsigned int behaviorIndex = 0; behaviorIndex < behaviorSet->behaviorCount; behaviorIndex++) { TileAdjacencyBehavior_calculateTotalBounds(&behaviorSet->behaviors[behaviorIndex], imageCollection); } } unsigned int TileAdjacencyBehaviorSet_getApplicableBehaviors(TileAdjacencyBehaviorSet * behaviorSet, Vector2i position, TileID tileID, TileQueryCallback queryCallback, void * callbackContext, unsigned int behaviorCountMax, TileAdjacencyBehavior ** outBehaviors) { unsigned int applicableBehaviorCount = 0; if (behaviorSet->lookupDirty) { updateBehaviorLookup(behaviorSet); } struct TileAdjacencyBehaviorLookupEntry * entry = HashTable_get(behaviorSet->behaviorLookup, HashTable_uint32Key(tileID)); if (entry != NULL) { for (unsigned int behaviorIndex = 0; behaviorIndex < entry->behaviorCount; behaviorIndex++) { if (TileAdjacencyBehavior_isApplicable(entry->behaviors[behaviorIndex], position, queryCallback, callbackContext)) { if (applicableBehaviorCount < behaviorCountMax) { outBehaviors[applicableBehaviorCount] = entry->behaviors[behaviorIndex]; } applicableBehaviorCount++; if (entry->behaviors[behaviorIndex]->exclusive || (behaviorCountMax > 0 && applicableBehaviorCount >= behaviorCountMax)) { break; } } } } return applicableBehaviorCount; }