JsonExpression.cpp 24 KB

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