From a3a75af9b5310361f856fb06058f0e2b8a6222bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 18 Oct 2022 19:59:35 +0200 Subject: [PATCH] finish up crafting system v0 --- code/foundation/CMakeLists.txt | 1 + code/foundation/src/gen/texgen_fallback.c | 1 + code/foundation/src/lists/assets_ids.lst | 1 + code/foundation/src/lists/assets_list.c | 1 + code/foundation/src/lists/crafting_list.c | 2 +- code/foundation/src/lists/entity_spawnlist.c | 2 ++ code/foundation/src/lists/items_list.c | 1 + .../foundation/src/models/prefabs/assembler.c | 27 +++++++++++++++++++ .../foundation/src/models/prefabs/assembler.h | 6 +++++ .../src/systems/modules/system_items.c | 1 - .../src/systems/modules/system_logistics.c | 5 ++++ 11 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 code/foundation/src/models/prefabs/assembler.c create mode 100644 code/foundation/src/models/prefabs/assembler.h diff --git a/code/foundation/CMakeLists.txt b/code/foundation/CMakeLists.txt index 023b4f2..a2f7cf7 100644 --- a/code/foundation/CMakeLists.txt +++ b/code/foundation/CMakeLists.txt @@ -22,6 +22,7 @@ add_library(eco2d-foundation STATIC src/models/prefabs/blueprint.c src/models/prefabs/splitter.c src/models/prefabs/craftbench.c + src/models/prefabs/assembler.c src/pkt/packet.c diff --git a/code/foundation/src/gen/texgen_fallback.c b/code/foundation/src/gen/texgen_fallback.c index 39aa183..5c555a6 100644 --- a/code/foundation/src/gen/texgen_fallback.c +++ b/code/foundation/src/gen/texgen_fallback.c @@ -53,6 +53,7 @@ Texture2D texgen_build_sprite_fallback(asset_id id) { case ASSET_FURNACE: return LoadTexEco("furnace-export"); case ASSET_CRAFTBENCH: return LoadTexEco("craftbench"); case ASSET_SPLITTER: return LoadTexEco("item_splitter"); + case ASSET_ASSEMBLER: return LoadTexEco("assembler"); default: break; } diff --git a/code/foundation/src/lists/assets_ids.lst b/code/foundation/src/lists/assets_ids.lst index 8672044..85245cf 100644 --- a/code/foundation/src/lists/assets_ids.lst +++ b/code/foundation/src/lists/assets_ids.lst @@ -7,6 +7,7 @@ X(ASSET_THING)\ X(ASSET_CHEST)\ X(ASSET_SPLITTER)\ + X(ASSET_ASSEMBLER)\ X(ASSET_FURNACE)\ X(ASSET_CRAFTBENCH)\ X(ASSET_BLUEPRINT_BEGIN)\ diff --git a/code/foundation/src/lists/assets_list.c b/code/foundation/src/lists/assets_list.c index 9b9243e..cc7fd03 100644 --- a/code/foundation/src/lists/assets_list.c +++ b/code/foundation/src/lists/assets_list.c @@ -25,6 +25,7 @@ static asset assets[] = { ASSET_TEX(ASSET_CHEST), ASSET_TEX(ASSET_FURNACE), ASSET_TEX(ASSET_SPLITTER), + ASSET_TEX(ASSET_ASSEMBLER), ASSET_TEX(ASSET_CRAFTBENCH), ASSET_TEX(ASSET_BLUEPRINT), ASSET_TEX(ASSET_BLUEPRINT_DEMO_HOUSE), diff --git a/code/foundation/src/lists/crafting_list.c b/code/foundation/src/lists/crafting_list.c index 6f403f3..29e5acf 100644 --- a/code/foundation/src/lists/crafting_list.c +++ b/code/foundation/src/lists/crafting_list.c @@ -20,7 +20,7 @@ static recipe recipes[] = { // NOTE(zaklaus): Belt RECIPE(ASSET_IRON_PLATES, ASSET_FURNACE, 4, R(ASSET_IRON_ORE, 1), {0}), RECIPE(ASSET_SCREWS, ASSET_CRAFTBENCH, 8, R(ASSET_IRON_PLATES, 1), {0}), - RECIPE(ASSET_BELT, ASSET_CRAFTBENCH, 1, R(ASSET_FENCE, 1), R(ASSET_SCREWS, 4), R(ASSET_IRON_PLATES, 2), {0}), + RECIPE(ASSET_BELT, ASSET_ASSEMBLER, 1, R(ASSET_FENCE, 1), R(ASSET_SCREWS, 4), R(ASSET_IRON_PLATES, 2), {0}), }; #define MAX_RECIPES (sizeof(recipes)/sizeof(recipes[0])) diff --git a/code/foundation/src/lists/entity_spawnlist.c b/code/foundation/src/lists/entity_spawnlist.c index bb7108c..226be04 100644 --- a/code/foundation/src/lists/entity_spawnlist.c +++ b/code/foundation/src/lists/entity_spawnlist.c @@ -4,6 +4,7 @@ #include "models/prefabs/blueprint.h" #include "models/prefabs/craftbench.h" #include "models/prefabs/splitter.h" +#include "models/prefabs/assembler.h" static struct { asset_id id; @@ -14,6 +15,7 @@ static struct { { .id = ASSET_FURNACE, .proc = furnace_spawn }, { .id = ASSET_CRAFTBENCH, .proc = craftbench_spawn }, { .id = ASSET_SPLITTER, .proc = splitter_spawn }, + { .id = ASSET_ASSEMBLER, .proc = assembler_spawn }, { .id = ASSET_BLUEPRINT, .proc_udata = blueprint_spawn_udata }, }; diff --git a/code/foundation/src/lists/items_list.c b/code/foundation/src/lists/items_list.c index 96debe8..2aefcd4 100644 --- a/code/foundation/src/lists/items_list.c +++ b/code/foundation/src/lists/items_list.c @@ -26,6 +26,7 @@ static item_desc items[] = { ITEM_ENT(ASSET_CRAFTBENCH, 32, ASSET_CRAFTBENCH), ITEM_ENT(ASSET_FURNACE, 32, ASSET_FURNACE), ITEM_ENT(ASSET_SPLITTER, 32, ASSET_SPLITTER), + ITEM_ENT(ASSET_ASSEMBLER, 32, ASSET_ASSEMBLER), ITEM_HOLD(ASSET_IRON_ORE, 64), ITEM_HOLD(ASSET_IRON_INGOT, 64), diff --git a/code/foundation/src/models/prefabs/assembler.c b/code/foundation/src/models/prefabs/assembler.c new file mode 100644 index 0000000..542837a --- /dev/null +++ b/code/foundation/src/models/prefabs/assembler.c @@ -0,0 +1,27 @@ +#include "assembler.h" +#include "models/device.h" +#include "world/world.h" + +#include "models/entity.h" +#include "models/components.h" + +uint64_t assembler_spawn(void) { + ecs_entity_t e = device_spawn(ASSET_ASSEMBLER); + + ItemContainer *storage = ecs_get_mut(world_ecs(), e, ItemContainer); + *storage = (ItemContainer){0}; + + Producer *producer = ecs_get_mut(world_ecs(), e, Producer); + *producer = (Producer){0}; + producer->energy_level = 69.0f; + producer->pending_task = PRODUCER_CRAFT_AUTO; + producer->push_filter = PRODUCER_PUSH_PRODUCT; + producer->target_item = ASSET_INVALID; + + ecs_set(world_ecs(), e, ItemRouter, {1}); + return (uint64_t)e; +} + +void assembler_despawn(uint64_t ent_id) { + entity_despawn(ent_id); +} diff --git a/code/foundation/src/models/prefabs/assembler.h b/code/foundation/src/models/prefabs/assembler.h new file mode 100644 index 0000000..0ab04fe --- /dev/null +++ b/code/foundation/src/models/prefabs/assembler.h @@ -0,0 +1,6 @@ +#pragma once + +#include "platform/system.h" + +uint64_t assembler_spawn(void); +void assembler_despawn(uint64_t id); diff --git a/code/foundation/src/systems/modules/system_items.c b/code/foundation/src/systems/modules/system_items.c index a3a789b..cda1e63 100644 --- a/code/foundation/src/systems/modules/system_items.c +++ b/code/foundation/src/systems/modules/system_items.c @@ -61,7 +61,6 @@ void CraftItem(ecs_iter_t *it) { for (int i = 0; i < it->count; i++) { if (in[i].craft_item == 0) continue; - zpl_printf("id: %d\n", in[i].craft_item); if (world_entity_valid(in[i].storage_ent)){ Producer *ic = 0; if ((ic = ecs_get_mut_if_ex(it->world, in[i].storage_ent, Producer))){ diff --git a/code/foundation/src/systems/modules/system_logistics.c b/code/foundation/src/systems/modules/system_logistics.c index 66152c7..85bc625 100644 --- a/code/foundation/src/systems/modules/system_logistics.c +++ b/code/foundation/src/systems/modules/system_logistics.c @@ -121,6 +121,11 @@ void PushItemsOnNodes(ecs_iter_t *it) { item->quantity -= zpl_min(r->push_qty, item->quantity); --num_nodes; ++counter; + + if (item->quantity == 0) { + item_despawn(item_slot_ent); + storage[i].items[j] = 0; + } } } }