Compare commits

...

3 Commits

10 changed files with 41 additions and 15 deletions

View File

@ -151,7 +151,7 @@ Assets::Assets(Settings settings, StringList assetSources) {
// re-add the assets callbacks with more functions // re-add the assets callbacks with more functions
context.remove("assets"); context.remove("assets");
auto callbacks = makeBaseAssetCallbacks(); auto callbacks = makeBaseAssetCallbacks();
callbacks.registerCallback("add", [&newFiles](LuaEngine& engine, String const& path, LuaValue const& data) { callbacks.registerCallback("add", [newFiles](LuaEngine& engine, String const& path, LuaValue const& data) {
ByteArray bytes; ByteArray bytes;
if (auto str = engine.luaMaybeTo<String>(data)) if (auto str = engine.luaMaybeTo<String>(data))
bytes = ByteArray(str->utf8Ptr(), str->utf8Size()); bytes = ByteArray(str->utf8Ptr(), str->utf8Size());
@ -165,7 +165,7 @@ Assets::Assets(Settings settings, StringList assetSources) {
newFiles->set(path, bytes); newFiles->set(path, bytes);
}); });
callbacks.registerCallback("patch", [this, &newFiles](String const& path, String const& patchPath) -> bool { callbacks.registerCallback("patch", [this, newFiles](String const& path, String const& patchPath) -> bool {
if (auto file = m_files.ptr(path)) { if (auto file = m_files.ptr(path)) {
if (newFiles->contains(patchPath)) { if (newFiles->contains(patchPath)) {
file->patchSources.append(make_pair(patchPath, newFiles)); file->patchSources.append(make_pair(patchPath, newFiles));

View File

@ -39,7 +39,7 @@ template <typename InputIterator>
class JsonParser { class JsonParser {
public: public:
JsonParser(JsonStream& stream) 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() {} virtual ~JsonParser() {}
// Does not throw. On error, returned iterator will not be equal to end, and // 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; return res;
} }
HashSet<ItemRecipe> ItemDatabase::allRecipes() const { HashSet<ItemRecipe> const& ItemDatabase::allRecipes() const {
return m_recipes; return m_recipes;
} }

View File

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

View File

@ -5,9 +5,10 @@
namespace Star { namespace Star {
Json ItemRecipe::toJson() { Json ItemRecipe::toJson() const {
JsonArray inputList; JsonArray inputList;
for (auto input : inputs) inputList.reserve(inputList.size());
for (auto& input : inputs)
inputList.append(input.toJson()); inputList.append(input.toJson());
return JsonObject{ 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(); return currencyInputs.size() == 0 && inputs.size() == 0 && output.isNull();
} }

View File

@ -8,9 +8,9 @@ namespace Star {
STAR_EXCEPTION(RecipeException, StarException); STAR_EXCEPTION(RecipeException, StarException);
struct ItemRecipe { struct ItemRecipe {
Json toJson(); Json toJson() const;
bool isNull(); bool isNull() const;
bool operator==(ItemRecipe const& rhs) const; bool operator==(ItemRecipe const& rhs) 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; return false;
auto getPieceImage = [](MaterialRenderPieceConstPtr const& piece, Box<float, 2> const& box, MaterialHue hue, Directives const* directives) -> AssetPath { 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={};{};{};{}", 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)); : strf("{}?crop={};{};{};{}?hueshift={}", piece->texture, box.xMin(), box.yMin(), box.xMax(), box.yMax(), materialHueToDegrees(hue));
AssetPath image(path);
if (directives) if (directives)
image.directives += *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("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) { callbacks.registerCallback("makeTechAvailable", [player](String const& tech) {
player->techs()->makeAvailable(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<Json, String>("npcConfig", bind(RootCallbacks::npcConfig, root, _1));
callbacks.registerCallbackWithSignature<float, String>("projectileGravityMultiplier", bind(RootCallbacks::projectileGravityMultiplier, 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>("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<String, String>("itemType", bind(RootCallbacks::itemType, root, _1));
callbacks.registerCallbackWithSignature<Json, String>("itemTags", bind(RootCallbacks::itemTags, root, _1)); callbacks.registerCallbackWithSignature<Json, String>("itemTags", bind(RootCallbacks::itemTags, root, _1));
callbacks.registerCallbackWithSignature<bool, String, String>("itemHasTag", bind(RootCallbacks::itemHasTag, root, _1, _2)); 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); 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); auto recipes = root->itemDatabase()->recipesForOutputItem(arg1);
JsonArray result; 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()); result.append(recipe.toJson());
return result; return result;
} }

View File

@ -26,7 +26,8 @@ namespace LuaBindings {
Json npcConfig(Root* root, String const& arg1); Json npcConfig(Root* root, String const& arg1);
float projectileGravityMultiplier(Root* root, String const& arg1); float projectileGravityMultiplier(Root* root, String const& arg1);
Json projectileConfig(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); String itemType(Root* root, String const& itemName);
Json itemTags(Root* root, String const& itemName); Json itemTags(Root* root, String const& itemName);
bool itemHasTag(Root* root, String const& itemName, String const& itemTag); bool itemHasTag(Root* root, String const& itemName, String const& itemTag);