player.availableRecipes and root.allRecipes

This commit is contained in:
Kae 2024-06-03 15:47:18 +10:00
parent add17da988
commit 5d1e85b241
9 changed files with 39 additions and 13 deletions

View File

@ -39,7 +39,7 @@ template <typename InputIterator>
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

View File

@ -375,7 +375,7 @@ ItemRecipe ItemDatabase::parseRecipe(Json const& config) const {
return res;
}
HashSet<ItemRecipe> ItemDatabase::allRecipes() const {
HashSet<ItemRecipe> const& ItemDatabase::allRecipes() const {
return m_recipes;
}

View File

@ -138,7 +138,7 @@ public:
ItemRecipe parseRecipe(Json const& config) const;
HashSet<ItemRecipe> allRecipes() const;
HashSet<ItemRecipe> const& allRecipes() const;
HashSet<ItemRecipe> allRecipes(StringSet const& types) const;
ItemPtr applyAugment(ItemPtr const item, AugmentItem* augment) const;

View File

@ -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();
}

View File

@ -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;

View File

@ -100,9 +100,11 @@ bool TileDrawer::produceTerrainDrawables(Drawables& drawables,
return false;
auto getPieceImage = [](MaterialRenderPieceConstPtr const& piece, Box<float, 2> 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;

View File

@ -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<StringSet> 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);
});

View File

@ -44,7 +44,8 @@ LuaCallbacks LuaBindings::makeRootCallbacks() {
callbacks.registerCallbackWithSignature<Json, String>("npcConfig", bind(RootCallbacks::npcConfig, root, _1));
callbacks.registerCallbackWithSignature<float, String>("projectileGravityMultiplier", bind(RootCallbacks::projectileGravityMultiplier, root, _1));
callbacks.registerCallbackWithSignature<Json, String>("projectileConfig", bind(RootCallbacks::projectileConfig, root, _1));
callbacks.registerCallbackWithSignature<Json, String>("recipesForItem", bind(RootCallbacks::recipesForItem, root, _1));
callbacks.registerCallbackWithSignature<JsonArray, String>("recipesForItem", bind(RootCallbacks::recipesForItem, root, _1));
callbacks.registerCallbackWithSignature<JsonArray>("allRecipes", bind(RootCallbacks::allRecipes, root));
callbacks.registerCallbackWithSignature<String, String>("itemType", bind(RootCallbacks::itemType, root, _1));
callbacks.registerCallbackWithSignature<Json, String>("itemTags", bind(RootCallbacks::itemTags, root, _1));
callbacks.registerCallbackWithSignature<bool, String, String>("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;
}

View File

@ -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);