JsonExpression.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #include "JsonExpression.h"
  2. #include "DimensionGenerator.h"
  3. #include "Game.h"
  4. JExpressionMemory::JExpressionMemory()
  5. : ReferenceCounter(),
  6. currentChunk(0)
  7. {}
  8. JExpressionMemory::~JExpressionMemory()
  9. {
  10. if (currentChunk) currentChunk->release();
  11. }
  12. void JExpressionMemory::lock()
  13. {
  14. cs.lock();
  15. }
  16. void JExpressionMemory::unlock()
  17. {
  18. cs.unlock();
  19. }
  20. float JExpressionMemory::getNoise(
  21. Framework::Text name, float x, float y, float z) const
  22. {
  23. Noise* currentNoise = noises.z(name, name.getLength());
  24. if (currentNoise)
  25. return (float)currentNoise->getNoise((double)x, (double)y, (double)z);
  26. return 0.f;
  27. }
  28. void JExpressionMemory::setNoise(Framework::Text name, Noise* noise)
  29. {
  30. noises.set(name, name.getLength(), noise);
  31. }
  32. void JExpressionMemory::setCurrentChunk(Chunk* chunk)
  33. {
  34. if (currentChunk) currentChunk->release();
  35. currentChunk = chunk;
  36. }
  37. float JExpressionMemory::getFloatVariable(Framework::Text name) const
  38. {
  39. return floatVariables.get(name, name.getLength());
  40. }
  41. void JExpressionMemory::setFloatVariable(Framework::Text name, float value)
  42. {
  43. floatVariables.set(name, name.getLength(), value);
  44. }
  45. bool JExpressionMemory::getBoolVariable(Framework::Text name) const
  46. {
  47. return boolVariables.get(name, name.getLength());
  48. }
  49. void JExpressionMemory::setBoolVariable(Framework::Text name, bool value)
  50. {
  51. return boolVariables.set(name, name.getLength(), value);
  52. }
  53. Chunk* JExpressionMemory::zCurrentChunk()
  54. {
  55. return currentChunk;
  56. }
  57. JFloatExpression::JFloatExpression()
  58. : ReferenceCounter()
  59. {}
  60. JBoolExpression::JBoolExpression()
  61. : ReferenceCounter()
  62. {}
  63. JVariableFloatExpression::JVariableFloatExpression(Framework::Text name)
  64. : JFloatExpression(),
  65. name(name)
  66. {}
  67. float JVariableFloatExpression::getValue(JExpressionMemory* zMemory)
  68. {
  69. return zMemory->getFloatVariable(name);
  70. }
  71. JVariableBoolExpression::JVariableBoolExpression(Framework::Text name)
  72. : JBoolExpression(),
  73. name(name)
  74. {}
  75. bool JVariableBoolExpression::getValue(JExpressionMemory* zMemory)
  76. {
  77. return zMemory->getBoolVariable(name);
  78. }
  79. JConstantFloatExpression::JConstantFloatExpression(float value)
  80. : JFloatExpression(),
  81. value(value)
  82. {}
  83. float JConstantFloatExpression::getValue(JExpressionMemory* zMemory)
  84. {
  85. return value;
  86. }
  87. JConstantBoolExpression::JConstantBoolExpression(bool value)
  88. : JBoolExpression(),
  89. value(value)
  90. {}
  91. bool JConstantBoolExpression::getValue(JExpressionMemory* zMemory)
  92. {
  93. return value;
  94. }
  95. JNoiseFloatExpression::JNoiseFloatExpression(Framework::Text name,
  96. JFloatExpression* x,
  97. JFloatExpression* y,
  98. JFloatExpression* z)
  99. : JFloatExpression(),
  100. name(name),
  101. x(x),
  102. y(y),
  103. z(z)
  104. {}
  105. JNoiseFloatExpression::~JNoiseFloatExpression()
  106. {
  107. x->release();
  108. y->release();
  109. z->release();
  110. }
  111. float JNoiseFloatExpression::getValue(JExpressionMemory* zMemory)
  112. {
  113. return zMemory->getNoise(
  114. name, x->getValue(zMemory), y->getValue(zMemory), z->getValue(zMemory));
  115. }
  116. JOperatorFloatExpression::JOperatorFloatExpression(
  117. std::function<float(float a, float b)> accumulator,
  118. Framework::RCArray<JFloatExpression>* values)
  119. : JFloatExpression(),
  120. accumulator(accumulator),
  121. values(values)
  122. {}
  123. JOperatorFloatExpression::~JOperatorFloatExpression()
  124. {
  125. values->release();
  126. }
  127. float JOperatorFloatExpression::getValue(JExpressionMemory* zMemory)
  128. {
  129. bool first = 1;
  130. float val = 0.f;
  131. for (JFloatExpression* expression : *values)
  132. {
  133. if (first)
  134. {
  135. first = 0;
  136. val = expression->getValue(zMemory);
  137. }
  138. else
  139. {
  140. val = accumulator(val, expression->getValue(zMemory));
  141. }
  142. }
  143. return val;
  144. }
  145. JBoolOperatorBoolExpression::JBoolOperatorBoolExpression(
  146. std::function<bool(bool a, bool b)> accumulator,
  147. Framework::RCArray<JBoolExpression>* values)
  148. : JBoolExpression(),
  149. accumulator(accumulator),
  150. values(values)
  151. {}
  152. JBoolOperatorBoolExpression::~JBoolOperatorBoolExpression()
  153. {
  154. values->release();
  155. }
  156. bool JBoolOperatorBoolExpression::getValue(JExpressionMemory* zMemory)
  157. {
  158. bool first = 1;
  159. bool val = 0;
  160. for (JBoolExpression* expression : *values)
  161. {
  162. if (first)
  163. {
  164. first = 0;
  165. val = expression->getValue(zMemory);
  166. }
  167. else
  168. {
  169. val = accumulator(val, expression->getValue(zMemory));
  170. }
  171. }
  172. return val;
  173. }
  174. JFloatOperatorBoolExpression::JFloatOperatorBoolExpression(
  175. std::function<bool(float a, float b)> accumulator,
  176. Framework::RCArray<JFloatExpression>* values)
  177. : JBoolExpression(),
  178. accumulator(accumulator),
  179. values(values)
  180. {}
  181. JFloatOperatorBoolExpression::~JFloatOperatorBoolExpression()
  182. {
  183. values->release();
  184. }
  185. bool JFloatOperatorBoolExpression::getValue(JExpressionMemory* zMemory)
  186. {
  187. bool first = 1;
  188. bool val = 1;
  189. float last = 0.f;
  190. for (JFloatExpression* expression : *values)
  191. {
  192. float current = expression->getValue(zMemory);
  193. if (!first) val &= accumulator(last, current);
  194. first = 0;
  195. last = current;
  196. }
  197. return val;
  198. }
  199. JBlockTypeBoolExpression::JBlockTypeBoolExpression(
  200. int typeId, JFloatExpression* x, JFloatExpression* y, JFloatExpression* z)
  201. : JBoolExpression(),
  202. typeId(typeId),
  203. x(x),
  204. y(y),
  205. z(z)
  206. {}
  207. JBlockTypeBoolExpression ::~JBlockTypeBoolExpression()
  208. {
  209. x->release();
  210. y->release();
  211. z->release();
  212. }
  213. bool JBlockTypeBoolExpression::getValue(JExpressionMemory* zMemory)
  214. {
  215. int x = (int)(round(this->x->getValue(zMemory)));
  216. int y = (int)(round(this->y->getValue(zMemory)));
  217. int z = (int)(round(this->z->getValue(zMemory)));
  218. if (z < 0 || z >= WORLD_HEIGHT
  219. || !zMemory->zCurrentChunk()
  220. || Game::getChunkCenter(x, y) != zMemory->zCurrentChunk()->getCenter())
  221. {
  222. return 0;
  223. }
  224. return zMemory->zCurrentChunk()->getBlockTypeAt(
  225. Dimension::chunkCoordinates({x, y, z}))
  226. == typeId;
  227. }
  228. using namespace Framework::JSON;
  229. using namespace Framework::JSON::Validator;
  230. JFloatExpression* JExpressionParser::parseFloatExpression(
  231. Framework::JSON::JSONValue* zValue)
  232. {
  233. JSONObject* obj = zValue->asObject();
  234. Framework::Text type = obj->zValue("type")->asString()->getString();
  235. if (type.istGleich("variable"))
  236. {
  237. return new JVariableFloatExpression(
  238. obj->zValue("name")->asString()->getString());
  239. }
  240. else if (type.istGleich("constant"))
  241. {
  242. return new JConstantFloatExpression(
  243. (float)obj->zValue("value")->asNumber()->getNumber());
  244. }
  245. else if (type.istGleich("noise"))
  246. {
  247. return new JNoiseFloatExpression(
  248. obj->zValue("name")->asString()->getString(),
  249. parseFloatExpression(obj->zValue("x")),
  250. parseFloatExpression(obj->zValue("y")),
  251. parseFloatExpression(obj->zValue("z")));
  252. }
  253. else if (type.istGleich("operator"))
  254. {
  255. Framework::RCArray<JFloatExpression>* values
  256. = new Framework::RCArray<JFloatExpression>();
  257. JSONArray* valuesArray = obj->zValue("values")->asArray();
  258. for (JSONValue* value : *valuesArray)
  259. {
  260. values->add(parseFloatExpression(value));
  261. }
  262. Framework::Text op = obj->zValue("operator")->asString()->getString();
  263. if (op.istGleich("+"))
  264. {
  265. return new JOperatorFloatExpression(
  266. [](float a, float b) { return a + b; }, values);
  267. }
  268. else if (op.istGleich("-"))
  269. {
  270. return new JOperatorFloatExpression(
  271. [](float a, float b) { return a - b; }, values);
  272. }
  273. else if (op.istGleich("*"))
  274. {
  275. return new JOperatorFloatExpression(
  276. [](float a, float b) { return a * b; }, values);
  277. }
  278. else if (op.istGleich("/"))
  279. {
  280. return new JOperatorFloatExpression(
  281. [](float a, float b) { return a / b; }, values);
  282. }
  283. else
  284. {
  285. values->release();
  286. }
  287. }
  288. return 0;
  289. }
  290. Framework::JSON::Validator::JSONValidator*
  291. JExpressionParser::getFloatValidator()
  292. {
  293. JSONValidator* refs
  294. = JSONValidator::buildForOneOf()
  295. ->typeSpecifiedByAttribute("type")
  296. ->addAcceptedType(
  297. JSONValidator::buildForObjectReference("jef_variable"))
  298. ->addAcceptedType(
  299. JSONValidator::buildForObjectReference("jef_constant"))
  300. ->addAcceptedType(
  301. JSONValidator::buildForObjectReference("jef_noise"))
  302. ->addAcceptedType(
  303. JSONValidator::buildForObjectReference("jef_operator"))
  304. ->finishOneOf();
  305. JSONValidator* validator
  306. = JSONValidator::buildForOneOf()
  307. ->typeSpecifiedByAttribute("type")
  308. ->addAcceptedType(JSONValidator::buildForObject()
  309. ->setObjectReferenceId("jef_variable")
  310. ->withRequiredString("type")
  311. ->whichIs("variable")
  312. ->finishString()
  313. ->withRequiredString("name")
  314. ->finishString()
  315. ->finishObject())
  316. ->addAcceptedType(JSONValidator::buildForObject()
  317. ->setObjectReferenceId("jef_constant")
  318. ->withRequiredString("type")
  319. ->whichIs("constant")
  320. ->finishString()
  321. ->withRequiredNumber("value")
  322. ->finishNumber()
  323. ->finishObject())
  324. ->addAcceptedType(
  325. JSONValidator::buildForObject()
  326. ->setObjectReferenceId("jef_noise")
  327. ->withRequiredString("type")
  328. ->whichIs("noise")
  329. ->finishString()
  330. ->withRequiredString("name")
  331. ->withDefault("noise")
  332. ->finishString()
  333. ->withRequiredAttribute(
  334. "x", dynamic_cast<JSONValidator*>(refs->getThis()))
  335. ->withRequiredAttribute(
  336. "y", dynamic_cast<JSONValidator*>(refs->getThis()))
  337. ->withRequiredAttribute(
  338. "z", dynamic_cast<JSONValidator*>(refs->getThis()))
  339. ->finishObject())
  340. ->addAcceptedType(
  341. JSONValidator::buildForObject()
  342. ->setObjectReferenceId("jef_operator")
  343. ->withRequiredString("type")
  344. ->whichIs("operator")
  345. ->finishString()
  346. ->withRequiredString("operator")
  347. ->whichIsOneOf({"+", "-", "*", "/"})
  348. ->finishString()
  349. ->withRequiredArray("values")
  350. ->addAcceptedTypeInArray(
  351. dynamic_cast<JSONValidator*>(refs->getThis()))
  352. ->finishArray()
  353. ->finishObject())
  354. ->finishOneOf();
  355. return validator;
  356. }
  357. JBoolExpression* JExpressionParser::parseBoolExpression(
  358. Framework::JSON::JSONValue* zValue)
  359. {
  360. JSONObject* obj = zValue->asObject();
  361. Framework::Text type = obj->zValue("type")->asString()->getString();
  362. if (type.istGleich("variable"))
  363. {
  364. return new JVariableBoolExpression(
  365. obj->zValue("name")->asString()->getString());
  366. }
  367. else if (type.istGleich("constant"))
  368. {
  369. return new JConstantBoolExpression(
  370. (float)obj->zValue("value")->asBool()->getBool());
  371. }
  372. else if (type.istGleich("blockType"))
  373. {
  374. return new JBlockTypeBoolExpression(
  375. BlockType::getTypeId(
  376. obj->zValue("blockType")->asString()->getString()),
  377. parseFloatExpression(obj->zValue("x")),
  378. parseFloatExpression(obj->zValue("y")),
  379. parseFloatExpression(obj->zValue("z")));
  380. }
  381. else if (type.istGleich("operator"))
  382. {
  383. Framework::Text op = obj->zValue("operator")->asString()->getString();
  384. if (op.istGleich("&&") || op.istGleich("||"))
  385. {
  386. Framework::RCArray<JBoolExpression>* values
  387. = new Framework::RCArray<JBoolExpression>();
  388. JSONArray* valuesArray = obj->zValue("values")->asArray();
  389. for (JSONValue* value : *valuesArray)
  390. {
  391. values->add(parseBoolExpression(value));
  392. }
  393. if (op.istGleich("&&"))
  394. {
  395. return new JBoolOperatorBoolExpression(
  396. [](bool a, bool b) { return a && b; }, values);
  397. }
  398. else if (op.istGleich("||"))
  399. {
  400. return new JBoolOperatorBoolExpression(
  401. [](bool a, bool b) { return a || b; }, values);
  402. }
  403. else
  404. {
  405. values->release();
  406. }
  407. }
  408. else
  409. {
  410. Framework::RCArray<JFloatExpression>* values
  411. = new Framework::RCArray<JFloatExpression>();
  412. JSONArray* valuesArray = obj->zValue("values")->asArray();
  413. for (JSONValue* value : *valuesArray)
  414. {
  415. values->add(parseFloatExpression(value));
  416. }
  417. if (op.istGleich(">"))
  418. {
  419. return new JFloatOperatorBoolExpression(
  420. [](float a, float b) { return a > b; }, values);
  421. }
  422. else if (op.istGleich("<"))
  423. {
  424. return new JFloatOperatorBoolExpression(
  425. [](float a, float b) { return a < b; }, values);
  426. }
  427. else if (op.istGleich(">="))
  428. {
  429. return new JFloatOperatorBoolExpression(
  430. [](float a, float b) { return a >= b; }, values);
  431. }
  432. else if (op.istGleich("<="))
  433. {
  434. return new JFloatOperatorBoolExpression(
  435. [](float a, float b) { return a <= b; }, values);
  436. }
  437. else if (op.istGleich("=="))
  438. {
  439. return new JFloatOperatorBoolExpression(
  440. [](float a, float b) { return a == b; }, values);
  441. }
  442. else if (op.istGleich("!="))
  443. {
  444. return new JFloatOperatorBoolExpression(
  445. [](float a, float b) { return a != b; }, values);
  446. }
  447. else if (op.istGleich(">i"))
  448. {
  449. return new JFloatOperatorBoolExpression(
  450. [](float a, float b) { return (int)a > (int)b; }, values);
  451. }
  452. else if (op.istGleich("<i"))
  453. {
  454. return new JFloatOperatorBoolExpression(
  455. [](float a, float b) { return (int)a < (int)b; }, values);
  456. }
  457. else if (op.istGleich(">=i"))
  458. {
  459. return new JFloatOperatorBoolExpression(
  460. [](float a, float b) { return (int)a >= (int)b; }, values);
  461. }
  462. else if (op.istGleich("<=i"))
  463. {
  464. return new JFloatOperatorBoolExpression(
  465. [](float a, float b) { return (int)a <= (int)b; }, values);
  466. }
  467. else if (op.istGleich("==i"))
  468. {
  469. return new JFloatOperatorBoolExpression(
  470. [](float a, float b) { return (int)a == (int)b; }, values);
  471. }
  472. else if (op.istGleich("!=i"))
  473. {
  474. return new JFloatOperatorBoolExpression(
  475. [](float a, float b) { return (int)a != (int)b; }, values);
  476. }
  477. else
  478. {
  479. values->release();
  480. }
  481. }
  482. }
  483. return 0;
  484. }
  485. Framework::JSON::Validator::JSONValidator* JExpressionParser::getBoolValidator()
  486. {
  487. Framework::RCArray<Framework::Text> blockTypeNames;
  488. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  489. {
  490. if (Game::INSTANCE->zBlockType(i))
  491. {
  492. blockTypeNames.add(new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
  493. }
  494. }
  495. JSONValidator* refs
  496. = JSONValidator::buildForOneOf()
  497. ->typeSpecifiedByAttribute("type")
  498. ->addAcceptedType(
  499. JSONValidator::buildForObjectReference("jeb_variable"))
  500. ->addAcceptedType(
  501. JSONValidator::buildForObjectReference("jeb_constant"))
  502. ->addAcceptedType(
  503. JSONValidator::buildForObjectReference("jeb_blockType"))
  504. ->addAcceptedType(
  505. JSONValidator::buildForObjectReference("jeb_bool_operator"))
  506. ->addAcceptedType(
  507. JSONValidator::buildForObjectReference("jeb_float_operator"))
  508. ->finishOneOf();
  509. JSONValidator* validator
  510. = JSONValidator::buildForOneOf()
  511. ->typeSpecifiedByAttribute("type")
  512. ->addAcceptedType(JSONValidator::buildForObject()
  513. ->setObjectReferenceId("jeb_variable")
  514. ->withRequiredString("type")
  515. ->whichIs("variable")
  516. ->finishString()
  517. ->withRequiredString("name")
  518. ->finishString()
  519. ->finishObject())
  520. ->addAcceptedType(JSONValidator::buildForObject()
  521. ->setObjectReferenceId("jeb_constant")
  522. ->withRequiredString("type")
  523. ->whichIs("constant")
  524. ->finishString()
  525. ->withRequiredBool("value")
  526. ->finishBool()
  527. ->finishObject())
  528. ->addAcceptedType(
  529. JSONValidator::buildForObject()
  530. ->setObjectReferenceId("jeb_blockType")
  531. ->withRequiredString("type")
  532. ->whichIs("blockType")
  533. ->finishString()
  534. ->withRequiredString("blockType")
  535. ->whichIsOneOf(blockTypeNames)
  536. ->finishString()
  537. ->withRequiredAttribute("x", getFloatValidator())
  538. ->withRequiredAttribute("y", getFloatValidator())
  539. ->withRequiredAttribute("z", getFloatValidator())
  540. ->finishObject())
  541. ->addAcceptedType(
  542. JSONValidator::buildForObject()
  543. ->setObjectReferenceId("jeb_bool_operator")
  544. ->withRequiredString("type")
  545. ->whichIs("operator")
  546. ->finishString()
  547. ->withRequiredString("operator")
  548. ->whichIsOneOf({"&&", "||"})
  549. ->finishString()
  550. ->withRequiredArray("values")
  551. ->addAcceptedTypeInArray(
  552. dynamic_cast<JSONValidator*>(refs->getThis()))
  553. ->finishArray()
  554. ->finishObject())
  555. ->addAcceptedType(
  556. JSONValidator::buildForObject()
  557. ->setObjectReferenceId("jeb_float_operator")
  558. ->withRequiredString("type")
  559. ->whichIs("operator")
  560. ->finishString()
  561. ->withRequiredString("operator")
  562. ->whichIsOneOf({">",
  563. "<",
  564. ">=",
  565. "<=",
  566. "==",
  567. "!=",
  568. ">i",
  569. "<i",
  570. ">=i",
  571. "<=i",
  572. "==i",
  573. "!=i"})
  574. ->finishString()
  575. ->withRequiredArray("values")
  576. ->addAcceptedTypeInArray(getFloatValidator())
  577. ->finishArray()
  578. ->finishObject())
  579. ->finishOneOf();
  580. return validator;
  581. }