#include "PlaceableProof.h" #include "BlockFilter.h" #include "Game.h" PlaceableProof::PlaceableProof() : ReferenceCounter() {} PlaceableProofAnd::PlaceableProofAnd(Framework::RCArray proofs) : 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; } const Framework::RCArray& PlaceableProofAnd::getProofs() const { return proofs; } PlaceableProofAndFactory::PlaceableProofAndFactory() : SubTypeFactory() {} PlaceableProofAnd* PlaceableProofAndFactory::fromJson( Framework::JSON::JSONObject* zJson) const { Framework::RCArray proofs; for (Framework::JSON::JSONValue* zProof : *zJson->zValue("proofs")->asArray()) { proofs.add( Game::INSTANCE->zTypeRegistry()->fromJson(zProof)); } return new PlaceableProofAnd(proofs); } Framework::JSON::JSONObject* PlaceableProofAndFactory::toJson( 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; } Framework::JSON::Validator::JSONValidator* PlaceableProofAndFactory::getValidator( Framework::JSON::Validator::ObjectValidationBuilder< Framework::JSON::Validator::JSONValidator>* builder) const { return builder->withRequiredArray("proofs") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->finishObject(); } Framework::Text PlaceableProofAndFactory::getTypeToken() const { return "and"; } PlaceableProofOr::PlaceableProofOr(Framework::RCArray proofs) : 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; } const Framework::RCArray& PlaceableProofOr::getProofs() const { return proofs; } PlaceableProofOrFactory::PlaceableProofOrFactory() : SubTypeFactory() {} PlaceableProofOr* PlaceableProofOrFactory::fromJson( Framework::JSON::JSONObject* zJson) const { Framework::RCArray proofs; for (Framework::JSON::JSONValue* zProof : *zJson->zValue("proofs")->asArray()) { proofs.add( Game::INSTANCE->zTypeRegistry()->fromJson(zProof)); } return new PlaceableProofOr(proofs); } Framework::JSON::JSONObject* PlaceableProofOrFactory::toJson( 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; } Framework::JSON::Validator::JSONValidator* PlaceableProofOrFactory::getValidator( Framework::JSON::Validator::ObjectValidationBuilder< Framework::JSON::Validator::JSONValidator>* builder) const { return builder->withRequiredArray("proofs") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->finishObject(); } Framework::Text PlaceableProofOrFactory::getTypeToken() const { return "or"; } PlaceableProofNot::PlaceableProofNot(PlaceableProof* proof) : PlaceableProof() {} PlaceableProofNot::~PlaceableProofNot() { proof->release(); } bool PlaceableProofNot::isPlacable( const Item* item, Framework::Vec3 pos, int dimensionId) { return !proof->isPlacable(item, pos, dimensionId); } PlaceableProof* PlaceableProofNot::zProof() const { return proof; } PlaceableProofNotFactory::PlaceableProofNotFactory() : SubTypeFactory() {} PlaceableProofNot* PlaceableProofNotFactory::fromJson( Framework::JSON::JSONObject* zJson) const { return new PlaceableProofNot( Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("proof"))); } Framework::JSON::JSONObject* PlaceableProofNotFactory::toJson( PlaceableProofNot* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "proof", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zProof())); return result; } Framework::JSON::Validator::JSONValidator* PlaceableProofNotFactory::getValidator( Framework::JSON::Validator::ObjectValidationBuilder< Framework::JSON::Validator::JSONValidator>* builder) const { return builder ->withRequiredAttribute("proof", Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishObject(); } Framework::Text PlaceableProofNotFactory::getTypeToken() const { return "not"; } PlaceableProofBlockFilter::PlaceableProofBlockFilter( Direction direction, int distance, BlockFilter* filter) : PlaceableProof(), direction(direction), distance(distance), filter(filter) {} 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)); } Direction PlaceableProofBlockFilter::getDirection() const { return direction; } int PlaceableProofBlockFilter::getDistance() const { return distance; } BlockFilter* PlaceableProofBlockFilter::zFilter() const { return filter; } PlaceableProofBlockFilterFactory::PlaceableProofBlockFilterFactory() : SubTypeFactory() {} PlaceableProofBlockFilter* PlaceableProofBlockFilterFactory::fromJson( Framework::JSON::JSONObject* zJson) const { 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; } return new PlaceableProofBlockFilter(dir, (int)zJson->zValue("distance")->asNumber()->getNumber(), Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("filter"))); } Framework::JSON::JSONObject* PlaceableProofBlockFilterFactory::toJson( PlaceableProofBlockFilter* zObject) const { 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"; } Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); 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; } Framework::JSON::Validator::JSONValidator* PlaceableProofBlockFilterFactory::getValidator( Framework::JSON::Validator::ObjectValidationBuilder< Framework::JSON::Validator::JSONValidator>* 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()) ->finishObject(); } Framework::Text PlaceableProofBlockFilterFactory::getTypeToken() const { return "blockFilter"; }