ItemFilter.cpp 3.7 KB

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