From 5d1e85b24154dea8ccf0fc98e54a5c0cbf32453c Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:47:18 +1000 Subject: [PATCH] player.availableRecipes and root.allRecipes --- source/core/StarJsonParser.hpp | 2 +- source/game/StarItemDatabase.cpp | 2 +- source/game/StarItemDatabase.hpp | 2 +- source/game/StarItemRecipe.cpp | 7 ++++--- source/game/StarItemRecipe.hpp | 4 ++-- source/game/StarTileDrawer.cpp | 4 +++- source/game/scripting/StarPlayerLuaBindings.cpp | 11 +++++++++++ source/game/scripting/StarRootLuaBindings.cpp | 17 ++++++++++++++--- source/game/scripting/StarRootLuaBindings.hpp | 3 ++- 9 files changed, 39 insertions(+), 13 deletions(-) diff --git a/source/core/StarJsonParser.hpp b/source/core/StarJsonParser.hpp index 5736f7f..a76a53a 100644 --- a/source/core/StarJsonParser.hpp +++ b/source/core/StarJsonParser.hpp @@ -39,7 +39,7 @@ template class JsonParser { public: JsonParser(JsonStream& stream) - : m_line(0), m_column(0), m_stream(stream), m_error(nullptr) {} + : m_line(0), m_column(0), m_error(nullptr), m_stream(stream) {} virtual ~JsonParser() {} // Does not throw. On error, returned iterator will not be equal to end, and diff --git a/source/game/StarItemDatabase.cpp b/source/game/StarItemDatabase.cpp index c3f2447..b3eb16b 100644 --- a/source/game/StarItemDatabase.cpp +++ b/source/game/StarItemDatabase.cpp @@ -375,7 +375,7 @@ ItemRecipe ItemDatabase::parseRecipe(Json const& config) const { return res; } -HashSet ItemDatabase::allRecipes() const { +HashSet const& ItemDatabase::allRecipes() const { return m_recipes; } diff --git a/source/game/StarItemDatabase.hpp b/source/game/StarItemDatabase.hpp index 50a8e35..d5b86fc 100644 --- a/source/game/StarItemDatabase.hpp +++ b/source/game/StarItemDatabase.hpp @@ -138,7 +138,7 @@ public: ItemRecipe parseRecipe(Json const& config) const; - HashSet allRecipes() const; + HashSet const& allRecipes() const; HashSet allRecipes(StringSet const& types) const; ItemPtr applyAugment(ItemPtr const item, AugmentItem* augment) const; diff --git a/source/game/StarItemRecipe.cpp b/source/game/StarItemRecipe.cpp index 7295cd1..1ba4b70 100644 --- a/source/game/StarItemRecipe.cpp +++ b/source/game/StarItemRecipe.cpp @@ -5,9 +5,10 @@ namespace Star { -Json ItemRecipe::toJson() { +Json ItemRecipe::toJson() const { JsonArray inputList; - for (auto input : inputs) + inputList.reserve(inputList.size()); + for (auto& input : inputs) inputList.append(input.toJson()); return JsonObject{ @@ -21,7 +22,7 @@ Json ItemRecipe::toJson() { }; } -bool ItemRecipe::isNull() { +bool ItemRecipe::isNull() const { return currencyInputs.size() == 0 && inputs.size() == 0 && output.isNull(); } diff --git a/source/game/StarItemRecipe.hpp b/source/game/StarItemRecipe.hpp index b2d51d7..5285c9a 100644 --- a/source/game/StarItemRecipe.hpp +++ b/source/game/StarItemRecipe.hpp @@ -8,9 +8,9 @@ namespace Star { STAR_EXCEPTION(RecipeException, StarException); struct ItemRecipe { - Json toJson(); + Json toJson() const; - bool isNull(); + bool isNull() const; bool operator==(ItemRecipe const& rhs) const; bool operator!=(ItemRecipe const& rhs) const; diff --git a/source/game/StarTileDrawer.cpp b/source/game/StarTileDrawer.cpp index 2fa5051..15e88f3 100644 --- a/source/game/StarTileDrawer.cpp +++ b/source/game/StarTileDrawer.cpp @@ -100,9 +100,11 @@ bool TileDrawer::produceTerrainDrawables(Drawables& drawables, return false; auto getPieceImage = [](MaterialRenderPieceConstPtr const& piece, Box const& box, MaterialHue hue, Directives const* directives) -> AssetPath { - AssetPath image = hue == 0 + String path = (hue == 0) ? strf("{}?crop={};{};{};{}", piece->texture, box.xMin(), box.yMin(), box.xMax(), box.yMax()) : strf("{}?crop={};{};{};{}?hueshift={}", piece->texture, box.xMin(), box.yMin(), box.xMax(), box.yMax(), materialHueToDegrees(hue)); + + AssetPath image(path); if (directives) image.directives += *directives; diff --git a/source/game/scripting/StarPlayerLuaBindings.cpp b/source/game/scripting/StarPlayerLuaBindings.cpp index d303c94..5f12ee3 100644 --- a/source/game/scripting/StarPlayerLuaBindings.cpp +++ b/source/game/scripting/StarPlayerLuaBindings.cpp @@ -247,6 +247,17 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { callbacks.registerCallback("blueprintKnown", [player](Json const& item) { return player->blueprintKnown(ItemDescriptor(item)); }); + callbacks.registerCallback("availableRecipes", [player](Maybe const& filter) { + auto itemDatabase = Root::singleton().itemDatabase(); + auto inventory = player->inventory(); + auto recipes = itemDatabase->recipesFromBagContents(inventory->availableItems(), inventory->availableCurrencies(), filter.value()); + JsonArray result; + result.reserve(recipes.size()); + for (auto& recipe : recipes) + result.append(recipe.toJson()); + return result; + }); + callbacks.registerCallback("makeTechAvailable", [player](String const& tech) { player->techs()->makeAvailable(tech); }); diff --git a/source/game/scripting/StarRootLuaBindings.cpp b/source/game/scripting/StarRootLuaBindings.cpp index a08be7f..50d18e8 100644 --- a/source/game/scripting/StarRootLuaBindings.cpp +++ b/source/game/scripting/StarRootLuaBindings.cpp @@ -44,7 +44,8 @@ LuaCallbacks LuaBindings::makeRootCallbacks() { callbacks.registerCallbackWithSignature("npcConfig", bind(RootCallbacks::npcConfig, root, _1)); callbacks.registerCallbackWithSignature("projectileGravityMultiplier", bind(RootCallbacks::projectileGravityMultiplier, root, _1)); callbacks.registerCallbackWithSignature("projectileConfig", bind(RootCallbacks::projectileConfig, root, _1)); - callbacks.registerCallbackWithSignature("recipesForItem", bind(RootCallbacks::recipesForItem, root, _1)); + callbacks.registerCallbackWithSignature("recipesForItem", bind(RootCallbacks::recipesForItem, root, _1)); + callbacks.registerCallbackWithSignature("allRecipes", bind(RootCallbacks::allRecipes, root)); callbacks.registerCallbackWithSignature("itemType", bind(RootCallbacks::itemType, root, _1)); callbacks.registerCallbackWithSignature("itemTags", bind(RootCallbacks::itemTags, root, _1)); callbacks.registerCallbackWithSignature("itemHasTag", bind(RootCallbacks::itemHasTag, root, _1, _2)); @@ -308,10 +309,20 @@ Json LuaBindings::RootCallbacks::projectileConfig(Root* root, String const& arg1 return projectileDatabase->projectileConfig(arg1); } -Json LuaBindings::RootCallbacks::recipesForItem(Root* root, String const& arg1) { +JsonArray LuaBindings::RootCallbacks::recipesForItem(Root* root, String const& arg1) { auto recipes = root->itemDatabase()->recipesForOutputItem(arg1); JsonArray result; - for (auto recipe : recipes) + result.reserve(recipes.size()); + for (auto& recipe : recipes) + result.append(recipe.toJson()); + return result; +} + +JsonArray LuaBindings::RootCallbacks::allRecipes(Root* root) { + auto& recipes = root->itemDatabase()->allRecipes(); + JsonArray result; + result.reserve(recipes.size()); + for (auto& recipe : recipes) result.append(recipe.toJson()); return result; } diff --git a/source/game/scripting/StarRootLuaBindings.hpp b/source/game/scripting/StarRootLuaBindings.hpp index 3979f2f..0cee77f 100644 --- a/source/game/scripting/StarRootLuaBindings.hpp +++ b/source/game/scripting/StarRootLuaBindings.hpp @@ -26,7 +26,8 @@ namespace LuaBindings { Json npcConfig(Root* root, String const& arg1); float projectileGravityMultiplier(Root* root, String const& arg1); Json projectileConfig(Root* root, String const& arg1); - Json recipesForItem(Root* root, String const& arg1); + JsonArray recipesForItem(Root* root, String const& arg1); + JsonArray allRecipes(Root* root); String itemType(Root* root, String const& itemName); Json itemTags(Root* root, String const& itemName); bool itemHasTag(Root* root, String const& itemName, String const& itemTag);