From e1d7b7308f466495f39019bd83ba66fb1b0c1bfc Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 24 Jan 2026 10:07:50 -0600 Subject: [PATCH] Progness --- src/overworld/CMakeLists.txt | 11 +++++++ src/overworld/map.c | 61 ++++++++++++++++++++++++++++++++++++ src/overworld/map.h | 22 +++++++++++++ src/overworld/mapentity.c | 23 ++++++++++++++ src/overworld/mapentity.h | 57 +++++++++++++++++++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 src/overworld/CMakeLists.txt create mode 100644 src/overworld/map.c create mode 100644 src/overworld/map.h create mode 100644 src/overworld/mapentity.c create mode 100644 src/overworld/mapentity.h diff --git a/src/overworld/CMakeLists.txt b/src/overworld/CMakeLists.txt new file mode 100644 index 0000000..d962e9a --- /dev/null +++ b/src/overworld/CMakeLists.txt @@ -0,0 +1,11 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_LIBRARY_TARGET_NAME} + PUBLIC + map.c + mapentity.c +) \ No newline at end of file diff --git a/src/overworld/map.c b/src/overworld/map.c new file mode 100644 index 0000000..2d39c21 --- /dev/null +++ b/src/overworld/map.c @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "map.h" +#include "util/memory.h" +#include "util/string.h" +#include "assert/assert.h" + +map_t MAP; + +void mapInit(void) { + memoryZero(&MAP, sizeof(map_t)); +} + +mapentity_t * mapEntityAdd(const mapentitytype_t type) { + assertTrue(type != MAP_ENTITY_TYPE_NULL, "Type cannot be NULL"); + assertTrue(type < MAP_ENTITY_TYPE_COUNT, "Type is out of range"); + + // Find any available entity. + mapentity_t *start = &MAP.entities[0]; + mapentity_t *end = &MAP.entities[MAP_ENTITY_COUNT_MAX]; + do { + if(start->type != MAP_ENTITY_TYPE_NULL) continue; + mapEntityInit(start, type); + return start; + } while(start->type != MAP_ENTITY_TYPE_NULL && ++start < end); + + return NULL; +} + +void mapEntityRemove(mapentity_t *ent) { + assertNotNull(ent, "Ent cannot be NULL"); + assertTrue(ent->type != MAP_ENTITY_TYPE_NULL, "Ent is already NULL"); + assertTrue( + ent >= &MAP.entities[0] && ent < &MAP.entities[MAP_ENTITY_COUNT_MAX], + "Ent is out of range" + ); + + ent->type = MAP_ENTITY_TYPE_NULL; +} + +mapentity_t * mapEntityGetByName(const char_t *name) { + assertNotNull(name, "Name cannot be NULL"); + assertStrLenMax(name, MAP_ENTITY_NAME_MAX_LENGTH, "Name is too long"); + + // Search for the entity by name. + mapentity_t *start = &MAP.entities[0]; + mapentity_t *end = &MAP.entities[MAP_ENTITY_COUNT_MAX]; + do { + if(start->type == MAP_ENTITY_TYPE_NULL) continue; + if(stringCompare(start->name, name) == 0) { + return start; + } + } while(++start < end); + + return NULL; +} \ No newline at end of file diff --git a/src/overworld/map.h b/src/overworld/map.h new file mode 100644 index 0000000..45fa00e --- /dev/null +++ b/src/overworld/map.h @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "mapentity.h" + +#define MAP_ENTITY_COUNT_MAX 32 + +typedef struct { + mapentity_t entities[MAP_ENTITY_COUNT_MAX]; +} map_t; + +extern map_t MAP; + +/** + * Initialize the overworld map. + */ +void mapInit(void); \ No newline at end of file diff --git a/src/overworld/mapentity.c b/src/overworld/mapentity.c new file mode 100644 index 0000000..5206b61 --- /dev/null +++ b/src/overworld/mapentity.c @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "mapentity.h" +#include "util/memory.h" +#include "util/string.h" + +void mapEntityInit(mapentity_t *ent, const mapentitytype_t type) { + memoryZero(ent, sizeof(mapentity_t)); + ent->type = type; +} + +void mapEntitySetName(mapentity_t *ent, const char_t *name) { + assertNotNull(ent, "Ent cannot be NULL"); + assertNotNull(name, "Name cannot be NULL"); + assertStrLenMax(name, MAP_ENTITY_NAME_MAX_LENGTH, "Name is too long"); + + stringCopy(ent->name, name, MAP_ENTITY_NAME_MAX_LENGTH); +} \ No newline at end of file diff --git a/src/overworld/mapentity.h b/src/overworld/mapentity.h new file mode 100644 index 0000000..e5ad150 --- /dev/null +++ b/src/overworld/mapentity.h @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.h" + +#define MAP_ENTITY_NAME_MAX_LENGTH 16 +#define MAP_ENTITY_SCRIPT_MAX_LENGTH 64 + +typedef enum { + MAP_ENTITY_DIR_NORTH = 0, + MAP_ENTITY_DIR_EAST = 1, + MAP_ENTITY_DIR_SOUTH = 2, + MAP_ENTITY_DIR_WEST = 3, + + MAP_ENTITY_DIR_COUNT = 4, + + MAP_ENTITY_DIR_UP = MAP_ENTITY_DIR_NORTH, + MAP_ENTITY_DIR_RIGHT = MAP_ENTITY_DIR_EAST, + MAP_ENTITY_DIR_DOWN = MAP_ENTITY_DIR_SOUTH, + MAP_ENTITY_DIR_LEFT = MAP_ENTITY_DIR_WEST +} mapentitydir_t; + +typedef enum { + MAP_ENTITY_TYPE_NULL, + + MAP_ENTITY_TYPE_NPC, + + MAP_ENTITY_TYPE_COUNT +} mapentitytype_t; + +typedef struct { + char_t name[MAP_ENTITY_NAME_MAX_LENGTH]; + + // Position within the map + uint16_t x, y; + uint8_t layer; + + // Rendering properties + mapentitydir_t dir; + mapentitytype_t type; + + // Script to execute on interaction + char_t script[MAP_ENTITY_SCRIPT_MAX_LENGTH]; +} mapentity_t; + +/** + * Initialize a map entity to default values. + * + * @param ent The entity to initialize. + * @param type The type of the entity. + */ +void mapEntityInit(mapentity_t *ent, const mapentitytype_t type); \ No newline at end of file