JsonExpression.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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. Noise* JExpressionMemory::zNoiseP(Framework::Text name)
  30. {
  31. return noises.z(name, name.getLength());
  32. }
  33. void JExpressionMemory::setNoise(Framework::Text name, Noise* noise)
  34. {
  35. noises.set(name, name.getLength(), noise);
  36. }
  37. void JExpressionMemory::setCurrentChunk(Chunk* chunk)
  38. {
  39. if (currentChunk) currentChunk->release();
  40. currentChunk = chunk;
  41. }
  42. float JExpressionMemory::getFloatVariable(const Framework::Text& name) const
  43. {
  44. return floatVariables.get(name, name.getLength());
  45. }
  46. float* JExpressionMemory::getFloatVariableP(const Framework::Text& name)
  47. {
  48. if (!floatVariables.contains(name, name.getLength()))
  49. {
  50. floatVariables.set(name, name.getLength(), 0.f);
  51. }
  52. return floatVariables.getP(name, name.getLength());
  53. }
  54. void JExpressionMemory::setFloatVariable(
  55. const Framework::Text& name, float value)
  56. {
  57. floatVariables.set(name, name.getLength(), value);
  58. }
  59. bool JExpressionMemory::getBoolVariable(const Framework::Text& name) const
  60. {
  61. return boolVariables.get(name, name.getLength());
  62. }
  63. bool* JExpressionMemory::getBoolVariableP(const Framework::Text& name)
  64. {
  65. if (!boolVariables.contains(name, name.getLength()))
  66. {
  67. boolVariables.set(name, name.getLength(), 0);
  68. }
  69. return boolVariables.getP(name, name.getLength());
  70. }
  71. void JExpressionMemory::setBoolVariable(const Framework::Text& name, bool value)
  72. {
  73. return boolVariables.set(name, name.getLength(), value);
  74. }
  75. Chunk* JExpressionMemory::zCurrentChunk()
  76. {
  77. return currentChunk;
  78. }
  79. Chunk** JExpressionMemory::zzCurrentChunk()
  80. {
  81. return &currentChunk;
  82. }
  83. JFloatExpression::JFloatExpression()
  84. : ReferenceCounter(),
  85. compiled(0)
  86. {}
  87. float JFloatExpression::getValue(JExpressionMemory* zMemory)
  88. {
  89. FloatFunc func = compile(zMemory);
  90. float result = func();
  91. /* float old = getValueOld(zMemory);
  92. if (result != old)
  93. {
  94. float x = func();
  95. Framework::Logging::debug() << "fehler" << x << " != " << old;
  96. }*/
  97. return result;
  98. }
  99. FloatFunc JFloatExpression::compile(JExpressionMemory* zMemory)
  100. {
  101. if (compiled)
  102. {
  103. if (zMemory != memory)
  104. {
  105. throw "Cannot compile the same expression for different memories";
  106. }
  107. return compiled;
  108. }
  109. memory = zMemory;
  110. return compiled = buildAssembly(zMemory).compileToFunction<FloatFunc>();
  111. }
  112. JBoolExpression::JBoolExpression()
  113. : ReferenceCounter(),
  114. compiled(0)
  115. {}
  116. bool JBoolExpression::getValue(JExpressionMemory* zMemory)
  117. {
  118. bool result = compile(zMemory)();
  119. /* bool old = getValueOld(zMemory);
  120. if (result != old)
  121. {
  122. Framework::Logging::debug() << "fehler";
  123. }*/
  124. return result;
  125. }
  126. BoolFunc JBoolExpression::compile(JExpressionMemory* zMemory)
  127. {
  128. if (compiled)
  129. {
  130. if (zMemory != memory)
  131. {
  132. throw "Cannot compile the same expression for different "
  133. "memories";
  134. }
  135. return compiled;
  136. }
  137. memory = zMemory;
  138. return compiled = buildAssembly(zMemory).compileToFunction<BoolFunc>();
  139. }
  140. JVariableFloatExpression::JVariableFloatExpression()
  141. : JFloatExpression()
  142. {}
  143. float JVariableFloatExpression::getValueOld(JExpressionMemory* zMemory)
  144. {
  145. return zMemory->getFloatVariable(name);
  146. }
  147. Framework::Assembly::AssemblyBlock& JVariableFloatExpression::buildAssembly(
  148. JExpressionMemory* zMemory)
  149. {
  150. codeBlock.addLoadValue(
  151. zMemory->getFloatVariableP(name), Framework::Assembly::MM0);
  152. return codeBlock;
  153. }
  154. void JVariableFloatExpression::setName(Framework::Text name)
  155. {
  156. this->name = name;
  157. }
  158. Framework::Text JVariableFloatExpression::getName() const
  159. {
  160. return name;
  161. }
  162. JVariableFloatExpressionFactory::JVariableFloatExpressionFactory()
  163. : SubTypeFactory()
  164. {}
  165. JVariableFloatExpression* JVariableFloatExpressionFactory::fromJson(
  166. Framework::JSON::JSONObject* zJson) const
  167. {
  168. JVariableFloatExpression* result = new JVariableFloatExpression();
  169. result->setName(zJson->zValue("name")->asString()->getString());
  170. return result;
  171. }
  172. Framework::JSON::JSONObject* JVariableFloatExpressionFactory::toJsonObject(
  173. JVariableFloatExpression* zObject) const
  174. {
  175. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  176. result->addValue(
  177. "name", new Framework::JSON::JSONString(zObject->getName()));
  178. return result;
  179. }
  180. JSONObjectValidationBuilder* JVariableFloatExpressionFactory::addToValidator(
  181. JSONObjectValidationBuilder* builder) const
  182. {
  183. return builder->withRequiredString("name")->finishString();
  184. }
  185. const char* JVariableFloatExpressionFactory::getTypeToken() const
  186. {
  187. return "variable";
  188. }
  189. JVariableBoolExpression::JVariableBoolExpression()
  190. : JBoolExpression()
  191. {}
  192. bool JVariableBoolExpression::getValueOld(JExpressionMemory* zMemory)
  193. {
  194. return zMemory->getBoolVariable(name);
  195. }
  196. Framework::Assembly::AssemblyBlock& JVariableBoolExpression::buildAssembly(
  197. JExpressionMemory* zMemory)
  198. {
  199. codeBlock.addLoadValue(
  200. (char*)zMemory->getBoolVariableP(name), Framework::Assembly::RAX);
  201. return codeBlock;
  202. }
  203. void JVariableBoolExpression::setName(Framework::Text name)
  204. {
  205. this->name = name;
  206. }
  207. Framework::Text JVariableBoolExpression::getName() const
  208. {
  209. return name;
  210. }
  211. JVariableBoolExpressionFactory::JVariableBoolExpressionFactory()
  212. : SubTypeFactory()
  213. {}
  214. JVariableBoolExpression* JVariableBoolExpressionFactory::fromJson(
  215. Framework::JSON::JSONObject* zJson) const
  216. {
  217. JVariableBoolExpression* result = new JVariableBoolExpression();
  218. result->setName(zJson->zValue("name")->asString()->getString());
  219. return result;
  220. }
  221. Framework::JSON::JSONObject* JVariableBoolExpressionFactory::toJsonObject(
  222. JVariableBoolExpression* zObject) const
  223. {
  224. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  225. result->addValue(
  226. "name", new Framework::JSON::JSONString(zObject->getName()));
  227. return result;
  228. }
  229. JSONObjectValidationBuilder* JVariableBoolExpressionFactory::addToValidator(
  230. JSONObjectValidationBuilder* builder) const
  231. {
  232. return builder->withRequiredString("name")->finishString();
  233. }
  234. const char* JVariableBoolExpressionFactory::getTypeToken() const
  235. {
  236. return "variable";
  237. }
  238. JConstantFloatExpression::JConstantFloatExpression()
  239. : JFloatExpression(),
  240. value(0)
  241. {}
  242. float JConstantFloatExpression::getValueOld(JExpressionMemory* zMemory)
  243. {
  244. return value;
  245. }
  246. Framework::Assembly::AssemblyBlock& JConstantFloatExpression::buildAssembly(
  247. JExpressionMemory* zMemory)
  248. {
  249. codeBlock.addLoadValue(&value, Framework::Assembly::MM0);
  250. return codeBlock;
  251. }
  252. void JConstantFloatExpression::setValue(float value)
  253. {
  254. this->value = value;
  255. }
  256. float JConstantFloatExpression::getValue() const
  257. {
  258. return value;
  259. }
  260. JConstantFloatExpressionFactory::JConstantFloatExpressionFactory()
  261. : SubTypeFactory()
  262. {}
  263. JConstantFloatExpression* JConstantFloatExpressionFactory::fromJson(
  264. Framework::JSON::JSONObject* zJson) const
  265. {
  266. JConstantFloatExpression* result = new JConstantFloatExpression();
  267. result->setValue((float)zJson->zValue("value")->asNumber()->getNumber());
  268. return result;
  269. }
  270. Framework::JSON::JSONObject* JConstantFloatExpressionFactory::toJsonObject(
  271. JConstantFloatExpression* zObject) const
  272. {
  273. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  274. result->addValue(
  275. "value", new Framework::JSON::JSONNumber(zObject->getValue()));
  276. return result;
  277. }
  278. JSONObjectValidationBuilder* JConstantFloatExpressionFactory::addToValidator(
  279. JSONObjectValidationBuilder* builder) const
  280. {
  281. return builder->withRequiredNumber("value")->finishNumber();
  282. }
  283. const char* JConstantFloatExpressionFactory::getTypeToken() const
  284. {
  285. return "constant";
  286. }
  287. JConstantBoolExpression::JConstantBoolExpression()
  288. : JBoolExpression()
  289. {}
  290. bool JConstantBoolExpression::getValueOld(JExpressionMemory* zMemory)
  291. {
  292. return value;
  293. }
  294. Framework::Assembly::AssemblyBlock& JConstantBoolExpression::buildAssembly(
  295. JExpressionMemory* zMemory)
  296. {
  297. codeBlock.addMoveValue(Framework::Assembly::RAX, (char)(value ? 1 : 0));
  298. return codeBlock;
  299. }
  300. void JConstantBoolExpression::setValue(bool value)
  301. {
  302. this->value = value;
  303. }
  304. bool JConstantBoolExpression::getValue() const
  305. {
  306. return value;
  307. }
  308. JConstantBoolExpressionFactory::JConstantBoolExpressionFactory()
  309. : SubTypeFactory()
  310. {}
  311. JConstantBoolExpression* JConstantBoolExpressionFactory::fromJson(
  312. Framework::JSON::JSONObject* zJson) const
  313. {
  314. JConstantBoolExpression* result = new JConstantBoolExpression();
  315. result->setValue(zJson->zValue("value")->asBool()->getBool());
  316. return result;
  317. }
  318. Framework::JSON::JSONObject* JConstantBoolExpressionFactory::toJsonObject(
  319. JConstantBoolExpression* zObject) const
  320. {
  321. Framework::JSON::JSONObject* zResult = new Framework::JSON::JSONObject();
  322. zResult->addValue(
  323. "value", new Framework::JSON::JSONBool(zObject->getValue()));
  324. return zResult;
  325. }
  326. JSONObjectValidationBuilder* JConstantBoolExpressionFactory::addToValidator(
  327. JSONObjectValidationBuilder* builder) const
  328. {
  329. return builder->withRequiredBool("value")->finishBool();
  330. }
  331. const char* JConstantBoolExpressionFactory::getTypeToken() const
  332. {
  333. return "constant";
  334. }
  335. JNoiseFloatExpression::JNoiseFloatExpression()
  336. : JFloatExpression(),
  337. x(0),
  338. y(0),
  339. z(0)
  340. {}
  341. JNoiseFloatExpression::~JNoiseFloatExpression()
  342. {
  343. if (x) x->release();
  344. if (y) y->release();
  345. if (z) z->release();
  346. }
  347. float JNoiseFloatExpression::getValueOld(JExpressionMemory* zMemory)
  348. {
  349. return zMemory->getNoise(name,
  350. x->getValueOld(zMemory),
  351. y->getValueOld(zMemory),
  352. z->getValueOld(zMemory));
  353. }
  354. Framework::Assembly::AssemblyBlock& JNoiseFloatExpression::buildAssembly(
  355. JExpressionMemory* zMemory)
  356. {
  357. Noise* noise = zMemory->zNoiseP(name);
  358. if (!noise)
  359. {
  360. Framework::Logging::error() << "no noise with name '" << name.getText()
  361. << "' found, behavior is undefined\n";
  362. return codeBlock;
  363. }
  364. Framework::Assembly::AssemblyBlock& xBlock = x->buildAssembly(zMemory);
  365. Framework::Assembly::AssemblyBlock& yBlock = y->buildAssembly(zMemory);
  366. Framework::Assembly::AssemblyBlock& zBlock = z->buildAssembly(zMemory);
  367. Framework::Assembly::FPRegister xTarget = Framework::Assembly::MM0;
  368. if (xBlock.isReplacementPossible(
  369. Framework::Assembly::MM0, Framework::Assembly::MM1))
  370. {
  371. xBlock.replaceRegister(
  372. Framework::Assembly::MM0, Framework::Assembly::MM1);
  373. xTarget = Framework::Assembly::MM1;
  374. }
  375. Framework::Assembly::FPRegister yTarget = Framework::Assembly::MM0;
  376. if (yBlock.isReplacementPossible(
  377. Framework::Assembly::MM0, Framework::Assembly::MM2))
  378. {
  379. yBlock.replaceRegister(
  380. Framework::Assembly::MM0, Framework::Assembly::MM2);
  381. yTarget = Framework::Assembly::MM2;
  382. }
  383. Framework::Assembly::FPRegister zTarget = Framework::Assembly::MM0;
  384. if (zBlock.isReplacementPossible(
  385. Framework::Assembly::MM0, Framework::Assembly::MM3))
  386. {
  387. zBlock.replaceRegister(
  388. Framework::Assembly::MM0, Framework::Assembly::MM3);
  389. zTarget = Framework::Assembly::MM3;
  390. }
  391. codeBlock.addBlock(&zBlock, {}, {}, {}, 0, &zTarget);
  392. if (zTarget != Framework::Assembly::MM3)
  393. {
  394. codeBlock.addMoveValue(Framework::Assembly::MM3,
  395. zTarget,
  396. Framework::Assembly::SINGLE_FLOAT,
  397. Framework::Assembly::X);
  398. }
  399. codeBlock.addBlock(&yBlock,
  400. {},
  401. {Framework::Assembly::MM3},
  402. {Framework::Assembly::SINGLE_FLOAT},
  403. 0,
  404. &yTarget);
  405. if (yTarget != Framework::Assembly::MM2)
  406. {
  407. codeBlock.addMoveValue(Framework::Assembly::MM2,
  408. yTarget,
  409. Framework::Assembly::SINGLE_FLOAT,
  410. Framework::Assembly::X);
  411. }
  412. codeBlock.addBlock(&xBlock,
  413. {},
  414. {Framework::Assembly::MM2, Framework::Assembly::MM3},
  415. {Framework::Assembly::SINGLE_FLOAT, Framework::Assembly::SINGLE_FLOAT},
  416. 0,
  417. &xTarget);
  418. if (xTarget != Framework::Assembly::MM1)
  419. {
  420. codeBlock.addMoveValue(Framework::Assembly::MM1,
  421. xTarget,
  422. Framework::Assembly::SINGLE_FLOAT,
  423. Framework::Assembly::X);
  424. }
  425. codeBlock.addLoadAddress(noise, Framework::Assembly::RCX);
  426. codeBlock.addMemberCall<float (Noise::*)(float, float, float)>(
  427. &Noise::getNoise,
  428. Framework::Assembly::FLOAT_VALUE,
  429. {Framework::Assembly::RCX},
  430. {Framework::Assembly::MM1,
  431. Framework::Assembly::MM2,
  432. Framework::Assembly::MM3});
  433. return codeBlock;
  434. }
  435. void JNoiseFloatExpression::setName(Framework::Text name)
  436. {
  437. this->name = name;
  438. }
  439. Framework::Text JNoiseFloatExpression::getName() const
  440. {
  441. return name;
  442. }
  443. void JNoiseFloatExpression::setX(JFloatExpression* x)
  444. {
  445. if (this->x) this->x->release();
  446. this->x = x;
  447. }
  448. JFloatExpression* JNoiseFloatExpression::zX() const
  449. {
  450. return x;
  451. }
  452. void JNoiseFloatExpression::setY(JFloatExpression* y)
  453. {
  454. if (this->y) this->y->release();
  455. this->y = y;
  456. }
  457. JFloatExpression* JNoiseFloatExpression::zY() const
  458. {
  459. return y;
  460. }
  461. void JNoiseFloatExpression::setZ(JFloatExpression* z)
  462. {
  463. if (this->z) this->z->release();
  464. this->z = z;
  465. }
  466. JFloatExpression* JNoiseFloatExpression::zZ() const
  467. {
  468. return z;
  469. }
  470. JNoiseFloatExpressionFactory::JNoiseFloatExpressionFactory()
  471. : SubTypeFactory()
  472. {}
  473. JNoiseFloatExpression* JNoiseFloatExpressionFactory::fromJson(
  474. Framework::JSON::JSONObject* zJson) const
  475. {
  476. JNoiseFloatExpression* result = new JNoiseFloatExpression();
  477. result->setName(zJson->zValue("name")->asString()->getString());
  478. result->setX(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  479. zJson->zValue("x")));
  480. result->setY(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  481. zJson->zValue("y")));
  482. result->setZ(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  483. zJson->zValue("z")));
  484. return result;
  485. }
  486. Framework::JSON::JSONObject* JNoiseFloatExpressionFactory::toJsonObject(
  487. JNoiseFloatExpression* zObject) const
  488. {
  489. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  490. result->addValue(
  491. "name", new Framework::JSON::JSONString(zObject->getName()));
  492. result->addValue(
  493. "x", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zX()));
  494. result->addValue(
  495. "y", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zY()));
  496. result->addValue(
  497. "z", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zZ()));
  498. return result;
  499. }
  500. JSONObjectValidationBuilder* JNoiseFloatExpressionFactory::addToValidator(
  501. JSONObjectValidationBuilder* builder) const
  502. {
  503. return builder->withRequiredString("name")
  504. ->finishString()
  505. ->withRequiredAttribute("x",
  506. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  507. ->withRequiredAttribute("y",
  508. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  509. ->withRequiredAttribute("z",
  510. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  511. }
  512. const char* JNoiseFloatExpressionFactory::getTypeToken() const
  513. {
  514. return "noise";
  515. }
  516. JOperatorFloatExpression::JOperatorFloatExpression()
  517. : JFloatExpression(),
  518. accumulator([](float a, float b) { return 0.f; })
  519. {}
  520. float JOperatorFloatExpression::getValueOld(JExpressionMemory* zMemory)
  521. {
  522. bool first = 1;
  523. float val = 0.f;
  524. for (JFloatExpression* expression : values)
  525. {
  526. if (first)
  527. {
  528. first = 0;
  529. val = expression->getValueOld(zMemory);
  530. }
  531. else
  532. {
  533. val = accumulator(val, expression->getValueOld(zMemory));
  534. }
  535. }
  536. return val;
  537. }
  538. Framework::Assembly::AssemblyBlock& JOperatorFloatExpression::buildAssembly(
  539. JExpressionMemory* zMemory)
  540. {
  541. bool first = 1;
  542. if (!values.getEintragAnzahl())
  543. {
  544. codeBlock.addMoveValue(Framework::Assembly::MM0, 0.f);
  545. }
  546. for (JFloatExpression* expression : values)
  547. {
  548. if (first)
  549. {
  550. first = 0;
  551. codeBlock.addBlock(
  552. &expression->buildAssembly(zMemory), {}, {}, {}, 0, 0);
  553. }
  554. else
  555. {
  556. Framework::Assembly::AssemblyBlock& exprBlock
  557. = expression->buildAssembly(zMemory);
  558. if (exprBlock.isReplacementPossible(
  559. Framework::Assembly::MM0, Framework::Assembly::MM1))
  560. {
  561. exprBlock.replaceRegister(
  562. Framework::Assembly::MM0, Framework::Assembly::MM1);
  563. }
  564. else
  565. {
  566. exprBlock.addMoveValue(Framework::Assembly::MM1,
  567. Framework::Assembly::MM0,
  568. Framework::Assembly::SINGLE_FLOAT,
  569. Framework::Assembly::X);
  570. }
  571. codeBlock.addBlock(&exprBlock,
  572. {},
  573. {Framework::Assembly::MM0},
  574. {Framework::Assembly::SINGLE_FLOAT},
  575. 0,
  576. 0);
  577. if (op.istGleich("+"))
  578. {
  579. codeBlock.addAddition(Framework::Assembly::MM0,
  580. Framework::Assembly::MM1,
  581. Framework::Assembly::SINGLE_FLOAT,
  582. Framework::Assembly::X);
  583. }
  584. else if (op.istGleich("-"))
  585. {
  586. codeBlock.addSubtraction(Framework::Assembly::MM0,
  587. Framework::Assembly::MM1,
  588. Framework::Assembly::SINGLE_FLOAT,
  589. Framework::Assembly::X);
  590. }
  591. else if (op.istGleich("*"))
  592. {
  593. codeBlock.addMultiplication(Framework::Assembly::MM0,
  594. Framework::Assembly::MM1,
  595. Framework::Assembly::SINGLE_FLOAT,
  596. Framework::Assembly::X);
  597. }
  598. else if (op.istGleich("/"))
  599. {
  600. codeBlock.addDivision(Framework::Assembly::MM0,
  601. Framework::Assembly::MM1,
  602. Framework::Assembly::SINGLE_FLOAT,
  603. Framework::Assembly::X);
  604. }
  605. }
  606. }
  607. return codeBlock;
  608. }
  609. void JOperatorFloatExpression::setOperator(
  610. Framework::Text op, std::function<float(float a, float b)> accumulator)
  611. {
  612. this->op = op;
  613. this->accumulator = accumulator;
  614. }
  615. Framework::Text JOperatorFloatExpression::getOperator()
  616. {
  617. return op;
  618. }
  619. void JOperatorFloatExpression::addValue(JFloatExpression* value)
  620. {
  621. values.add(value);
  622. }
  623. const Framework::RCArray<JFloatExpression>&
  624. JOperatorFloatExpression::getValues() const
  625. {
  626. return values;
  627. }
  628. JOperatorFloatExpressionFactory::JOperatorFloatExpressionFactory()
  629. : SubTypeFactory()
  630. {}
  631. JOperatorFloatExpression* JOperatorFloatExpressionFactory::fromJson(
  632. Framework::JSON::JSONObject* zJson) const
  633. {
  634. JOperatorFloatExpression* result = new JOperatorFloatExpression();
  635. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  636. if (op.istGleich("+"))
  637. {
  638. result->setOperator("+", [](float a, float b) { return a + b; });
  639. }
  640. else if (op.istGleich("-"))
  641. {
  642. result->setOperator("-", [](float a, float b) { return a - b; });
  643. }
  644. else if (op.istGleich("*"))
  645. {
  646. result->setOperator("*", [](float a, float b) { return a * b; });
  647. }
  648. else if (op.istGleich("/"))
  649. {
  650. result->setOperator("/", [](float a, float b) { return a / b; });
  651. }
  652. for (Framework::JSON::JSONValue* value :
  653. *zJson->zValue("values")->asArray())
  654. {
  655. result->addValue(
  656. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(value));
  657. }
  658. return result;
  659. }
  660. Framework::JSON::JSONObject* JOperatorFloatExpressionFactory::toJsonObject(
  661. JOperatorFloatExpression* zObject) const
  662. {
  663. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  664. result->addValue(
  665. "operator", new Framework::JSON::JSONString(zObject->getOperator()));
  666. Framework::JSON::JSONArray* values = new Framework::JSON::JSONArray();
  667. for (JFloatExpression* expression : zObject->getValues())
  668. {
  669. values->addValue(
  670. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  671. expression));
  672. }
  673. result->addValue("values", values);
  674. return result;
  675. }
  676. JSONObjectValidationBuilder* JOperatorFloatExpressionFactory::addToValidator(
  677. JSONObjectValidationBuilder* builder) const
  678. {
  679. return builder->withRequiredString("operator")
  680. ->whichIsOneOf({"+", "-", "*", "/"})
  681. ->finishString()
  682. ->withRequiredArray("values")
  683. ->addAcceptedTypeInArray(
  684. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  685. ->finishArray();
  686. }
  687. const char* JOperatorFloatExpressionFactory::getTypeToken() const
  688. {
  689. return "operator";
  690. }
  691. JBoolOperatorBoolExpression::JBoolOperatorBoolExpression()
  692. : JBoolExpression()
  693. {}
  694. bool JBoolOperatorBoolExpression::getValueOld(JExpressionMemory* zMemory)
  695. {
  696. bool first = 1;
  697. bool val = 0;
  698. for (JBoolExpression* expression : values)
  699. {
  700. if (first)
  701. {
  702. first = 0;
  703. val = expression->getValueOld(zMemory);
  704. }
  705. else
  706. {
  707. val = accumulator(val, expression->getValueOld(zMemory));
  708. }
  709. }
  710. return val;
  711. }
  712. Framework::Assembly::AssemblyBlock& JBoolOperatorBoolExpression::buildAssembly(
  713. JExpressionMemory* zMemory)
  714. {
  715. bool first = 1;
  716. if (!values.getEintragAnzahl())
  717. {
  718. codeBlock.addMoveValue(Framework::Assembly::RAX, (char)0);
  719. }
  720. for (JBoolExpression* expression : values)
  721. {
  722. if (first)
  723. {
  724. first = 0;
  725. codeBlock.addBlock(
  726. &expression->buildAssembly(zMemory), {}, {}, {}, 0, 0);
  727. }
  728. else
  729. {
  730. Framework::Assembly::AssemblyBlock& exprBlock
  731. = expression->buildAssembly(zMemory);
  732. if (exprBlock.isReplacementPossible(
  733. Framework::Assembly::RAX, Framework::Assembly::RCX))
  734. {
  735. exprBlock.replaceRegister(
  736. Framework::Assembly::RAX, Framework::Assembly::RCX);
  737. }
  738. else
  739. {
  740. exprBlock.addMoveValue(Framework::Assembly::RCX,
  741. Framework::Assembly::RAX,
  742. Framework::Assembly::LOWER8);
  743. }
  744. codeBlock.addBlock(
  745. &exprBlock, {Framework::Assembly::RAX}, {}, {}, 0, 0);
  746. if (op.istGleich("&&"))
  747. {
  748. codeBlock.addAnd(Framework::Assembly::RAX,
  749. Framework::Assembly::RCX,
  750. Framework::Assembly::LOWER8);
  751. }
  752. else if (op.istGleich("||"))
  753. {
  754. codeBlock.addOr(Framework::Assembly::RAX,
  755. Framework::Assembly::RCX,
  756. Framework::Assembly::LOWER8);
  757. }
  758. }
  759. }
  760. return codeBlock;
  761. }
  762. void JBoolOperatorBoolExpression::setOperator(
  763. Framework::Text op, std::function<float(float a, float b)> accumulator)
  764. {
  765. this->op = op;
  766. this->accumulator = accumulator;
  767. }
  768. Framework::Text JBoolOperatorBoolExpression::getOperator()
  769. {
  770. return op;
  771. }
  772. void JBoolOperatorBoolExpression::addValue(JBoolExpression* value)
  773. {
  774. values.add(value);
  775. }
  776. const Framework::RCArray<JBoolExpression>&
  777. JBoolOperatorBoolExpression::getValues() const
  778. {
  779. return values;
  780. }
  781. JBoolOperatorBoolExpressionFactory::JBoolOperatorBoolExpressionFactory()
  782. : SubTypeFactory()
  783. {}
  784. JBoolOperatorBoolExpression* JBoolOperatorBoolExpressionFactory::fromJson(
  785. Framework::JSON::JSONObject* zJson) const
  786. {
  787. JBoolOperatorBoolExpression* result = new JBoolOperatorBoolExpression();
  788. for (Framework::JSON::JSONValue* value :
  789. *zJson->zValue("values")->asArray())
  790. {
  791. result->addValue(
  792. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(value));
  793. }
  794. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  795. if (op.istGleich("&&"))
  796. {
  797. result->setOperator("&&", [](bool a, bool b) { return a && b; });
  798. }
  799. else if (op.istGleich("||"))
  800. {
  801. result->setOperator("||", [](bool a, bool b) { return a || b; });
  802. }
  803. return result;
  804. }
  805. Framework::JSON::JSONObject* JBoolOperatorBoolExpressionFactory::toJsonObject(
  806. JBoolOperatorBoolExpression* zObject) const
  807. {
  808. Framework::JSON::JSONObject* zResult = new Framework::JSON::JSONObject();
  809. Framework::JSON::JSONArray* values = new Framework::JSON::JSONArray();
  810. for (JBoolExpression* expression : zObject->getValues())
  811. {
  812. values->addValue(
  813. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  814. expression));
  815. }
  816. zResult->addValue("values", values);
  817. zResult->addValue(
  818. "operator", new Framework::JSON::JSONString(zObject->getOperator()));
  819. return zResult;
  820. }
  821. JSONObjectValidationBuilder* JBoolOperatorBoolExpressionFactory::addToValidator(
  822. JSONObjectValidationBuilder* builder) const
  823. {
  824. return builder->withRequiredString("operator")
  825. ->whichIsOneOf({"&&", "||"})
  826. ->finishString()
  827. ->withRequiredArray("values")
  828. ->addAcceptedTypeInArray(
  829. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  830. ->finishArray();
  831. }
  832. const char* JBoolOperatorBoolExpressionFactory::getTypeToken() const
  833. {
  834. return "operator";
  835. }
  836. JFloatOperatorBoolExpression::JFloatOperatorBoolExpression()
  837. : JBoolExpression()
  838. {}
  839. bool JFloatOperatorBoolExpression::getValueOld(JExpressionMemory* zMemory)
  840. {
  841. bool first = 1;
  842. bool val = 1;
  843. float last = 0.f;
  844. for (JFloatExpression* expression : values)
  845. {
  846. float current = expression->getValueOld(zMemory);
  847. if (!first) val &= accumulator(last, current);
  848. first = 0;
  849. last = current;
  850. if (!val) break;
  851. }
  852. return val;
  853. }
  854. Framework::Assembly::AssemblyBlock& JFloatOperatorBoolExpression::buildAssembly(
  855. JExpressionMemory* zMemory)
  856. {
  857. bool first = 1;
  858. Framework::Assembly::FPRegister lastResultSorage = Framework::Assembly::MM0;
  859. for (JFloatExpression* expression : values)
  860. {
  861. if (first)
  862. {
  863. first = 0;
  864. codeBlock.addBlock(
  865. &expression->buildAssembly(zMemory), {}, {}, {}, 0, 0);
  866. }
  867. else
  868. {
  869. Framework::Assembly::FPRegister currentResultSorage
  870. = lastResultSorage == Framework::Assembly::MM0
  871. ? Framework::Assembly::MM1
  872. : Framework::Assembly::MM0;
  873. Framework::Assembly::AssemblyBlock& exprBlock
  874. = expression->buildAssembly(zMemory);
  875. if (currentResultSorage != Framework::Assembly::MM0)
  876. {
  877. if (exprBlock.isReplacementPossible(
  878. Framework::Assembly::MM0, currentResultSorage))
  879. {
  880. exprBlock.replaceRegister(
  881. Framework::Assembly::MM0, currentResultSorage);
  882. }
  883. else
  884. {
  885. exprBlock.addMoveValue(currentResultSorage,
  886. Framework::Assembly::MM0,
  887. Framework::Assembly::SINGLE_FLOAT,
  888. Framework::Assembly::X);
  889. }
  890. }
  891. codeBlock.addBlock(&exprBlock,
  892. {},
  893. {lastResultSorage},
  894. {Framework::Assembly::SINGLE_FLOAT},
  895. 0,
  896. 0);
  897. Framework::Assembly::Operation jumpOp = Framework::Assembly::NOP;
  898. bool needConversion = false;
  899. if (op.istGleich(">"))
  900. {
  901. jumpOp = Framework::Assembly::JBE; // jump if below or equal
  902. }
  903. else if (op.istGleich("<"))
  904. {
  905. jumpOp = Framework::Assembly::JNB; // jump if not below
  906. }
  907. else if (op.istGleich(">="))
  908. {
  909. jumpOp = Framework::Assembly::JB; // jump if below
  910. }
  911. else if (op.istGleich("<="))
  912. {
  913. jumpOp = Framework::Assembly::JA; // jump if above
  914. }
  915. else if (op.istGleich("=="))
  916. {
  917. jumpOp = Framework::Assembly::JNE; // jump if not equal
  918. }
  919. else if (op.istGleich("!="))
  920. {
  921. jumpOp = Framework::Assembly::JE; // jump if equal
  922. }
  923. else
  924. {
  925. needConversion = true;
  926. if (op.istGleich(">i"))
  927. {
  928. jumpOp = Framework::Assembly::JLE; // jump if less or equal
  929. }
  930. else if (op.istGleich("<i"))
  931. {
  932. jumpOp = Framework::Assembly::JGE; // jump if greater or
  933. // equal
  934. }
  935. else if (op.istGleich(">=i"))
  936. {
  937. jumpOp = Framework::Assembly::JL; // jump if less
  938. }
  939. else if (op.istGleich("<=i"))
  940. {
  941. jumpOp = Framework::Assembly::JG; // jump if greater
  942. }
  943. else if (op.istGleich("==i"))
  944. {
  945. jumpOp = Framework::Assembly::JNE; // jump if not equal
  946. }
  947. else if (op.istGleich("!=i"))
  948. {
  949. jumpOp = Framework::Assembly::JE; // jump if equal
  950. }
  951. }
  952. if (needConversion)
  953. {
  954. codeBlock.addConversion(Framework::Assembly::RAX,
  955. lastResultSorage,
  956. Framework::Assembly::SINGLE_FLOAT,
  957. Framework::Assembly::LOWER32);
  958. codeBlock.addConversion(Framework::Assembly::RCX,
  959. currentResultSorage,
  960. Framework::Assembly::SINGLE_FLOAT,
  961. Framework::Assembly::LOWER32);
  962. codeBlock.addCompare(Framework::Assembly::RAX,
  963. Framework::Assembly::RCX,
  964. Framework::Assembly::LOWER32);
  965. }
  966. else
  967. {
  968. codeBlock.addCompare(lastResultSorage,
  969. currentResultSorage,
  970. Framework::Assembly::SINGLE_FLOAT);
  971. }
  972. codeBlock.addJump(jumpOp, "end_false");
  973. lastResultSorage = currentResultSorage;
  974. }
  975. }
  976. codeBlock.addMoveValue(Framework::Assembly::RAX, (char)1);
  977. codeBlock.addJump(Framework::Assembly::JMP, "end");
  978. codeBlock.defineJumpTarget("end_false");
  979. codeBlock.addMoveValue(Framework::Assembly::RAX, (char)0);
  980. codeBlock.defineJumpTarget("end");
  981. return codeBlock;
  982. }
  983. void JFloatOperatorBoolExpression::setOperator(
  984. Framework::Text op, std::function<bool(float a, float b)> accumulator)
  985. {
  986. this->op = op;
  987. this->accumulator = accumulator;
  988. }
  989. Framework::Text JFloatOperatorBoolExpression::getOperator()
  990. {
  991. return op;
  992. }
  993. void JFloatOperatorBoolExpression::addValue(JFloatExpression* value)
  994. {
  995. values.add(value);
  996. }
  997. const Framework::RCArray<JFloatExpression>&
  998. JFloatOperatorBoolExpression::getValues() const
  999. {
  1000. return values;
  1001. }
  1002. JFloatOperatorBoolExpressionFactory::JFloatOperatorBoolExpressionFactory()
  1003. : SubTypeFactory()
  1004. {}
  1005. JFloatOperatorBoolExpression* JFloatOperatorBoolExpressionFactory::fromJson(
  1006. Framework::JSON::JSONObject* zJson) const
  1007. {
  1008. JFloatOperatorBoolExpression* result = new JFloatOperatorBoolExpression();
  1009. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  1010. if (op.istGleich(">"))
  1011. {
  1012. result->setOperator(">", [](float a, float b) { return a > b; });
  1013. }
  1014. else if (op.istGleich("<"))
  1015. {
  1016. result->setOperator("<", [](float a, float b) { return a < b; });
  1017. }
  1018. else if (op.istGleich(">="))
  1019. {
  1020. result->setOperator(">=", [](float a, float b) { return a >= b; });
  1021. }
  1022. else if (op.istGleich("<="))
  1023. {
  1024. result->setOperator("<=", [](float a, float b) { return a <= b; });
  1025. }
  1026. else if (op.istGleich("=="))
  1027. {
  1028. result->setOperator("==", [](float a, float b) { return a == b; });
  1029. }
  1030. else if (op.istGleich("!="))
  1031. {
  1032. result->setOperator("!=", [](float a, float b) { return a != b; });
  1033. }
  1034. else if (op.istGleich(">i"))
  1035. {
  1036. result->setOperator(
  1037. ">i", [](float a, float b) { return (int)a > (int)b; });
  1038. }
  1039. else if (op.istGleich("<i"))
  1040. {
  1041. result->setOperator(
  1042. "<i", [](float a, float b) { return (int)a < (int)b; });
  1043. }
  1044. else if (op.istGleich(">=i"))
  1045. {
  1046. result->setOperator(
  1047. ">=i", [](float a, float b) { return (int)a >= (int)b; });
  1048. }
  1049. else if (op.istGleich("<=i"))
  1050. {
  1051. result->setOperator(
  1052. "<=i", [](float a, float b) { return (int)a <= (int)b; });
  1053. }
  1054. else if (op.istGleich("==i"))
  1055. {
  1056. result->setOperator(
  1057. "==i", [](float a, float b) { return (int)a == (int)b; });
  1058. }
  1059. else if (op.istGleich("!=i"))
  1060. {
  1061. result->setOperator(
  1062. "!=i", [](float a, float b) { return (int)a != (int)b; });
  1063. }
  1064. for (Framework::JSON::JSONValue* value :
  1065. *zJson->zValue("values")->asArray())
  1066. {
  1067. result->addValue(
  1068. Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(value));
  1069. }
  1070. return result;
  1071. }
  1072. Framework::JSON::JSONObject* JFloatOperatorBoolExpressionFactory::toJsonObject(
  1073. JFloatOperatorBoolExpression* zObject) const
  1074. {
  1075. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  1076. result->addValue(
  1077. "operator", new Framework::JSON::JSONString(zObject->getOperator()));
  1078. Framework::JSON::JSONArray* values = new Framework::JSON::JSONArray();
  1079. for (JFloatExpression* expression : zObject->getValues())
  1080. {
  1081. values->addValue(
  1082. Game::INSTANCE->zTypeRegistry()->toJson<JFloatExpression>(
  1083. expression));
  1084. }
  1085. result->addValue("values", values);
  1086. return result;
  1087. }
  1088. JSONObjectValidationBuilder*
  1089. JFloatOperatorBoolExpressionFactory::addToValidator(
  1090. JSONObjectValidationBuilder* builder) const
  1091. {
  1092. return builder->withRequiredString("operator")
  1093. ->whichIsOneOf({">",
  1094. "<",
  1095. ">=",
  1096. "<=",
  1097. "==",
  1098. "!=",
  1099. "<i",
  1100. ">i",
  1101. ">=i",
  1102. "<=i",
  1103. "==i",
  1104. "!=i"})
  1105. ->finishString()
  1106. ->withRequiredArray("values")
  1107. ->addAcceptedTypeInArray(
  1108. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  1109. ->finishArray();
  1110. }
  1111. const char* JFloatOperatorBoolExpressionFactory::getTypeToken() const
  1112. {
  1113. return "comparsion";
  1114. }
  1115. JBlockTypeBoolExpression::JBlockTypeBoolExpression()
  1116. : JBoolExpression(),
  1117. typeId(0),
  1118. x(0),
  1119. y(0),
  1120. z(0)
  1121. {}
  1122. JBlockTypeBoolExpression ::~JBlockTypeBoolExpression()
  1123. {
  1124. if (x) x->release();
  1125. if (y) y->release();
  1126. if (z) z->release();
  1127. }
  1128. bool JBlockTypeBoolExpression::isValidPosition(
  1129. int x, int y, Chunk* currentChunk)
  1130. {
  1131. return currentChunk
  1132. && Game::getChunkCenter(x, y) == currentChunk->getCenter();
  1133. }
  1134. bool JBlockTypeBoolExpression::getValueOld(JExpressionMemory* zMemory)
  1135. {
  1136. int x = (int)(round(this->x->getValueOld(zMemory)));
  1137. int y = (int)(round(this->y->getValueOld(zMemory)));
  1138. int z = (int)(round(this->z->getValueOld(zMemory)));
  1139. if (z < 0 || z >= WORLD_HEIGHT || !zMemory->zCurrentChunk()
  1140. || Game::getChunkCenter(x, y) != zMemory->zCurrentChunk()->getCenter())
  1141. {
  1142. return 0;
  1143. }
  1144. return zMemory->zCurrentChunk()->getBlockTypeAt(
  1145. Dimension::chunkCoordinates({x, y, z}))
  1146. == typeId;
  1147. }
  1148. Framework::Assembly::AssemblyBlock& JBlockTypeBoolExpression::buildAssembly(
  1149. JExpressionMemory* zMemory)
  1150. {
  1151. Framework::Assembly::AssemblyBlock& xBlock = x->buildAssembly(zMemory);
  1152. Framework::Assembly::AssemblyBlock& yBlock = y->buildAssembly(zMemory);
  1153. Framework::Assembly::AssemblyBlock& zBlock = z->buildAssembly(zMemory);
  1154. Framework::Assembly::FPRegister xTarget = Framework::Assembly::MM0;
  1155. if (xBlock.isReplacementPossible(
  1156. Framework::Assembly::MM0, Framework::Assembly::MM1))
  1157. {
  1158. xBlock.replaceRegister(
  1159. Framework::Assembly::MM0, Framework::Assembly::MM1);
  1160. xTarget = Framework::Assembly::MM1;
  1161. }
  1162. Framework::Assembly::FPRegister yTarget = Framework::Assembly::MM0;
  1163. if (yBlock.isReplacementPossible(
  1164. Framework::Assembly::MM0, Framework::Assembly::MM2))
  1165. {
  1166. yBlock.replaceRegister(
  1167. Framework::Assembly::MM0, Framework::Assembly::MM2);
  1168. yTarget = Framework::Assembly::MM2;
  1169. }
  1170. Framework::Assembly::FPRegister zTarget = Framework::Assembly::MM0;
  1171. if (zBlock.isReplacementPossible(
  1172. Framework::Assembly::MM0, Framework::Assembly::MM3))
  1173. {
  1174. zBlock.replaceRegister(
  1175. Framework::Assembly::MM0, Framework::Assembly::MM3);
  1176. zTarget = Framework::Assembly::MM3;
  1177. }
  1178. codeBlock.addBlock(&zBlock, {}, {}, {}, 0, &zTarget);
  1179. codeBlock.addConversion(Framework::Assembly::R9,
  1180. zTarget,
  1181. Framework::Assembly::SINGLE_FLOAT,
  1182. Framework::Assembly::LOWER32,
  1183. 1);
  1184. codeBlock.addTest(Framework::Assembly::R9,
  1185. Framework::Assembly::R9,
  1186. Framework::Assembly::LOWER32);
  1187. codeBlock.addJump(Framework::Assembly::JL, "end_false");
  1188. codeBlock.addCompare(Framework::Assembly::R9, WORLD_HEIGHT);
  1189. codeBlock.addJump(Framework::Assembly::JGE, "end_false");
  1190. codeBlock.addBlock(&yBlock, {Framework::Assembly::R9}, {}, {}, 0, &yTarget);
  1191. codeBlock.addConversion(Framework::Assembly::R8,
  1192. yTarget,
  1193. Framework::Assembly::SINGLE_FLOAT,
  1194. Framework::Assembly::LOWER32,
  1195. 1);
  1196. codeBlock.addBlock(&xBlock,
  1197. {Framework::Assembly::R8, Framework::Assembly::R9},
  1198. {},
  1199. {},
  1200. 0,
  1201. &xTarget);
  1202. codeBlock.addConversion(Framework::Assembly::RDX,
  1203. xTarget,
  1204. Framework::Assembly::SINGLE_FLOAT,
  1205. Framework::Assembly::LOWER32,
  1206. 1);
  1207. codeBlock.addLoadAddress(this, Framework::Assembly::RCX);
  1208. codeBlock.addPush(Framework::Assembly::RDX, Framework::Assembly::LOWER32);
  1209. codeBlock.addPush(Framework::Assembly::R8, Framework::Assembly::LOWER32);
  1210. codeBlock.addPush(Framework::Assembly::R9, Framework::Assembly::LOWER32);
  1211. codeBlock.addLoadValue(
  1212. (__int64*)zMemory->zzCurrentChunk(), Framework::Assembly::R9);
  1213. codeBlock
  1214. .addMemberCall<bool (JBlockTypeBoolExpression::*)(int, int, Chunk*)>(
  1215. &JBlockTypeBoolExpression::isValidPosition,
  1216. Framework::Assembly::INT_VALUE,
  1217. {Framework::Assembly::R9},
  1218. {});
  1219. codeBlock.addPop(Framework::Assembly::R9, Framework::Assembly::LOWER32);
  1220. codeBlock.addPop(Framework::Assembly::R8, Framework::Assembly::LOWER32);
  1221. codeBlock.addPop(Framework::Assembly::RDX, Framework::Assembly::LOWER32);
  1222. codeBlock.addTest(Framework::Assembly::RAX,
  1223. Framework::Assembly::RAX,
  1224. Framework::Assembly::LOWER8);
  1225. codeBlock.addJump(Framework::Assembly::JZ, "end");
  1226. codeBlock.addLoadValue(
  1227. (__int64*)zMemory->zzCurrentChunk(), Framework::Assembly::RCX);
  1228. codeBlock.addMemberCall<int (Chunk::*)(int, int, int) const>(
  1229. &Chunk::getBlockTypeAtWC,
  1230. Framework::Assembly::INT_VALUE,
  1231. {Framework::Assembly::RCX,
  1232. Framework::Assembly::RDX,
  1233. Framework::Assembly::R8,
  1234. Framework::Assembly::R9},
  1235. {});
  1236. codeBlock.addCompare(Framework::Assembly::RAX,
  1237. Framework::Assembly::RCX,
  1238. Framework::Assembly::LOWER32);
  1239. codeBlock.addJump(Framework::Assembly::JNE, "end_false");
  1240. codeBlock.addMoveValue(Framework::Assembly::RAX, (char)1);
  1241. codeBlock.addJump(Framework::Assembly::JMP, "end");
  1242. codeBlock.defineJumpTarget("end_false");
  1243. codeBlock.addMoveValue(Framework::Assembly::RAX, (char)0);
  1244. codeBlock.defineJumpTarget("end");
  1245. return codeBlock;
  1246. }
  1247. void JBlockTypeBoolExpression::setTypeId(int typeId)
  1248. {
  1249. this->typeId = typeId;
  1250. }
  1251. int JBlockTypeBoolExpression::getTypeId() const
  1252. {
  1253. return typeId;
  1254. }
  1255. void JBlockTypeBoolExpression::setX(JFloatExpression* x)
  1256. {
  1257. if (this->x) this->x->release();
  1258. this->x = x;
  1259. }
  1260. JFloatExpression* JBlockTypeBoolExpression::zX() const
  1261. {
  1262. return x;
  1263. }
  1264. void JBlockTypeBoolExpression::setY(JFloatExpression* y)
  1265. {
  1266. if (this->y) this->y->release();
  1267. this->y = y;
  1268. }
  1269. JFloatExpression* JBlockTypeBoolExpression::zY() const
  1270. {
  1271. return y;
  1272. }
  1273. void JBlockTypeBoolExpression::setZ(JFloatExpression* z)
  1274. {
  1275. if (this->z) this->z->release();
  1276. this->z = z;
  1277. }
  1278. JFloatExpression* JBlockTypeBoolExpression::zZ() const
  1279. {
  1280. return z;
  1281. }
  1282. JBlockTypeBoolExpressionFactory::JBlockTypeBoolExpressionFactory()
  1283. : SubTypeFactory()
  1284. {}
  1285. JBlockTypeBoolExpression* JBlockTypeBoolExpressionFactory::fromJson(
  1286. Framework::JSON::JSONObject* zJson) const
  1287. {
  1288. JBlockTypeBoolExpression* result = new JBlockTypeBoolExpression();
  1289. result->setTypeId(Game::INSTANCE->getBlockTypeId(
  1290. zJson->zValue("blockType")->asString()->getString()));
  1291. result->setX(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  1292. zJson->zValue("x")));
  1293. result->setY(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  1294. zJson->zValue("y")));
  1295. result->setZ(Game::INSTANCE->zTypeRegistry()->fromJson<JFloatExpression>(
  1296. zJson->zValue("z")));
  1297. return result;
  1298. }
  1299. Framework::JSON::JSONObject* JBlockTypeBoolExpressionFactory::toJsonObject(
  1300. JBlockTypeBoolExpression* zObject) const
  1301. {
  1302. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  1303. result->addValue("blockType",
  1304. new Framework::JSON::JSONString(
  1305. Game::INSTANCE->zBlockType(zObject->getTypeId())->getName()));
  1306. result->addValue(
  1307. "x", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zX()));
  1308. result->addValue(
  1309. "y", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zY()));
  1310. result->addValue(
  1311. "z", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zZ()));
  1312. return result;
  1313. }
  1314. JSONObjectValidationBuilder* JBlockTypeBoolExpressionFactory::addToValidator(
  1315. JSONObjectValidationBuilder* builder) const
  1316. {
  1317. return builder
  1318. ->withRequiredAttribute("blockType",
  1319. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  1320. BlockTypeNameFactory::TYPE_ID))
  1321. ->withRequiredAttribute("x",
  1322. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  1323. ->withRequiredAttribute("y",
  1324. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>())
  1325. ->withRequiredAttribute("z",
  1326. Game::INSTANCE->zTypeRegistry()->getValidator<JFloatExpression>());
  1327. }
  1328. const char* JBlockTypeBoolExpressionFactory::getTypeToken() const
  1329. {
  1330. return "blockType";
  1331. }