123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- #include "JsonExpression.h"
- #include "DimensionGenerator.h"
- #include "Game.h"
- JExpressionMemory::JExpressionMemory()
- : ReferenceCounter(),
- currentChunk(0)
- {}
- JExpressionMemory::~JExpressionMemory()
- {
- if (currentChunk) currentChunk->release();
- }
- void JExpressionMemory::lock()
- {
- cs.lock();
- }
- void JExpressionMemory::unlock()
- {
- cs.unlock();
- }
- float JExpressionMemory::getNoise(
- Framework::Text name, float x, float y, float z) const
- {
- Noise* currentNoise = noises.z(name, name.getLength());
- if (currentNoise)
- return (float)currentNoise->getNoise((double)x, (double)y, (double)z);
- return 0.f;
- }
- void JExpressionMemory::setNoise(Framework::Text name, Noise* noise)
- {
- noises.set(name, name.getLength(), noise);
- }
- void JExpressionMemory::setCurrentChunk(Chunk* chunk)
- {
- if (currentChunk) currentChunk->release();
- currentChunk = chunk;
- }
- float JExpressionMemory::getFloatVariable(Framework::Text name) const
- {
- return floatVariables.get(name, name.getLength());
- }
- void JExpressionMemory::setFloatVariable(Framework::Text name, float value)
- {
- floatVariables.set(name, name.getLength(), value);
- }
- bool JExpressionMemory::getBoolVariable(Framework::Text name) const
- {
- return boolVariables.get(name, name.getLength());
- }
- void JExpressionMemory::setBoolVariable(Framework::Text name, bool value)
- {
- return boolVariables.set(name, name.getLength(), value);
- }
- Chunk* JExpressionMemory::zCurrentChunk()
- {
- return currentChunk;
- }
- JFloatExpression::JFloatExpression()
- : ReferenceCounter()
- {}
- JBoolExpression::JBoolExpression()
- : ReferenceCounter()
- {}
- JVariableFloatExpression::JVariableFloatExpression(Framework::Text name)
- : JFloatExpression(),
- name(name)
- {}
- float JVariableFloatExpression::getValue(JExpressionMemory* zMemory)
- {
- return zMemory->getFloatVariable(name);
- }
- JVariableBoolExpression::JVariableBoolExpression(Framework::Text name)
- : JBoolExpression(),
- name(name)
- {}
- bool JVariableBoolExpression::getValue(JExpressionMemory* zMemory)
- {
- return zMemory->getBoolVariable(name);
- }
- JConstantFloatExpression::JConstantFloatExpression(float value)
- : JFloatExpression(),
- value(value)
- {}
- float JConstantFloatExpression::getValue(JExpressionMemory* zMemory)
- {
- return value;
- }
- JConstantBoolExpression::JConstantBoolExpression(bool value)
- : JBoolExpression(),
- value(value)
- {}
- bool JConstantBoolExpression::getValue(JExpressionMemory* zMemory)
- {
- return value;
- }
- JNoiseFloatExpression::JNoiseFloatExpression(Framework::Text name,
- JFloatExpression* x,
- JFloatExpression* y,
- JFloatExpression* z)
- : JFloatExpression(),
- name(name),
- x(x),
- y(y),
- z(z)
- {}
- JNoiseFloatExpression::~JNoiseFloatExpression()
- {
- x->release();
- y->release();
- z->release();
- }
- float JNoiseFloatExpression::getValue(JExpressionMemory* zMemory)
- {
- return zMemory->getNoise(
- name, x->getValue(zMemory), y->getValue(zMemory), z->getValue(zMemory));
- }
- JOperatorFloatExpression::JOperatorFloatExpression(
- std::function<float(float a, float b)> accumulator,
- Framework::RCArray<JFloatExpression>* values)
- : JFloatExpression(),
- accumulator(accumulator),
- values(values)
- {}
- JOperatorFloatExpression::~JOperatorFloatExpression()
- {
- values->release();
- }
- float JOperatorFloatExpression::getValue(JExpressionMemory* zMemory)
- {
- bool first = 1;
- float val = 0.f;
- for (JFloatExpression* expression : *values)
- {
- if (first)
- {
- first = 0;
- val = expression->getValue(zMemory);
- }
- else
- {
- val = accumulator(val, expression->getValue(zMemory));
- }
- }
- return val;
- }
- JBoolOperatorBoolExpression::JBoolOperatorBoolExpression(
- std::function<bool(bool a, bool b)> accumulator,
- Framework::RCArray<JBoolExpression>* values)
- : JBoolExpression(),
- accumulator(accumulator),
- values(values)
- {}
- JBoolOperatorBoolExpression::~JBoolOperatorBoolExpression()
- {
- values->release();
- }
- bool JBoolOperatorBoolExpression::getValue(JExpressionMemory* zMemory)
- {
- bool first = 1;
- bool val = 0;
- for (JBoolExpression* expression : *values)
- {
- if (first)
- {
- first = 0;
- val = expression->getValue(zMemory);
- }
- else
- {
- val = accumulator(val, expression->getValue(zMemory));
- }
- }
- return val;
- }
- JFloatOperatorBoolExpression::JFloatOperatorBoolExpression(
- std::function<bool(float a, float b)> accumulator,
- Framework::RCArray<JFloatExpression>* values)
- : JBoolExpression(),
- accumulator(accumulator),
- values(values)
- {}
- JFloatOperatorBoolExpression::~JFloatOperatorBoolExpression()
- {
- values->release();
- }
- bool JFloatOperatorBoolExpression::getValue(JExpressionMemory* zMemory)
- {
- bool first = 1;
- bool val = 1;
- float last = 0.f;
- for (JFloatExpression* expression : *values)
- {
- float current = expression->getValue(zMemory);
- if (!first) val &= accumulator(last, current);
- first = 0;
- last = current;
- }
- return val;
- }
- JBlockTypeBoolExpression::JBlockTypeBoolExpression(
- int typeId, JFloatExpression* x, JFloatExpression* y, JFloatExpression* z)
- : JBoolExpression(),
- typeId(typeId),
- x(x),
- y(y),
- z(z)
- {}
- JBlockTypeBoolExpression ::~JBlockTypeBoolExpression()
- {
- x->release();
- y->release();
- z->release();
- }
- bool JBlockTypeBoolExpression::getValue(JExpressionMemory* zMemory)
- {
- int x = (int)(round(this->x->getValue(zMemory)));
- int y = (int)(round(this->y->getValue(zMemory)));
- int z = (int)(round(this->z->getValue(zMemory)));
- if (z < 0 || z >= WORLD_HEIGHT
- || !zMemory->zCurrentChunk()
- || Game::getChunkCenter(x, y) != zMemory->zCurrentChunk()->getCenter())
- {
- return 0;
- }
- return zMemory->zCurrentChunk()->getBlockTypeAt(
- Dimension::chunkCoordinates({x, y, z}))
- == typeId;
- }
- using namespace Framework::JSON;
- using namespace Framework::JSON::Validator;
- JFloatExpression* JExpressionParser::parseFloatExpression(
- Framework::JSON::JSONValue* zValue)
- {
- JSONObject* obj = zValue->asObject();
- Framework::Text type = obj->zValue("type")->asString()->getString();
- if (type.istGleich("variable"))
- {
- return new JVariableFloatExpression(
- obj->zValue("name")->asString()->getString());
- }
- else if (type.istGleich("constant"))
- {
- return new JConstantFloatExpression(
- (float)obj->zValue("value")->asNumber()->getNumber());
- }
- else if (type.istGleich("noise"))
- {
- return new JNoiseFloatExpression(
- obj->zValue("name")->asString()->getString(),
- parseFloatExpression(obj->zValue("x")),
- parseFloatExpression(obj->zValue("y")),
- parseFloatExpression(obj->zValue("z")));
- }
- else if (type.istGleich("operator"))
- {
- Framework::RCArray<JFloatExpression>* values
- = new Framework::RCArray<JFloatExpression>();
- JSONArray* valuesArray = obj->zValue("values")->asArray();
- for (JSONValue* value : *valuesArray)
- {
- values->add(parseFloatExpression(value));
- }
- Framework::Text op = obj->zValue("operator")->asString()->getString();
- if (op.istGleich("+"))
- {
- return new JOperatorFloatExpression(
- [](float a, float b) { return a + b; }, values);
- }
- else if (op.istGleich("-"))
- {
- return new JOperatorFloatExpression(
- [](float a, float b) { return a - b; }, values);
- }
- else if (op.istGleich("*"))
- {
- return new JOperatorFloatExpression(
- [](float a, float b) { return a * b; }, values);
- }
- else if (op.istGleich("/"))
- {
- return new JOperatorFloatExpression(
- [](float a, float b) { return a / b; }, values);
- }
- else
- {
- values->release();
- }
- }
- return 0;
- }
- Framework::JSON::Validator::JSONValidator*
- JExpressionParser::getFloatValidator()
- {
- JSONValidator* refs
- = JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jef_variable"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jef_constant"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jef_noise"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jef_operator"))
- ->finishOneOf();
- JSONValidator* validator
- = JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedType(JSONValidator::buildForObject()
- ->setObjectReferenceId("jef_variable")
- ->withRequiredString("type")
- ->whichIs("variable")
- ->finishString()
- ->withRequiredString("name")
- ->finishString()
- ->finishObject())
- ->addAcceptedType(JSONValidator::buildForObject()
- ->setObjectReferenceId("jef_constant")
- ->withRequiredString("type")
- ->whichIs("constant")
- ->finishString()
- ->withRequiredNumber("value")
- ->finishNumber()
- ->finishObject())
- ->addAcceptedType(
- JSONValidator::buildForObject()
- ->setObjectReferenceId("jef_noise")
- ->withRequiredString("type")
- ->whichIs("noise")
- ->finishString()
- ->withRequiredString("name")
- ->withDefault("noise")
- ->finishString()
- ->withRequiredAttribute(
- "x", dynamic_cast<JSONValidator*>(refs->getThis()))
- ->withRequiredAttribute(
- "y", dynamic_cast<JSONValidator*>(refs->getThis()))
- ->withRequiredAttribute(
- "z", dynamic_cast<JSONValidator*>(refs->getThis()))
- ->finishObject())
- ->addAcceptedType(
- JSONValidator::buildForObject()
- ->setObjectReferenceId("jef_operator")
- ->withRequiredString("type")
- ->whichIs("operator")
- ->finishString()
- ->withRequiredString("operator")
- ->whichIsOneOf({"+", "-", "*", "/"})
- ->finishString()
- ->withRequiredArray("values")
- ->addAcceptedTypeInArray(
- dynamic_cast<JSONValidator*>(refs->getThis()))
- ->finishArray()
- ->finishObject())
- ->finishOneOf();
- return validator;
- }
- JBoolExpression* JExpressionParser::parseBoolExpression(
- Framework::JSON::JSONValue* zValue)
- {
- JSONObject* obj = zValue->asObject();
- Framework::Text type = obj->zValue("type")->asString()->getString();
- if (type.istGleich("variable"))
- {
- return new JVariableBoolExpression(
- obj->zValue("name")->asString()->getString());
- }
- else if (type.istGleich("constant"))
- {
- return new JConstantBoolExpression(
- (float)obj->zValue("value")->asBool()->getBool());
- }
- else if (type.istGleich("blockType"))
- {
- return new JBlockTypeBoolExpression(
- BlockType::getTypeId(
- obj->zValue("blockType")->asString()->getString()),
- parseFloatExpression(obj->zValue("x")),
- parseFloatExpression(obj->zValue("y")),
- parseFloatExpression(obj->zValue("z")));
- }
- else if (type.istGleich("operator"))
- {
- Framework::Text op = obj->zValue("operator")->asString()->getString();
- if (op.istGleich("&&") || op.istGleich("||"))
- {
- Framework::RCArray<JBoolExpression>* values
- = new Framework::RCArray<JBoolExpression>();
- JSONArray* valuesArray = obj->zValue("values")->asArray();
- for (JSONValue* value : *valuesArray)
- {
- values->add(parseBoolExpression(value));
- }
- if (op.istGleich("&&"))
- {
- return new JBoolOperatorBoolExpression(
- [](bool a, bool b) { return a && b; }, values);
- }
- else if (op.istGleich("||"))
- {
- return new JBoolOperatorBoolExpression(
- [](bool a, bool b) { return a || b; }, values);
- }
- else
- {
- values->release();
- }
- }
- else
- {
- Framework::RCArray<JFloatExpression>* values
- = new Framework::RCArray<JFloatExpression>();
- JSONArray* valuesArray = obj->zValue("values")->asArray();
- for (JSONValue* value : *valuesArray)
- {
- values->add(parseFloatExpression(value));
- }
- if (op.istGleich(">"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return a > b; }, values);
- }
- else if (op.istGleich("<"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return a < b; }, values);
- }
- else if (op.istGleich(">="))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return a >= b; }, values);
- }
- else if (op.istGleich("<="))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return a <= b; }, values);
- }
- else if (op.istGleich("=="))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return a == b; }, values);
- }
- else if (op.istGleich("!="))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return a != b; }, values);
- }
- else if (op.istGleich(">i"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return (int)a > (int)b; }, values);
- }
- else if (op.istGleich("<i"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return (int)a < (int)b; }, values);
- }
- else if (op.istGleich(">=i"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return (int)a >= (int)b; }, values);
- }
- else if (op.istGleich("<=i"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return (int)a <= (int)b; }, values);
- }
- else if (op.istGleich("==i"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return (int)a == (int)b; }, values);
- }
- else if (op.istGleich("!=i"))
- {
- return new JFloatOperatorBoolExpression(
- [](float a, float b) { return (int)a != (int)b; }, values);
- }
- else
- {
- values->release();
- }
- }
- }
- return 0;
- }
- Framework::JSON::Validator::JSONValidator* JExpressionParser::getBoolValidator()
- {
- Framework::RCArray<Framework::Text> blockTypeNames;
- for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<BlockType>::INSTANCE.zElement(i))
- {
- blockTypeNames.add(new Framework::Text(
- StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()));
- }
- }
- JSONValidator* refs
- = JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jeb_variable"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jeb_constant"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jeb_blockType"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jeb_bool_operator"))
- ->addAcceptedType(
- JSONValidator::buildForObjectReference("jeb_float_operator"))
- ->finishOneOf();
- JSONValidator* validator
- = JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedType(JSONValidator::buildForObject()
- ->setObjectReferenceId("jeb_variable")
- ->withRequiredString("type")
- ->whichIs("variable")
- ->finishString()
- ->withRequiredString("name")
- ->finishString()
- ->finishObject())
- ->addAcceptedType(JSONValidator::buildForObject()
- ->setObjectReferenceId("jeb_constant")
- ->withRequiredString("type")
- ->whichIs("constant")
- ->finishString()
- ->withRequiredBool("value")
- ->finishBool()
- ->finishObject())
- ->addAcceptedType(
- JSONValidator::buildForObject()
- ->setObjectReferenceId("jeb_blockType")
- ->withRequiredString("type")
- ->whichIs("blockType")
- ->finishString()
- ->withRequiredString("blockType")
- ->whichIsOneOf(blockTypeNames)
- ->finishString()
- ->withRequiredAttribute("x", getFloatValidator())
- ->withRequiredAttribute("y", getFloatValidator())
- ->withRequiredAttribute("z", getFloatValidator())
- ->finishObject())
- ->addAcceptedType(
- JSONValidator::buildForObject()
- ->setObjectReferenceId("jeb_bool_operator")
- ->withRequiredString("type")
- ->whichIs("operator")
- ->finishString()
- ->withRequiredString("operator")
- ->whichIsOneOf({"&&", "||"})
- ->finishString()
- ->withRequiredArray("values")
- ->addAcceptedTypeInArray(
- dynamic_cast<JSONValidator*>(refs->getThis()))
- ->finishArray()
- ->finishObject())
- ->addAcceptedType(
- JSONValidator::buildForObject()
- ->setObjectReferenceId("jeb_float_operator")
- ->withRequiredString("type")
- ->whichIs("operator")
- ->finishString()
- ->withRequiredString("operator")
- ->whichIsOneOf({">",
- "<",
- ">=",
- "<=",
- "==",
- "!=",
- ">i",
- "<i",
- ">=i",
- "<=i",
- "==i",
- "!=i"})
- ->finishString()
- ->withRequiredArray("values")
- ->addAcceptedTypeInArray(getFloatValidator())
- ->finishArray()
- ->finishObject())
- ->finishOneOf();
- return validator;
- }
|