ItemFilter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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::fromJson(
  124. Framework::JSON::JSONObject* zJson) const
  125. {
  126. CombinedItemFilter* result = new CombinedItemFilter();
  127. std::function<bool(bool, bool)> func;
  128. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  129. if (op.istGleich("or"))
  130. {
  131. func = [](bool a, bool b) { return a || b; };
  132. }
  133. else if (op.istGleich("and"))
  134. {
  135. func = [](bool a, bool b) { return a && b; };
  136. }
  137. else if (op.istGleich("xor"))
  138. {
  139. func = [](bool a, bool b) { return (!a || !b) && (a || b); };
  140. }
  141. else if (op.istGleich("nor"))
  142. {
  143. func = [](bool a, bool b) { return !(a || b); };
  144. }
  145. else if (op.istGleich("nand"))
  146. {
  147. func = [](bool a, bool b) { return !(a && b); };
  148. }
  149. else if (op.istGleich("left"))
  150. {
  151. func = [](bool a, bool b) { return a; };
  152. }
  153. else if (op.istGleich("right"))
  154. {
  155. func = [](bool a, bool b) { return b; };
  156. }
  157. else if (op.istGleich("onlyLeft"))
  158. {
  159. func = [](bool a, bool b) { return a && !b; };
  160. }
  161. else if (op.istGleich("onlyRight"))
  162. {
  163. func = [](bool a, bool b) { return !a && b; };
  164. }
  165. else if (op.istGleich("notLeft"))
  166. {
  167. func = [](bool a, bool b) { return !a; };
  168. }
  169. else if (op.istGleich("notRight"))
  170. {
  171. func = [](bool a, bool b) { return !b; };
  172. }
  173. else if (op.istGleich("eq"))
  174. {
  175. func = [](bool a, bool b) { return a == b; };
  176. }
  177. else if (op.istGleich("leftOrEq"))
  178. {
  179. func = [](bool a, bool b) { return a || (a == b); };
  180. }
  181. else if (op.istGleich("rightOrEq"))
  182. {
  183. func = [](bool a, bool b) { return b || (a == b); };
  184. }
  185. else if (op.istGleich("true"))
  186. {
  187. func = [](bool a, bool b) { return true; };
  188. }
  189. else
  190. {
  191. func = [](bool a, bool b) { return false; };
  192. }
  193. result->setOp(func);
  194. Framework::RCArray<ItemFilter> filters;
  195. for (Framework::JSON::JSONValue* value :
  196. *zJson->zValue("filters")->asArray())
  197. {
  198. result->addFilter(
  199. Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(value));
  200. }
  201. return result;
  202. }
  203. Framework::JSON::JSONObject* CombinedItemFilterFactory::toJsonObject(
  204. CombinedItemFilter* zObject) const
  205. {
  206. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  207. Framework::Text op;
  208. std::function<bool(bool, bool)> func = zObject->getOp();
  209. if (func(0, 0))
  210. {
  211. if (func(0, 1))
  212. {
  213. if (func(1, 0))
  214. {
  215. if (func(1, 1))
  216. {
  217. op = "true";
  218. }
  219. else
  220. {
  221. op = "nand";
  222. }
  223. }
  224. else
  225. {
  226. if (func(1, 1))
  227. {
  228. op = "rightOrEq";
  229. }
  230. else
  231. {
  232. op = "notLeft";
  233. }
  234. }
  235. }
  236. else
  237. {
  238. if (func(1, 0))
  239. {
  240. if (func(1, 1))
  241. {
  242. op = "leftOrEq";
  243. }
  244. else
  245. {
  246. op = "notRight";
  247. }
  248. }
  249. else
  250. {
  251. if (func(1, 1))
  252. {
  253. op = "eq";
  254. }
  255. else
  256. {
  257. op = "nor";
  258. }
  259. }
  260. }
  261. }
  262. else
  263. {
  264. if (func(0, 1))
  265. {
  266. if (func(1, 0))
  267. {
  268. if (func(1, 1))
  269. {
  270. op = "or";
  271. }
  272. else
  273. {
  274. op = "xor";
  275. }
  276. }
  277. else
  278. {
  279. if (func(1, 1))
  280. {
  281. op = "right";
  282. }
  283. else
  284. {
  285. op = "onlyRight";
  286. }
  287. }
  288. }
  289. else
  290. {
  291. if (func(1, 0))
  292. {
  293. if (func(1, 1))
  294. {
  295. op = "left";
  296. }
  297. else
  298. {
  299. op = "onlyLeft";
  300. }
  301. }
  302. else
  303. {
  304. if (func(1, 1))
  305. {
  306. op = "and";
  307. }
  308. else
  309. {
  310. op = "false";
  311. }
  312. }
  313. }
  314. }
  315. result->addValue("operator", new Framework::JSON::JSONString(op));
  316. Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
  317. for (ItemFilter* filter : zObject->getFilters())
  318. {
  319. filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
  320. }
  321. result->addValue("filters", filters);
  322. return result;
  323. }
  324. JSONObjectValidationBuilder* CombinedItemFilterFactory::addToValidator(
  325. JSONObjectValidationBuilder* builder) const
  326. {
  327. return builder->withRequiredString("operator")
  328. ->whichIsOneOf({"or",
  329. "and",
  330. "xor",
  331. "nor",
  332. "nand",
  333. "left",
  334. "right",
  335. "onlyLeft",
  336. "onlyRight",
  337. "notLeft",
  338. "notRight",
  339. "eq",
  340. "leftOrEq",
  341. "rightOrEq",
  342. "true",
  343. "false"})
  344. ->finishString()
  345. ->withRequiredArray("filters")
  346. ->addAcceptedTypeInArray(
  347. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  348. ->finishArray();
  349. }
  350. const char* CombinedItemFilterFactory::getTypeToken() const
  351. {
  352. return "operator";
  353. }
  354. AnyItemFilter::AnyItemFilter()
  355. : ItemFilter()
  356. {}
  357. bool AnyItemFilter::matchItem(const Item* zItem) const
  358. {
  359. return true;
  360. }
  361. Framework::Text AnyItemFilter::getLogicUIML() const
  362. {
  363. return "<anyItem/>";
  364. }
  365. AnyItemFilterFactory::AnyItemFilterFactory()
  366. : SubTypeFactory()
  367. {}
  368. AnyItemFilter* AnyItemFilterFactory::fromJson(
  369. Framework::JSON::JSONObject* zJson) const
  370. {
  371. return new AnyItemFilter();
  372. }
  373. Framework::JSON::JSONObject* AnyItemFilterFactory::toJsonObject(
  374. AnyItemFilter* zObject) const
  375. {
  376. return new Framework::JSON::JSONObject();
  377. }
  378. JSONObjectValidationBuilder* AnyItemFilterFactory::addToValidator(
  379. JSONObjectValidationBuilder* builder) const
  380. {
  381. return builder;
  382. }
  383. const char* AnyItemFilterFactory::getTypeToken() const
  384. {
  385. return "anyItem";
  386. }
  387. TypeItemFilter::TypeItemFilter()
  388. : ItemFilter(),
  389. type(0)
  390. {}
  391. bool TypeItemFilter::matchItem(const Item* zItem) const
  392. {
  393. return zItem->zItemType() == type;
  394. }
  395. Framework::Text TypeItemFilter::getLogicUIML() const
  396. {
  397. Framework::Text result = "<attribute name=\"Type\" operator=\"=\" value=\"";
  398. result.append() << type->getId() << "\"/>";
  399. return result;
  400. }
  401. void TypeItemFilter::setType(const ItemType* type)
  402. {
  403. this->type = type;
  404. }
  405. const ItemType* TypeItemFilter::zType() const
  406. {
  407. return type;
  408. }
  409. TypeItemFilterFactory::TypeItemFilterFactory()
  410. : SubTypeFactory()
  411. {}
  412. TypeItemFilter* TypeItemFilterFactory::fromJson(
  413. Framework::JSON::JSONObject* zJson) const
  414. {
  415. TypeItemFilter* result = new TypeItemFilter();
  416. result->setType(Game::INSTANCE->zItemType(Game::INSTANCE->getItemTypeId(
  417. zJson->zValue("itemType")->asString()->getString())));
  418. return result;
  419. }
  420. Framework::JSON::JSONObject* TypeItemFilterFactory::toJsonObject(
  421. TypeItemFilter* zObject) const
  422. {
  423. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  424. result->addValue("itemType",
  425. new Framework::JSON::JSONString(zObject->zType()->getName()));
  426. return result;
  427. }
  428. JSONObjectValidationBuilder* TypeItemFilterFactory::addToValidator(
  429. JSONObjectValidationBuilder* builder) const
  430. {
  431. return builder->withRequiredAttribute("itemType",
  432. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  433. ItemTypeNameFactory::TYPE_ID));
  434. }
  435. const char* TypeItemFilterFactory::getTypeToken() const
  436. {
  437. return "type";
  438. }
  439. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  440. : ItemFilter(),
  441. sourceSlotId(sourceSlotId),
  442. targetSlotId(targetSlotId)
  443. {}
  444. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  445. {
  446. return sourceSlotId == zSlot->getId();
  447. }
  448. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  449. {
  450. return targetSlotId == zSlot->getId();
  451. }
  452. Framework::Text SpecificSlotFilter::getLogicUIML() const
  453. {
  454. return "<anyItem/>";
  455. }
  456. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  457. : ItemFilter()
  458. {}
  459. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  460. {
  461. blackList.add(id);
  462. }
  463. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  464. {
  465. for (int black : blackList)
  466. {
  467. if (black == zSlot->getId()) return 0;
  468. }
  469. return 1;
  470. }
  471. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  472. {
  473. return 1;
  474. }
  475. Framework::Text SourceSlotBlacklistFilter::getLogicUIML() const
  476. {
  477. return "<anyItem/>";
  478. }
  479. TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
  480. : ItemFilter()
  481. {}
  482. void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
  483. {
  484. blackList.add(id);
  485. }
  486. bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  487. {
  488. return 1;
  489. }
  490. bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  491. {
  492. for (int black : blackList)
  493. {
  494. if (black == zSlot->getId()) return 0;
  495. }
  496. return 1;
  497. }
  498. Framework::Text TargetSlotBlacklistFilter::getLogicUIML() const
  499. {
  500. return "<anyItem/>";
  501. }
  502. GroupItemFilter::GroupItemFilter()
  503. : ItemFilter()
  504. {}
  505. bool GroupItemFilter::matchItem(const Item* zItem) const
  506. {
  507. for (Framework::Text* group : groups)
  508. {
  509. for (Framework::Text* typeGroup : zItem->zItemType()->getGroups())
  510. {
  511. if (typeGroup->istGleich(group)) return true;
  512. }
  513. }
  514. return false;
  515. }
  516. Framework::Text GroupItemFilter::getLogicUIML() const
  517. {
  518. CombinedItemFilter filter;
  519. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  520. {
  521. if (Game::INSTANCE->zItemType(i))
  522. {
  523. bool found = false;
  524. for (Framework::Text* group : groups)
  525. {
  526. for (Framework::Text* typeGroup :
  527. Game::INSTANCE->zItemType(i)->getGroups())
  528. {
  529. if (typeGroup->istGleich(group))
  530. {
  531. found = true;
  532. break;
  533. }
  534. }
  535. if (found) break;
  536. }
  537. if (found)
  538. {
  539. TypeItemFilter* f = new TypeItemFilter();
  540. f->setType(Game::INSTANCE->zItemType(i));
  541. filter.addFilter(f);
  542. }
  543. }
  544. }
  545. filter.setOp([](bool a, bool b) { return a || b; });
  546. return filter.getLogicUIML();
  547. }
  548. void GroupItemFilter::addGroup(const Framework::Text group)
  549. {
  550. groups.add(new Framework::Text(group));
  551. }
  552. const Framework::RCArray<Framework::Text>& GroupItemFilter::getGroups() const
  553. {
  554. return groups;
  555. }
  556. GroupItemFilterFactory::GroupItemFilterFactory()
  557. : SubTypeFactory()
  558. {}
  559. GroupItemFilter* GroupItemFilterFactory::fromJson(
  560. Framework::JSON::JSONObject* zJson) const
  561. {
  562. GroupItemFilter* result = new GroupItemFilter();
  563. for (Framework::JSON::JSONValue* group :
  564. *zJson->zValue("groupNames")->asArray())
  565. {
  566. result->addGroup(group->asString()->getString());
  567. }
  568. return result;
  569. }
  570. Framework::JSON::JSONObject* GroupItemFilterFactory::toJsonObject(
  571. GroupItemFilter* zObject) const
  572. {
  573. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  574. Framework::JSON::JSONArray* groups = new Framework::JSON::JSONArray();
  575. for (Framework::Text* group : zObject->getGroups())
  576. {
  577. groups->addValue(new Framework::JSON::JSONString(group->getText()));
  578. }
  579. result->addValue("groupNames", groups);
  580. return result;
  581. }
  582. JSONObjectValidationBuilder* GroupItemFilterFactory::addToValidator(
  583. JSONObjectValidationBuilder* builder) const
  584. {
  585. return builder->withRequiredArray("groupNames")
  586. ->addAcceptedStringInArray()
  587. ->finishString()
  588. ->finishArray();
  589. }
  590. const char* GroupItemFilterFactory::getTypeToken() const
  591. {
  592. return "groups";
  593. }