#include "PlaceableProof.h" #include "BlockFilter.h" #include "Dimension.h" #include "Game.h" PlaceableProof::PlaceableProof() : ReferenceCounter() {} PlaceableProofAnd::PlaceableProofAnd() : proofs(proofs) {} bool PlaceableProofAnd::isPlacable( const Item* item, Framework::Vec3 pos, int dimensionId) { for (PlaceableProof* proof : proofs) { if (!proof->isPlacable(item, pos, dimensionId)) { return false; } } return true; } void PlaceableProofAnd::addProof(PlaceableProof* proof) { proofs.add(proof); } const Framework::RCArray& PlaceableProofAnd::getProofs() const { return proofs; } PlaceableProofAndFactory::PlaceableProofAndFactory() : SubTypeFactory() {} PlaceableProofAnd* PlaceableProofAndFactory::fromJson( Framework::JSON::JSONObject* zJson) const { PlaceableProofAnd* result = new PlaceableProofAnd(); for (Framework::JSON::JSONValue* zProof : *zJson->zValue("proofs")->asArray()) { result->addProof( Game::INSTANCE->zTypeRegistry()->fromJson(zProof)); } return result; } Framework::JSON::JSONObject* PlaceableProofAndFactory::toJsonObject( PlaceableProofAnd* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); Framework::JSON::JSONArray* proofs = new Framework::JSON::JSONArray(); for (PlaceableProof* proof : zObject->getProofs()) { proofs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(proof)); } result->addValue("proofs", proofs); return result; } JSONObjectValidationBuilder* PlaceableProofAndFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->withRequiredArray("proofs") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray(); } const char* PlaceableProofAndFactory::getTypeToken() const { return "and"; } PlaceableProofOr::PlaceableProofOr() : proofs(proofs) {} bool PlaceableProofOr::isPlacable( const Item* item, Framework::Vec3 pos, int dimensionId) { for (PlaceableProof* proof : proofs) { if (proof->isPlacable(item, pos, dimensionId)) { return true; } } return false; } void PlaceableProofOr::addProof(PlaceableProof* proof) { proofs.add(proof); } const Framework::RCArray& PlaceableProofOr::getProofs() const { return proofs; } PlaceableProofOrFactory::PlaceableProofOrFactory() : SubTypeFactory() {} PlaceableProofOr* PlaceableProofOrFactory::fromJson( Framework::JSON::JSONObject* zJson) const { PlaceableProofOr* result = new PlaceableProofOr(); for (Framework::JSON::JSONValue* zProof : *zJson->zValue("proofs")->asArray()) { result->addProof( Game::INSTANCE->zTypeRegistry()->fromJson(zProof)); } return result; } Framework::JSON::JSONObject* PlaceableProofOrFactory::toJsonObject( PlaceableProofOr* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); Framework::JSON::JSONArray* proofs = new Framework::JSON::JSONArray(); for (PlaceableProof* proof : zObject->getProofs()) { proofs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(proof)); } result->addValue("proofs", proofs); return result; } JSONObjectValidationBuilder* PlaceableProofOrFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->withRequiredArray("proofs") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray(); } const char* PlaceableProofOrFactory::getTypeToken() const { return "or"; } PlaceableProofNot::PlaceableProofNot() : PlaceableProof(), proof(proof) {} PlaceableProofNot::~PlaceableProofNot() { proof->release(); } bool PlaceableProofNot::isPlacable( const Item* item, Framework::Vec3 pos, int dimensionId) { return !proof->isPlacable(item, pos, dimensionId); } void PlaceableProofNot::setProof(PlaceableProof* proof) {} PlaceableProof* PlaceableProofNot::zProof() const { return proof; } PlaceableProofNotFactory::PlaceableProofNotFactory() : SubTypeFactory() {} PlaceableProofNot* PlaceableProofNotFactory::fromJson( Framework::JSON::JSONObject* zJson) const { PlaceableProofNot* result = new PlaceableProofNot(); result->setProof(Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("proof"))); return result; } Framework::JSON::JSONObject* PlaceableProofNotFactory::toJsonObject( PlaceableProofNot* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "proof", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zProof())); return result; } JSONObjectValidationBuilder* PlaceableProofNotFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->withRequiredAttribute("proof", Game::INSTANCE->zTypeRegistry()->getValidator()); } const char* PlaceableProofNotFactory::getTypeToken() const { return "not"; } PlaceableProofBlockFilter::PlaceableProofBlockFilter() : PlaceableProof(), direction(NO_DIRECTION), distance(1), filter(0) {} PlaceableProofBlockFilter::~PlaceableProofBlockFilter() { filter->release(); } bool PlaceableProofBlockFilter::isPlacable( const Item* item, Framework::Vec3 pos, int dimensionId) { pos += (Framework::Vec3)(::getDirection(direction) * distance); Dimension* dim = Game::INSTANCE->zDimension(dimensionId); return dim && pos.z >= 0 && pos.z < WORLD_HEIGHT && filter->test(dim->zBlockOrDefault(pos)); } void PlaceableProofBlockFilter::setDirection(Direction direction) { this->direction = direction; } Direction PlaceableProofBlockFilter::getDirection() const { return direction; } void PlaceableProofBlockFilter::setDistance(int distance) { this->distance = distance; } int PlaceableProofBlockFilter::getDistance() const { return distance; } void PlaceableProofBlockFilter::setFilter(BlockFilter* filter) { this->filter = filter; } BlockFilter* PlaceableProofBlockFilter::zFilter() const { return filter; } PlaceableProofBlockFilterFactory::PlaceableProofBlockFilterFactory() : SubTypeFactory() {} PlaceableProofBlockFilter* PlaceableProofBlockFilterFactory::fromJson( Framework::JSON::JSONObject* zJson) const { PlaceableProofBlockFilter* result = new PlaceableProofBlockFilter(); Direction dir = NO_DIRECTION; if (zJson->zValue("direction")->asString()->getString().istGleich("top")) { dir = TOP; } else if (zJson->zValue("direction") ->asString() ->getString() .istGleich("bottom")) { dir = BOTTOM; } else if (zJson->zValue("direction") ->asString() ->getString() .istGleich("north")) { dir = NORTH; } else if (zJson->zValue("direction") ->asString() ->getString() .istGleich("east")) { dir = EAST; } else if (zJson->zValue("direction") ->asString() ->getString() .istGleich("south")) { dir = SOUTH; } else if (zJson->zValue("direction") ->asString() ->getString() .istGleich("west")) { dir = WEST; } result->setDirection(dir); result->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("filter"))); return result; } Framework::JSON::JSONObject* PlaceableProofBlockFilterFactory::toJsonObject( PlaceableProofBlockFilter* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); Framework::Text dir = ""; if (zObject->getDirection() == NORTH) { dir = "north"; } else if (zObject->getDirection() == EAST) { dir = "east"; } else if (zObject->getDirection() == SOUTH) { dir = "south"; } else if (zObject->getDirection() == WEST) { dir = "west"; } else if (zObject->getDirection() == TOP) { dir = "top"; } else if (zObject->getDirection() == BOTTOM) { dir = "bottom"; } result->addValue("direction", new Framework::JSON::JSONString(dir)); result->addValue( "distance", new Framework::JSON::JSONNumber(zObject->getDistance())); result->addValue( "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter())); return result; } JSONObjectValidationBuilder* PlaceableProofBlockFilterFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->withRequiredString("direction") ->whichIsOneOf({"top", "bottom", "north", "east", "south", "west"}) ->finishString() ->withRequiredNumber("distance") ->whichIsGreaterOrEqual(0.0) ->withDefault(1.0) ->finishNumber() ->withRequiredAttribute("filter", Game::INSTANCE->zTypeRegistry()->getValidator()); } const char* PlaceableProofBlockFilterFactory::getTypeToken() const { return "blockFilter"; }