ItemFilter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "ItemFilter.h"
  2. #include "Item.h"
  3. #include "ItemSlot.h"
  4. #include "ItemType.h"
  5. ItemFilter::ItemFilter()
  6. : ReferenceCounter()
  7. {}
  8. bool ItemFilter::matchItem(const Item* zItem) const
  9. {
  10. return 1;
  11. }
  12. bool ItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  13. {
  14. return zSlot->zStack() ? matchItem(zSlot->zStack()->zItem()) : 0;
  15. }
  16. bool ItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  17. {
  18. return 1;
  19. }
  20. CombinedItemFilter::CombinedItemFilter(ItemFilter* filterA,
  21. ItemFilter* filterB,
  22. std::function<bool(bool, bool)> op)
  23. : ItemFilter(),
  24. filterA(filterA),
  25. filterB(filterB),
  26. op(op)
  27. {}
  28. CombinedItemFilter::~CombinedItemFilter()
  29. {
  30. filterA->release();
  31. filterB->release();
  32. }
  33. bool CombinedItemFilter::matchItem(const Item* zItem) const
  34. {
  35. return op(filterA->matchItem(zItem), filterB->matchItem(zItem));
  36. }
  37. bool CombinedItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  38. {
  39. return op(filterA->matchSourceSlot(zSlot), filterB->matchSourceSlot(zSlot));
  40. }
  41. bool CombinedItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  42. {
  43. return op(filterA->matchTargetSlot(zSlot), filterB->matchTargetSlot(zSlot));
  44. }
  45. Framework::Text CombinedItemFilter::getLogicUIML() const
  46. {
  47. Framework::Text result = "<operator result_0_0=\"";
  48. result += (int)op(0, 0);
  49. result += "\" result_0_1=\"";
  50. result += (int)op(0, 1);
  51. result += "\" result_1_0=\"";
  52. result += (int)op(1, 0);
  53. result += "\" result_1_1=\"";
  54. result += (int)op(1, 1);
  55. result += "\">";
  56. result += filterA->getLogicUIML();
  57. result += filterB->getLogicUIML();
  58. result += "</operator>";
  59. return result;
  60. }
  61. AnyItemFilter::AnyItemFilter()
  62. : ItemFilter()
  63. {}
  64. bool AnyItemFilter::matchItem(const Item* zItem) const
  65. {
  66. return true;
  67. }
  68. Framework::Text AnyItemFilter::getLogicUIML() const
  69. {
  70. return "<anyItem/>";
  71. }
  72. TypeItemFilter::TypeItemFilter(const ItemType* zType)
  73. : ItemFilter(),
  74. zType(zType)
  75. {}
  76. bool TypeItemFilter::matchItem(const Item* zItem) const
  77. {
  78. return zItem->zItemType() == zType;
  79. }
  80. Framework::Text TypeItemFilter::getLogicUIML() const
  81. {
  82. Framework::Text result = "<attribute name=\"Type\" operator=\"=\" value=\"";
  83. result += zType->getId();
  84. result += "\"/>";
  85. return result;
  86. }
  87. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  88. : ItemFilter(),
  89. sourceSlotId(sourceSlotId),
  90. targetSlotId(targetSlotId)
  91. {}
  92. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  93. {
  94. return sourceSlotId == zSlot->getId();
  95. }
  96. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  97. {
  98. return targetSlotId == zSlot->getId();
  99. }
  100. Framework::Text SpecificSlotFilter::getLogicUIML() const
  101. {
  102. return "<anyItem/>";
  103. }
  104. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  105. : ItemFilter()
  106. {}
  107. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  108. {
  109. blackList.add(id);
  110. }
  111. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  112. {
  113. for (int black : blackList)
  114. {
  115. if (black == zSlot->getId()) return 0;
  116. }
  117. return 1;
  118. }
  119. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  120. {
  121. return 1;
  122. }
  123. Framework::Text SourceSlotBlacklistFilter::getLogicUIML() const
  124. {
  125. return "<anyItem/>";
  126. }
  127. TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
  128. : ItemFilter()
  129. {}
  130. void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
  131. {
  132. blackList.add(id);
  133. }
  134. bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  135. {
  136. return 1;
  137. }
  138. bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  139. {
  140. for (int black : blackList)
  141. {
  142. if (black == zSlot->getId()) return 0;
  143. }
  144. return 1;
  145. }
  146. Framework::Text TargetSlotBlacklistFilter::getLogicUIML() const
  147. {
  148. return "<anyItem/>";
  149. }