123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642 |
- #include "ItemFilter.h"
- #include "Game.h"
- #include "Item.h"
- #include "ItemSlot.h"
- #include "ItemStack.h"
- #include "ItemType.h"
- ItemFilter::ItemFilter()
- : ReferenceCounter()
- {}
- bool ItemFilter::matchItem(const Item* zItem) const
- {
- return 1;
- }
- bool ItemFilter::matchSourceSlot(ItemSlot* zSlot) const
- {
- return zSlot->zStack() ? matchItem(zSlot->zStack()->zItem()) : 0;
- }
- bool ItemFilter::matchTargetSlot(ItemSlot* zSlot) const
- {
- return 1;
- }
- CombinedItemFilter::CombinedItemFilter(
- Framework::RCArray<ItemFilter> filters, std::function<bool(bool, bool)> op)
- : ItemFilter(),
- filters(filters),
- op(op)
- {}
- bool CombinedItemFilter::matchItem(const Item* zItem) const
- {
- bool result = false;
- for (int i = 0; i < filters.getEintragAnzahl(); i++)
- {
- if (i == 0)
- result = filters.z(i)->matchItem(zItem);
- else
- result = op(result, filters.z(i)->matchItem(zItem));
- }
- return result;
- }
- bool CombinedItemFilter::matchSourceSlot(ItemSlot* zSlot) const
- {
- bool result = false;
- for (int i = 0; i < filters.getEintragAnzahl(); i++)
- {
- if (i == 0)
- result = filters.z(i)->matchSourceSlot(zSlot);
- else
- result = op(result, filters.z(i)->matchSourceSlot(zSlot));
- }
- return result;
- }
- bool CombinedItemFilter::matchTargetSlot(ItemSlot* zSlot) const
- {
- bool result = false;
- for (int i = 0; i < filters.getEintragAnzahl(); i++)
- {
- if (i == 0)
- result = filters.z(i)->matchTargetSlot(zSlot);
- else
- result = op(result, filters.z(i)->matchTargetSlot(zSlot));
- }
- return result;
- }
- Framework::Text CombinedItemFilter::getLogicUIML() const
- {
- if (filters.getEintragAnzahl() == 0)
- {
- return "";
- }
- if (filters.getEintragAnzahl() == 1)
- {
- return filters.z(0)->getLogicUIML();
- }
- else
- {
- Framework::Text result = "<operator result_0_0=\"";
- result.append() << (int)op(0, 0) << "\" result_0_1=\"" << (int)op(0, 1)
- << "\" result_1_0=\"" << (int)op(1, 0)
- << "\" result_1_1=\"" << (int)op(1, 1) << "\">";
- int openCount = 1;
- for (int i = 0; i < filters.getEintragAnzahl() - 1; i++)
- {
- if (i < filters.getEintragAnzahl() - 2)
- {
- result.append()
- << filters.z(i)->getLogicUIML() << "<operator result_0_0=\""
- << (int)op(0, 0) << "\" result_0_1=\"" << (int)op(0, 1)
- << "\" result_1_0=\"" << (int)op(1, 0) << "\" result_1_1=\""
- << (int)op(1, 1) << "\">";
- }
- else
- {
- filters.z(i)->getLogicUIML();
- filters.z(i + 1)->getLogicUIML();
- }
- }
- return result;
- }
- }
- const Framework::RCArray<ItemFilter>& CombinedItemFilter::getFilters() const
- {
- return filters;
- }
- std::function<bool(bool, bool)> CombinedItemFilter::getOp() const
- {
- return op;
- }
- CombinedItemFilterFactory::CombinedItemFilterFactory()
- : SubTypeFactory()
- {}
- CombinedItemFilter* CombinedItemFilterFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- std::function<bool(bool, bool)> func;
- Framework::Text op = zJson->zValue("operator")->asString()->getString();
- if (op.istGleich("or"))
- {
- func = [](bool a, bool b) { return a || b; };
- }
- else if (op.istGleich("and"))
- {
- func = [](bool a, bool b) { return a && b; };
- }
- else if (op.istGleich("xor"))
- {
- func = [](bool a, bool b) { return (!a || !b) && (a || b); };
- }
- else if (op.istGleich("nor"))
- {
- func = [](bool a, bool b) { return !(a || b); };
- }
- else if (op.istGleich("nand"))
- {
- func = [](bool a, bool b) { return !(a && b); };
- }
- else if (op.istGleich("left"))
- {
- func = [](bool a, bool b) { return a; };
- }
- else if (op.istGleich("right"))
- {
- func = [](bool a, bool b) { return b; };
- }
- else if (op.istGleich("onlyLeft"))
- {
- func = [](bool a, bool b) { return a && !b; };
- }
- else if (op.istGleich("onlyRight"))
- {
- func = [](bool a, bool b) { return !a && b; };
- }
- else if (op.istGleich("notLeft"))
- {
- func = [](bool a, bool b) { return !a; };
- }
- else if (op.istGleich("notRight"))
- {
- func = [](bool a, bool b) { return !b; };
- }
- else if (op.istGleich("eq"))
- {
- func = [](bool a, bool b) { return a == b; };
- }
- else if (op.istGleich("leftOrEq"))
- {
- func = [](bool a, bool b) { return a || (a == b); };
- }
- else if (op.istGleich("rightOrEq"))
- {
- func = [](bool a, bool b) { return b || (a == b); };
- }
- else if (op.istGleich("true"))
- {
- func = [](bool a, bool b) { return true; };
- }
- else
- {
- func = [](bool a, bool b) { return false; };
- }
- Framework::RCArray<ItemFilter> filters;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("filters")->asArray())
- {
- filters.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(value));
- }
- return new CombinedItemFilter(filters, func);
- }
- Framework::JSON::JSONObject* CombinedItemFilterFactory::toJson(
- CombinedItemFilter* zObject) const
- {
- Framework::Text op;
- std::function<bool(bool, bool)> func = zObject->getOp();
- if (func(0, 0))
- {
- if (func(0, 1))
- {
- if (func(1, 0))
- {
- if (func(1, 1))
- {
- op = "true";
- }
- else
- {
- op = "nand";
- }
- }
- else
- {
- if (func(1, 1))
- {
- op = "rightOrEq";
- }
- else
- {
- op = "notLeft";
- }
- }
- }
- else
- {
- if (func(1, 0))
- {
- if (func(1, 1))
- {
- op = "leftOrEq";
- }
- else
- {
- op = "notRight";
- }
- }
- else
- {
- if (func(1, 1))
- {
- op = "eq";
- }
- else
- {
- op = "nor";
- }
- }
- }
- }
- else
- {
- if (func(0, 1))
- {
- if (func(1, 0))
- {
- if (func(1, 1))
- {
- op = "or";
- }
- else
- {
- op = "xor";
- }
- }
- else
- {
- if (func(1, 1))
- {
- op = "right";
- }
- else
- {
- op = "onlyRight";
- }
- }
- }
- else
- {
- if (func(1, 0))
- {
- if (func(1, 1))
- {
- op = "left";
- }
- else
- {
- op = "onlyLeft";
- }
- }
- else
- {
- if (func(1, 1))
- {
- op = "and";
- }
- else
- {
- op = "false";
- }
- }
- }
- }
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("operator", new Framework::JSON::JSONString(op));
- Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
- for (ItemFilter* filter : zObject->getFilters())
- {
- filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
- }
- result->addValue("filters", filters);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- CombinedItemFilterFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredString("operator")
- ->whichIsOneOf({"or",
- "and",
- "xor",
- "nor",
- "nand",
- "left",
- "right",
- "onlyLeft",
- "onlyRight",
- "notLeft",
- "notRight",
- "eq",
- "leftOrEq",
- "rightOrEq",
- "true",
- "false"})
- ->finishString()
- ->withRequiredArray("filters")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
- ->finishArray()
- ->finishObject();
- }
- Framework::Text CombinedItemFilterFactory::getTypeToken() const
- {
- return "operator";
- }
- AnyItemFilter::AnyItemFilter()
- : ItemFilter()
- {}
- bool AnyItemFilter::matchItem(const Item* zItem) const
- {
- return true;
- }
- Framework::Text AnyItemFilter::getLogicUIML() const
- {
- return "<anyItem/>";
- }
- AnyItemFilterFactory::AnyItemFilterFactory()
- : SubTypeFactory()
- {}
- AnyItemFilter* AnyItemFilterFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- return new AnyItemFilter();
- }
- Framework::JSON::JSONObject* AnyItemFilterFactory::toJson(
- AnyItemFilter* zObject) const
- {
- return new Framework::JSON::JSONObject();
- }
- Framework::JSON::Validator::JSONValidator* AnyItemFilterFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->finishObject();
- }
- Framework::Text AnyItemFilterFactory::getTypeToken() const
- {
- return "anyItem";
- }
- TypeItemFilter::TypeItemFilter(const ItemType* zType)
- : ItemFilter(),
- type(zType)
- {}
- bool TypeItemFilter::matchItem(const Item* zItem) const
- {
- return zItem->zItemType() == type;
- }
- Framework::Text TypeItemFilter::getLogicUIML() const
- {
- Framework::Text result = "<attribute name=\"Type\" operator=\"=\" value=\"";
- result.append() << type->getId() << "\"/>";
- return result;
- }
- const ItemType* TypeItemFilter::zType() const
- {
- return type;
- }
- TypeItemFilterFactory::TypeItemFilterFactory()
- : SubTypeFactory()
- {}
- TypeItemFilter* TypeItemFilterFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- return new TypeItemFilter(
- Game::INSTANCE->zItemType(Game::INSTANCE->getItemTypeId(
- zJson->zValue("itemType")->asString()->getString())));
- }
- Framework::JSON::JSONObject* TypeItemFilterFactory::toJson(
- TypeItemFilter* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("itemType",
- new Framework::JSON::JSONString(zObject->zType()->getName()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator* TypeItemFilterFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- Framework::RCArray<Text> types;
- for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
- {
- if (Game::INSTANCE->zItemType(i))
- {
- types.add(
- new Framework::Text(Game::INSTANCE->zItemType(i)->getName()));
- }
- }
- return builder->withRequiredString("itemType")
- ->whichIsOneOf(types)
- ->finishString()
- ->finishObject();
- }
- Framework::Text TypeItemFilterFactory::getTypeToken() const
- {
- return "type";
- }
- SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
- : ItemFilter(),
- sourceSlotId(sourceSlotId),
- targetSlotId(targetSlotId)
- {}
- bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
- {
- return sourceSlotId == zSlot->getId();
- }
- bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
- {
- return targetSlotId == zSlot->getId();
- }
- Framework::Text SpecificSlotFilter::getLogicUIML() const
- {
- return "<anyItem/>";
- }
- SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
- : ItemFilter()
- {}
- void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
- {
- blackList.add(id);
- }
- bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
- {
- for (int black : blackList)
- {
- if (black == zSlot->getId()) return 0;
- }
- return 1;
- }
- bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
- {
- return 1;
- }
- Framework::Text SourceSlotBlacklistFilter::getLogicUIML() const
- {
- return "<anyItem/>";
- }
- TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
- : ItemFilter()
- {}
- void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
- {
- blackList.add(id);
- }
- bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
- {
- return 1;
- }
- bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
- {
- for (int black : blackList)
- {
- if (black == zSlot->getId()) return 0;
- }
- return 1;
- }
- Framework::Text TargetSlotBlacklistFilter::getLogicUIML() const
- {
- return "<anyItem/>";
- }
- GroupItemFilter::GroupItemFilter(Framework::RCArray<Framework::Text> groups)
- : ItemFilter(),
- groups(groups)
- {}
- bool GroupItemFilter::matchItem(const Item* zItem) const
- {
- for (Framework::Text* group : groups)
- {
- for (Framework::Text* typeGroup : zItem->zItemType()->getGroups())
- {
- if (typeGroup->istGleich(group)) return true;
- }
- }
- return false;
- }
- Framework::Text GroupItemFilter::getLogicUIML() const
- {
- Framework::RCArray<ItemFilter> filters;
- for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
- {
- if (Game::INSTANCE->zItemType(i))
- {
- bool found = false;
- for (Framework::Text* group : groups)
- {
- for (Framework::Text* typeGroup :
- Game::INSTANCE->zItemType(i)->getGroups())
- {
- if (typeGroup->istGleich(group))
- {
- found = true;
- break;
- }
- }
- if (found) break;
- }
- if (found)
- filters.add(new TypeItemFilter(Game::INSTANCE->zItemType(i)));
- }
- }
- return CombinedItemFilter(filters, [](bool a, bool b) {
- return a || b;
- }).getLogicUIML();
- }
- const Framework::RCArray<Framework::Text>& GroupItemFilter::getGroups() const
- {
- return groups;
- }
- GroupItemFilterFactory::GroupItemFilterFactory()
- : SubTypeFactory()
- {}
- GroupItemFilter* GroupItemFilterFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groups;
- for (Framework::JSON::JSONValue* group :
- *zJson->zValue("groupNames")->asArray())
- {
- groups.add(new Framework::Text(group->asString()->getString()));
- }
- return new GroupItemFilter(groups);
- }
- Framework::JSON::JSONObject* GroupItemFilterFactory::toJson(
- GroupItemFilter* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* groups = new Framework::JSON::JSONArray();
- for (Framework::Text* group : zObject->getGroups())
- {
- groups->addValue(new Framework::JSON::JSONString(group->getText()));
- }
- result->addValue("groupNames", groups);
- return result;
- }
- Framework::JSON::Validator::JSONValidator* GroupItemFilterFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("groupNames")
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text GroupItemFilterFactory::getTypeToken() const
- {
- return "groups";
- }
|