123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- #include "Recipie.h"
- #include <Logging.h>
- #include "CraftingStorage.h"
- #include "Game.h"
- #include "Item.h"
- RecipieInput::RecipieInput()
- : ReferenceCounter(),
- filter(0),
- modifier(0),
- amount(0)
- {}
- RecipieInput::~RecipieInput()
- {
- if (filter) filter->release();
- if (modifier) modifier->release();
- }
- void RecipieInput::setFilter(ItemFilter* filter)
- {
- if (this->filter) this->filter->release();
- this->filter = filter;
- }
- ItemFilter* RecipieInput::zFilter() const
- {
- return filter;
- }
- void RecipieInput::setModifier(ItemModifier* modifier)
- {
- if (this->modifier) this->modifier->release();
- this->modifier = modifier;
- }
- ItemModifier* RecipieInput::zModifier() const
- {
- return modifier;
- }
- void RecipieInput::setAmount(int amount)
- {
- this->amount = amount;
- }
- int RecipieInput::getAmount() const
- {
- return amount;
- }
- RecipieInputFactory::RecipieInputFactory()
- : TypeFactory()
- {}
- RecipieInput* RecipieInputFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new RecipieInput();
- }
- void RecipieInputFactory::fromJson(
- RecipieInput* zResult, Framework::JSON::JSONObject* zJson) const
- {
- zResult->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
- zJson->asObject()->zValue("filter")));
- zResult->setModifier(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
- zJson->asObject()->zValue("modifier")));
- zResult->setAmount(
- (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
- }
- void RecipieInputFactory::toJson(
- RecipieInput* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue(
- "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
- zResult->addValue("modifier",
- Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
- zResult->addValue("amount",
- new Framework::JSON::JSONNumber((double)zObject->getAmount()));
- }
- JSONObjectValidationBuilder* RecipieInputFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- Framework::JSON::JSONObject* defaultModifier
- = new Framework::JSON::JSONObject();
- defaultModifier->addValue(
- "type", new Framework::JSON::JSONString("consume"));
- return builder
- ->withRequiredAttribute("filter",
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
- ->withRequiredAttribute("modifier",
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
- ->withRequiredObject("modifier")
- ->withRequiredString("type")
- ->withExactMatch("consume")
- ->finishString()
- ->withDefault(defaultModifier)
- ->finishObject()
- ->withRequiredNumber("amount")
- ->whichIsGreaterOrEqual(1.0)
- ->withDefault(1.0)
- ->finishNumber();
- }
- RecipieOutput::RecipieOutput()
- : ReferenceCounter(),
- itemTypeId(0),
- amount(0),
- modifier(0)
- {}
- RecipieOutput::~RecipieOutput()
- {
- modifier->release();
- }
- void RecipieOutput::setItemTypeId(int itemTypeId)
- {
- this->itemTypeId = itemTypeId;
- }
- int RecipieOutput::getItemTypeId() const
- {
- return itemTypeId;
- }
- void RecipieOutput::setAmount(int amount)
- {
- this->amount = amount;
- }
- int RecipieOutput::getAmount() const
- {
- return amount;
- }
- void RecipieOutput::setModifier(ItemModifier* modifier)
- {
- if (this->modifier) this->modifier->release();
- this->modifier = modifier;
- }
- ItemModifier* RecipieOutput::zModifier() const
- {
- return modifier;
- }
- Item* RecipieOutput::createItem() const
- {
- Item* result = Game::INSTANCE->zItemType(itemTypeId)->createItem();
- if (result)
- {
- modifier->applyOn(result);
- }
- return result;
- }
- RecipieOutputFactory::RecipieOutputFactory()
- : TypeFactory()
- {}
- RecipieOutput* RecipieOutputFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new RecipieOutput();
- }
- void RecipieOutputFactory::fromJson(
- RecipieOutput* zResult, Framework::JSON::JSONObject* zJson) const
- {
- zResult->setItemTypeId(Game::INSTANCE->getItemTypeId(
- zJson->asObject()->zValue("itemType")->asString()->getString()));
- zResult->setAmount(
- (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
- zResult->setModifier(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
- zJson->asObject()->zValue("modifier")));
- }
- void RecipieOutputFactory::toJson(
- RecipieOutput* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue("itemType",
- new Framework::JSON::JSONString(
- Game::INSTANCE->zItemType(zObject->getItemTypeId())->getName()));
- zResult->addValue("amount",
- new Framework::JSON::JSONNumber((double)zObject->getAmount()));
- zResult->addValue("modifier",
- Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
- }
- JSONObjectValidationBuilder* RecipieOutputFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- Framework::JSON::JSONObject* defaultModifier
- = new Framework::JSON::JSONObject();
- defaultModifier->addValue(
- "type", new Framework::JSON::JSONString("doNothing"));
- Framework::RCArray<Framework::Text> itemTypes;
- for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
- {
- if (Game::INSTANCE->zItemType(i))
- {
- itemTypes.add(
- new Framework::Text(Game::INSTANCE->zItemType(i)->getName()));
- }
- }
- return builder->withRequiredString("itemType")
- ->whichIsOneOf(itemTypes)
- ->finishString()
- ->withRequiredAttribute("modifier",
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
- ->withRequiredObject("modifier")
- ->withRequiredString("type")
- ->withExactMatch("doNothing")
- ->finishString()
- ->withDefault(defaultModifier)
- ->finishObject()
- ->withRequiredNumber("amount")
- ->whichIsGreaterOrEqual(1.0)
- ->withDefault(1.0)
- ->finishNumber();
- }
- Recipie::Recipie()
- : ReferenceCounter()
- {}
- void Recipie::addOutput(RecipieOutput* outputs)
- {
- this->outputs.add(outputs);
- }
- Framework::Array<ItemInfo> Recipie::getOutput() const
- {
- Framework::Array<ItemInfo> result;
- for (const RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- result.add({item->zItemType()->getId(),
- output->getAmount(),
- item->getHp(),
- item->getMaxHp(),
- item->getDurability(),
- item->getMaxDurability()});
- item->release();
- }
- }
- return result;
- }
- bool Recipie::createsOutput(int itemTypeId)
- {
- for (RecipieOutput* output : outputs)
- {
- if (output->getItemTypeId() == itemTypeId)
- {
- return 1;
- }
- }
- return 0;
- }
- void Recipie::setGroupName(Framework::Text groupName)
- {
- this->groupName = groupName;
- }
- const Framework::RCArray<RecipieOutput>& Recipie::getOutputs() const
- {
- return outputs;
- }
- Framework::Text Recipie::getGroupName() const
- {
- return groupName;
- }
- UnshapedRecipie::UnshapedRecipie()
- : Recipie()
- {}
- bool UnshapedRecipie::testApplicability(CraftingStorage* zStorage)
- {
- for (RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- if (!zStorage->hasFreeSpace(item, output->getAmount()))
- {
- item->release();
- return 0;
- }
- item->release();
- }
- }
- return zStorage->isAllAvailable(inputs);
- }
- void UnshapedRecipie::apply(CraftingStorage* zStorage)
- {
- zStorage->consume(inputs);
- for (RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- ItemStack* stack = new ItemStack(item, output->getAmount());
- zStorage->addCraftingResult(stack);
- stack->release();
- }
- }
- }
- Framework::Text UnshapedRecipie::getRecipieUIML()
- {
- Framework::Text result = "<recipie type=\"unshaped\"><ingredients>";
- for (RecipieInput* input : inputs)
- {
- result.append() << "<ingredient amount=\"" << input->getAmount()
- << "\">";
- if (input->zFilter())
- {
- result.append()
- << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
- }
- result += "</ingredient>";
- // TODO: add modifiers
- }
- result += "</ingredients><outputs>";
- for (RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- result.append()
- << "<output amount=\"" << output->getAmount()
- << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
- << item->getHp() << "\" durability=\"" << item->getDurability()
- << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
- << item->getMaxDurability() << "\">" << item->getTooltipUIML()
- << "</output>";
- item->release();
- }
- }
- result += "</outputs></recipie>";
- return result;
- }
- void UnshapedRecipie::addInput(RecipieInput* input)
- {
- inputs.add(input);
- }
- const Framework::RCArray<RecipieInput>& UnshapedRecipie::getInputs() const
- {
- return inputs;
- }
- UnshapedRecipieFactory::UnshapedRecipieFactory()
- : RecipieFactory()
- {}
- UnshapedRecipie* UnshapedRecipieFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new UnshapedRecipie();
- }
- void UnshapedRecipieFactory::fromJson(
- UnshapedRecipie* zResult, Framework::JSON::JSONObject* zJson) const
- {
- for (Framework::JSON::JSONValue* input :
- *zJson->zValue("inputs")->asArray())
- {
- zResult->addInput(
- Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(input));
- }
- RecipieFactory::fromJson(zResult, zJson);
- }
- void UnshapedRecipieFactory::toJson(
- UnshapedRecipie* zObject, Framework::JSON::JSONObject* zResult) const
- {
- Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
- for (RecipieInput* input : zObject->getInputs())
- {
- inputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(input));
- }
- zResult->addValue("inputs", inputs);
- RecipieFactory::toJson(zObject, zResult);
- }
- JSONObjectValidationBuilder* UnshapedRecipieFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return RecipieFactory::addToValidator(
- builder->withRequiredArray("inputs")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
- ->finishArray());
- }
- Framework::Text UnshapedRecipieFactory::getTypeToken() const
- {
- return "unshaped";
- }
- ShapedRecipie::ShapedRecipie()
- : Recipie(),
- width(0),
- height(0)
- {}
- bool ShapedRecipie::testApplicability(CraftingStorage* zStorage)
- {
- ShapedCraftingStorage* zShapedStorage
- = dynamic_cast<ShapedCraftingStorage*>(zStorage);
- if (!zShapedStorage) return 0;
- for (RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- if (!zShapedStorage->hasFreeSpace(item, output->getAmount()))
- {
- item->release();
- return 0;
- }
- item->release();
- }
- }
- return zShapedStorage->isAllAvailable(inputs, width, height);
- }
- void ShapedRecipie::apply(CraftingStorage* zStorage)
- {
- ShapedCraftingStorage* zShapedStorage
- = dynamic_cast<ShapedCraftingStorage*>(zStorage);
- zShapedStorage->consume(inputs, width, height);
- for (RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- ItemStack* stack = new ItemStack(item, output->getAmount());
- zStorage->addCraftingResult(stack);
- stack->release();
- }
- }
- }
- Framework::Text ShapedRecipie::getRecipieUIML()
- {
- Framework::Text result = "<recipie type=\"shaped\" width=\"";
- result.append() << width << "\" height=\"" << height << "\"><ingredients>";
- for (RecipieInput* input : inputs)
- {
- result.append() << "<ingredient amount=\"" << input->getAmount()
- << "\">";
- if (input->zFilter())
- {
- result.append()
- << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
- }
- result += "</ingredient>";
- // TODO: add modifiers
- }
- result += "</ingredients><outputs>";
- for (RecipieOutput* output : outputs)
- {
- Item* item = output->createItem();
- if (item)
- {
- result.append()
- << "<output amount=\"" << output->getAmount()
- << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
- << item->getHp() << "\" durability=\"" << item->getDurability()
- << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
- << item->getMaxDurability() << "\">" << item->getTooltipUIML()
- << "</output>";
- item->release();
- }
- }
- result += "</outputs></recipie>";
- return result;
- }
- void ShapedRecipie::setWidth(int width)
- {
- this->width = width;
- }
- int ShapedRecipie::getWidth() const
- {
- return width;
- }
- void ShapedRecipie::setHeight(int height)
- {
- this->height = height;
- }
- int ShapedRecipie::getHeight() const
- {
- return height;
- }
- void ShapedRecipie::addInput(RecipieInput* input)
- {
- inputs.add(input);
- }
- void ShapedRecipie::setInput(int index, RecipieInput* input)
- {
- inputs.set(input, index);
- }
- const Framework::RCArray<RecipieInput>& ShapedRecipie::getInputs() const
- {
- return inputs;
- }
- ShapedRecipieFactory::ShapedRecipieFactory()
- : RecipieFactory()
- {}
- ShapedRecipie* ShapedRecipieFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new ShapedRecipie();
- }
- void ShapedRecipieFactory::fromJson(
- ShapedRecipie* zResult, Framework::JSON::JSONObject* zJson) const
- {
- int width = (int)zJson->zValue("width")->asNumber()->getNumber();
- int height = (int)zJson->zValue("height")->asNumber()->getNumber();
- for (int i = 0; i < width * height; i++)
- {
- zResult->addInput(new RecipieInput());
- }
- for (Framework::JSON::JSONValue* input :
- *zJson->zValue("inputs")->asArray())
- {
- int x = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
- int y = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
- if (x >= width || y >= height)
- {
- Framework::Logging::warning()
- << "Invalid input position in shaped recipie with width="
- << width << ", height=" << height << ", x=" << x << ", y=" << y;
- return;
- }
- zResult->setInput(y * width + x,
- Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(
- input->asObject()->zValue("input")));
- }
- zResult->setWidth((int)zJson->zValue("width")->asNumber()->getNumber());
- zResult->setHeight((int)zJson->zValue("height")->asNumber()->getNumber());
- RecipieFactory::fromJson(zResult, zJson);
- }
- void ShapedRecipieFactory::toJson(
- ShapedRecipie* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue(
- "width", new Framework::JSON::JSONNumber(zObject->getWidth()));
- zResult->addValue(
- "height", new Framework::JSON::JSONNumber(zObject->getHeight()));
- Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
- for (int i = 0; i < zObject->getWidth() * zObject->getHeight(); i++)
- {
- Framework::JSON::JSONObject* input = new Framework::JSON::JSONObject();
- input->addValue(
- "x", new Framework::JSON::JSONNumber(i % zObject->getHeight()));
- input->addValue(
- "y", new Framework::JSON::JSONNumber(i / zObject->getHeight()));
- input->addValue("input",
- Game::INSTANCE->zTypeRegistry()->toJson(zObject->getInputs().z(i)));
- inputs->addValue(input);
- }
- zResult->addValue("inputs", inputs);
- RecipieFactory::toJson(zObject, zResult);
- }
- JSONObjectValidationBuilder* ShapedRecipieFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return RecipieFactory::addToValidator(
- builder->withRequiredArray("inputs")
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("x")
- ->whichIsGreaterOrEqual(0.0)
- ->finishNumber()
- ->withRequiredNumber("y")
- ->whichIsGreaterOrEqual(0.0)
- ->finishNumber()
- ->withRequiredAttribute("input",
- Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
- ->finishObject()
- ->finishArray()
- ->withRequiredNumber("width")
- ->whichIsGreaterOrEqual(1.0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterOrEqual(1.0)
- ->finishNumber());
- }
- Framework::Text ShapedRecipieFactory::getTypeToken() const
- {
- return "shaped";
- }
|