From 7f098734f50b7ed24772358f641d3d488e0e1537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 29 Sep 2022 19:56:00 +0200 Subject: [PATCH] rework blueprinting to use asset ids instead --- code/foundation/src/models/components.h | 2 +- code/foundation/src/models/items.h | 2 +- code/foundation/src/models/items_list.c | 6 +++++- code/foundation/src/models/items_list_helpers.h | 2 +- code/foundation/src/models/prefabs/blueprint.c | 5 ++--- code/foundation/src/models/prefabs/blueprint.h | 2 +- code/foundation/src/platform/system.h | 2 ++ code/foundation/src/systems/modules/system_blueprint.c | 6 +++--- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/code/foundation/src/models/components.h b/code/foundation/src/models/components.h index 619e8ee..bc7de00 100644 --- a/code/foundation/src/models/components.h +++ b/code/foundation/src/models/components.h @@ -157,7 +157,7 @@ typedef struct { typedef struct { uint8_t w; uint8_t h; - char plan[256]; + asset_id plan[256]; } Blueprint; typedef struct { diff --git a/code/foundation/src/models/items.h b/code/foundation/src/models/items.h index 4185d1d..699b49d 100644 --- a/code/foundation/src/models/items.h +++ b/code/foundation/src/models/items.h @@ -64,7 +64,7 @@ typedef struct { struct { uint8_t w; uint8_t h; - const char plan[256]; + const asset_id *plan; } blueprint; }; } item_desc; diff --git a/code/foundation/src/models/items_list.c b/code/foundation/src/models/items_list.c index 8bb4349..a235b8f 100644 --- a/code/foundation/src/models/items_list.c +++ b/code/foundation/src/models/items_list.c @@ -9,7 +9,11 @@ static item_desc items[] = { ITEM_ENERGY(ASSET_WOOD, ASSET_FURNACE, 64, 15.0f), ITEM_HOLD(ASSET_TREE, 64), - ITEM_BLUEPRINT(ASSET_BLUEPRINT, 1, 4, 4, "]]]]]CF] ]]]]]"), + // ITEM_BLUEPRINT(ASSET_BLUEPRINT, 1, 4, 4, "]]]]]CF] ]]]]]"), + ITEM_BLUEPRINT(ASSET_BLUEPRINT, 1, 4, 4, PROT({ ASSET_WOOD,ASSET_WOOD,ASSET_WOOD,ASSET_WOOD, + ASSET_WOOD,ASSET_FURNACE,ASSET_CHEST,ASSET_WOOD, + ASSET_FENCE,ASSET_EMPTY,ASSET_EMPTY,ASSET_WOOD, + ASSET_WALL,ASSET_EMPTY,ASSET_EMPTY,ASSET_WOOD})), ITEM_SELF_DIR(ASSET_BELT, 999), ITEM_PROXY(ASSET_BELT_LEFT, ASSET_BELT), diff --git a/code/foundation/src/models/items_list_helpers.h b/code/foundation/src/models/items_list_helpers.h index 6a85a19..fe00b85 100644 --- a/code/foundation/src/models/items_list_helpers.h +++ b/code/foundation/src/models/items_list_helpers.h @@ -29,7 +29,7 @@ .blueprint = {\ .w = w_,\ .h = h_,\ -.plan = plan_\ +.plan = (const asset_id[])plan_\ },\ .place_item = {\ .id = asset\ diff --git a/code/foundation/src/models/prefabs/blueprint.c b/code/foundation/src/models/prefabs/blueprint.c index ce83658..29d24f0 100644 --- a/code/foundation/src/models/prefabs/blueprint.c +++ b/code/foundation/src/models/prefabs/blueprint.c @@ -8,15 +8,14 @@ #include "models/items.h" #include "models/components.h" -uint64_t blueprint_spawn(uint8_t w, uint8_t h, const char *plan) { - ZPL_ASSERT((w*h) == zpl_strlen(plan)); +uint64_t blueprint_spawn(uint8_t w, uint8_t h, const asset_id *plan) { ZPL_ASSERT((w*h) < 256); ecs_entity_t e = device_spawn(ASSET_BLUEPRINT); Blueprint *blueprint = ecs_get_mut(world_ecs(), e, Blueprint); blueprint->w = w; blueprint->h = h; - zpl_memcopy(blueprint->plan, plan, w*h); + zpl_memcopy(blueprint->plan, plan, w*h*sizeof(asset_id)); return (uint64_t)e; } diff --git a/code/foundation/src/models/prefabs/blueprint.h b/code/foundation/src/models/prefabs/blueprint.h index 514d7ce..38180d2 100644 --- a/code/foundation/src/models/prefabs/blueprint.h +++ b/code/foundation/src/models/prefabs/blueprint.h @@ -1,7 +1,7 @@ #pragma once #include "platform/system.h" -uint64_t blueprint_spawn(uint8_t w, uint8_t h, const char *plan); +uint64_t blueprint_spawn(uint8_t w, uint8_t h, const uint16_t *plan); uint64_t blueprint_spawn_udata(void* udata); void blueprint_despawn(uint64_t id); diff --git a/code/foundation/src/platform/system.h b/code/foundation/src/platform/system.h index 562dcf3..080d8d1 100644 --- a/code/foundation/src/platform/system.h +++ b/code/foundation/src/platform/system.h @@ -18,3 +18,5 @@ void reset_cached_time(void); uint32_t defer_var = (s, 0); \ !defer_var; \ (defer_var += 1), e) + +#define PROT(...) __VA_ARGS__ diff --git a/code/foundation/src/systems/modules/system_blueprint.c b/code/foundation/src/systems/modules/system_blueprint.c index 54c1353..72b5f1c 100644 --- a/code/foundation/src/systems/modules/system_blueprint.c +++ b/code/foundation/src/systems/modules/system_blueprint.c @@ -12,10 +12,10 @@ void BuildBlueprints(ecs_iter_t *it) { for (int y = 0; y < blueprint[i].h; y++) { for (int x = 0; x < blueprint[i].w; x++) { - char c = blueprint[i].plan[y*w + x]; - if (c == ' ') continue; + asset_id c = blueprint[i].plan[y*w + x]; + if (c == ASSET_EMPTY) continue; world_block_lookup l = world_block_from_realpos(p[i].x + x * WORLD_BLOCK_SIZE - (w * WORLD_BLOCK_SIZE)/2, p[i].y + y * WORLD_BLOCK_SIZE - (h * WORLD_BLOCK_SIZE)/2); - world_chunk_place_block(l.chunk_id, l.id, blocks_find_by_symbol(c)); + world_chunk_place_block(l.chunk_id, l.id, blocks_find(c)); } }