CraftingGrid.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include <XML.h>
  2. #include "CraftingGrid.h"
  3. #include "Globals.h"
  4. #include "DragController.h"
  5. #include "Game.h"
  6. using namespace Framework;
  7. CraftingGridElement::CraftingGridElement()
  8. : UIMLElement()
  9. {}
  10. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
  11. bool CraftingGridElement::isApplicableFor(Framework::XML::Element& element)
  12. {
  13. return element.getName().istGleich("craftingGrid");
  14. }
  15. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  16. Framework::Zeichnung* CraftingGridElement::parseElement(Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  17. {
  18. Text targetValue = element.getAttributeValue("target");
  19. Vec3<int> blockPos(0, 0, 0);
  20. Framework::Either<int, VecN<int, 4>> target((int)targetValue);
  21. if (targetValue.hat(','))
  22. {
  23. Text* first = targetValue.getTeilText(0, targetValue.positionVon(",", 0) + 1);
  24. Text* second = targetValue.getTeilText(targetValue.positionVon(",", 0) + 1, targetValue.positionVon(",", 1));
  25. Text* third = targetValue.getTeilText(targetValue.positionVon(",", 1) + 1, targetValue.positionVon(",", 2));
  26. Text* forth = targetValue.getTeilText(targetValue.positionVon(",", 2) + 1);
  27. target = Framework::Either<int, VecN<int, 4>>(VecN<int, 4>({ (int)*first, (int)*second, (int)*third, (int)*forth }));
  28. first->release();
  29. second->release();
  30. third->release();
  31. forth->release();
  32. }
  33. return new CraftingGridView(element.getAttributeValue("id"), (int)element.getAttributeValue("rowSize"), (int)element.getAttributeValue("colSize"), (int)element.getAttributeValue("numOutputSlots"), target);
  34. }
  35. //! wendet die layout parameter zu einer Zeichnung an
  36. void CraftingGridElement::layout(Framework::XML::Element& element, Framework::Zeichnung& z, int pWidth, int pHeight, Framework::UIMLContainer& generalLayouter)
  37. {
  38. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  39. }
  40. CraftingGridView::CraftingGridView(Text id, int rowSize, int colSize, int numOutputSlots, Either<int, VecN<int, 4>> target)
  41. : ZeichnungHintergrund(),
  42. rowSize(rowSize),
  43. colSize(colSize),
  44. numOutputSlots(numOutputSlots),
  45. target(target),
  46. slots(0),
  47. outputs(0),
  48. id(id),
  49. dragStartId(-1),
  50. dragStopId(-1)
  51. {
  52. craft = uiFactory.createKnopf(uiFactory.initParam);
  53. craft->setPosition(rowSize * 60, 10);
  54. craft->setSize(40, 20);
  55. craft->setText("Craft");
  56. craft->setMausEreignis([this](void* p, void* o, MausEreignis me)
  57. {
  58. if (me.id == ME_RLinks)
  59. {
  60. char* msg = new char[2 + (this->target.isA() ? 4 : 16)];
  61. msg[0] = 7; // request crafting
  62. msg[1] = (char)this->target.isA();
  63. if (msg[1])
  64. *(int*)(msg + 2) = this->target.getA();
  65. else
  66. {
  67. *(int*)(msg + 2) = this->target.getB()[0];
  68. *(int*)(msg + 6) = this->target.getB()[1];
  69. *(int*)(msg + 10) = this->target.getB()[2];
  70. *(int*)(msg + 14) = this->target.getB()[3];
  71. }
  72. network->zFactoryClient()->sendPlayerAction(msg, 2 + (this->target.isA() ? 4 : 16));
  73. delete[] msg;
  74. }
  75. return 1;
  76. });
  77. setStyle(ZeichnungHintergrund::Style::Sichtbar | ZeichnungHintergrund::Style::Erlaubt);
  78. char* msg = new char[id.getLength() + 12 + 3];
  79. msg[0] = 0; // request inventory content
  80. msg[1] = (char)id.getLength();
  81. memcpy(msg + 2, id.getText(), id.getLength());
  82. msg[2 + id.getLength()] = (char)12;
  83. memcpy(msg + 3 + id.getLength(), "CraftingGrid", 12);
  84. network->zFactoryClient()->inventoryAPIRequest(target, msg, id.getLength() + 12 + 3);
  85. delete[] msg;
  86. }
  87. CraftingGridView::~CraftingGridView()
  88. {
  89. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  90. if (controller->getCurrentDragContainer() == this)
  91. controller->stopDrag();
  92. if (slots)
  93. slots->release();
  94. if (outputs)
  95. outputs->release();
  96. char* msg = new char[id.getLength() + 2];
  97. msg[0] = 1;
  98. msg[1] = (char)id.getLength();
  99. memcpy(msg + 2, id.getText(), id.getLength());
  100. network->zFactoryClient()->inventoryAPIRequest(target, msg, id.getLength() + 2);
  101. delete[] msg;
  102. craft->release();
  103. }
  104. void CraftingGridView::api(char* message)
  105. {
  106. switch (message[0])
  107. {
  108. case 0:
  109. // send inventory content
  110. {
  111. Array<SlotInfo>* slots = new Array<SlotInfo>();
  112. int count = *(int*)(++message);
  113. for (int i = 0; i < count; i++)
  114. {
  115. SlotInfo info;
  116. info.id = *(int*)(message += 4);
  117. info.itemCount = *(int*)(message += 4);
  118. if (info.itemCount > 0)
  119. {
  120. info.damage = *(float*)(message += 4);
  121. info.maxDamage = *(float*)(message += 4);
  122. info.durability = *(float*)(message += 4);
  123. info.maxDurability = *(float*)(message += 4);
  124. info.zItem = itemIcons->z(*(int*)(message += 4));
  125. }
  126. slots->add(info);
  127. }
  128. postAction([this, slots]()
  129. {
  130. if (this->slots)
  131. this->slots->release();
  132. this->slots = slots;
  133. });
  134. break;
  135. }
  136. case 1: // set count of items
  137. {
  138. int id = *(int*)(message + 1);
  139. int count = *(int*)(message + 5);
  140. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  141. {
  142. if (slots->get(i).id == id)
  143. {
  144. SlotInfo info = slots->get(i);
  145. info.itemCount = count;
  146. if (info.itemCount == 0)
  147. {
  148. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  149. if (controller && controller->getCurrentDragContainer() == this && controller->getCurrentDaragElement() == info.id)
  150. {
  151. controller->stopDrag();
  152. }
  153. }
  154. slots->set(info, i);
  155. break;
  156. }
  157. }
  158. break;
  159. }
  160. case 2: // add new stack
  161. {
  162. int id = *(int*)(message + 1);
  163. for (int i = 0; i < slots->getEintragAnzahl(); i++)
  164. {
  165. if (slots->get(i).id == id)
  166. {
  167. SlotInfo info = slots->get(i);
  168. info.itemCount = *(int*)(message + 5);
  169. info.damage = *(float*)(message + 9);
  170. info.maxDamage = *(float*)(message + 13);
  171. info.durability = *(float*)(message + 17);
  172. info.maxDurability = *(float*)(message + 21);
  173. info.zItem = itemIcons->z(*(int*)(message + 25));
  174. slots->set(info, i);
  175. break;
  176. }
  177. }
  178. break;
  179. }
  180. case 100: // set crafting result
  181. {
  182. Array<SlotInfo>* outputs = new Array<SlotInfo>();
  183. int count = *(int*)(++message);
  184. for (int i = 0; i < count; i++)
  185. {
  186. SlotInfo info;
  187. info.id = i;
  188. info.itemCount = *(int*)(message += 4);
  189. if (info.itemCount > 0)
  190. {
  191. info.damage = *(float*)(message += 4);
  192. info.maxDamage = *(float*)(message += 4);
  193. info.durability = *(float*)(message += 4);
  194. info.maxDurability = *(float*)(message += 4);
  195. info.zItem = itemIcons->z(*(int*)(message += 4));
  196. }
  197. outputs->add(info);
  198. }
  199. postAction([this, outputs]()
  200. {
  201. if (this->outputs)
  202. this->outputs->release();
  203. this->outputs = outputs;
  204. });
  205. break;
  206. }
  207. }
  208. }
  209. bool CraftingGridView::tick(double tickVal)
  210. {
  211. return ZeichnungHintergrund::tick(tickVal);
  212. }
  213. void CraftingGridView::render(Bild& rObj)
  214. {
  215. ZeichnungHintergrund::render(rObj);
  216. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  217. return;
  218. int numRows = 1;
  219. if (slots)
  220. {
  221. int x = 0;
  222. int y = 0;
  223. int rowCount = 0;
  224. int index = 0;
  225. for (SlotInfo info : *slots)
  226. {
  227. info.render(x, y, rObj, dragStartId == info.id, dragStopId == info.id);
  228. x += 60;
  229. if (++rowCount >= rowSize)
  230. {
  231. y += 60;
  232. x = 0;
  233. rowCount = 0;
  234. if (index < slots->getEintragAnzahl() - 1)
  235. numRows++;
  236. }
  237. index++;
  238. }
  239. }
  240. craft->render(rObj);
  241. rObj.fillRegion(rowSize * 60, gr.y / 2 - 5, 25, 10, 0xFF52525E);
  242. rObj.drawDreieck(Punkt(rowSize * 60 + 25, gr.y / 2 - 15), Punkt(rowSize * 60 + 40, gr.y / 2), Punkt(rowSize * 60 + 25, gr.y / 2 + 15), 0xFF52525E);
  243. if (outputs)
  244. {
  245. int x = rowSize * 60 + 50;
  246. int y = 0;
  247. int colCount = 0;
  248. for (SlotInfo info : *outputs)
  249. {
  250. info.render(x, y, rObj, dragStartId == info.id, dragStopId == info.id);
  251. y += 60;
  252. if (++colCount >= numRows)
  253. {
  254. x += 60;
  255. y = 0;
  256. colCount = 0;
  257. }
  258. }
  259. }
  260. rObj.releaseDrawOptions();
  261. }
  262. void CraftingGridView::doMausEreignis(MausEreignis& me, bool userRet)
  263. {
  264. craft->doPublicMausEreignis(me);
  265. DragController<InventoryDragSource, int>* controller = ((Game*)(Menu*)menuRegister->get("game"))->zInventoryDragController();
  266. int x = 0;
  267. int y = 0;
  268. int rowCount = 0;
  269. int slot = 0;
  270. dragStopId = -1;
  271. for (SlotInfo info : *slots)
  272. {
  273. if (me.mx >= x && me.mx < x + 50 && me.my >= y && me.my < y + 50)
  274. {
  275. if (me.id == ME_RLinks)
  276. {
  277. if (!controller->getCurrentDragContainer() && info.itemCount > 0)
  278. {
  279. controller->beginDrag(this, info.id, info.zItem, [this]()
  280. {
  281. dragStartId = -1;
  282. });
  283. dragStartId = info.id;
  284. }
  285. else if (controller->getCurrentDragContainer())
  286. {
  287. // request to transfer items from source to target slot
  288. Framework::Either<int, Framework::VecN<int, 4>> source = controller->getCurrentDragContainer()->getInventoryTarget();
  289. int len = 2 + (source.isA() ? 4 : 16) + 5 + (target.isA() ? 4 : 16) + 4;
  290. char* msg = new char[len];
  291. int index = 0;
  292. msg[index++] = 6;
  293. msg[index++] = (char)source.isA();
  294. if (source.isA())
  295. {
  296. *(int*)(msg + index) = source.getA();
  297. index += 4;
  298. }
  299. else
  300. {
  301. *(int*)(msg + index) = source.getB()[0];
  302. *(int*)(msg + index + 4) = source.getB()[1];
  303. *(int*)(msg + index + 8) = source.getB()[2];
  304. *(int*)(msg + index + 12) = source.getB()[3];
  305. index += 16;
  306. }
  307. *(int*)(msg + index) = controller->getCurrentDaragElement();
  308. index += 4;
  309. msg[index++] = target.isA();
  310. if (target.isA())
  311. {
  312. *(int*)(msg + index) = target.getA();
  313. index += 4;
  314. }
  315. else
  316. {
  317. *(int*)(msg + index) = target.getB()[0];
  318. *(int*)(msg + index + 4) = target.getB()[1];
  319. *(int*)(msg + index + 8) = target.getB()[2];
  320. *(int*)(msg + index + 12) = target.getB()[3];
  321. index += 16;
  322. }
  323. *(int*)(msg + index) = info.id;
  324. network->zFactoryClient()->sendPlayerAction(msg, len);
  325. delete[] msg;
  326. }
  327. }
  328. else
  329. {
  330. if (controller->getCurrentDragContainer() && (controller->getCurrentDragContainer() != this || controller->getCurrentDaragElement() != info.id))
  331. {
  332. dragStopId = info.id;
  333. }
  334. }
  335. break;
  336. }
  337. x += 60;
  338. if (++rowCount >= rowSize)
  339. {
  340. y += 60;
  341. x = 0;
  342. rowCount = 0;
  343. }
  344. slot++;
  345. }
  346. ZeichnungHintergrund::doMausEreignis(me, userRet);
  347. }
  348. Framework::Either<int, Framework::VecN<int, 4>> CraftingGridView::getInventoryTarget() const
  349. {
  350. return target;
  351. }