ItemFilter.cpp 16 KB

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