Going to redo assets.

This commit is contained in:
2025-08-24 13:57:12 -05:00
parent 329925ea54
commit 479aad2f06
36 changed files with 285 additions and 128 deletions

View File

@@ -0,0 +1,21 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
find_package(Python3 COMPONENTS Interpreter REQUIRED)
# Custom command to generate all header files
add_custom_target(DUSK_CHUNKS
# OUTPUT ${DUSK_GENERATED_HEADERS_DIR}/world/world.h
COMMAND
${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/mapcompile.py
--output ${DUSK_GENERATED_HEADERS_DIR}
--input ${DUSK_DATA_DIR}/map.tmj
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mapcompile.py
COMMENT "Generating chunk header files"
VERBATIM
)
# Ensure headers are generated before compiling main
add_dependencies(${DUSK_TARGET_NAME} DUSK_CHUNKS)

View File

@@ -0,0 +1,127 @@
import sys
from constants import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_TILE_COUNT, TILE_WIDTH_HEIGHT
from entityParser import parseEntity
import math
def parseChunkLayerData(layer, mapData, chunkData):
layerData = []
for y in range(CHUNK_HEIGHT):
for x in range(CHUNK_WIDTH):
inputTileIndex = chunkGetTileIndex(x, y, mapData, chunkData)
outputTileIndex = chunkGetOutputTileIndex(x, y)
layerData.append(layer['data'][inputTileIndex])
if len(layerData) != CHUNK_TILE_COUNT:
print(f"Error: Layer data length {len(layerData)} does not match expected chunk tile count {CHUNK_TILE_COUNT}.")
sys.exit(1)
return layerData
def parseChunk(chunkX, chunkY, mapData):
chunkData = { }
chunkData['topLeftTileX'] = chunkX * CHUNK_WIDTH
chunkData['topLeftTileY'] = chunkY * CHUNK_HEIGHT
chunkData['inputTopLeftTileX'] = math.floor(
float(chunkData['topLeftTileX']) / float(mapData['inputLayerWidthInTiles'])
) * mapData['inputLayerWidthInTiles']
chunkData['inputTopLeftTileY'] = math.floor(
float(chunkData['topLeftTileY']) / float(mapData['inputLayerHeightInTiles'])
) * mapData['inputLayerHeightInTiles']
# Get the data for this chunk out of the map data.
chunkData['layers'] = []
for layer in mapData['tileLayers']:
foundChunk = None
if 'chunks' not in layer or not isinstance(layer['chunks'], list):
print(f"Error: Layer '{layer['name']}' does not contain 'chunks' key or it is not a list.")
sys.exit(1)
for chunk in layer['chunks']:
if 'x' not in chunk or 'y' not in chunk:
print(f"Error: Chunk in layer '{layer['name']}' does not contain 'x' or 'y' key.")
sys.exit(1)
# Check if this chunk is within the bounds of the top left tile.
if chunk['x'] != chunkData['inputTopLeftTileX'] or chunk['y'] != chunkData['inputTopLeftTileY']:
continue
foundChunk = chunk
break
if foundChunk is None:
chunkData['layers'].append(None)
continue
# Is layer empty?
layerEmpty = True
for tile in foundChunk['data']:
if tile == 0:
continue
layerEmpty = False
break
if layerEmpty:
chunkData['layers'].append(None)
else:
chunkData['layers'].append(foundChunk)
# Any layers for this chunk?
if all(chunk is None for chunk in chunkData['layers']):
return None
if len(chunkData['layers']) == 0:
return None
# Parse Layer Data
chunkData['layerBase'] = chunkData['layers'][0]
chunkData['layerBaseOverlay'] = None
if len(chunkData['layers']) > 1:
chunkData['layerBaseOverlay'] = chunkData['layers'][1]
chunkData['layerBaseData'] = parseChunkLayerData(chunkData['layerBase'], mapData, chunkData)
if chunkData['layerBaseOverlay'] is not None:
chunkData['layerBaseOverlayData'] = parseChunkLayerData(chunkData['layerBaseOverlay'], mapData, chunkData)
else:
chunkData['layerBaseOverlayData'] = []
# Parse chunk entities.
chunkData['entities'] = []
for ob in mapData['objectLayer']['objects']:
if 'x' not in ob or 'y' not in ob:
print(f"Error: Object in object layer does not contain 'x' or 'y' key.")
sys.exit(1)
# Is this object within the chunk?
if ob['x'] < chunkData['topLeftTileX'] * TILE_WIDTH_HEIGHT:
continue
if ob['x'] >= (chunkData['topLeftTileX'] + CHUNK_WIDTH) * TILE_WIDTH_HEIGHT:
continue
if ob['y'] < chunkData['topLeftTileY'] * TILE_WIDTH_HEIGHT:
continue
if ob['y'] >= (chunkData['topLeftTileY'] + CHUNK_HEIGHT) * TILE_WIDTH_HEIGHT:
continue
ent = parseEntity(ob, chunkData)
if ent is None:
continue
chunkData['entities'].append(ent)
return chunkData
def chunkGetLocalTileX(absoluteTileX, mapData):
return absoluteTileX % mapData['inputLayerWidthInTiles']
def chunkGetLocalTileY(absoluteTileY, mapData):
return absoluteTileY % mapData['inputLayerHeightInTiles']
def chunkGetTileIndex(localX, localY, mapData, chunkData):
absoluteTileX = chunkData['topLeftTileX'] + localX
absoluteTileY = chunkData['topLeftTileY'] + localY
inputLocalTileX = chunkGetLocalTileX(absoluteTileX, mapData)
inputLocalTileY = chunkGetLocalTileY(absoluteTileY, mapData)
return inputLocalTileY * mapData['inputLayerWidthInTiles'] + inputLocalTileX
def chunkGetOutputTileIndex(localX, localY):
return localY * CHUNK_WIDTH + localX

View File

@@ -0,0 +1,6 @@
# Values defined within C
CHUNK_WIDTH = 8
CHUNK_HEIGHT = 8
CHUNK_TILE_COUNT = CHUNK_WIDTH * CHUNK_HEIGHT
CHUNK_ENTITY_COUNT_MAX = 8
TILE_WIDTH_HEIGHT = 16

View File

@@ -0,0 +1,64 @@
import sys
from constants import TILE_WIDTH_HEIGHT
ENTITY_TYPE_MAP = {
'templates/NPC.tx': 'ENTITY_TYPE_NPC',
}
def parseEntity(obj, chunkData):
if 'type' in obj:
if obj['type'] not in ENTITY_TYPE_MAP:
print(f"Unknown entity type: {obj['type']}")
return None
entType = ENTITY_TYPE_MAP[obj['type']]
elif 'template' in obj:
if obj['template'] not in ENTITY_TYPE_MAP:
print(f"Unknown entity template: {obj['template']}")
return None
entType = ENTITY_TYPE_MAP[obj['template']]
else:
return None
if 'properties' not in obj:
obj['properties'] = {}
obj['localX'] = obj['x'] - (chunkData['topLeftTileX'] * TILE_WIDTH_HEIGHT)
obj['localY'] = obj['y'] - (chunkData['topLeftTileY'] * TILE_WIDTH_HEIGHT)
obj['dir'] = 'DIRECTION_SOUTH'
obj['type'] = entType
def getProperty(propName):
for prop in obj['properties']:
if prop['name'] == propName:
return prop['value']
return None
# Handle per-type properties
if entType == 'ENTITY_TYPE_NPC':
interactType = getProperty('interactType')
if interactType is None:
print(f"NPC entity missing 'interactType' property: {obj['id']}")
sys.exit(1)
obj['data'] = {}
obj['data']['npc'] = {}
obj['data']['npc']['interactType'] = interactType
if interactType == 'NPC_INTERACT_TYPE_TEXT':
text = getProperty('interactText')
if text is None:
print(f"NPC entity missing 'interactText' property: {obj['id']}")
sys.exit(1)
obj['data']['npc']['text'] = text
elif interactType == 'NPC_INTERACT_TYPE_EVENT':
event = getProperty('interactEvent')
if event is None:
print(f"NPC entity missing 'interactEvent' property: {obj['id']}")
sys.exit(1)
obj['data']['npc']['eventData'] = f'&EVENT_{event.upper()}'
return obj

View File

@@ -0,0 +1,32 @@
import os
import sys
import json
def parseInputFile(inputFile):
if not os.path.isfile(inputFile):
print(f"Error: Input file '{inputFile}' does not exist.")
sys.exit(1)
data = None
with open(inputFile, 'r') as f:
data = json.load(f)
# Data should have height key
if 'height' not in data or 'width' not in data:
print(f"Error: Input file '{inputFile}' does not contain 'height' or 'width' key.")
sys.exit(1)
if 'tilewidth' not in data or 'tileheight' not in data:
print(f"Error: Input file '{inputFile}' does not contain 'tilewidth' or 'tileheight' key.")
sys.exit(1)
if 'infinite' not in data or not isinstance(data['infinite'], bool):
print(f"Error: Input file '{inputFile}' does not contain 'infinite' key.")
sys.exit(1)
# Need layers
if 'layers' not in data or not isinstance(data['layers'], list) or len(data['layers']) == 0:
print(f"Error: Input file '{inputFile}' does not contain 'layers' key.")
sys.exit(1)
return data

View File

@@ -0,0 +1,159 @@
import sys
from constants import TILE_WIDTH_HEIGHT, CHUNK_WIDTH, CHUNK_HEIGHT
import math
def parseMap(data):
mapData = {
'layers': data['layers'],
'playerSpawnX': 0,
'playerSpawnY': 0,
}
# Object layer
for layer in mapData['layers']:
if layer.get('type') == 'objectgroup':
mapData['objectLayer'] = layer
break
if mapData['objectLayer'] is None:
print(f"Error: Data does not contain an object layer.")
sys.exit(1)
if 'objects' not in mapData['objectLayer'] or not isinstance(mapData['objectLayer']['objects'], list):
print(f"Error: Object layer does not contain 'objects' key or it is not a list.")
sys.exit(1)
# Tile Layers
mapData['tileLayers'] = []
for layer in mapData['layers']:
if layer.get('type') == 'tilelayer':
mapData['tileLayers'].append(layer)
if len(mapData['tileLayers']) == 0:
print(f"Error: Data does not contain any tile layers.")
sys.exit(1)
# First layer
mapData['firstLayer'] = mapData['tileLayers'][0]
if 'width' not in mapData['firstLayer'] or 'height' not in mapData['firstLayer']:
print(f"Error: First layer does not contain 'width' or 'height' key.")
sys.exit(1)
if 'chunks' not in mapData['firstLayer'] or not isinstance(mapData['firstLayer']['chunks'], list):
print(f"Error: First layer does not contain 'chunks' key.")
sys.exit(1)
if len(mapData['firstLayer']['chunks']) == 0:
print(f"Error: First layer does not contain any chunks.")
sys.exit(1)
mapData['firstLayerFirstChunk'] = mapData['firstLayer']['chunks'][0]
# Now determine the input map bounds.
isMinXFound = False
isMaxXFound = False
isMinYFound = False
isMaxYFound = False
mapData['inputMapLowestX'] = 0
mapData['inputMapHighestX'] = 0
mapData['inputMapLowestY'] = 0
mapData['inputMapHighestY'] = 0
mapData['inputLayerWidthInTiles'] = mapData['firstLayerFirstChunk']['width']
mapData['inputLayerHeightInTiles'] = mapData['firstLayerFirstChunk']['height']
for chunk in mapData['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']) != mapData['inputLayerWidthInTiles'] * mapData['inputLayerHeightInTiles']:
print(f"Error: Chunk in first layer does not contain the expected number of tiles.")
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 mapData['inputMapLowestX'] > chunkX or not isMinXFound:
mapData['inputMapLowestX'] = chunkX
isMinXFound = True
if mapData['inputMapHighestX'] < chunkX or not isMaxXFound:
mapData['inputMapHighestX'] = chunkX
isMaxXFound = True
if mapData['inputMapLowestY'] > chunkY or not isMinYFound:
mapData['inputMapLowestY'] = chunkY
isMinYFound = True
if mapData['inputMapHighestY'] < chunkY or not isMaxYFound:
mapData['inputMapHighestY'] = chunkY
isMaxYFound = True
mapData['inputMapHighestX'] += mapData['inputLayerWidthInTiles']
mapData['inputMapHighestY'] += mapData['inputLayerHeightInTiles']
# We now offset all chunks by the lowest X/Y values to make them start at (0, 0).
for layerIndex, layer in enumerate(mapData['tileLayers']):
for chunkIndex, chunk in enumerate(layer['chunks']):
chunk['x'] -= mapData['inputMapLowestX']
chunk['y'] -= mapData['inputMapLowestY']
layer['chunks'][chunkIndex] = chunk
mapData['layers'][layerIndex] = layer
# Same for object layers
for obIndex, ob in enumerate(mapData['objectLayer']['objects']):
if 'x' not in ob or 'y' not in ob:
print(f"Error: Object in object layer does not contain 'x' or 'y' key.")
sys.exit(1)
if 'id' not in ob:
print(f"Error: Object in object layer does not contain 'id' key.")
sys.exit(1)
ob['x'] -= mapData['inputMapLowestX'] * TILE_WIDTH_HEIGHT
ob['y'] -= mapData['inputMapLowestY'] * TILE_WIDTH_HEIGHT
# Objects are bottom aligned in tiled, so we need to adjust the Y coordinate.
ob['y'] -= TILE_WIDTH_HEIGHT
# Round off the coordinates
ob['x'] = round(ob['x'])
ob['y'] = round(ob['y'])
mapData['objectLayer']['objects'][obIndex] = ob
mapData['mapWidthInTiles'] = mapData['inputMapHighestX'] - mapData['inputMapLowestX']
mapData['mapHeightInTiles'] = mapData['inputMapHighestY'] - mapData['inputMapLowestY']
mapData['mapWidthInRealChunks'] = math.ceil(float(mapData['mapWidthInTiles']) / float(CHUNK_WIDTH))
mapData['mapHeightInRealChunks'] = math.ceil(float(mapData['mapHeightInTiles']) / float(CHUNK_HEIGHT))
if mapData['inputLayerWidthInTiles'] < CHUNK_WIDTH or mapData['inputLayerHeightInTiles'] < CHUNK_HEIGHT:
print(f"Error: Input layer size is smaller than chunk size.")
sys.exit(1)
# Extract player spawn point
for ob in mapData['objectLayer']['objects']:
if 'type' not in ob or ob['type'] != 'player_spawn':
continue
if 'x' not in ob or 'y' not in ob:
print(f"Error: Player spawn object does not contain 'x' or 'y' key.")
sys.exit(1)
mapData['playerSpawnX'] = ob['x']
mapData['playerSpawnY'] = ob['y']
return mapData

