ItemFilter.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "ItemFilter.h"
  2. #include "ItemType.h"
  3. #include "Item.h"
  4. #include "ItemSlot.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, ItemFilter* filterB, std::function<bool(bool, bool)> op)
  21. : ItemFilter(),
  22. filterA(filterA),
  23. filterB(filterB),
  24. op(op)
  25. {}
  26. CombinedItemFilter::~CombinedItemFilter()
  27. {
  28. filterA->release();
  29. filterB->release();
  30. }
  31. bool CombinedItemFilter::matchItem(const Item* zItem) const
  32. {
  33. return op(filterA->matchItem(zItem), filterB->matchItem(zItem));
  34. }
  35. bool CombinedItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  36. {
  37. return op(filterA->matchSourceSlot(zSlot), filterB->matchSourceSlot(zSlot));
  38. }
  39. bool CombinedItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  40. {
  41. return op(filterA->matchTargetSlot(zSlot), filterB->matchTargetSlot(zSlot));
  42. }
  43. AnyItemFilter::AnyItemFilter()
  44. : ItemFilter()
  45. {}
  46. bool AnyItemFilter::matchItem(const Item* zItem) const
  47. {
  48. return true;
  49. }
  50. TypeItemFilter::TypeItemFilter(const ItemType* zType)
  51. : ItemFilter(),
  52. zType(zType)
  53. {}
  54. bool TypeItemFilter::matchItem(const Item* zItem) const
  55. {
  56. return zItem->zItemType() == zType;
  57. }
  58. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  59. : ItemFilter(),
  60. sourceSlotId(sourceSlotId),
  61. targetSlotId(targetSlotId)
  62. {}
  63. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  64. {
  65. return sourceSlotId == zSlot->getId();
  66. }
  67. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  68. {
  69. return targetSlotId == zSlot->getId();
  70. }
  71. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  72. : ItemFilter()
  73. {}
  74. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  75. {
  76. blackList.add(id);
  77. }
  78. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  79. {
  80. for (int black : blackList)
  81. {
  82. if (black == zSlot->getId())
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  88. {
  89. return 1;
  90. }