JsonExpression.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #include "JsonExpression.h"
  2. #include "Dimension.h"
  3. #include "DimensionGenerator.h"
  4. #include "Game.h"
  5. JExpressionMemory::JExpressionMemory()
  6. : ReferenceCounter(),
  7. currentChunk(0)
  8. {}
  9. JExpressionMemory::~JExpressionMemory()
  10. {
  11. if (currentChunk) currentChunk->release();
  12. }
  13. void JExpressionMemory::lock()
  14. {
  15. cs.lock();
  16. }
  17. void JExpressionMemory::unlock()
  18. {
  19. cs.unlock();
  20. }
  21. float JExpressionMemory::getNoise(
  22. Framework::Text name, float x, float y, float z) const
  23. {
  24. Noise* currentNoise = noises.z(name, name.getLength());
  25. if (currentNoise)
  26. return (float)currentNoise->getNoise((double)x, (double)y, (double)z);
  27. return 0.f;
  28. }
  29. void JExpressionMemory::setNoise(Framework::Text name, Noise* noise)
  30. {
  31. noises.set(name, name.getLength(), noise);
  32. }
  33. void JExpressionMemory::setCurrentChunk(Chunk* chunk)
  34. {
  35. if (currentChunk) currentChunk->release();
  36. currentChunk = chunk;
  37. }
  38. float JExpressionMemory::getFloatVariable(Framework::Text name) const
  39. {
  40. return floatVariables.get(name, name.getLength());
  41. }
  42. void JExpressionMemory::setFloatVariable(Framework::Text name, float value)
  43. {
  44. floatVariables.set(name, name.getLength(), value);
  45. }
  46. bool JExpressionMemory::getBoolVariable(Framework::Text name) const
  47. {
  48. return boolVariables.get(name, name.getLength());
  49. }
  50. void JExpressionMemory::setBoolVariable(Framework::Text name, bool value)
  51. {
  52. return boolVariables.set(name, name.getLength(), value);
  53. }
  54. Chunk* JExpressionMemory::zCurrentChunk()
  55. {
  56. return currentChunk;
  57. }
  58. JFloatExpression::JFloatExpression()
  59. : ReferenceCounter()
  60. {}
  61. JBoolExpression::JBoolExpression()
  62. : ReferenceCounter()
  63. {}
  64. JVariableFloatExpression::JVariableFloatExpression()
  65. : JFloatExpression()
  66. {}
  67. float JVariableFloatExpression::getValue(JExpressionMemory* zMemory)
  68. {
  69. return zMemory->getFloatVariable(name);
  70. }
  71. void JVariableFloatExpression::setName(Framework::Text name)
  72. {
  73. this->name = name;
  74. }
  75. Framework::Text JVariableFloatExpression::getName() const
  76. {
  77. return name;
  78. }
  79. JVariableFloatExpressionFactory::JVariableFloatExpressionFactory()
  80. : SubTypeFactory()
  81. {}
  82. JVariableFloatExpression* JVariableFloatExpressionFactory::fromJson(
  83. Framework::JSON::JSONObject* zJson) const
  84. {
  85. JVariableFloatExpression* result = new JVariableFloatExpression();
  86. result->setName(zJson->zValue("name")->asString()->getString());
  87. return result;
  88. }
  89. Framework::JSON::JSONObject* JVariableFloatExpressionFactory::toJsonObject(
  90. JVariableFloatExpression* zObject) const
  91. {
  92. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  93. result->addValue(
  94. "name", new Framework::JSON::JSONString(zObject->getName()));
  95. return result;
  96. }
  97. JSONObjectValidationBuilder* JVariableFloatExpressionFactory::addToValidator(
  98. JSONObjectValidationBuilder* builder) const
  99. {
  100. return builder->withRequiredString("name")->finishString();
  101. }
  102. const char* JVariableFloatExpressionFactory::getTypeToken() const
  103. {
  104. return "variable";
  105. }
  106. JVariableBoolExpression::JVariableBoolExpression()
  107. : JBoolExpression()
  108. {}
  109. bool JVariableBoolExpression::getValue(JExpressionMemory* zMemory)
  110. {
  111. return zMemory->getBoolVariable(name);
  112. }
  113. void JVariableBoolExpression::setName(Framework::Text name)
  114. {
  115. this->name = name;
  116. }
  117. Framework::Text JVariableBoolExpression::getName() const
  118. {
  119. return name;
  120. }
  121. JVariableBoolExpressionFactory::JVariableBoolExpressionFactory()
  122. : SubTypeFactory()
  123. {}
  124. JVariableBoolExpression* JVariableBoolExpressionFactory::fromJson(
  125. Framework::JSON::JSONObject* zJson) const
  126. {
  127. JVariableBoolExpression* result = new JVariableBoolExpression();
  128. result->setName(zJson->zValue("name")->asString()->getString());
  129. return result;
  130. }
  131. Framework::JSON::JSONObject* JVariableBoolExpressionFactory::toJsonObject(
  132. JVariableBoolExpression* zObject) const
  133. {
  134. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  135. result->addValue(
  136. "name", new Framework::JSON::JSONString(zObject->getName()));
  137. return result;
  138. }
  139. JSONObjectValidationBuilder* JVariableBoolExpressionFactory::addToValidator(
  140. JSONObjectValidationBuilder* builder) const
  141. {
  142. return builder->withRequiredString("name")->finishString();
  143. }
  144. const char* JVariableBoolExpressionFactory::getTypeToken() const
  145. {
  146. return "variable";
  147. }
  148. JConstantFloatExpression::JConstantFloatExpression()
  149. : JFloatExpression(),
  150. value(0)
  151. {}
  152. float JConstantFloatExpression::getValue(JExpressionMemory* zMemory)
  153. {
  154. return value;
  155. }
  156. void JConstantFloatExpression::setValue(float value)
  157. {
  158. this->value = value;
  159. }
  160. float JConstantFloatExpression::getValue() const
  161. {
  162. return value;
  163. }
  164. JConstantFloatExpressionFactory::JConstantFloatExpressionFactory()
  165. : SubTypeFactory()
  166. {}
  167. JConstantFloatExpression* JConstantFloatExpressionFactory::fromJson(
  168. Framework::JSON::JSONObject* zJson) const
  169. {
  170. JConstantFloatExpression* result = new JConstantFloatExpression();
  171. result->setValue((float)zJson->zValue("value")->asNumber()->getNumber());
  172. return result;
  173. }
  174. Framework::JSON::JSONObject* JConstantFloatExpressionFactory::toJsonObject(
  175. JConstantFloatExpression* zObject) const
  176. {
  177. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  178. result->addValue(
  179. "value", new Framework::JSON::JSONNumber(zObject->getValue()));
  180. return result;
  181. }
  182. JSONObjectValidationBuilder* JConstantFloatExpressionFactory::addToValidator(
  183. JSONObjectValidationBuilder* builder) const
  184. {
  185. return builder->withRequiredNumber("value")->finishNumber();
  186. }
  187. const char* JConstantFloatExpressionFactory::getTypeToken() const
  188. {
  189. return "constant";
  190. }
  191. JConstantBoolExpression::JConstantBoolExpression()
  192. : JBoolExpression()
  193. {}
  194. bool JConstantBoolExpression::getValue(JExpressionMemory* zMemory)
  195. {
  196. return value;
  197. }
  198. void JConstantBoolExpression::setValue(bool value)
  199. {
  200. this->value = value;
  201. }
  202. bool JConstantBoolExpression::getValue() const
  203. {
  204. return value;
  205. }
  206. JConstantBoolExpressionFactory::JConstantBoolExpressionFactory()
  207. : SubTypeFactory()
  208. {}
  209. JConstantBoolExpression* JConstantBoolExpressionFactory::fromJson(
  210. Framework::JSON::JSONObject* zJson) const
  211. {
  212. JConstantBoolExpression* result = new JConstantBoolExpression();
  213. result->setValue(zJson->zValue("value")->asBool()->getBool());
  214. return result;
  215. }
  216. Framework::JSON::JSONObject* JConstantBoolExpressionFactory::toJsonObject(
  217. JConstantBoolExpression* zObject) const
  218. {
  219. Framework::JSON::JSONObject* zResult = new Framework::JSON::JSONObject();
  220. zResult->addValue(
  221. "value", new Framework::JSON::JSONBool(zObject->getValue()));
  222. return zResult;
  223. }
  224. JSONObjectValidationBuilder* JConstantBoolExpressionFactory::addToValidator(
  225. JSONObjectValidationBuilder* builder) const
  226. {
  227. return builder->withRequiredBool("value")->finishBool();
  228. }
  229. const char* JConstantBoolExpressionFactory::getTypeToken() const
  230. {
  231. return "constant";
  232. }
  233. JNoiseFloatExpression::JNoiseFloatExpression()
  234. : JFloatExpression(),
  235. x(0),
  236. y(0),
  237. z(0)
  238. {}
  239. JNoiseFloatExpression::~JNoiseFloatExpression()
  240. {
  241. if (x) x->release();
  242. if (y) y->release();
  243. if (z) z->release();
  244. }
  245. float JNoiseFloatExpression::getValue(JExpressionMemory* zMemory)
  246. {
  247. return zMemory->getNoise(
  248. name, x->getValue(zMemory), y->getValue(zMemory), z->getValue(zMemory));
  249. }
  250. void JNoiseFloatExpression::setName(Framework::Text name)
  251. {
  252. this->name = name;
  253. }
  254. Framework::Text JNoiseFloatExpression::getName() const
  255. {
  256. return name;
  257. }
  258. void JNoiseFloatExpression::setX(JFloatExpression* x)
  259. {
  260. if (this->x) this->x->release();
  261. this->x = x;
  262. }
  263. JFloatExpression* JNoiseFloatExpression::zX() const
  264. {
  265. return x;
  266. }
  267. void JNoiseFloatExpression::setY(JFloatExpression* y)
  268. {
  269. if (this->y) this->y->release();
  270. this->y = y;
  271. }
  272. JFloatExpression* JNoiseFloatExpression::zY() const
  273. {
  274. return y;
  275. }
  276. void JNoiseFloatExpression::setZ(JFloatExpression* z)
  277. {
  278. if (this->z) this->z->release();
  279. this->z = z;
  280. }
  281. JFloatExpression* JNoiseFloatExpression::zZ() const
  282. {
  283. return z;
  284. }
  285. JNoiseFloatExpressionFactory::JNoiseFloatExpressionFactory()
  286. : SubTypeFactory()
  287. {}
  288. JNoiseFloatExpression* JNoiseFloatExpressionFactory::fromJson(
  289. Framework::JSON::JSONObject* zJson) const
  290. {
  291. JNoiseFloatExpression* result = new JNoiseFloatExpression();
  292. result->setName(zJson->zValue("name")->asString()->getString());
  293. result->setX(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  294. zJson->zValue("x")));
  295. result->setY(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  296. zJson->zValue("y")));
  297. result->setZ(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  298. zJson->zValue("z")));
  299. return result;
  300. }
  301. Framework::JSON::JSONObject* JNoiseFloatExpressionFactory::toJsonObject(
  302. JNoiseFloatExpression* zObject) const
  303. {
  304. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  305. result->addValue(
  306. "name", new Framework::JSON::JSONString(zObject->getName()));
  307. result->addValue(
  308. "x", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zX()));
  309. result->addValue(
  310. "y", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zY()));
  311. result->addValue(
  312. "z", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zZ()));
  313. return result;
  314. }
  315. JSONObjectValidationBuilder* JNoiseFloatExpressionFactory::addToValidator(
  316. JSONObjectValidationBuilder* builder) const
  317. {
  318. return builder->withRequiredString("name")
  319. ->finishString()
  320. ->withRequiredAttribute("x",
  321. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  322. ->withRequiredAttribute("y",
  323. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  324. ->withRequiredAttribute("z",
  325. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  326. }
  327. const char* JNoiseFloatExpressionFactory::getTypeToken() const
  328. {
  329. return "noise";
  330. }
  331. JOperatorFloatExpression::JOperatorFloatExpression()
  332. : JFloatExpression(),
  333. accumulator([](float a, float b) { return 0.f; })
  334. {}
  335. float JOperatorFloatExpression::getValue(JExpressionMemory* zMemory)
  336. {
  337. bool first = 1;
  338. float val = 0.f;
  339. for (JFloatExpression* expression : values)
  340. {
  341. if (first)
  342. {
  343. first = 0;
  344. val = expression->getValue(zMemory);
  345. }
  346. else
  347. {
  348. val = accumulator(val, expression->getValue(zMemory));
  349. }
  350. }
  351. return val;
  352. }
  353. void JOperatorFloatExpression::setOperator(
  354. Framework::Text op, std::function<float(float a, float b)> accumulator)
  355. {
  356. this->op = op;
  357. this->accumulator = accumulator;
  358. }
  359. Framework::Text JOperatorFloatExpression::getOperator()
  360. {
  361. return op;
  362. }
  363. void JOperatorFloatExpression::addValue(JFloatExpression* value)
  364. {
  365. values.add(value);
  366. }
  367. const Framework::RCArray<JFloatExpression>&
  368. JOperatorFloatExpression::getValues() const
  369. {
  370. return values;
  371. }
  372. JOperatorFloatExpressionFactory::JOperatorFloatExpressionFactory()
  373. : SubTypeFactory()
  374. {}
  375. JOperatorFloatExpression* JOperatorFloatExpressionFactory::fromJson(
  376. Framework::JSON::JSONObject* zJson) const
  377. {
  378. JOperatorFloatExpression* result = new JOperatorFloatExpression();
  379. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  380. if (op.istGleich("+"))
  381. {
  382. result->setOperator("+", [](float a, float b) { return a + b; });
  383. }
  384. else if (op.istGleich("-"))
  385. {
  386. result->setOperator("-", [](float a, float b) { return a - b; });
  387. }
  388. else if (op.istGleich("*"))
  389. {
  390. result->setOperator("*", [](float a, float b) { return a * b; });
  391. }
  392. else if (op.istGleich("/"))
  393. {
  394. result->setOperator("/", [](float a, float b) { return a / b; });
  395. }
  396. for (Framework::JSON::JSONValue* value :
  397. *zJson->zValue("values")->asArray())
  398. {
  399. result->addValue(
  400. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(value));
  401. }
  402. return result;
  403. }
  404. Framework::JSON::JSONObject* JOperatorFloatExpressionFactory::toJsonObject(
  405. JOperatorFloatExpression* zObject) const
  406. {
  407. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  408. result->addValue(
  409. "operator", new Framework::JSON::JSONString(zObject->getOperator()));
  410. Framework::JSON::JSONArray* values = new Framework::JSON::JSONArray();
  411. for (JFloatExpression* expression : zObject->getValues())
  412. {
  413. values->addValue(
  414. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  415. expression));
  416. }
  417. result->addValue("values", values);
  418. return result;
  419. }
  420. JSONObjectValidationBuilder* JOperatorFloatExpressionFactory::addToValidator(
  421. JSONObjectValidationBuilder* builder) const
  422. {
  423. return builder->withRequiredString("operator")
  424. ->whichIsOneOf({"+", "-", "*", "/"})
  425. ->finishString()
  426. ->withRequiredArray("values")
  427. ->addAcceptedTypeInArray(
  428. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  429. ->finishArray();
  430. }
  431. const char* JOperatorFloatExpressionFactory::getTypeToken() const
  432. {
  433. return "operator";
  434. }
  435. JBoolOperatorBoolExpression::JBoolOperatorBoolExpression()
  436. : JBoolExpression()
  437. {}
  438. bool JBoolOperatorBoolExpression::getValue(JExpressionMemory* zMemory)
  439. {
  440. bool first = 1;
  441. bool val = 0;
  442. for (JBoolExpression* expression : values)
  443. {
  444. if (first)
  445. {
  446. first = 0;
  447. val = expression->getValue(zMemory);
  448. }
  449. else
  450. {
  451. val = accumulator(val, expression->getValue(zMemory));
  452. }
  453. }
  454. return val;
  455. }
  456. void JBoolOperatorBoolExpression::setOperator(
  457. Framework::Text op, std::function<float(float a, float b)> accumulator)
  458. {
  459. this->op = op;
  460. this->accumulator = accumulator;
  461. }
  462. Framework::Text JBoolOperatorBoolExpression::getOperator()
  463. {
  464. return op;
  465. }
  466. void JBoolOperatorBoolExpression::addValue(JBoolExpression* value)
  467. {
  468. values.add(value);
  469. }
  470. const Framework::RCArray<JBoolExpression>&
  471. JBoolOperatorBoolExpression::getValues() const
  472. {
  473. return values;
  474. }
  475. JBoolOperatorBoolExpressionFactory::JBoolOperatorBoolExpressionFactory()
  476. : SubTypeFactory()
  477. {}
  478. JBoolOperatorBoolExpression* JBoolOperatorBoolExpressionFactory::fromJson(
  479. Framework::JSON::JSONObject* zJson) const
  480. {
  481. JBoolOperatorBoolExpression* result = new JBoolOperatorBoolExpression();
  482. for (Framework::JSON::JSONValue* value :
  483. *zJson->zValue("values")->asArray())
  484. {
  485. result->addValue(
  486. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(value));
  487. }
  488. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  489. if (op.istGleich("&&"))
  490. {
  491. result->setOperator("&&", [](bool a, bool b) { return a && b; });
  492. }
  493. else if (op.istGleich("||"))
  494. {
  495. result->setOperator("||", [](bool a, bool b) { return a || b; });
  496. }
  497. return result;
  498. }
  499. Framework::JSON::JSONObject* JBoolOperatorBoolExpressionFactory::toJsonObject(
  500. JBoolOperatorBoolExpression* zObject) const
  501. {
  502. Framework::JSON::JSONObject* zResult = new Framework::JSON::JSONObject();
  503. Framework::JSON::JSONArray* values = new Framework::JSON::JSONArray();
  504. for (JBoolExpression* expression : zObject->getValues())
  505. {
  506. values->addValue(
  507. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  508. expression));
  509. }
  510. zResult->addValue("values", values);
  511. zResult->addValue(
  512. "operator", new Framework::JSON::JSONString(zObject->getOperator()));
  513. return zResult;
  514. }
  515. JSONObjectValidationBuilder* JBoolOperatorBoolExpressionFactory::addToValidator(
  516. JSONObjectValidationBuilder* builder) const
  517. {
  518. return builder->withRequiredString("operator")
  519. ->whichIsOneOf({"&&", "||"})
  520. ->finishString()
  521. ->withRequiredArray("values")
  522. ->addAcceptedTypeInArray(
  523. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  524. ->finishArray();
  525. }
  526. const char* JBoolOperatorBoolExpressionFactory::getTypeToken() const
  527. {
  528. return "operator";
  529. }
  530. JFloatOperatorBoolExpression::JFloatOperatorBoolExpression()
  531. : JBoolExpression()
  532. {}
  533. bool JFloatOperatorBoolExpression::getValue(JExpressionMemory* zMemory)
  534. {
  535. bool first = 1;
  536. bool val = 1;
  537. float last = 0.f;
  538. for (JFloatExpression* expression : values)
  539. {
  540. float current = expression->getValue(zMemory);
  541. if (!first) val &= accumulator(last, current);
  542. first = 0;
  543. last = current;
  544. }
  545. return val;
  546. }
  547. void JFloatOperatorBoolExpression::setOperator(
  548. Framework::Text op, std::function<bool(float a, float b)> accumulator)
  549. {
  550. this->op = op;
  551. this->accumulator = accumulator;
  552. }
  553. Framework::Text JFloatOperatorBoolExpression::getOperator()
  554. {
  555. return op;
  556. }
  557. void JFloatOperatorBoolExpression::addValue(JFloatExpression* value)
  558. {
  559. values.add(value);
  560. }
  561. const Framework::RCArray<JFloatExpression>&
  562. JFloatOperatorBoolExpression::getValues() const
  563. {
  564. return values;
  565. }
  566. JFloatOperatorBoolExpressionFactory::JFloatOperatorBoolExpressionFactory()
  567. : SubTypeFactory()
  568. {}
  569. JFloatOperatorBoolExpression* JFloatOperatorBoolExpressionFactory::fromJson(
  570. Framework::JSON::JSONObject* zJson) const
  571. {
  572. JFloatOperatorBoolExpression* result = new JFloatOperatorBoolExpression();
  573. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  574. if (op.istGleich(">"))
  575. {
  576. result->setOperator(">", [](float a, float b) { return a > b; });
  577. }
  578. else if (op.istGleich("<"))
  579. {
  580. result->setOperator("<", [](float a, float b) { return a < b; });
  581. }
  582. else if (op.istGleich(">="))
  583. {
  584. result->setOperator(">=", [](float a, float b) { return a >= b; });
  585. }
  586. else if (op.istGleich("<="))
  587. {
  588. result->setOperator("<=", [](float a, float b) { return a <= b; });
  589. }
  590. else if (op.istGleich("=="))
  591. {
  592. result->setOperator("==", [](float a, float b) { return a == b; });
  593. }
  594. else if (op.istGleich("!="))
  595. {
  596. result->setOperator("!=", [](float a, float b) { return a != b; });
  597. }
  598. else if (op.istGleich(">i"))
  599. {
  600. result->setOperator(
  601. ">i", [](float a, float b) { return (int)a > (int)b; });
  602. }
  603. else if (op.istGleich("<i"))
  604. {
  605. result->setOperator(
  606. "<i", [](float a, float b) { return (int)a < (int)b; });
  607. }
  608. else if (op.istGleich(">=i"))
  609. {
  610. result->setOperator(
  611. ">=i", [](float a, float b) { return (int)a >= (int)b; });
  612. }
  613. else if (op.istGleich("<=i"))
  614. {
  615. result->setOperator(
  616. "<=i", [](float a, float b) { return (int)a <= (int)b; });
  617. }
  618. else if (op.istGleich("==i"))
  619. {
  620. result->setOperator(
  621. "==i", [](float a, float b) { return (int)a == (int)b; });
  622. }
  623. else if (op.istGleich("!=i"))
  624. {
  625. result->setOperator(
  626. "!=i", [](float a, float b) { return (int)a != (int)b; });
  627. }
  628. for (Framework::JSON::JSONValue* value :
  629. *zJson->zValue("values")->asArray())
  630. {
  631. result->addValue(
  632. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(value));
  633. }
  634. return result;
  635. }
  636. Framework::JSON::JSONObject* JFloatOperatorBoolExpressionFactory::toJsonObject(
  637. JFloatOperatorBoolExpression* zObject) const
  638. {
  639. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  640. result->addValue(
  641. "operator", new Framework::JSON::JSONString(zObject->getOperator()));
  642. Framework::JSON::JSONArray* values = new Framework::JSON::JSONArray();
  643. for (JFloatExpression* expression : zObject->getValues())
  644. {
  645. values->addValue(
  646. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  647. expression));
  648. }
  649. result->addValue("values", values);
  650. return result;
  651. }
  652. JSONObjectValidationBuilder*
  653. JFloatOperatorBoolExpressionFactory::addToValidator(
  654. JSONObjectValidationBuilder* builder) const
  655. {
  656. return builder->withRequiredString("operator")
  657. ->whichIsOneOf({">",
  658. "<",
  659. ">=",
  660. "<=",
  661. "==",
  662. "!=",
  663. "<i",
  664. ">i",
  665. ">=i",
  666. "<=i",
  667. "==i",
  668. "!=i"})
  669. ->finishString()
  670. ->withRequiredArray("values")
  671. ->addAcceptedTypeInArray(
  672. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  673. ->finishArray();
  674. }
  675. const char* JFloatOperatorBoolExpressionFactory::getTypeToken() const
  676. {
  677. return "comparsion";
  678. }
  679. JBlockTypeBoolExpression::JBlockTypeBoolExpression()
  680. : JBoolExpression(),
  681. typeId(0),
  682. x(0),
  683. y(0),
  684. z(0)
  685. {}
  686. JBlockTypeBoolExpression ::~JBlockTypeBoolExpression()
  687. {
  688. if (x) x->release();
  689. if (y) y->release();
  690. if (z) z->release();
  691. }
  692. bool JBlockTypeBoolExpression::getValue(JExpressionMemory* zMemory)
  693. {
  694. int x = (int)(round(this->x->getValue(zMemory)));
  695. int y = (int)(round(this->y->getValue(zMemory)));
  696. int z = (int)(round(this->z->getValue(zMemory)));
  697. if (z < 0 || z >= WORLD_HEIGHT || !zMemory->zCurrentChunk()
  698. || Game::getChunkCenter(x, y) != zMemory->zCurrentChunk()->getCenter())
  699. {
  700. return 0;
  701. }
  702. return zMemory->zCurrentChunk()->getBlockTypeAt(
  703. Dimension::chunkCoordinates({x, y, z}))
  704. == typeId;
  705. }
  706. void JBlockTypeBoolExpression::setTypeId(int typeId)
  707. {
  708. this->typeId = typeId;
  709. }
  710. int JBlockTypeBoolExpression::getTypeId() const
  711. {
  712. return typeId;
  713. }
  714. void JBlockTypeBoolExpression::setX(JFloatExpression* x)
  715. {
  716. if (this->x) this->x->release();
  717. this->x = x;
  718. }
  719. JFloatExpression* JBlockTypeBoolExpression::zX() const
  720. {
  721. return x;
  722. }
  723. void JBlockTypeBoolExpression::setY(JFloatExpression* y)
  724. {
  725. if (this->y) this->y->release();
  726. this->y = y;
  727. }
  728. JFloatExpression* JBlockTypeBoolExpression::zY() const
  729. {
  730. return y;
  731. }
  732. void JBlockTypeBoolExpression::setZ(JFloatExpression* z)
  733. {
  734. if (this->z) this->z->release();
  735. this->z = z;
  736. }
  737. JFloatExpression* JBlockTypeBoolExpression::zZ() const
  738. {
  739. return z;
  740. }
  741. JBlockTypeBoolExpressionFactory::JBlockTypeBoolExpressionFactory()
  742. : SubTypeFactory()
  743. {}
  744. JBlockTypeBoolExpression* JBlockTypeBoolExpressionFactory::fromJson(
  745. Framework::JSON::JSONObject* zJson) const
  746. {
  747. JBlockTypeBoolExpression* result = new JBlockTypeBoolExpression();
  748. result->setTypeId(Game::INSTANCE->getBlockTypeId(
  749. zJson->zValue("blockType")->asString()->getString()));
  750. result->setX(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  751. zJson->zValue("x")));
  752. result->setY(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  753. zJson->zValue("y")));
  754. result->setZ(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  755. zJson->zValue("z")));
  756. return result;
  757. }
  758. Framework::JSON::JSONObject* JBlockTypeBoolExpressionFactory::toJsonObject(
  759. JBlockTypeBoolExpression* zObject) const
  760. {
  761. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  762. result->addValue("blockType",
  763. new Framework::JSON::JSONString(
  764. Game::INSTANCE->zBlockType(zObject->getTypeId())->getName()));
  765. result->addValue(
  766. "x", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zX()));
  767. result->addValue(
  768. "y", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zY()));
  769. result->addValue(
  770. "z", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zZ()));
  771. return result;
  772. }
  773. JSONObjectValidationBuilder* JBlockTypeBoolExpressionFactory::addToValidator(
  774. JSONObjectValidationBuilder* builder) const
  775. {
  776. return builder
  777. ->withRequiredAttribute("blockType",
  778. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  779. BlockTypeNameFactory::TYPE_ID))
  780. ->withRequiredAttribute("x",
  781. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  782. ->withRequiredAttribute("y",
  783. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  784. ->withRequiredAttribute("z",
  785. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  786. }
  787. const char* JBlockTypeBoolExpressionFactory::getTypeToken() const
  788. {
  789. return "blockType";
  790. }