ItemFilter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.append() << (int)op(0, 0) << "\" result_0_1=\"" << (int)op(0, 1)
  49. << "\" result_1_0=\"" << (int)op(1, 0) << "\" result_1_1=\""
  50. << (int)op(1, 1) << "\">" << filterA->getLogicUIML()
  51. << filterB->getLogicUIML() << "</operator>";
  52. return result;
  53. }
  54. AnyItemFilter::AnyItemFilter()
  55. : ItemFilter()
  56. {}
  57. bool AnyItemFilter::matchItem(const Item* zItem) const
  58. {
  59. return true;
  60. }
  61. Framework::Text AnyItemFilter::getLogicUIML() const
  62. {
  63. return "<anyItem/>";
  64. }
  65. TypeItemFilter::TypeItemFilter(const ItemType* zType)
  66. : ItemFilter(),
  67. zType(zType)
  68. {}
  69. bool TypeItemFilter::matchItem(const Item* zItem) const
  70. {
  71. return zItem->zItemType() == zType;
  72. }
  73. Framework::Text TypeItemFilter::getLogicUIML() const
  74. {
  75. Framework::Text result = "<attribute name=\"Type\" operator=\"=\" value=\"";
  76. result.append() << zType->getId() << "\"/>";
  77. return result;
  78. }
  79. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  80. : ItemFilter(),
  81. sourceSlotId(sourceSlotId),
  82. targetSlotId(targetSlotId)
  83. {}
  84. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  85. {
  86. return sourceSlotId == zSlot->getId();
  87. }
  88. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  89. {
  90. return targetSlotId == zSlot->getId();
  91. }
  92. Framework::Text SpecificSlotFilter::getLogicUIML() const
  93. {
  94. return "<anyItem/>";
  95. }
  96. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  97. : ItemFilter()
  98. {}
  99. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  100. {
  101. blackList.add(id);
  102. }
  103. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  104. {
  105. for (int black : blackList)
  106. {
  107. if (black == zSlot->getId()) return 0;
  108. }
  109. return 1;
  110. }
  111. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  112. {
  113. return 1;
  114. }
  115. Framework::Text SourceSlotBlacklistFilter::getLogicUIML() const
  116. {
  117. return "<anyItem/>";
  118. }
  119. TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
  120. : ItemFilter()
  121. {}
  122. void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
  123. {
  124. blackList.add(id);
  125. }
  126. bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  127. {
  128. return 1;
  129. }
  130. bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  131. {
  132. for (int black : blackList)
  133. {
  134. if (black == zSlot->getId()) return 0;
  135. }
  136. return 1;
  137. }
  138. Framework::Text TargetSlotBlacklistFilter::getLogicUIML() const
  139. {
  140. return "<anyItem/>";
  141. }