From d51d88002d008c4082364bdf6abe888565edd942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sun, 15 Jan 2023 18:43:03 +0100 Subject: [PATCH] recipe filtering in crafting list --- code/foundation/src/gui/inventory.c | 15 ++++++++------- code/foundation/src/models/crafting.c | 10 ++++++++++ code/foundation/src/models/crafting.h | 3 +++ code/foundation/src/world/entity_view.h | 2 +- code/foundation/src/world/world.c | 13 +++++++++++-- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/code/foundation/src/gui/inventory.c b/code/foundation/src/gui/inventory.c index d1b295d..0c54360 100644 --- a/code/foundation/src/gui/inventory.c +++ b/code/foundation/src/gui/inventory.c @@ -101,14 +101,15 @@ inv_draw_result inventory_draw_crafting_btn(float xpos, float ypos, const char * } static inline -bool inventory_draw_crafting_list(float xpos, float ypos) { - // NOTE(zaklaus): collect the list of supported recipes - // TODO(zaklaus): too lazy, draw all recipes everywhere for now - +bool inventory_draw_crafting_list(entity_view *e, float xpos, float ypos) { float start_xpos = xpos; float start_ypos = ypos; - for (uint16_t i = 0; i < craft_get_num_recipes(); ++i) { - asset_id id = craft_get_recipe_asset(i); + + if (!e->sel_ent) + return DAREA_OUTSIDE; + + for (uint16_t i = 0; e->craftables[i]; ++i) { + asset_id id = e->craftables[i]; inventory_draw_crafting_btn(start_xpos+1, ypos+1, asset_names[id], id, BLACK); inv_draw_result entry = inventory_draw_crafting_btn(start_xpos, ypos, asset_names[id], id, RAYWHITE); ypos = entry.y; @@ -134,7 +135,7 @@ void inventory_draw_panel(entity_view *e, bool is_player, float sx, float sy){ inv_keystate *inv = (!is_player) ? &storage_inv : &player_inv; inv_keystate *inv2 = (is_player) ? &storage_inv : &player_inv; - bool inside_craft = !is_player && inventory_draw_crafting_list(screenWidth/2.0f - 684, screenHeight/2.0f - 128); + bool inside_craft = !is_player && inventory_draw_crafting_list(e, screenWidth/2.0f - 684, screenHeight/2.0f - 128); inv->is_inside = check_mouse_area(sx, sy, (float)grid_size, (float)grid_size) != DAREA_OUTSIDE; inv_is_inside |= inv->is_inside || inside_craft; diff --git a/code/foundation/src/models/crafting.c b/code/foundation/src/models/crafting.c index 568a71b..8f2a36d 100644 --- a/code/foundation/src/models/crafting.c +++ b/code/foundation/src/models/crafting.c @@ -52,6 +52,16 @@ bool craft_is_reagent_used_in_producer(asset_id reagent, asset_id producer) { return craft__find_num_recipes_by_reagent(producer, reagent) > 0; } +bool craft_is_item_produced_by_producer(asset_id item, asset_id producer) { + uint32_t num_recipes=0; + for (int i = 0; i < (int)MAX_RECIPES; ++i) { + if (recipes[i].producer == producer && item == recipes[i].product) { + return true; + } + } + return false; +} + uint16_t craft_get_num_recipes(void) { return MAX_RECIPES; } diff --git a/code/foundation/src/models/crafting.h b/code/foundation/src/models/crafting.h index a81a0bd..63f8c75 100644 --- a/code/foundation/src/models/crafting.h +++ b/code/foundation/src/models/crafting.h @@ -12,6 +12,9 @@ asset_id craft_perform_recipe(ecs_entity_t *items, asset_id producer, asset_id t // NOTE(zaklaus): mostly used by item router so we don't push reagents out bool craft_is_reagent_used_in_producer(asset_id reagent, asset_id producer); +// used to filter out craftables +bool craft_is_item_produced_by_producer(asset_id item, asset_id producer); + // NOTE(zaklaus): utilities uint16_t craft_get_num_recipes(void); asset_id craft_get_recipe_asset(uint16_t id); diff --git a/code/foundation/src/world/entity_view.h b/code/foundation/src/world/entity_view.h index 5b2934e..1d2d54a 100644 --- a/code/foundation/src/world/entity_view.h +++ b/code/foundation/src/world/entity_view.h @@ -90,7 +90,7 @@ typedef struct entity_view { uint8_t storage_selected_item; // NOTE(zaklaus): craftable recipes - uint8_t craftables[MAX_CRAFTABLES]; + uint16_t craftables[MAX_CRAFTABLES]; // NOTE(zaklaus): entity picking uint64_t pick_ent; diff --git a/code/foundation/src/world/world.c b/code/foundation/src/world/world.c index f1eb1fb..44b86bd 100644 --- a/code/foundation/src/world/world.c +++ b/code/foundation/src/world/world.c @@ -11,6 +11,7 @@ #include "platform/profiler.h" #include "core/game.h" #include "models/entity.h" +#include "models/crafting.h" #include "packets/pkt_send_librg_update.h" @@ -103,9 +104,17 @@ entity_view *world_build_entity_view(int64_t e) { view.storage_selected_item = in->storage_selected_item; - if (ecs_get(world_ecs(), e, Producer)) { - Device const* dev = ecs_get(world_ecs(), e, Device); + if (ecs_get(world_ecs(), in->storage_ent, Producer)) { + Device const* dev = ecs_get(world_ecs(), in->storage_ent, Device); + zpl_zero_array(view.craftables, MAX_CRAFTABLES); + for (uint16_t i = 0, si = 0; i < craft_get_num_recipes(); i++) { + ZPL_ASSERT(si < MAX_CRAFTABLES); + asset_id pid = craft_get_recipe_asset(i); + if (craft_is_item_produced_by_producer(pid, dev->asset)) { + view.craftables[si++] = pid; + } + } } } }