Map generator done
This commit is contained in:
@ -87,12 +87,8 @@ inputLayerWidthInTiles = firstLayerFirstChunk['width']
|
||||
inputLayerHeightInTiles = firstLayerFirstChunk['height']
|
||||
mapWidthInTiles = firstLayer['width']
|
||||
mapHeightInTiles = firstLayer['height']
|
||||
mapWidthInRealTiles = math.ceil(float(mapWidthInTiles) / float(CHUNK_WIDTH))
|
||||
mapHeightInRealTiles = math.ceil(float(mapHeightInTiles) / float(CHUNK_HEIGHT))
|
||||
|
||||
print(f"Input Layer Size: {inputLayerWidthInTiles}x{inputLayerHeightInTiles} tiles")
|
||||
print(f"Map Size: {mapWidthInTiles}x{mapHeightInTiles} tiles")
|
||||
print(f"Map Real Size: {mapWidthInRealTiles}x{mapHeightInRealTiles} chunks")
|
||||
mapWidthInRealChunks = math.ceil(float(mapWidthInTiles) / float(CHUNK_WIDTH))
|
||||
mapHeightInRealChunks = math.ceil(float(mapHeightInTiles) / float(CHUNK_HEIGHT))
|
||||
|
||||
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}.")
|
||||
@ -103,8 +99,8 @@ worldWidth = 0
|
||||
worldHeight = 0
|
||||
chunksDone = set()
|
||||
|
||||
for chunkY in range(mapHeightInRealTiles):
|
||||
for chunkX in range(mapWidthInRealTiles):
|
||||
for chunkY in range(mapHeightInRealChunks):
|
||||
for chunkX in range(mapWidthInRealChunks):
|
||||
# Top left X/Y based on real chunk size
|
||||
topLeftTileX = chunkX * CHUNK_WIDTH
|
||||
topLeftTileY = chunkY * CHUNK_HEIGHT
|
||||
@ -128,7 +124,7 @@ for chunkY in range(mapHeightInRealTiles):
|
||||
print(f"Error: Chunk in layer {layerIndex} does not contain 'x' or 'y' key.")
|
||||
sys.exit(1)
|
||||
|
||||
if chunk['x'] == topLeftTileX and chunk['y'] == topLeftTileY:
|
||||
if chunk['x'] == inputTopLeftTileX and chunk['y'] == inputTopLeftTileY:
|
||||
foundChunk = chunk
|
||||
break
|
||||
|
||||
@ -147,29 +143,38 @@ for chunkY in range(mapHeightInRealTiles):
|
||||
if layerEmpty:
|
||||
chunkLayers.append(None)
|
||||
continue
|
||||
|
||||
|
||||
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:
|
||||
print(f"Warning: No valid chunks found for chunk at ({chunkX}, {chunkY}). Skipping.")
|
||||
continue
|
||||
|
||||
|
||||
if len(chunkLayers) > 2:
|
||||
print(f"Error: Expected 2 layers for chunk at ({chunkX}, {chunkY}), found {len(chunkLayers)}.")
|
||||
sys.exit(1)
|
||||
|
||||
def getInputLocalTileX(absoluteTileX):
|
||||
return absoluteTileX % inputLayerWidthInTiles
|
||||
|
||||
def getInputLocalTileY(absoluteTileY):
|
||||
return absoluteTileY % inputLayerHeightInTiles
|
||||
|
||||
layerBase = chunkLayers[0]
|
||||
layerBaseData = []
|
||||
# for y in range(CHUNK_HEIGHT):
|
||||
# for x in range(CHUNK_WIDTH):
|
||||
# # Calculate the tile index in the chunk
|
||||
# tileIndex = y * CHUNK_WIDTH + x
|
||||
# if layerBase is not None and tileIndex < len(layerBase.get('data', [])):
|
||||
# tileId = layerBase['data'][tileIndex]
|
||||
# layerBaseData.append(tileId)
|
||||
# else:
|
||||
# layerBaseData.append(0)
|
||||
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
|
||||
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)
|
||||
|
||||
# This is a valid chunk.
|
||||
worldWidth = max(worldWidth, chunkX + 1)
|
||||
@ -184,8 +189,13 @@ for chunkY in range(mapHeightInRealTiles):
|
||||
f.write("#include \"world/chunkdata.h\"\n\n")
|
||||
f.write(f"static const chunkdata_t CHUNK_{chunkX}_{chunkY} = {{\n")
|
||||
f.write(f" .layerBase = {{\n")
|
||||
for byte in layerBase.get('data', []):
|
||||
f.write(f" 0x{byte:02x}, \n")
|
||||
for y in range(CHUNK_HEIGHT):
|
||||
f.write(f" ")
|
||||
for x in range(CHUNK_WIDTH):
|
||||
i = y * CHUNK_WIDTH + x
|
||||
byte = layerBaseData[i]
|
||||
f.write(f"0x{byte:02x}, ")
|
||||
f.write(f"\n")
|
||||
f.write(" },\n\n")
|
||||
f.write(f" .layerOverlay = {{}},\n")
|
||||
f.write(f" .entities = {{}},\n")
|
||||
@ -220,11 +230,9 @@ for chunkY in range(mapHeightInRealTiles):
|
||||
# f.write("};\n\n")
|
||||
# pass
|
||||
|
||||
|
||||
|
||||
# Output header file.
|
||||
header_path = os.path.join(worldDir, "world.h")
|
||||
with open(header_path, 'w') as f:
|
||||
headerPath = os.path.join(worldDir, "world.h")
|
||||
with open(headerPath, 'w') as f:
|
||||
f.write(f"// Generated chunks file. Generated at {now}\n\n")
|
||||
f.write("#pragma once\n")
|
||||
f.write("#include \"dusk.h\"\n")
|
||||
@ -248,4 +256,4 @@ with open(header_path, 'w') as f:
|
||||
f.write("\n")
|
||||
f.write("};\n\n")
|
||||
|
||||
print(f"chunks.h generated at: {header_path}")
|
||||
print(f"chunks.h generated at: {headerPath}")
|
Reference in New Issue
Block a user