ItemSlot.cpp 9.6 KB

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