ItemFilter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #include "ItemFilter.h"
  2. #include "Game.h"
  3. #include "Item.h"
  4. #include "ItemSlot.h"
  5. #include "ItemStack.h"
  6. #include "ItemType.h"
  7. ItemFilter::ItemFilter()
  8. : ReferenceCounter()
  9. {}
  10. bool ItemFilter::matchItem(const Item* zItem) const
  11. {
  12. return 1;
  13. }
  14. bool ItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  15. {
  16. return zSlot->zStack() ? matchItem(zSlot->zStack()->zItem()) : 0;
  17. }
  18. bool ItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  19. {
  20. return 1;
  21. }
  22. CombinedItemFilter::CombinedItemFilter()
  23. : ItemFilter(),
  24. op([](bool a, bool b) { return false; })
  25. {}
  26. CombinedItemFilter::CombinedItemFilter(
  27. Framework::RCArray<ItemFilter> filters, std::function<bool(bool, bool)> op)
  28. : ItemFilter(),
  29. filters(filters),
  30. op(op)
  31. {}
  32. bool CombinedItemFilter::matchItem(const Item* zItem) const
  33. {
  34. bool result = false;
  35. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  36. {
  37. if (i == 0)
  38. result = filters.z(i)->matchItem(zItem);
  39. else
  40. result = op(result, filters.z(i)->matchItem(zItem));
  41. }
  42. return result;
  43. }
  44. bool CombinedItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  45. {
  46. bool result = false;
  47. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  48. {
  49. if (i == 0)
  50. result = filters.z(i)->matchSourceSlot(zSlot);
  51. else
  52. result = op(result, filters.z(i)->matchSourceSlot(zSlot));
  53. }
  54. return result;
  55. }
  56. bool CombinedItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  57. {
  58. bool result = false;
  59. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  60. {
  61. if (i == 0)
  62. result = filters.z(i)->matchTargetSlot(zSlot);
  63. else
  64. result = op(result, filters.z(i)->matchTargetSlot(zSlot));
  65. }
  66. return result;
  67. }
  68. Framework::Text CombinedItemFilter::getLogicUIML() const
  69. {
  70. if (filters.getEintragAnzahl() == 0)
  71. {
  72. return "";
  73. }
  74. if (filters.getEintragAnzahl() == 1)
  75. {
  76. return filters.z(0)->getLogicUIML();
  77. }
  78. else
  79. {
  80. Framework::Text result = "<operator result_0_0=\"";
  81. result.append() << (int)op(0, 0) << "\" result_0_1=\"" << (int)op(0, 1)
  82. << "\" result_1_0=\"" << (int)op(1, 0)
  83. << "\" result_1_1=\"" << (int)op(1, 1) << "\">";
  84. int openCount = 1;
  85. for (int i = 0; i < filters.getEintragAnzahl() - 1; i++)
  86. {
  87. if (i < filters.getEintragAnzahl() - 2)
  88. {
  89. result.append()
  90. << filters.z(i)->getLogicUIML() << "<operator result_0_0=\""
  91. << (int)op(0, 0) << "\" result_0_1=\"" << (int)op(0, 1)
  92. << "\" result_1_0=\"" << (int)op(1, 0) << "\" result_1_1=\""
  93. << (int)op(1, 1) << "\">";
  94. }
  95. else
  96. {
  97. filters.z(i)->getLogicUIML();
  98. filters.z(i + 1)->getLogicUIML();
  99. }
  100. }
  101. return result;
  102. }
  103. }
  104. void CombinedItemFilter::addFilter(ItemFilter* filter)
  105. {
  106. filters.add(filter);
  107. }
  108. const Framework::RCArray<ItemFilter>& CombinedItemFilter::getFilters() const
  109. {
  110. return filters;
  111. }
  112. void CombinedItemFilter::setOp(std::function<bool(bool, bool)> op)
  113. {
  114. this->op = op;
  115. }
  116. std::function<bool(bool, bool)> CombinedItemFilter::getOp() const
  117. {
  118. return op;
  119. }
  120. CombinedItemFilterFactory::CombinedItemFilterFactory()
  121. : SubTypeFactory()
  122. {}
  123. CombinedItemFilter* CombinedItemFilterFactory::createValue(
  124. Framework::JSON::JSONObject* zJson) const
  125. {
  126. return new CombinedItemFilter();
  127. }
  128. void CombinedItemFilterFactory::fromJson(
  129. CombinedItemFilter* zResult, Framework::JSON::JSONObject* zJson) const
  130. {
  131. std::function<bool(bool, bool)> func;
  132. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  133. if (op.istGleich("or"))
  134. {
  135. func = [](bool a, bool b) { return a || b; };
  136. }
  137. else if (op.istGleich("and"))
  138. {
  139. func = [](bool a, bool b) { return a && b; };
  140. }
  141. else if (op.istGleich("xor"))
  142. {
  143. func = [](bool a, bool b) { return (!a || !b) && (a || b); };
  144. }
  145. else if (op.istGleich("nor"))
  146. {
  147. func = [](bool a, bool b) { return !(a || b); };
  148. }
  149. else if (op.istGleich("nand"))
  150. {
  151. func = [](bool a, bool b) { return !(a && b); };
  152. }
  153. else if (op.istGleich("left"))
  154. {
  155. func = [](bool a, bool b) { return a; };
  156. }
  157. else if (op.istGleich("right"))
  158. {
  159. func = [](bool a, bool b) { return b; };
  160. }
  161. else if (op.istGleich("onlyLeft"))
  162. {
  163. func = [](bool a, bool b) { return a && !b; };
  164. }
  165. else if (op.istGleich("onlyRight"))
  166. {
  167. func = [](bool a, bool b) { return !a && b; };
  168. }
  169. else if (op.istGleich("notLeft"))
  170. {
  171. func = [](bool a, bool b) { return !a; };
  172. }
  173. else if (op.istGleich("notRight"))
  174. {
  175. func = [](bool a, bool b) { return !b; };
  176. }
  177. else if (op.istGleich("eq"))
  178. {
  179. func = [](bool a, bool b) { return a == b; };
  180. }
  181. else if (op.istGleich("leftOrEq"))
  182. {
  183. func = [](bool a, bool b) { return a || (a == b); };
  184. }
  185. else if (op.istGleich("rightOrEq"))
  186. {
  187. func = [](bool a, bool b) { return b || (a == b); };
  188. }
  189. else if (op.istGleich("true"))
  190. {
  191. func = [](bool a, bool b) { return true; };
  192. }
  193. else
  194. {
  195. func = [](bool a, bool b) { return false; };
  196. }
  197. zResult->setOp(func);
  198. Framework::RCArray<ItemFilter> filters;
  199. for (Framework::JSON::JSONValue* value :
  200. *zJson->zValue("filters")->asArray())
  201. {
  202. zResult->addFilter(
  203. Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(value));
  204. }
  205. }
  206. void CombinedItemFilterFactory::toJson(
  207. CombinedItemFilter* zObject, Framework::JSON::JSONObject* zResult) const
  208. {
  209. Framework::Text op;
  210. std::function<bool(bool, bool)> func = zObject->getOp();
  211. if (func(0, 0))
  212. {
  213. if (func(0, 1))
  214. {
  215. if (func(1, 0))
  216. {
  217. if (func(1, 1))
  218. {
  219. op = "true";
  220. }
  221. else
  222. {
  223. op = "nand";
  224. }
  225. }
  226. else
  227. {
  228. if (func(1, 1))
  229. {
  230. op = "rightOrEq";
  231. }
  232. else
  233. {
  234. op = "notLeft";
  235. }
  236. }
  237. }
  238. else
  239. {
  240. if (func(1, 0))
  241. {
  242. if (func(1, 1))
  243. {
  244. op = "leftOrEq";
  245. }
  246. else
  247. {
  248. op = "notRight";
  249. }
  250. }
  251. else
  252. {
  253. if (func(1, 1))
  254. {
  255. op = "eq";
  256. }
  257. else
  258. {
  259. op = "nor";
  260. }
  261. }
  262. }
  263. }
  264. else
  265. {
  266. if (func(0, 1))
  267. {
  268. if (func(1, 0))
  269. {
  270. if (func(1, 1))
  271. {
  272. op = "or";
  273. }
  274. else
  275. {
  276. op = "xor";
  277. }
  278. }
  279. else
  280. {
  281. if (func(1, 1))
  282. {
  283. op = "right";
  284. }
  285. else
  286. {
  287. op = "onlyRight";
  288. }
  289. }
  290. }
  291. else
  292. {
  293. if (func(1, 0))
  294. {
  295. if (func(1, 1))
  296. {
  297. op = "left";
  298. }
  299. else
  300. {
  301. op = "onlyLeft";
  302. }
  303. }
  304. else
  305. {
  306. if (func(1, 1))
  307. {
  308. op = "and";
  309. }
  310. else
  311. {
  312. op = "false";
  313. }
  314. }
  315. }
  316. }
  317. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  318. zResult->addValue("operator", new Framework::JSON::JSONString(op));
  319. Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
  320. for (ItemFilter* filter : zObject->getFilters())
  321. {
  322. filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
  323. }
  324. zResult->addValue("filters", filters);
  325. }
  326. JSONObjectValidationBuilder* CombinedItemFilterFactory::addToValidator(
  327. JSONObjectValidationBuilder* builder) const
  328. {
  329. return builder->withRequiredString("operator")
  330. ->whichIsOneOf({"or",
  331. "and",
  332. "xor",
  333. "nor",
  334. "nand",
  335. "left",
  336. "right",
  337. "onlyLeft",
  338. "onlyRight",
  339. "notLeft",
  340. "notRight",
  341. "eq",
  342. "leftOrEq",
  343. "rightOrEq",
  344. "true",
  345. "false"})
  346. ->finishString()
  347. ->withRequiredArray("filters")
  348. ->addAcceptedTypeInArray(
  349. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  350. ->finishArray();
  351. }
  352. Framework::Text CombinedItemFilterFactory::getTypeToken() const
  353. {
  354. return "operator";
  355. }
  356. AnyItemFilter::AnyItemFilter()
  357. : ItemFilter()
  358. {}
  359. bool AnyItemFilter::matchItem(const Item* zItem) const
  360. {
  361. return true;
  362. }
  363. Framework::Text AnyItemFilter::getLogicUIML() const
  364. {
  365. return "<anyItem/>";
  366. }
  367. AnyItemFilterFactory::AnyItemFilterFactory()
  368. : SubTypeFactory()
  369. {}
  370. AnyItemFilter* AnyItemFilterFactory::createValue(
  371. Framework::JSON::JSONObject* zJson) const
  372. {
  373. return new AnyItemFilter();
  374. }
  375. void AnyItemFilterFactory::fromJson(
  376. AnyItemFilter* zResult, Framework::JSON::JSONObject* zJson) const
  377. {}
  378. void AnyItemFilterFactory::toJson(
  379. AnyItemFilter* zObject, Framework::JSON::JSONObject* zResult) const
  380. {}
  381. JSONObjectValidationBuilder* AnyItemFilterFactory::addToValidator(
  382. JSONObjectValidationBuilder* builder) const
  383. {
  384. return builder;
  385. }
  386. Framework::Text AnyItemFilterFactory::getTypeToken() const
  387. {
  388. return "anyItem";
  389. }
  390. TypeItemFilter::TypeItemFilter()
  391. : ItemFilter(),
  392. type(0)
  393. {}
  394. bool TypeItemFilter::matchItem(const Item* zItem) const
  395. {
  396. return zItem->zItemType() == type;
  397. }
  398. Framework::Text TypeItemFilter::getLogicUIML() const
  399. {
  400. Framework::Text result = "<attribute name=\"Type\" operator=\"=\" value=\"";
  401. result.append() << type->getId() << "\"/>";
  402. return result;
  403. }
  404. void TypeItemFilter::setType(const ItemType* type)
  405. {
  406. this->type = type;
  407. }
  408. const ItemType* TypeItemFilter::zType() const
  409. {
  410. return type;
  411. }
  412. TypeItemFilterFactory::TypeItemFilterFactory()
  413. : SubTypeFactory()
  414. {}
  415. TypeItemFilter* TypeItemFilterFactory::createValue(
  416. Framework::JSON::JSONObject* zJson) const
  417. {
  418. return new TypeItemFilter();
  419. }
  420. void TypeItemFilterFactory::fromJson(
  421. TypeItemFilter* zResult, Framework::JSON::JSONObject* zJson) const
  422. {
  423. zResult->setType(Game::INSTANCE->zItemType(Game::INSTANCE->getItemTypeId(
  424. zJson->zValue("itemType")->asString()->getString())));
  425. }
  426. void TypeItemFilterFactory::toJson(
  427. TypeItemFilter* zObject, Framework::JSON::JSONObject* zResult) const
  428. {
  429. zResult->addValue("itemType",
  430. new Framework::JSON::JSONString(zObject->zType()->getName()));
  431. }
  432. JSONObjectValidationBuilder* TypeItemFilterFactory::addToValidator(
  433. JSONObjectValidationBuilder* builder) const
  434. {
  435. Framework::RCArray<Framework::Text> types;
  436. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  437. {
  438. if (Game::INSTANCE->zItemType(i))
  439. {
  440. types.add(
  441. new Framework::Text(Game::INSTANCE->zItemType(i)->getName()));
  442. }
  443. }
  444. return builder->withRequiredString("itemType")
  445. ->whichIsOneOf(types)
  446. ->finishString();
  447. }
  448. Framework::Text TypeItemFilterFactory::getTypeToken() const
  449. {
  450. return "type";
  451. }
  452. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  453. : ItemFilter(),
  454. sourceSlotId(sourceSlotId),
  455. targetSlotId(targetSlotId)
  456. {}
  457. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  458. {
  459. return sourceSlotId == zSlot->getId();
  460. }
  461. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  462. {
  463. return targetSlotId == zSlot->getId();
  464. }
  465. Framework::Text SpecificSlotFilter::getLogicUIML() const
  466. {
  467. return "<anyItem/>";
  468. }
  469. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  470. : ItemFilter()
  471. {}
  472. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  473. {
  474. blackList.add(id);
  475. }
  476. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  477. {
  478. for (int black : blackList)
  479. {
  480. if (black == zSlot->getId()) return 0;
  481. }
  482. return 1;
  483. }
  484. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  485. {
  486. return 1;
  487. }
  488. Framework::Text SourceSlotBlacklistFilter::getLogicUIML() const
  489. {
  490. return "<anyItem/>";
  491. }
  492. TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
  493. : ItemFilter()
  494. {}
  495. void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
  496. {
  497. blackList.add(id);
  498. }
  499. bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  500. {
  501. return 1;
  502. }
  503. bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  504. {
  505. for (int black : blackList)
  506. {
  507. if (black == zSlot->getId()) return 0;
  508. }
  509. return 1;
  510. }
  511. Framework::Text TargetSlotBlacklistFilter::getLogicUIML() const
  512. {
  513. return "<anyItem/>";
  514. }
  515. GroupItemFilter::GroupItemFilter()
  516. : ItemFilter()
  517. {}
  518. bool GroupItemFilter::matchItem(const Item* zItem) const
  519. {
  520. for (Framework::Text* group : groups)
  521. {
  522. for (Framework::Text* typeGroup : zItem->zItemType()->getGroups())
  523. {
  524. if (typeGroup->istGleich(group)) return true;
  525. }
  526. }
  527. return false;
  528. }
  529. Framework::Text GroupItemFilter::getLogicUIML() const
  530. {
  531. CombinedItemFilter filter;
  532. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  533. {
  534. if (Game::INSTANCE->zItemType(i))
  535. {
  536. bool found = false;
  537. for (Framework::Text* group : groups)
  538. {
  539. for (Framework::Text* typeGroup :
  540. Game::INSTANCE->zItemType(i)->getGroups())
  541. {
  542. if (typeGroup->istGleich(group))
  543. {
  544. found = true;
  545. break;
  546. }
  547. }
  548. if (found) break;
  549. }
  550. if (found)
  551. {
  552. TypeItemFilter* f = new TypeItemFilter();
  553. f->setType(Game::INSTANCE->zItemType(i));
  554. filter.addFilter(f);
  555. }
  556. }
  557. }
  558. filter.setOp([](bool a, bool b) { return a || b; });
  559. return filter.getLogicUIML();
  560. }
  561. void GroupItemFilter::addGroup(const Framework::Text group)
  562. {
  563. groups.add(new Framework::Text(group));
  564. }
  565. const Framework::RCArray<Framework::Text>& GroupItemFilter::getGroups() const
  566. {
  567. return groups;
  568. }
  569. GroupItemFilterFactory::GroupItemFilterFactory()
  570. : SubTypeFactory()
  571. {}
  572. GroupItemFilter* GroupItemFilterFactory::createValue(
  573. Framework::JSON::JSONObject* zJson) const
  574. {
  575. return new GroupItemFilter();
  576. }
  577. void GroupItemFilterFactory::fromJson(
  578. GroupItemFilter* zResult, Framework::JSON::JSONObject* zJson) const
  579. {
  580. for (Framework::JSON::JSONValue* group :
  581. *zJson->zValue("groupNames")->asArray())
  582. {
  583. zResult->addGroup(group->asString()->getString());
  584. }
  585. }
  586. void GroupItemFilterFactory::toJson(
  587. GroupItemFilter* zObject, Framework::JSON::JSONObject* zResult) const
  588. {
  589. Framework::JSON::JSONArray* groups = new Framework::JSON::JSONArray();
  590. for (Framework::Text* group : zObject->getGroups())
  591. {
  592. groups->addValue(new Framework::JSON::JSONString(group->getText()));
  593. }
  594. zResult->addValue("groupNames", groups);
  595. }
  596. JSONObjectValidationBuilder* GroupItemFilterFactory::addToValidator(
  597. JSONObjectValidationBuilder* builder) const
  598. {
  599. return builder->withRequiredArray("groupNames")
  600. ->addAcceptedStringInArray()
  601. ->finishString()
  602. ->finishArray();
  603. }
  604. Framework::Text GroupItemFilterFactory::getTypeToken() const
  605. {
  606. return "groups";
  607. }