Testing more infinite map stuff
This commit is contained in:
@ -104,12 +104,83 @@ if len(firstLayer['chunks']) == 0:
|
||||
|
||||
firstLayerFirstChunk = firstLayer['chunks'][0]
|
||||
|
||||
# Now determine the input map bounds.
|
||||
isMinXFound = False
|
||||
isMaxXFound = False
|
||||
isMinYFound = False
|
||||
isMaxYFound = False
|
||||
inputMapLowestX = 0
|
||||
inputMapHighestX = 0
|
||||
inputMapLowestY = 0
|
||||
inputMapHighestY = 0
|
||||
inputLayerWidthInTiles = firstLayerFirstChunk['width']
|
||||
inputLayerHeightInTiles = firstLayerFirstChunk['height']
|
||||
mapWidthInTiles = firstLayer['width']
|
||||
mapHeightInTiles = firstLayer['height']
|
||||
|
||||
for chunk in firstLayer['chunks']:
|
||||
if 'x' not in chunk or 'y' not in chunk:
|
||||
print(f"Error: Chunk in first layer does not contain 'x' or 'y' key.")
|
||||
sys.exit(1)
|
||||
|
||||
# Check chunk is not empty
|
||||
if 'data' not in chunk or not isinstance(chunk['data'], list):
|
||||
print(f"Error: Chunk in first layer does not contain 'data' key or it is not a list.")
|
||||
sys.exit(1)
|
||||
|
||||
if len(chunk['data']) != inputLayerWidthInTiles * inputLayerHeightInTiles:
|
||||
print(f"Error: Chunk in first layer does not contain the expected number of tiles ({inputLayerWidthInTiles * inputLayerHeightInTiles}).")
|
||||
sys.exit(1)
|
||||
|
||||
chunkEmpty = True
|
||||
for tile in chunk['data']:
|
||||
if tile == 0:
|
||||
continue
|
||||
chunkEmpty = False
|
||||
break
|
||||
|
||||
if chunkEmpty:
|
||||
print(f"Warning: Chunk at ({chunk['x']}, {chunk['y']}) is empty, skipping.")
|
||||
continue
|
||||
|
||||
chunkX = chunk['x']
|
||||
chunkY = chunk['y']
|
||||
|
||||
if inputMapLowestX > chunkX or not isMinXFound:
|
||||
inputMapLowestX = chunkX
|
||||
isMinXFound = True
|
||||
if inputMapHighestX < chunkX or not isMaxXFound:
|
||||
inputMapHighestX = chunkX
|
||||
isMaxXFound = True
|
||||
|
||||
if inputMapLowestY > chunkY or not isMinYFound:
|
||||
inputMapLowestY = chunkY
|
||||
isMinYFound = True
|
||||
if inputMapHighestY < chunkY or not isMaxYFound:
|
||||
inputMapHighestY = chunkY
|
||||
isMaxYFound = True
|
||||
|
||||
inputMapHighestX += inputLayerWidthInTiles
|
||||
inputMapHighestY += inputLayerHeightInTiles
|
||||
|
||||
print(f"Input map lowest X: {inputMapLowestX}, highest X: {inputMapHighestX}")
|
||||
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
|
||||
layer['chunks'][chunkIndex] = chunk
|
||||
|
||||
layers[layerIndex] = layer
|
||||
|
||||
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}.")
|
||||
@ -139,6 +210,7 @@ for chunkY in range(mapHeightInRealChunks):
|
||||
print(f"Error: Layer {layerIndex} in '{inputFile}' does not contain 'chunks' key.")
|
||||
sys.exit(1)
|
||||
|
||||
# Find the chunk in this layer that matches the output chunk coordinates.
|
||||
chunks = layer['chunks']
|
||||
for chunk in chunks:
|
||||
if 'x' not in chunk or 'y' not in chunk:
|
||||
@ -149,6 +221,7 @@ for chunkY in range(mapHeightInRealChunks):
|
||||
foundChunk = chunk
|
||||
break
|
||||
|
||||
# If we did not find a chunk for this layer, append None.
|
||||
if foundChunk is None:
|
||||
chunkLayers.append(None)
|
||||
continue
|
||||
@ -165,22 +238,26 @@ for chunkY in range(mapHeightInRealChunks):
|
||||
chunkLayers.append(None)
|
||||
continue
|
||||
|
||||
# Append the found chunk to the chunkLayers list.
|
||||
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)
|
||||
|
||||
# Shorthand functions
|
||||
def getInputLocalTileX(absoluteTileX):
|
||||
return absoluteTileX % inputLayerWidthInTiles
|
||||
|
||||
def getInputLocalTileY(absoluteTileY):
|
||||
return absoluteTileY % inputLayerHeightInTiles
|
||||
|
||||
|
||||
# Determine base layer data.
|
||||
layerBase = chunkLayers[0]
|
||||
layerBaseData = []
|
||||
for y in range(CHUNK_HEIGHT):
|
||||
@ -219,7 +296,8 @@ for chunkY in range(mapHeightInRealChunks):
|
||||
f.write(f"\n")
|
||||
f.write(" },\n\n")
|
||||
f.write(f" .layerOverlay = {{}},\n")
|
||||
f.write(f" .entities = {{}},\n")
|
||||
f.write(f" .entities = {{\n")
|
||||
f.write(f" }},\n")
|
||||
f.write("};\n\n")
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user