ItemFilter.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. AnyItemFilter::AnyItemFilter()
  46. : ItemFilter()
  47. {}
  48. bool AnyItemFilter::matchItem(const Item* zItem) const
  49. {
  50. return true;
  51. }
  52. TypeItemFilter::TypeItemFilter(const ItemType* zType)
  53. : ItemFilter(),
  54. zType(zType)
  55. {}
  56. bool TypeItemFilter::matchItem(const Item* zItem) const
  57. {
  58. return zItem->zItemType() == zType;
  59. }
  60. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  61. : ItemFilter(),
  62. sourceSlotId(sourceSlotId),
  63. targetSlotId(targetSlotId)
  64. {}
  65. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  66. {
  67. return sourceSlotId == zSlot->getId();
  68. }
  69. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  70. {
  71. return targetSlotId == zSlot->getId();
  72. }
  73. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  74. : ItemFilter()
  75. {}
  76. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  77. {
  78. blackList.add(id);
  79. }
  80. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  81. {
  82. for (int black : blackList)
  83. {
  84. if (black == zSlot->getId()) return 0;
  85. }
  86. return 1;
  87. }
  88. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  89. {
  90. return 1;
  91. }