123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- #include "BlockFilter.h"
- #include "Block.h"
- #include "FluidBlock.h"
- #include "Game.h"
- BlockFilter::BlockFilter()
- : ReferenceCounter()
- {}
- BlockFilterAnd::BlockFilterAnd(Framework::RCArray<BlockFilter> filters)
- : BlockFilter(),
- filters(filters)
- {}
- bool BlockFilterAnd::test(const Block* zBlock)
- {
- for (BlockFilter* filter : filters)
- {
- if (!filter->test(zBlock))
- {
- return false;
- }
- }
- return true;
- }
- Framework::RCArray<BlockFilter> BlockFilterAnd::getFilters() const
- {
- return filters;
- }
- BlockFilterAndFactory::BlockFilterAndFactory()
- : SubTypeFactory()
- {}
- BlockFilterAnd* BlockFilterAndFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<BlockFilter> filters;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("filters")->asArray())
- {
- filters.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(value));
- }
- return new BlockFilterAnd(filters);
- }
- Framework::JSON::JSONObject* BlockFilterAndFactory::toJson(
- BlockFilterAnd* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
- for (BlockFilter* filter : zObject->getFilters())
- {
- filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
- }
- result->addValue("filters", filters);
- return result;
- }
- Framework::JSON::Validator::JSONValidator* BlockFilterAndFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("filters")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BlockFilterAndFactory::getTypeToken() const
- {
- return "and";
- }
- BlockFilterOr::BlockFilterOr(Framework::RCArray<BlockFilter> filters)
- : BlockFilter(),
- filters(filters)
- {}
- bool BlockFilterOr::test(const Block* zBlock)
- {
- for (BlockFilter* filter : filters)
- {
- if (filter->test(zBlock))
- {
- return true;
- }
- }
- return false;
- }
- Framework::RCArray<BlockFilter> BlockFilterOr::getFilters() const
- {
- return filters;
- }
- BlockFilterOrFactory::BlockFilterOrFactory()
- : SubTypeFactory()
- {}
- BlockFilterOr* BlockFilterOrFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<BlockFilter> filters;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("filters")->asArray())
- {
- filters.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(value));
- }
- return new BlockFilterOr(filters);
- }
- Framework::JSON::JSONObject* BlockFilterOrFactory::toJson(
- BlockFilterOr* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
- for (BlockFilter* filter : zObject->getFilters())
- {
- filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
- }
- result->addValue("filters", filters);
- return result;
- }
- Framework::JSON::Validator::JSONValidator* BlockFilterOrFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("filters")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BlockFilterOrFactory::getTypeToken() const
- {
- return "or";
- }
- BlockFilterNot::BlockFilterNot(BlockFilter* filter)
- : BlockFilter(),
- filter(filter)
- {}
- bool BlockFilterNot::test(const Block* zBlock)
- {
- return !filter->test(zBlock);
- }
- BlockFilter* BlockFilterNot::zFilter() const
- {
- return filter;
- }
- BlockFilterNotFactory::BlockFilterNotFactory()
- : SubTypeFactory()
- {}
- BlockFilterNot* BlockFilterNotFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BlockFilterNot(
- Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
- zJson->zValue("filter")));
- }
- Framework::JSON::JSONObject* BlockFilterNotFactory::toJson(
- BlockFilterNot* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator* BlockFilterNotFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder
- ->withRequiredAttribute("filter",
- Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
- ->finishObject();
- }
- Framework::Text BlockFilterNotFactory::getTypeToken() const
- {
- return "not";
- }
- BlockFilterBlockType::BlockFilterBlockType(Framework::Array<int> blockTypeIds)
- : BlockFilter(),
- blockTypeIds(blockTypeIds)
- {}
- bool BlockFilterBlockType::test(const Block* zBlock)
- {
- for (int blockTypeId : blockTypeIds)
- {
- if (zBlock->zBlockType()->getId() == blockTypeId)
- {
- return true;
- }
- }
- return false;
- }
- const Framework::Array<int>& BlockFilterBlockType::getBlockTypeIds() const
- {
- return blockTypeIds;
- }
- BlockFilterBlockTypeFactory::BlockFilterBlockTypeFactory()
- : SubTypeFactory()
- {}
- BlockFilterBlockType* BlockFilterBlockTypeFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::Array<int> blockTypeIds;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("typeNames")->asArray())
- {
- blockTypeIds.add(
- Game::INSTANCE->getBlockTypeId(value->asString()->getString()));
- }
- return new BlockFilterBlockType(blockTypeIds);
- }
- Framework::JSON::JSONObject* BlockFilterBlockTypeFactory::toJson(
- BlockFilterBlockType* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* typeNames = new Framework::JSON::JSONArray();
- for (int typeId : zObject->getBlockTypeIds())
- {
- typeNames->addValue(new Framework::JSON::JSONString(
- Game::INSTANCE->zBlockType(typeId)->getName()));
- }
- result->addValue("typeNames", typeNames);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- BlockFilterBlockTypeFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("typeNames")
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BlockFilterBlockTypeFactory::getTypeToken() const
- {
- return "types";
- }
- BlockFilterTypeGroup::BlockFilterTypeGroup(
- Framework::RCArray<Framework::Text> groupNames)
- : BlockFilter(),
- groupNames(groupNames)
- {}
- bool BlockFilterTypeGroup::test(const Block* zBlock)
- {
- for (Framework::Text* groupName : groupNames)
- {
- for (Framework::Text* otherGroupName :
- zBlock->zBlockType()->getGroupNames())
- {
- if (groupName->istGleich(*otherGroupName))
- {
- return true;
- }
- }
- }
- return false;
- }
- const Framework::RCArray<Framework::Text>&
- BlockFilterTypeGroup::getGroupNames() const
- {
- return groupNames;
- }
- BlockFilterTypeGroupFactory::BlockFilterTypeGroupFactory()
- : SubTypeFactory()
- {}
- BlockFilterTypeGroup* BlockFilterTypeGroupFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> groupNames;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- groupNames.add(new Framework::Text(value->asString()->getString()));
- }
- return new BlockFilterTypeGroup(groupNames);
- }
- Framework::JSON::JSONObject* BlockFilterTypeGroupFactory::toJson(
- BlockFilterTypeGroup* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zObject->getGroupNames())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- result->addValue("groupNames", groupNames);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- BlockFilterTypeGroupFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("groupNames")
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BlockFilterTypeGroupFactory::getTypeToken() const
- {
- return "groups";
- }
- BlockFilterMaxHardness::BlockFilterMaxHardness(float maxHardness)
- : BlockFilter(),
- maxHardness(maxHardness)
- {}
- bool BlockFilterMaxHardness::test(const Block* zBlock)
- {
- return zBlock->zBlockType()->getHardness() <= maxHardness;
- }
- float BlockFilterMaxHardness::getMaxHardness() const
- {
- return maxHardness;
- }
- BlockFilterMaxHardnessFactory::BlockFilterMaxHardnessFactory()
- : SubTypeFactory()
- {}
- BlockFilterMaxHardness* BlockFilterMaxHardnessFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BlockFilterMaxHardness(
- (float)zJson->zValue("maxHardness")->asNumber()->getNumber());
- }
- Framework::JSON::JSONObject* BlockFilterMaxHardnessFactory::toJson(
- BlockFilterMaxHardness* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("maxHardness",
- new Framework::JSON::JSONNumber(zObject->getMaxHardness()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- BlockFilterMaxHardnessFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredNumber("maxHardness")
- ->whichIsGreaterOrEqual(0.0)
- ->finishNumber()
- ->finishObject();
- }
- Framework::Text BlockFilterMaxHardnessFactory::getTypeToken() const
- {
- return "maxHardness";
- }
- BlockFilterMinHardness::BlockFilterMinHardness(float minHardness)
- : BlockFilter(),
- minHardness(minHardness)
- {}
- bool BlockFilterMinHardness::test(const Block* zBlock)
- {
- return zBlock->zBlockType()->getHardness() >= minHardness;
- }
- float BlockFilterMinHardness::getMinHardness() const
- {
- return minHardness;
- }
- BlockFilterMinHardnessFactory::BlockFilterMinHardnessFactory()
- : SubTypeFactory()
- {}
- BlockFilterMinHardness* BlockFilterMinHardnessFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BlockFilterMinHardness(
- (float)zJson->zValue("minHardness")->asNumber()->getNumber());
- }
- Framework::JSON::JSONObject* BlockFilterMinHardnessFactory::toJson(
- BlockFilterMinHardness* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("minHardness",
- new Framework::JSON::JSONNumber(zObject->getMinHardness()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- BlockFilterMinHardnessFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredNumber("minHardness")
- ->whichIsGreaterOrEqual(0.0)
- ->finishNumber()
- ->finishObject();
- }
- Framework::Text BlockFilterMinHardnessFactory::getTypeToken() const
- {
- return "minHardness";
- }
- BlockFilterMaxHeat::BlockFilterMaxHeat(float maxHeat)
- : BlockFilter(),
- maxHeat(maxHeat)
- {}
- bool BlockFilterMaxHeat::test(const Block* zBlock)
- {
- const FluidBlockType* type
- = dynamic_cast<const FluidBlockType*>(zBlock->zBlockType());
- return type && type->getHeat() <= maxHeat;
- }
- float BlockFilterMaxHeat::getMaxHeat() const
- {
- return maxHeat;
- }
- BlockFilterMaxHeatFactory::BlockFilterMaxHeatFactory()
- : SubTypeFactory()
- {}
- BlockFilterMaxHeat* BlockFilterMaxHeatFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BlockFilterMaxHeat(
- (float)zJson->zValue("heat")->asNumber()->getNumber());
- }
- Framework::JSON::JSONObject* BlockFilterMaxHeatFactory::toJson(
- BlockFilterMaxHeat* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "heat", new Framework::JSON::JSONNumber(zObject->getMaxHeat()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- BlockFilterMaxHeatFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredNumber("heat")->finishNumber()->finishObject();
- }
- Framework::Text BlockFilterMaxHeatFactory::getTypeToken() const
- {
- return "maxHeat";
- }
|