View File

@@ -0,0 +1,146 @@
import sys, os
import argparse
from datetime import datetime
import math
from inputParser import parseInputFile
from mapParser import parseMap
from chunkParser import parseChunk
from constants import CHUNK_WIDTH, CHUNK_HEIGHT, TILE_WIDTH_HEIGHT
# Check if the script is run with the correct arguments
parser = argparse.ArgumentParser(description="Generate chunk header files")
parser.add_argument('--output', required=True, help='Dir to output headers')
parser.add_argument('--input', required=True, help='Input JSON file from tiled')
args = parser.parse_args()
# Ensure outdir exists
outputDir = args.output
os.makedirs(outputDir, exist_ok=True)
# Create world directory if it does not exist
worldDir = os.path.join(outputDir, "world")
os.makedirs(worldDir, exist_ok=True)
# Create chunks directory if it does not exist
chunksDir = os.path.join(worldDir, "chunk")
os.makedirs(chunksDir, exist_ok=True)
# Some vars used during printing
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Read the input JSON file
data = parseInputFile(args.input)
mapData = parseMap(data)
# For each output chunk.
worldWidth = 0
worldHeight = 0
chunksDone = set()
for chunkY in range(mapData['mapHeightInRealChunks']):
for chunkX in range(mapData['mapWidthInRealChunks']):
chunkData = parseChunk(chunkX, chunkY, mapData)
if chunkData is None:
continue
# This is a valid chunk.
worldWidth = max(worldWidth, chunkX + 1)
worldHeight = max(worldHeight, chunkY + 1)
chunksDone.add((chunkX, chunkY))
chunkHeaderPath = os.path.join(chunksDir, f"chunk_{chunkX}_{chunkY}.h")
with open(chunkHeaderPath, 'w') as f:
f.write(f"// Generated chunk header for chunk at position ({chunkX}, {chunkY})\n")
f.write(f"// Generated at {now}\n")
f.write("#pragma once\n")
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 y in range(CHUNK_HEIGHT):
f.write(f" ")
for x in range(CHUNK_WIDTH):
i = y * CHUNK_WIDTH + x
byte = chunkData['layerBaseData'][i]
f.write(f"0x{byte:02x}, ")
f.write(f"\n")
f.write(" },\n\n")
f.write(" .layerBaseOverlay = {\n")
if chunkData['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 = chunkData['layerBaseOverlayData'][i]
f.write(f"0x{byte:02x}, ")
f.write(f"\n")
f.write(" },\n\n")
f.write(f" .entities = {{\n")
for entity in chunkData['entities']:
f.write(" {\n")
f.write(f" .id = {entity['id']},\n")
f.write(f" .type = {entity['type']},\n")
f.write(f" .x = {round(entity['x'] / TILE_WIDTH_HEIGHT)},\n")
f.write(f" .y = {round(entity['y'] / TILE_WIDTH_HEIGHT)},\n")
if 'dir' in entity:
f.write(f" .dir = {entity['dir']},\n")
def printRecurse(obj, tabs = " "):
for key, value in obj:
if isinstance(value, dict):
f.write(f"{tabs}.{key} = {{\n")
printRecurse(value.items(), tabs + " ")
f.write(f"{tabs}}},\n")
elif isinstance(value, list):
f.write(f"{tabs}.{key} = {{\n")
for item in value:
f.write(f"{tabs} {item},\n")
f.write(f"{tabs}}},\n")
else:
f.write(f"{tabs}.{key} = {value},\n")
if 'data' in entity:
printRecurse(entity['data'].items())
f.write(" },\n")
f.write(f" }},\n")
f.write("};\n\n")
# Output header file.
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")
# Now, for each chunk, include its header file
for (x, y) in chunksDone:
f.write(f"#include \"world/chunk/chunk_{x}_{y}.h\"\n")
f.write("\n")
f.write(f"#define WORLD_WIDTH {worldWidth}\n")
f.write(f"#define WORLD_HEIGHT {worldHeight}\n")
# Write out other global variables.
f.write(f"#define WORLD_PLAYER_SPAWN_X ({round(mapData['playerSpawnX'] / TILE_WIDTH_HEIGHT)})\n")
f.write(f"#define WORLD_PLAYER_SPAWN_Y ({round(mapData['playerSpawnY'] / TILE_WIDTH_HEIGHT)})\n")
f.write("\n")
f.write(f"static const chunkdata_t* WORLD_CHUNKS[] = {{\n")
for i in range(worldHeight):
f.write(" ")
for j in range(worldWidth):
if(j, i) in chunksDone:
f.write(f"&CHUNK_{j}_{i}, ")
else:
f.write("NULL, ")
f.write("\n")
f.write("};\n\n")
print(f"chunks.h generated at: {headerPath}")