Add base overlay

This commit is contained in:
2025-06-16 12:49:51 -05:00
parent 6c7faedd74
commit ba6ecf9006
10 changed files with 192 additions and 80 deletions

View File

@ -172,9 +172,6 @@ print(f"Input map lowest Y: {inputMapLowestY}, highest Y: {inputMapHighestY}")
# We now offset all chunks by the lowest X/Y values to make them start at (0, 0).
for layerIndex, layer in enumerate(tileLayers):
if layer['startx'] != inputMapLowestX or layer['starty'] != inputMapLowestY:
continue
for chunkIndex, chunk in enumerate(layer['chunks']):
chunk['x'] -= inputMapLowestX
chunk['y'] -= inputMapLowestY
@ -208,7 +205,6 @@ mapWidthInTiles = inputMapHighestX - inputMapLowestX
mapHeightInTiles = inputMapHighestY - inputMapLowestY
mapWidthInRealChunks = math.ceil(float(mapWidthInTiles) / float(CHUNK_WIDTH))
mapHeightInRealChunks = math.ceil(float(mapHeightInTiles) / float(CHUNK_HEIGHT))
print(f"Map width in chunks: {mapWidthInRealChunks}, height in chunks: {mapHeightInRealChunks}")
if inputLayerWidthInTiles < CHUNK_WIDTH or inputLayerHeightInTiles < CHUNK_HEIGHT:
print(f"Error: Input layer size {inputLayerWidthInTiles}x{inputLayerHeightInTiles} is smaller than chunk size {CHUNK_WIDTH}x{CHUNK_HEIGHT}.")
@ -231,52 +227,46 @@ for chunkY in range(mapHeightInRealChunks):
# Get the layers for this chunk.
chunkLayers = []
for layerIndex, layer in enumerate(tileLayers):
for layer in tileLayers:
foundChunk = None
if 'chunks' not in layer or not isinstance(layer['chunks'], list):
print(f"Error: Layer {layerIndex} in '{inputFile}' does not contain 'chunks' key.")
print(f"Error: Layer '{layer['name']}' does not contain 'chunks' key or it is not a list.")
sys.exit(1)
# Find the chunk in this layer that matches the output chunk coordinates.
chunks = layer['chunks']
for chunk in chunks:
for chunk in layer['chunks']:
if 'x' not in chunk or 'y' not in chunk:
print(f"Error: Chunk in layer {layerIndex} does not contain 'x' or 'y' key.")
print(f"Error: Chunk in layer '{layer['name']}' does not contain 'x' or 'y' key.")
sys.exit(1)
if chunk['x'] == inputTopLeftTileX and chunk['y'] == inputTopLeftTileY:
foundChunk = chunk
break
# If we did not find a chunk for this layer, append None.
# Check if this chunk is within the bounds of the top left tile.
if chunk['x'] != inputTopLeftTileX or chunk['y'] != inputTopLeftTileY:
continue
foundChunk = chunk
break
if foundChunk is None:
chunkLayers.append(None)
continue
# Is this chunk layer just empty?
# Is layer empty?
layerEmpty = True
for tile in foundChunk.get('data', []):
for tile in foundChunk['data']:
if tile == 0:
continue
layerEmpty = False
break
if layerEmpty:
chunkLayers.append(None)
continue
# Append the found chunk to the chunkLayers list.
chunkLayers.append(foundChunk)
else:
chunkLayers.append(foundChunk)
# Now we have a chunkLayers list with the found chunks for each layer.
if all(chunk is None for chunk in chunkLayers) or len(chunkLayers) == 0:
continue
# If we have more than 2 layers, we cannot handle this (yet).
if len(chunkLayers) > 2:
print(f"Error: Expected 2 layers for chunk at ({chunkX}, {chunkY}), found {len(chunkLayers)}.")
sys.exit(1)
entities = []
for ob in objectLayer['objects']:
if 'x' not in ob or 'y' not in ob:
@ -302,23 +292,43 @@ for chunkY in range(mapHeightInRealChunks):
def getInputLocalTileY(absoluteTileY):
return absoluteTileY % inputLayerHeightInTiles
# Determine base layer data.
def getInputTileIndex(localX, localY):
absoluteTileX = topLeftTileX + localX
absoluteTileY = topLeftTileY + localY
inputLocalTileX = getInputLocalTileX(absoluteTileX)
inputLocalTileY = getInputLocalTileY(absoluteTileY)
return inputLocalTileY * inputLayerWidthInTiles + inputLocalTileX
def getOutputTileIndex(localX, localY):
return localY * CHUNK_WIDTH + localX
# Determine the layer base.
layerBase = chunkLayers[0]
layerBaseOverlay = None
if len(chunkLayers) > 1:
layerBaseOverlay = chunkLayers[1]
# Determine base layer data.
layerBaseData = []
for y in range(CHUNK_HEIGHT):
for x in range(CHUNK_WIDTH):
absoluteTileX = topLeftTileX + x
absoluteTileY = topLeftTileY + y
inputLocalTileX = getInputLocalTileX(absoluteTileX)
inputLocalTileY = getInputLocalTileY(absoluteTileY)
inputTileIndex = inputLocalTileY * inputLayerWidthInTiles + inputLocalTileX
outputTileIndex = y * CHUNK_WIDTH + x
inputTileIndex = getInputTileIndex(x, y)
outputTileIndex = getOutputTileIndex(x, y)
layerBaseData.append(layerBase['data'][inputTileIndex])
if len(layerBaseData) != CHUNK_TILE_COUNT:
print(f"Error: Layer base data length {len(layerBaseData)} does not match expected chunk tile count {CHUNK_TILE_COUNT}.")
sys.exit(1)
# Layer base overlay.
layerOverlayData = []
if layerBaseOverlay is not None:
for y in range(CHUNK_HEIGHT):
for x in range(CHUNK_WIDTH):
inputTileIndex = getInputTileIndex(x, y)
outputTileIndex = getOutputTileIndex(x, y)
layerOverlayData.append(layerBaseOverlay['data'][inputTileIndex])
# This is a valid chunk.
worldWidth = max(worldWidth, chunkX + 1)
worldHeight = max(worldHeight, chunkY + 1)
@ -341,7 +351,16 @@ for chunkY in range(mapHeightInRealChunks):
f.write(f"\n")
f.write(" },\n\n")
f.write(f" .layerOverlay = {{}},\n")
f.write(" .layerBaseOverlay = {\n")
if layerBaseOverlay is not None:
for y in range(CHUNK_HEIGHT):
f.write(f" ")
for x in range(CHUNK_WIDTH):
i = y * CHUNK_WIDTH + x
byte = layerOverlayData[i]
f.write(f"0x{byte:02x}, ")
f.write(f"\n")
f.write(" },\n\n")
f.write(f" .entities = {{\n")
for entity in entities:
@ -349,8 +368,6 @@ for chunkY in range(mapHeightInRealChunks):
localX = entity['x'] - (topLeftTileX * TILE_WIDTH)
localY = entity['y'] - (topLeftTileY * TILE_HEIGHT)
print(f"Entity at ({entity['x']}, {entity['y']}) in chunk ({chunkX}, {chunkY}) is at local position ({localX}, {localY})")
f.write(" {\n")
f.write(f" .id = {entity['id']},\n")
f.write(f" .type = ENTITY_TYPE_NPC,\n")