123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- #include "Recipie.h"
- #include "CraftingStorage.h"
- #include "Game.h"
- #include "Item.h"
- RecipieInput::RecipieInput(
- ItemFilter* filter, ItemModifier* modifier, int amount)
- : ReferenceCounter(),
- filter(filter),
- modifier(modifier),
- amount(amount)
- {}
- RecipieInput::~RecipieInput()
- {
- if (filter) filter->release();
- if (modifier) modifier->release();
- }
- ItemFilter* RecipieInput::zFilter() const
- {
- return filter;
- }
- ItemModifier* RecipieInput::zModifier() const
- {
- return modifier;
- }
- int RecipieInput::getAmount() const
- {
- return amount;
- }
- RecipieInputFactory::RecipieInputFactory()
- : TypeFactory()
- {}
- RecipieInput* RecipieInputFactory::fromJson(
- Framework::JSON::JSONValue* zJson) const
- {
- return new RecipieInput(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
- zJson->asObject()->zValue("filter")),
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
- zJson->asObject()->zValue("modifier")),
- (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
- }
- Framework::JSON::JSONValue* RecipieInputFactory::toJson(
- RecipieInput* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
- result->addValue("modifier",
- Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
- result->addValue("amount",
- new Framework::JSON::JSONNumber((double)zObject->getAmount()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- RecipieInputFactory::getValidator() const
- {
- Framework::JSON::JSONObject* defaultModifier
- = new Framework::JSON::JSONObject();
- defaultModifier->addValue(
- "type", new Framework::JSON::JSONString("consume"));
- return Framework::JSON::Validator::JSONValidator::buildForObject()
- ->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()
- ->finishObject();
- }
- RecipieOutput::RecipieOutput(int itemTypeId, int amount, ItemModifier* modifier)
- : ReferenceCounter(),
- itemTypeId(itemTypeId),
- amount(amount),
- modifier(modifier)
- {}
- RecipieOutput::~RecipieOutput()
- {
- modifier->release();
- }
- int RecipieOutput::getItemTypeId() const
- {
- return itemTypeId;
- }
- int RecipieOutput::getAmount() const
- {
- return amount;
- }
- 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::fromJson(
- Framework::JSON::JSONValue* zJson) const
- {
- return new RecipieOutput(
- Game::INSTANCE->getItemTypeId(
- zJson->asObject()->zValue("itemType")->asString()->getString()),
- (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
- zJson->asObject()->zValue("modifier")));
- }
- Framework::JSON::JSONValue* RecipieOutputFactory::toJson(
- RecipieOutput* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("itemType",
- new Framework::JSON::JSONString(
- Game::INSTANCE->zItemType(zObject->getItemTypeId())->getName()));
- result->addValue("amount",
- new Framework::JSON::JSONNumber((double)zObject->getAmount()));
- result->addValue("modifier",
- Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- RecipieOutputFactory::getValidator() 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 Framework::JSON::Validator::JSONValidator::buildForObject()
- ->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()
- ->finishObject();
- }
- Recipie::Recipie(
- Framework::RCArray<RecipieOutput> outputs, Framework::Text groupName)
- : ReferenceCounter(),
- groupName(groupName),
- outputs(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;
- }
- const Framework::RCArray<RecipieOutput>& Recipie::getOutputs() const
- {
- return outputs;
- }
- Framework::Text Recipie::getGroupName() const
- {
- return groupName;
- }
- UnshapedRecipie::UnshapedRecipie(Framework::RCArray<RecipieInput> inputs,
- Framework::RCArray<RecipieOutput> outputs,
- Framework::Text groupName)
- : Recipie(outputs, groupName),
- inputs(inputs)
- {}
- 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;
- }
- const Framework::RCArray<RecipieInput>& UnshapedRecipie::getInputs() const
- {
- return inputs;
- }
- UnshapedRecipieFactory::UnshapedRecipieFactory()
- : SubTypeFactory()
- {}
- UnshapedRecipie* UnshapedRecipieFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<RecipieInput> inputs;
- Framework::RCArray<RecipieOutput> outputs;
- for (Framework::JSON::JSONValue* input :
- *zJson->zValue("inputs")->asArray())
- {
- inputs.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(input));
- }
- for (Framework::JSON::JSONValue* output :
- *zJson->zValue("outputs")->asArray())
- {
- outputs.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<RecipieOutput>(output));
- }
- return new UnshapedRecipie(
- inputs, outputs, zJson->zValue("group")->asString()->getString());
- }
- Framework::JSON::JSONObject* UnshapedRecipieFactory::toJson(
- UnshapedRecipie* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
- for (RecipieInput* input : zObject->getInputs())
- {
- inputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(input));
- }
- result->addValue("inputs", inputs);
- Framework::JSON::JSONArray* outputs = new Framework::JSON::JSONArray();
- for (RecipieOutput* output : zObject->getOutputs())
- {
- inputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(output));
- }
- result->addValue("outputs", outputs);
- result->addValue(
- "group", new Framework::JSON::JSONString(zObject->getGroupName()));
- return nullptr;
- }
- Framework::JSON::Validator::JSONValidator* UnshapedRecipieFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("inputs")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
- ->finishArray()
- ->withRequiredArray("outputs")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<RecipieOutput>())
- ->finishArray()
- ->withRequiredString("group")
- ->finishString()
- ->finishObject();
- }
- Framework::Text UnshapedRecipieFactory::getTypeToken() const
- {
- return "unshaped";
- }
- ShapedRecipie::ShapedRecipie(int width,
- int height,
- Framework::RCArray<RecipieInput> inputs,
- Framework::RCArray<RecipieOutput> outputs,
- Framework::Text groupName)
- : Recipie(outputs, groupName),
- inputs(inputs),
- width(width),
- height(height)
- {
- while (this->inputs.getEintragAnzahl() < width * height)
- {
- this->inputs.add(new RecipieInput(0, 0, 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;
- }
- int ShapedRecipie::getWidth() const
- {
- return width;
- }
- int ShapedRecipie::getHeight() const
- {
- return height;
- }
- const Framework::RCArray<RecipieInput>& ShapedRecipie::getInputs() const
- {
- return inputs;
- }
- ShapedRecipieFactory::ShapedRecipieFactory()
- : SubTypeFactory()
- {}
- ShapedRecipie* ShapedRecipieFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- int width = (int)zJson->zValue("width")->asNumber()->getNumber();
- int height = (int)zJson->zValue("height")->asNumber()->getNumber();
- Framework::RCArray<RecipieInput> inputs;
- for (int i = 0; i < width * height; i++)
- {
- inputs.add(new RecipieInput(0, 0, 0));
- }
- 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)
- {
- std::cout << "Invalid input position in shaped recipie with width="
- << width << ", height=" << height << "\n"
- << (x >= width ? x : y) << "\n";
- return 0;
- }
- inputs.set(Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(
- input->asObject()->zValue("input")),
- y * width + x);
- }
- Framework::RCArray<RecipieOutput> outputs;
- for (Framework::JSON::JSONValue* output :
- *zJson->zValue("outputs")->asArray())
- {
- outputs.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<RecipieOutput>(output));
- }
- return new ShapedRecipie(
- (int)zJson->zValue("width")->asNumber()->getNumber(),
- (int)zJson->zValue("height")->asNumber()->getNumber(),
- inputs,
- outputs,
- zJson->zValue("group")->asString()->getString());
- }
- Framework::JSON::JSONObject* ShapedRecipieFactory::toJson(
- ShapedRecipie* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "width", new Framework::JSON::JSONNumber(zObject->getWidth()));
- result->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);
- }
- result->addValue("inputs", inputs);
- Framework::JSON::JSONArray* outputs = new Framework::JSON::JSONArray();
- for (RecipieOutput* output : zObject->getOutputs())
- {
- outputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(output));
- }
- result->addValue("outputs", outputs);
- result->addValue(
- "group", new Framework::JSON::JSONString(zObject->getGroupName()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator* ShapedRecipieFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return 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()
- ->withRequiredArray("outputs")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<RecipieOutput>())
- ->finishArray()
- ->withRequiredNumber("width")
- ->whichIsGreaterOrEqual(1.0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterOrEqual(1.0)
- ->finishNumber()
- ->withRequiredString("group")
- ->finishString()
- ->finishObject();
- }
- Framework::Text ShapedRecipieFactory::getTypeToken() const
- {
- return "shaped";
- }
|