ItemSlot.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 ItemStack* zStack, 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, zStack->zItem()->getMaxStackSize());
  85. }
  86. else if (zStack->zItem()
  87. && items->zItem()->canBeStackedWith(zStack->zItem()))
  88. return items->getMaxSize() - items->getSize();
  89. }
  90. return 0;
  91. }
  92. const ItemStack* ItemSlot::zStack() const
  93. {
  94. return items;
  95. }
  96. int ItemSlot::getPullPriority() const
  97. {
  98. return pullPriority;
  99. }
  100. int ItemSlot::getPushPriority() const
  101. {
  102. return pushPriority;
  103. }
  104. bool ItemSlot::isFull() const
  105. {
  106. return items ? items->getSize() >= items->getMaxSize() : maxSize == 0;
  107. }
  108. int ItemSlot::getFreeSpace() const
  109. {
  110. return items ? items->getMaxSize() - items->getSize() : maxSize;
  111. }
  112. bool ItemSlot::isEmpty() const
  113. {
  114. return !items;
  115. }
  116. int ItemSlot::getNumberOfItems() const
  117. {
  118. return items ? items->getSize() : 0;
  119. }
  120. const Framework::Text& ItemSlot::getName() const
  121. {
  122. return name;
  123. }
  124. int ItemSlot::getId() const
  125. {
  126. return id;
  127. }
  128. ItemSlotFactory::ItemSlotFactory()
  129. : ObjectTypeFactory()
  130. {}
  131. JSONObjectValidationBuilder* ItemSlotFactory::addToValidator(
  132. JSONObjectValidationBuilder* builder) const
  133. {
  134. Framework::JSON::JSONArray* defaultSides = new Framework::JSON::JSONArray();
  135. defaultSides->addValue(new Framework::JSON::JSONString("TOP"));
  136. defaultSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
  137. defaultSides->addValue(new Framework::JSON::JSONString("NORTH"));
  138. defaultSides->addValue(new Framework::JSON::JSONString("EAST"));
  139. defaultSides->addValue(new Framework::JSON::JSONString("SOUTH"));
  140. defaultSides->addValue(new Framework::JSON::JSONString("WEST"));
  141. return builder->withRequiredString("category")
  142. ->withDefault("Inventory")
  143. ->finishString()
  144. ->withRequiredNumber("maxSize")
  145. ->withDefault(50.0)
  146. ->finishNumber()
  147. ->withRequiredNumber("pullPriority")
  148. ->whichIsGreaterThen(0.0)
  149. ->withDefault(1.0)
  150. ->finishNumber()
  151. ->withRequiredNumber("pushPriority")
  152. ->whichIsGreaterThen(0.0)
  153. ->withDefault(1.0)
  154. ->finishNumber()
  155. ->withRequiredArray("allowedPullSides")
  156. ->withDefault(
  157. dynamic_cast<Framework::JSON::JSONArray*>(defaultSides->getThis()))
  158. ->addAcceptedStringInArray()
  159. ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
  160. ->finishString()
  161. ->finishArray()
  162. ->withRequiredArray("allowedPushSides")
  163. ->withDefault(defaultSides)
  164. ->addAcceptedStringInArray()
  165. ->whichIsOneOf({"TOP", "BOTTOM", "NORTH", "EAST", "SOUTH", "WEST"})
  166. ->finishString()
  167. ->finishArray()
  168. ->withRequiredBool("allowHigherStackSize")
  169. ->withDefault(false)
  170. ->finishBool();
  171. }
  172. ItemSlot* ItemSlotFactory::fromJson(Framework::JSON::JSONObject* zJson) const
  173. {
  174. Framework::Text category
  175. = zJson->zValue("category")->asString()->getString();
  176. int maxSize = (int)zJson->zValue("maxSize")->asNumber()->getNumber();
  177. int pullPriority
  178. = (int)zJson->zValue("pullPriority")->asNumber()->getNumber();
  179. int pushPriority
  180. = (int)zJson->zValue("pushPriority")->asNumber()->getNumber();
  181. int allowedPullSides = 0;
  182. int allowedPushSides = 0;
  183. for (Framework::JSON::JSONValue* side :
  184. *zJson->zValue("allowedPullSides")->asArray())
  185. {
  186. Framework::Text sideText = side->asString()->getString();
  187. if (sideText.istGleich("TOP"))
  188. {
  189. allowedPullSides |= TOP;
  190. }
  191. else if (sideText.istGleich("BOTTOM"))
  192. {
  193. allowedPullSides |= BOTTOM;
  194. }
  195. else if (sideText.istGleich("NORTH"))
  196. {
  197. allowedPullSides |= NORTH;
  198. }
  199. else if (sideText.istGleich("EAST"))
  200. {
  201. allowedPullSides |= EAST;
  202. }
  203. else if (sideText.istGleich("SOUTH"))
  204. {
  205. allowedPullSides |= SOUTH;
  206. }
  207. else if (sideText.istGleich("WEST"))
  208. {
  209. allowedPullSides |= WEST;
  210. }
  211. }
  212. for (Framework::JSON::JSONValue* side :
  213. *zJson->zValue("allowedPushSides")->asArray())
  214. {
  215. Framework::Text sideText = side->asString()->getString();
  216. if (sideText.istGleich("TOP"))
  217. {
  218. allowedPushSides |= TOP;
  219. }
  220. else if (sideText.istGleich("BOTTOM"))
  221. {
  222. allowedPushSides |= BOTTOM;
  223. }
  224. else if (sideText.istGleich("NORTH"))
  225. {
  226. allowedPushSides |= NORTH;
  227. }
  228. else if (sideText.istGleich("EAST"))
  229. {
  230. allowedPushSides |= EAST;
  231. }
  232. else if (sideText.istGleich("SOUTH"))
  233. {
  234. allowedPushSides |= SOUTH;
  235. }
  236. else if (sideText.istGleich("WEST"))
  237. {
  238. allowedPushSides |= WEST;
  239. }
  240. }
  241. bool allowHigherStackSize
  242. = zJson->zValue("allowHigherStackSize")->asBool()->getBool();
  243. return new ItemSlot(category,
  244. maxSize,
  245. pullPriority,
  246. pushPriority,
  247. allowedPullSides,
  248. allowedPushSides,
  249. allowHigherStackSize);
  250. }
  251. Framework::JSON::JSONObject* ItemSlotFactory::toJsonObject(
  252. ItemSlot* zObject) const
  253. {
  254. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  255. result->addValue(
  256. "category", new Framework::JSON::JSONString(zObject->getName()));
  257. result->addValue(
  258. "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxSize));
  259. result->addValue("pullPriority",
  260. new Framework::JSON::JSONNumber((double)zObject->getPullPriority()));
  261. result->addValue("pushPriority",
  262. new Framework::JSON::JSONNumber((double)zObject->getPushPriority()));
  263. Framework::JSON::JSONArray* allowedPullSides
  264. = new Framework::JSON::JSONArray();
  265. if (zObject->allowedPullSide & TOP)
  266. allowedPullSides->addValue(new Framework::JSON::JSONString("TOP"));
  267. if (zObject->allowedPullSide & BOTTOM)
  268. allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
  269. if (zObject->allowedPullSide & NORTH)
  270. allowedPullSides->addValue(new Framework::JSON::JSONString("NORTH"));
  271. if (zObject->allowedPullSide & EAST)
  272. allowedPullSides->addValue(new Framework::JSON::JSONString("EAST"));
  273. if (zObject->allowedPullSide & SOUTH)
  274. allowedPullSides->addValue(new Framework::JSON::JSONString("SOUTH"));
  275. if (zObject->allowedPullSide & WEST)
  276. allowedPullSides->addValue(new Framework::JSON::JSONString("WEST"));
  277. result->addValue("allowedPullSides", allowedPullSides);
  278. Framework::JSON::JSONArray* allowedPushSides
  279. = new Framework::JSON::JSONArray();
  280. if (zObject->allowedPushSides & TOP)
  281. allowedPushSides->addValue(new Framework::JSON::JSONString("TOP"));
  282. if (zObject->allowedPushSides & BOTTOM)
  283. allowedPullSides->addValue(new Framework::JSON::JSONString("BOTTOM"));
  284. if (zObject->allowedPushSides & NORTH)
  285. allowedPushSides->addValue(new Framework::JSON::JSONString("NORTH"));
  286. if (zObject->allowedPushSides & EAST)
  287. allowedPushSides->addValue(new Framework::JSON::JSONString("EAST"));
  288. if (zObject->allowedPushSides & SOUTH)
  289. allowedPushSides->addValue(new Framework::JSON::JSONString("SOUTH"));
  290. if (zObject->allowedPushSides & WEST)
  291. allowedPushSides->addValue(new Framework::JSON::JSONString("WEST"));
  292. result->addValue("allowedPushSides", allowedPushSides);
  293. result->addValue("allowHigherStackSize",
  294. new Framework::JSON::JSONBool(zObject->allowHigherStackSize));
  295. return result;
  296. }