From f6f6194b4db9449c328310ed1a423f12370fcf1f Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 13 Jun 2025 19:53:55 -0500 Subject: [PATCH] Dusk progress --- .gitignore | 4 +- CMakeLists.txt | 8 +-- tools/CMakeLists.txt | 3 +- tools/assetstool/CMakeLists.txt | 19 ------ tools/assetstool/assetstool.py | 67 ------------------- tools/copytool/CMakeLists.txt | 16 ----- {data => tools/mapcompile}/CMakeLists.txt | 4 +- .../mapcompile/mapcompile.py | 31 ++++----- tools/mapeditor/mapeditor.py | 10 +++ tools/shared/worlddefs.py | 11 +++ 10 files changed, 43 insertions(+), 130 deletions(-) delete mode 100644 tools/assetstool/CMakeLists.txt delete mode 100755 tools/assetstool/assetstool.py delete mode 100644 tools/copytool/CMakeLists.txt rename {data => tools/mapcompile}/CMakeLists.txt (74%) rename data/chunks.py => tools/mapcompile/mapcompile.py (90%) create mode 100755 tools/mapeditor/mapeditor.py create mode 100644 tools/shared/worlddefs.py diff --git a/.gitignore b/.gitignore index 3a9a395..7d4fe5a 100644 --- a/.gitignore +++ b/.gitignore @@ -92,4 +92,6 @@ assets/borrowed /archive0 /archive1 -/archive \ No newline at end of file +/archive + +__pycache__ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3272084..5284318 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,8 +21,9 @@ set(DUSK_BUILD_DIR "${CMAKE_BINARY_DIR}") set(DUSK_SOURCES_DIR "${DUSK_ROOT_DIR}/src") set(DUSK_TEMP_DIR "${DUSK_BUILD_DIR}/temp") set(DUSK_TOOLS_DIR "${DUSK_ROOT_DIR}/tools") -set(DUSK_ASSETS_DIR "${DUSK_ROOT_DIR}/assets") -set(DUSK_ASSETS_BUILD_DIR "${DUSK_BUILD_DIR}/assets") +# set(DUSK_ASSETS_DIR "${DUSK_ROOT_DIR}/assets") +# set(DUSK_ASSETS_BUILD_DIR "${DUSK_BUILD_DIR}/assets") +set(DUSK_DATA_DIR "${DUSK_ROOT_DIR}/data") set(DUSK_GENERATED_HEADERS_DIR "${DUSK_BUILD_DIR}/generated") set(DUSK_TARGET_NAME "Dusk" CACHE INTERNAL ${DUSK_CACHE_TARGET}) set(DUSK_BUILD_BINARY ${DUSK_BUILD_DIR}/Dusk CACHE INTERNAL ${DUSK_CACHE_TARGET}) @@ -46,9 +47,6 @@ add_executable(${DUSK_TARGET_NAME}) # Add tools add_subdirectory(tools) -# Add data -add_subdirectory(data) - # Add code add_subdirectory(src) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index c080a69..6b9f03a 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -4,5 +4,4 @@ # https://opensource.org/licenses/MIT # Tools -add_subdirectory(assetstool) -add_subdirectory(copytool) \ No newline at end of file +add_subdirectory(mapcompile) \ No newline at end of file diff --git a/tools/assetstool/CMakeLists.txt b/tools/assetstool/CMakeLists.txt deleted file mode 100644 index 33b33d2..0000000 --- a/tools/assetstool/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) 2023 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -find_package(Python3 REQUIRED COMPONENTS Interpreter) - -add_custom_target(duskassets - COMMAND - ${Python3_EXECUTABLE} - ${DUSK_TOOLS_DIR}/assetstool/assetstool.py - --input=${DUSK_ASSETS_BUILD_DIR} - --output=${DUSK_BUILD_DIR}/dusk.tar - COMMENT "Bundling assets..." - USES_TERMINAL - DEPENDS ${DUSK_ASSETS} -) - -add_dependencies(${DUSK_TARGET_NAME} duskassets) \ No newline at end of file diff --git a/tools/assetstool/assetstool.py b/tools/assetstool/assetstool.py deleted file mode 100755 index 17f9c33..0000000 --- a/tools/assetstool/assetstool.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright (c) 2023 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -import os -import tarfile -import argparse - -# Args -parser = argparse.ArgumentParser( - description='Bundles all assets into the internal archive format.' -) -parser.add_argument('-i', '--input'); -parser.add_argument('-o', '--output'); -args = parser.parse_args() - -# Ensure the directory for the output path exists -if not os.path.exists(os.path.dirname(args.output)): - os.makedirs(os.path.dirname(args.output)) - -# Create a ZIP archive and add the specified directory -# archive = tarfile.open(args.output, 'w:bz2') # BZ2 Compression - -# Does the archive already exist? -filesInArchive = [] - -if os.path.exists(args.output) and False: -# if os.path.exists(args.output): - # Yes, open it - archive = tarfile.open(args.output, 'a:') - - # Get all the files in the archive - for member in archive.getmembers(): - filesInArchive.append(member.name) - - archive.close() - - # Open archive for appending. - archive = tarfile.open(args.output, 'a:') -else: - # No, create it - archive = tarfile.open(args.output, 'w:') - -# Add all files in the input directory -for foldername, subfolders, filenames in os.walk(args.input): - for filename in filenames: - - # Is the file already in the archive? - absolute_path = os.path.join(foldername, filename) - relative_path = os.path.relpath(absolute_path, args.input) - - if relative_path in filesInArchive: - if relative_path.endswith('.texture'): - print(f"Skipping {filename}...") - continue - else: - print(f"Overwriting {filename}...") - # Does not work in python, throw error - exit (1) - else: - print(f"Archiving asset {filename}...") - - archive.add(absolute_path, arcname=relative_path) - -# Close the archive -archive.close() diff --git a/tools/copytool/CMakeLists.txt b/tools/copytool/CMakeLists.txt deleted file mode 100644 index f0c06d7..0000000 --- a/tools/copytool/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright (c) 2023 Dominic Msters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -function(copytool file) - set(TARGET_NAME "copytool_${file}") - - # Replace slashes with underscores - string(REPLACE "/" "_" TARGET_NAME ${TARGET_NAME}) - - add_custom_target(${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E copy ${DUSK_ASSETS_DIR}/${file} ${DUSK_ASSETS_BUILD_DIR}/${file} - ) - add_dependencies(duskassets ${TARGET_NAME}) -endfunction() \ No newline at end of file diff --git a/data/CMakeLists.txt b/tools/mapcompile/CMakeLists.txt similarity index 74% rename from data/CMakeLists.txt rename to tools/mapcompile/CMakeLists.txt index 6299e7a..8c3453b 100644 --- a/data/CMakeLists.txt +++ b/tools/mapcompile/CMakeLists.txt @@ -8,8 +8,8 @@ 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}/chunks.py --output ${DUSK_GENERATED_HEADERS_DIR} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/chunks.py + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/mapcompile.py --output ${DUSK_GENERATED_HEADERS_DIR} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mapcompile.py COMMENT "Generating chunk header files" VERBATIM ) diff --git a/data/chunks.py b/tools/mapcompile/mapcompile.py similarity index 90% rename from data/chunks.py rename to tools/mapcompile/mapcompile.py index 2de3457..e260381 100644 --- a/data/chunks.py +++ b/tools/mapcompile/mapcompile.py @@ -1,17 +1,16 @@ -#!/usr/bin/python3 -import os +import sys, os import argparse from datetime import datetime import json -# Constants that are defined in the C code -CHUNK_WIDTH = 8 -CHUNK_HEIGHT = 8 -CHUNK_ENTITY_COUNT_MAX = 8 +shared_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'shared')) +sys.path.append(shared_path) -ENTITY_TYPE = { - "npc": "ENTITY_TYPE_NPC", -} +from worlddefs import CHUNK_WIDTH, CHUNK_HEIGHT, ENTITY_TYPE_MAP, CHUNK_DATA_DIR + +# Dynamically add ../shared to sys.path +shared_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'shared')) +sys.path.append(shared_path) # Check if the script is run with the correct arguments parser = argparse.ArgumentParser(description="Generate chunk header files") @@ -30,12 +29,6 @@ os.makedirs(worldDir, exist_ok=True) chunksDir = os.path.join(worldDir, "chunk") os.makedirs(chunksDir, exist_ok=True) -# Scan ./chunks folder -chunks_dir = os.path.join(os.path.dirname(__file__), "chunks") -if not os.path.exists(chunks_dir): - print(f"Error: Chunks directory '{chunks_dir}' does not exist.") - exit(1) - # Some vars used during printing now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") @@ -46,8 +39,8 @@ chunksDone = [] entityIdNext = 1 # For each chunk file -for chunkFile in os.listdir(chunks_dir): - data = json.load(open(os.path.join(chunks_dir, chunkFile))) +for chunkFile in os.listdir(CHUNK_DATA_DIR): + data = json.load(open(os.path.join(CHUNK_DATA_DIR, chunkFile))) print(f"Processing chunk: {chunkFile}") if not 'chunk' in data: @@ -103,7 +96,9 @@ for chunkFile in os.listdir(chunks_dir): if 'overlayLayer' not in data['chunk']: print(f"Error: Chunk file '{chunkFile}' does not contain 'overlayLayer' key.") exit(1) + overlayLayer = data['chunk']['overlayLayer'] + # Overlay layer should exactly CHUNK_WIDTH * CHUNK_HEIGHT elements if len(overlayLayer) != CHUNK_HEIGHT: print(f"Error: Chunk file '{chunkFile}' has invalid 'overlayLayer' length.") @@ -160,7 +155,7 @@ for chunkFile in os.listdir(chunks_dir): f.write(" {\n") f.write(f" .id = {entityId},\n") - f.write(f" .type = {ENTITY_TYPE.get(entity['type'], 'ENTITY_TYPE_UNKNOWN')},\n"), + f.write(f" .type = {ENTITY_TYPE_MAP.get(entity['type'], 'ENTITY_TYPE_UNKNOWN')},\n"), f.write(f" .x = {entity['x']},\n") f.write(f" .y = {entity['y']},\n") f.write(f" }},\n") diff --git a/tools/mapeditor/mapeditor.py b/tools/mapeditor/mapeditor.py new file mode 100755 index 0000000..3ae5e5a --- /dev/null +++ b/tools/mapeditor/mapeditor.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 +import sys, os + +# Dynamically add ../shared to sys.path +shared_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'shared')) +sys.path.append(shared_path) + +# Import shared modules +from worlddefs import CHUNK_WIDTH, CHUNK_HEIGHT, ENTITY_TYPE_MAP, CHUNK_DATA_DIR + diff --git a/tools/shared/worlddefs.py b/tools/shared/worlddefs.py new file mode 100644 index 0000000..744c354 --- /dev/null +++ b/tools/shared/worlddefs.py @@ -0,0 +1,11 @@ +import os + +CHUNK_WIDTH = 8 +CHUNK_HEIGHT = 8 +CHUNK_ENTITY_COUNT_MAX = 8 + +ENTITY_TYPE_MAP = { + "npc": "ENTITY_TYPE_NPC", +} + +CHUNK_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'chunks')) \ No newline at end of file