Player.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "Player.h"
  2. #include "Game.h"
  3. #include "PlayerHand.h"
  4. #include "ItemFilter.h"
  5. Player::Player(Framework::Vec3<float> location, int dimensionId, int entityId)
  6. : Entity(PlayerEntityType::INSTANCE, location, dimensionId, entityId),
  7. BasicShapedCrafter(3, 3, this, "inventory")
  8. {
  9. for (int i = 0; i < 10; i++)
  10. {
  11. ItemSlot* slot = new ItemSlot("ItemBar", 50, 0, i, 0, ANY_DIRECTION, 0);
  12. itemBar.add(slot);
  13. addSlot(slot);
  14. }
  15. for (int i = 0; i < 30; i++)
  16. {
  17. ItemSlot* slot = new ItemSlot("Inventory", 50, 0, i + 10, 0, ANY_DIRECTION, 0);
  18. addSlot(slot);
  19. }
  20. leftHandPosition = 0;
  21. maxHP = 10;
  22. currentHP = 10;
  23. stamina = 10;
  24. maxStamina = 10;
  25. hunger = 10;
  26. maxHunger = 10;
  27. thirst = 10;
  28. maxThirst = 10;
  29. keyState = 0;
  30. jumping = 0;
  31. faceOffset = { 0.f, 0.f, 1.5f };
  32. targetDistanceLimit = 4;
  33. maxMovementSpeed = 4;
  34. }
  35. void Player::onTargetChange()
  36. {
  37. NetworkMessage msg;
  38. ActionTarget::toMessage(zTarget(), msg);
  39. Game::INSTANCE->sendMessage(&msg, this);
  40. }
  41. Framework::Text Player::getInventoryUIML()
  42. {
  43. Framework::Text result = "<dialog id=\"player_inventory\" title=\"Inventory\" width=\"610\" height=\"450\">";
  44. result += "<craftingGrid id=\"crafting\" margin-top=\"9\" align-top=\"start\" align-left=\"start\" margin-left=\"9\" width=\"282\" height=\"172\" rowSize=\"3\" colSize=\"3\" numOutputSlots=\"1\" target=\"";
  45. result += getId();
  46. result += "\"/>";
  47. result += "<inventory id=\"inventory\" margin-bottom=\"18\" align-bottom=\"item_bar\" align-left=\"start\" margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" numSlots=\"30\" slotNameFilter=\"Inventory\" target=\"";
  48. result += getId();
  49. result += "\"/>";
  50. result += "<inventory id=\"item_bar\" margin-bottom=\"9\" align-bottom=\"end\" align-left=\"start\" margin-left=\"9\" width=\"592\" height=\"52\" rowSize=\"10\" numSlots=\"10\" slotNameFilter=\"ItemBar\" target=\"";
  51. result += getId();
  52. result += "\"/>";
  53. result += "</dialog>";
  54. return result;
  55. }
  56. Framework::Text Player::getPlayerGUI()
  57. {
  58. Framework::Text result = "<gui id=\"player_gui\">";
  59. result += "<itemBar id=\"gui_item_bar\" margin-bottom=\"9\" align-bottom=\"end\" align-left=\"center\" width=\"592\" height=\"52\" rowSize=\"10\" slotNameFilter=\"ItemBar\" target=\"";
  60. result += getId();
  61. result += "\"/>";
  62. result += "</gui>";
  63. return result;
  64. }
  65. void Player::useItemSlot(ItemSlot* zSlot)
  66. {
  67. if (zSlot->zStack())
  68. {
  69. ItemStack* stack = takeItemsOut(zSlot, 1, NO_DIRECTION);
  70. if (stack)
  71. {
  72. Item* item = stack->extractFromStack();
  73. Entity::useItem(item->zItemType(), item);
  74. if (item->getDurability() > 0)
  75. { // put used item back
  76. stack->addToStack(item);
  77. // TODO: use inventory wrapper to update the cache of the inventory
  78. if (!zSlot->numberOfAddableItems(stack, NO_DIRECTION))
  79. { // move other items to other space
  80. ItemStack* oldItems = takeItemsOut(zSlot, zSlot->zStack()->getSize(), NO_DIRECTION);
  81. addItems(zSlot, stack, NO_DIRECTION);
  82. addItems(oldItems, NO_DIRECTION);
  83. if (oldItems->getSize() > 0)
  84. {
  85. // TODO: drop remaining items
  86. }
  87. }
  88. else
  89. addItems(zSlot, stack, NO_DIRECTION);
  90. }
  91. else
  92. { // item is broken
  93. // move other items of the same type to the slot
  94. Array< ItemSlot*> fromSlots;
  95. for (ItemSlot* slot : itemBar)
  96. {
  97. if (slot != zSlot)
  98. fromSlots.add(slot);
  99. }
  100. Array<ItemSlot*> targetSlots;
  101. targetSlots.add(zSlot);
  102. TypeItemFilter filter(item->zItemType());
  103. localTransaction(&fromSlots, &targetSlots, &filter, zSlot->getFreeSpace(), NO_DIRECTION, NO_DIRECTION);
  104. // place broken item in inventory
  105. const ItemType* brokenType = item->zItemType()->zBrokenItemType();
  106. if (brokenType)
  107. {
  108. Item* broken = item->zItemType()->breakItem(item);
  109. if (broken)
  110. {
  111. stack->addToStack(broken);
  112. addItems(stack, NO_DIRECTION);
  113. if (stack->getSize() > 0)
  114. {
  115. // TODO: drop remaining items
  116. }
  117. }
  118. }
  119. item->release();
  120. }
  121. stack->release();
  122. }
  123. }
  124. else
  125. Entity::useItem(PlayerHandItemType::INSTANCE, 0); // hand usage
  126. }
  127. void Player::setName(Framework::Text name)
  128. {
  129. this->name = name;
  130. }
  131. const char* Player::getName() const
  132. {
  133. return name;
  134. }
  135. void Player::tick(const Dimension* zDimension)
  136. {
  137. if ((keyState | Key::LEFT_HAND_ACTION) == keyState)
  138. useItemSlot(itemBar.get(leftHandPosition));
  139. if ((keyState | Key::RIGHT_HAND_ACTION) == keyState)
  140. useItemSlot(itemBar.get((leftHandPosition + 1) % itemBar.getEintragAnzahl()));
  141. return Entity::tick(zDimension);
  142. }
  143. void Player::playerApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  144. {
  145. char byte;
  146. zRequest->lese(&byte, 1);
  147. switch (byte)
  148. {
  149. case 0:
  150. // stop action
  151. zRequest->lese(&byte, 1);
  152. switch (byte)
  153. {
  154. case 8:
  155. keyState = keyState & ~Key::LEFT_HAND_ACTION;
  156. break;
  157. case 9:
  158. keyState = keyState & ~Key::RIGHT_HAND_ACTION;
  159. break;
  160. }
  161. break;
  162. case 1:
  163. // begin action
  164. zRequest->lese(&byte, 1);
  165. switch (byte)
  166. {
  167. case 8:
  168. keyState = keyState | Key::LEFT_HAND_ACTION;
  169. break;
  170. case 9:
  171. keyState = keyState | Key::RIGHT_HAND_ACTION;
  172. break;
  173. }
  174. break;
  175. case 2:
  176. // set movement
  177. {
  178. MovementFrame frame;
  179. zRequest->lese((char*)&frame.direction.x, 4);
  180. zRequest->lese((char*)&frame.direction.y, 4);
  181. zRequest->lese((char*)&frame.direction.z, 4);
  182. zRequest->lese((char*)&frame.targetPosition.x, 4);
  183. zRequest->lese((char*)&frame.targetPosition.y, 4);
  184. zRequest->lese((char*)&frame.targetPosition.z, 4);
  185. zRequest->lese((char*)&frame.movementFlags, 4);
  186. zRequest->lese((char*)&frame.duration, 8);
  187. addMovementFrame(frame);
  188. calculateTarget(frame.targetPosition, frame.direction);
  189. break;
  190. }
  191. case 3:
  192. { // switch item bar position
  193. zRequest->lese((char*)&leftHandPosition, 4);
  194. leftHandPosition = leftHandPosition % itemBar.getEintragAnzahl();
  195. NetworkMessage msg;
  196. msg.addressGui("gui_item_bar");
  197. char message[5];
  198. message[0] = 3; // set selected slot
  199. *(int*)(message + 1) = leftHandPosition;
  200. msg.setMessage(message, 5, 0);
  201. Game::INSTANCE->sendMessage(&msg, this);
  202. break;
  203. }
  204. case 4:
  205. {
  206. // open inventory
  207. zResponse->openDialog("player_inventory");
  208. Text uiml = getInventoryUIML();
  209. int msgSize = 4 + uiml.getLength();
  210. char* msg = new char[msgSize];
  211. *(int*)msg = uiml.getLength();
  212. memcpy(msg + 4, uiml.getText(), uiml.getLength());
  213. zResponse->setMessage(msg, msgSize, 1);
  214. break;
  215. }
  216. case 5:
  217. {
  218. // request gui
  219. Text uiml = getPlayerGUI();
  220. int msgSize = 6 + uiml.getLength();
  221. char* msg = new char[msgSize];
  222. msg[0] = 2; // gui message
  223. msg[1] = 2; // set gui
  224. *(int*)(msg + 2) = uiml.getLength();
  225. memcpy(msg + 6, uiml.getText(), uiml.getLength());
  226. zResponse->setMessage(msg, msgSize, 1);
  227. break;
  228. }
  229. case 6:
  230. { // inventory transaction
  231. bool isEntity;
  232. zRequest->lese((char*)&isEntity, 1);
  233. Inventory* source;
  234. if (isEntity)
  235. {
  236. int id;
  237. zRequest->lese((char*)&id, 4);
  238. source = Game::INSTANCE->zEntity(id, getCurrentDimensionId());
  239. }
  240. else
  241. {
  242. int dim;
  243. Framework::Vec3<int> pos;
  244. zRequest->lese((char*)&dim, 4);
  245. zRequest->lese((char*)&pos.x, 4);
  246. zRequest->lese((char*)&pos.y, 4);
  247. zRequest->lese((char*)&pos.z, 4);
  248. source = Game::INSTANCE->zBlockAt(pos, dim);
  249. }
  250. int sourceSlotId;
  251. zRequest->lese((char*)&sourceSlotId, 4);
  252. zRequest->lese((char*)&isEntity, 1);
  253. Inventory* target;
  254. if (isEntity)
  255. {
  256. int id;
  257. zRequest->lese((char*)&id, 4);
  258. target = Game::INSTANCE->zEntity(id, getCurrentDimensionId());
  259. }
  260. else
  261. {
  262. int dim;
  263. Framework::Vec3<int> pos;
  264. zRequest->lese((char*)&dim, 4);
  265. zRequest->lese((char*)&pos.x, 4);
  266. zRequest->lese((char*)&pos.y, 4);
  267. zRequest->lese((char*)&pos.z, 4);
  268. target = Game::INSTANCE->zBlockAt(pos, dim);
  269. }
  270. if (source && target)
  271. {
  272. int targetSlotId;
  273. zRequest->lese((char*)&targetSlotId, 4);
  274. SpecificSlotFilter filter(sourceSlotId, targetSlotId);
  275. source->interactWith(target, Direction::NO_DIRECTION).pushItems(source->zSlot(sourceSlotId)->getNumberOfItems(), &filter);
  276. }
  277. break;
  278. }
  279. case 7: // craft action
  280. {
  281. bool isEntity;
  282. zRequest->lese((char*)&isEntity, 1);
  283. BasicShapedCrafter* target;
  284. if (isEntity)
  285. {
  286. int id;
  287. zRequest->lese((char*)&id, 4);
  288. target = dynamic_cast<BasicShapedCrafter*>(Game::INSTANCE->zEntity(id, getCurrentDimensionId()));
  289. }
  290. else
  291. {
  292. int dim;
  293. Framework::Vec3<int> pos;
  294. zRequest->lese((char*)&dim, 4);
  295. zRequest->lese((char*)&pos.x, 4);
  296. zRequest->lese((char*)&pos.y, 4);
  297. zRequest->lese((char*)&pos.z, 4);
  298. target = dynamic_cast<BasicShapedCrafter*>(Game::INSTANCE->zRealBlockInstance(pos, dim));
  299. }
  300. if (target)
  301. target->applyCurrentRecipie();
  302. break;
  303. }
  304. }
  305. }
  306. void Player::onFall(float collisionSpeed)
  307. {
  308. Entity::onFall(collisionSpeed);
  309. gravityMultiplier = 1.f;
  310. jumping = 0;
  311. }
  312. PlayerEntityType::PlayerEntityType()
  313. : EntityType(ID, ModelInfo("player", "player.ltdb/player.png", 6))
  314. {}
  315. void PlayerEntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader* zReader) const
  316. {
  317. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  318. if (!zPlayer)
  319. throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
  320. zReader->lese((char*)&zPlayer->leftHandPosition, 4);
  321. char len;
  322. zReader->lese(&len, 1);
  323. char* name = new char[(int)len + 1];
  324. zReader->lese(name, (int)len);
  325. name[(int)len] = 0;
  326. zPlayer->name = name;
  327. delete[] name;
  328. EntityType::loadSuperEntity(zPlayer, zReader);
  329. }
  330. void PlayerEntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWriter) const
  331. {
  332. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  333. if (!zPlayer)
  334. throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
  335. zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
  336. char len = (char)textLength(zPlayer->getName());
  337. zWriter->schreibe(&len, 1);
  338. zWriter->schreibe(zPlayer->getName(), (int)len);
  339. EntityType::saveSuperEntity(zEntity, zWriter);
  340. }
  341. Entity* PlayerEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  342. {
  343. return new Player(position, dimensionId, entityId);
  344. }