ItemSlot.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "ItemSlot.h"
  2. #include "ItemStack.h"
  3. ItemSlot::ItemSlot(Framework::Text name,
  4. int maxSize,
  5. int pullPriority,
  6. int pushPriority,
  7. int allowedPullSide,
  8. int allowedPushSides,
  9. bool allowHigherStackSize)
  10. : ReferenceCounter(),
  11. items(0),
  12. maxSize(maxSize),
  13. allowedPullSide(allowedPullSide),
  14. allowedPushSides(allowedPushSides),
  15. pullPriority(pullPriority),
  16. pushPriority(pushPriority),
  17. allowHigherStackSize(allowHigherStackSize),
  18. name(name),
  19. id(0)
  20. {}
  21. ItemSlot::~ItemSlot()
  22. {
  23. if (items) items->release();
  24. }
  25. void ItemSlot::setId(int id)
  26. {
  27. this->id = id;
  28. }
  29. ItemStack* ItemSlot::takeItemsOut(int count, Direction dir)
  30. {
  31. if (!items) return 0;
  32. if ((dir | allowedPullSide) == allowedPullSide)
  33. {
  34. ItemStack* result = items->split(count);
  35. if (items->getSize() == 0)
  36. {
  37. items->release();
  38. items = 0;
  39. }
  40. return result;
  41. }
  42. return 0;
  43. }
  44. void ItemSlot::addItems(ItemStack* zStack, Direction dir)
  45. {
  46. if ((dir | allowedPushSides) == allowedPushSides)
  47. {
  48. if (!items)
  49. {
  50. if (allowHigherStackSize)
  51. {
  52. items = zStack->split(maxSize);
  53. items->setMaxSize(maxSize);
  54. }
  55. else
  56. {
  57. items = zStack->split(
  58. MIN(maxSize, zStack->zItem()->getMaxStackSize()));
  59. items->setMaxSize(
  60. MIN(maxSize, items->zItem()->getMaxStackSize()));
  61. }
  62. }
  63. else
  64. items->addItemStack(zStack);
  65. }
  66. }
  67. void ItemSlot::update()
  68. {
  69. if (items && items->getSize() == 0)
  70. {
  71. items->release();
  72. items = 0;
  73. }
  74. }
  75. int ItemSlot::numberOfAddableItems(const Item* zItem, Direction dir) const
  76. {
  77. if ((dir | allowedPushSides) == allowedPushSides)
  78. {
  79. if (!items)
  80. {
  81. if (allowHigherStackSize)
  82. return maxSize;
  83. else
  84. return MIN(maxSize, zItem->getMaxStackSize());
  85. }
  86. else if (zItem && items->zItem()->canBeStackedWith(zItem))
  87. {
  88. return items->getMaxSize() - items->getSize();
  89. }
  90. }
  91. return 0;
  92. }
  93. const ItemStack* ItemSlot::zStack() const
  94. {
  95. return items;
  96. }
  97. int ItemSlot::getPullPriority() const
  98. {
  99. return pullPriority;
  100. }
  101. int ItemSlot::getPushPriority() const
  102. {
  103. return pushPriority;
  104. }
  105. bool ItemSlot::isFull() const
  106. {
  107. return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
  108. }
  109. int ItemSlot::getFreeSpace() const
  110. {
  111. return items ? items->getMaxSize() - items->getSize() : maxSize;
  112. }
  113. bool ItemSlot::isEmpty() const
  114. {
  115. return !items;
  116. }
  117. int ItemSlot::getNumberOfItems() const
  118. {
  119. return items ? items->getSize() : 0;
  120. }
  121. const Framework::Text& ItemSlot::getName() const
  122. {
  123. return name;
  124. }
  125. int ItemSlot::getId() const
  126. {
  127. return id;
  128. }
  129. ItemSlotFactory::ItemSlotFactory()
  130. : ObjectTypeFactory()
  131. {}
  132. JSONObjectValidationBuilder* ItemSlotFactory::addToValidator(
  133. JSONObjectValidationBuilder* builder) const
  134. {
  135. Framework::JSON::JSONArray* defaultSides = new Framework::JSON::JSONArray();
  136. defaultSides->addValue(new Framework::JSON::JSONString("TOP"));
  137. defaultSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
  138. defaultSides->addValue(new Framework::JSON::JSONString("NORTH"));
  139. defaultSides->addValue(new Framework::JSON::JSONString("EAST"));
  140. defaultSides->addValue(new Framework::JSON::JSONString("SOUTH"));
  141. defaultSides->addValue(new Framework::JSON::JSONString("WEST"));
  142. return builder->withRequiredString("category")
  143. ->withDefault("Inventory")
  144. ->finishString()
  145. ->withRequiredNumber("maxSize")
  146. ->withDefault(50.0)
  147. ->finishNumber()
  148. ->withRequiredNumber("pullPriority")
  149. ->whichIsGreaterThen(0.0)
  150. ->withDefault(1.0)
  151. ->finishNumber()
  152. ->withRequiredNumber("pushPriority")
  153. ->whichIsGreaterThen(0.0)
  154. ->withDefault(1.0)
  155. ->finishNumber()
  156. ->withRequiredArray("allowedPullSides")
  157. ->withDefault(
  158. dynamic_cast<Framework::JSON::JSONArray*>(defaultSides->getThis()))
  159. ->addAcceptedStringInArray()
  160. ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
  161. ->finishString()
  162. ->finishArray()
  163. ->withRequiredArray("allowedPushSides")
  164. ->withDefault(defaultSides)
  165. ->addAcceptedStringInArray()
  166. ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
  167. ->finishString()
  168. ->finishArray()
  169. ->withRequiredBool("allowHigherStackSize")
  170. ->withDefault(false)
  171. ->finishBool();
  172. }
  173. ItemSlot* ItemSlotFactory::fromJson(Framework::JSON::JSONObject* zJson) const
  174. {
  175. Framework::Text category
  176. = zJson->zValue("category")->asString()->getString();
  177. int maxSize = (int)zJson->zValue("maxSize")->asNumber()->getNumber();
  178. int pullPriority
  179. = (int)zJson->zValue("pullPriority")->asNumber()->getNumber();
  180. int pushPriority
  181. = (int)zJson->zValue("pushPriority")->asNumber()->getNumber();
  182. int allowedPullSides = 0;
  183. int allowedPushSides = 0;
  184. for (Framework::JSON::JSONValue* side :
  185. *zJson->zValue("allowedPullSides")->asArray())
  186. {
  187. Framework::Text sideText = side->asString()->getString();
  188. if (sideText.istGleich("TOP"))
  189. {
  190. allowedPullSides |= TOP;
  191. }
  192. else if (sideText.istGleich("BOTTOM"))
  193. {
  194. allowedPullSides |= BOTTOM;
  195. }
  196. else if (sideText.istGleich("NORTH"))
  197. {
  198. allowedPullSides |= NORTH;
  199. }
  200. else if (sideText.istGleich("EAST"))
  201. {
  202. allowedPullSides |= EAST;
  203. }
  204. else if (sideText.istGleich("SOUTH"))
  205. {
  206. allowedPullSides |= SOUTH;
  207. }
  208. else if (sideText.istGleich("WEST"))
  209. {
  210. allowedPullSides |= WEST;
  211. }
  212. }
  213. for (Framework::JSON::JSONValue* side :
  214. *zJson->zValue("allowedPushSides")->asArray())
  215. {
  216. Framework::Text sideText = side->asString()->getString();
  217. if (sideText.istGleich("TOP"))
  218. {
  219. allowedPushSides |= TOP;
  220. }
  221. else if (sideText.istGleich("BOTTOM"))
  222. {
  223. allowedPushSides |= BOTTOM;
  224. }
  225. else if (sideText.istGleich("NORTH"))
  226. {
  227. allowedPushSides |= NORTH;
  228. }
  229. else if (sideText.istGleich("EAST"))
  230. {
  231. allowedPushSides |= EAST;
  232. }
  233. else if (sideText.istGleich("SOUTH"))
  234. {
  235. allowedPushSides |= SOUTH;
  236. }
  237. else if (sideText.istGleich("WEST"))
  238. {
  239. allowedPushSides |= WEST;
  240. }
  241. }
  242. bool allowHigherStackSize
  243. = zJson->zValue("allowHigherStackSize")->asBool()->getBool();
  244. return new ItemSlot(category,
  245. maxSize,
  246. pullPriority,
  247. pushPriority,
  248. allowedPullSides,
  249. allowedPushSides,
  250. allowHigherStackSize);
  251. }
  252. Framework::JSON::JSONObject* ItemSlotFactory::toJsonObject(
  253. ItemSlot* zObject) const
  254. {
  255. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  256. result->addValue(
  257. "category", new Framework::JSON::JSONString(zObject->getName()));
  258. result->addValue(
  259. "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxSize));
  260. result->addValue("pullPriority",
  261. new Framework::JSON::JSONNumber((double)zObject->getPullPriority()));
  262. result->addValue("pushPriority",
  263. new Framework::JSON::JSONNumber((double)zObject->getPushPriority()));
  264. Framework::JSON::JSONArray* allowedPullSides
  265. = new Framework::JSON::JSONArray();
  266. if (zObject->allowedPullSide & TOP)
  267. allowedPullSides->addValue(new Framework::JSON::JSONString("TOP"));
  268. if (zObject->allowedPullSide & BOTTOM)
  269. allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
  270. if (zObject->allowedPullSide & NORTH)
  271. allowedPullSides->addValue(new Framework::JSON::JSONString("NORTH"));
  272. if (zObject->allowedPullSide & EAST)
  273. allowedPullSides->addValue(new Framework::JSON::JSONString("EAST"));
  274. if (zObject->allowedPullSide & SOUTH)
  275. allowedPullSides->addValue(new Framework::JSON::JSONString("SOUTH"));
  276. if (zObject->allowedPullSide & WEST)
  277. allowedPullSides->addValue(new Framework::JSON::JSONString("WEST"));
  278. result->addValue("allowedPullSides", allowedPullSides);
  279. Framework::JSON::JSONArray* allowedPushSides
  280. = new Framework::JSON::JSONArray();
  281. if (zObject->allowedPushSides & TOP)
  282. allowedPushSides->addValue(new Framework::JSON::JSONString("TOP"));
  283. if (zObject->allowedPushSides & BOTTOM)
  284. allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
  285. if (zObject->allowedPushSides & NORTH)
  286. allowedPushSides->addValue(new Framework::JSON::JSONString("NORTH"));
  287. if (zObject->allowedPushSides & EAST)
  288. allowedPushSides->addValue(new Framework::JSON::JSONString("EAST"));
  289. if (zObject->allowedPushSides & SOUTH)
  290. allowedPushSides->addValue(new Framework::JSON::JSONString("SOUTH"));
  291. if (zObject->allowedPushSides & WEST)
  292. allowedPushSides->addValue(new Framework::JSON::JSONString("WEST"));
  293. result->addValue("allowedPushSides", allowedPushSides);
  294. result->addValue("allowHigherStackSize",
  295. new Framework::JSON::JSONBool(zObject->allowHigherStackSize));
  296. return result;
  297. }