Player.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 = new NetworkMessage();
  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. if (!zSlot->numberOfAddableItems(stack, NO_DIRECTION))
  78. { // move other items to other space
  79. ItemStack* oldItems = takeItemsOut(zSlot, zSlot->zStack()->getSize(), NO_DIRECTION);
  80. addItems(zSlot, stack, NO_DIRECTION);
  81. addItems(oldItems, NO_DIRECTION);
  82. if (oldItems->getSize() > 0)
  83. {
  84. // TODO: drop remaining items
  85. }
  86. }
  87. else
  88. addItems(zSlot, stack, NO_DIRECTION);
  89. }
  90. else
  91. { // item is broken
  92. // move other items of the same type to the slot
  93. Array< ItemSlot*> fromSlots;
  94. for (ItemSlot* slot : *this)
  95. {
  96. if (slot != zSlot)
  97. fromSlots.add(slot);
  98. }
  99. Array<ItemSlot*> targetSlots;
  100. targetSlots.add(zSlot);
  101. TypeItemFilter filter(item->zItemType());
  102. localTransaction(&fromSlots, &targetSlots, &filter, zSlot->getFreeSpace(), NO_DIRECTION, NO_DIRECTION);
  103. // place broken item in inventory
  104. const ItemType* brokenType = item->zItemType()->zBrokenItemType();
  105. if (brokenType)
  106. {
  107. Item* broken = item->zItemType()->breakItem(item);
  108. if (broken)
  109. {
  110. stack->addToStack(broken);
  111. addItems(stack, NO_DIRECTION);
  112. if (stack->getSize() > 0)
  113. {
  114. // TODO: drop remaining items
  115. }
  116. }
  117. }
  118. item->release();
  119. }
  120. stack->release();
  121. }
  122. }
  123. else
  124. Entity::useItem(PlayerHandItemType::INSTANCE, 0); // hand usage
  125. }
  126. void Player::setName(Framework::Text name)
  127. {
  128. this->name = name;
  129. }
  130. const char* Player::getName() const
  131. {
  132. return name;
  133. }
  134. void Player::tick(const Dimension* zDimension)
  135. {
  136. if ((keyState | Key::LEFT_HAND_ACTION) == keyState)
  137. useItemSlot(itemBar.get(leftHandPosition));
  138. if ((keyState | Key::RIGHT_HAND_ACTION) == keyState)
  139. useItemSlot(itemBar.get((leftHandPosition + 1) % itemBar.getEintragAnzahl()));
  140. return Entity::tick(zDimension);
  141. }
  142. void Player::playerApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  143. {
  144. char byte;
  145. zRequest->lese(&byte, 1);
  146. switch (byte)
  147. {
  148. case 0:
  149. // stop action
  150. zRequest->lese(&byte, 1);
  151. switch (byte)
  152. {
  153. case 8:
  154. keyState = keyState & ~Key::LEFT_HAND_ACTION;
  155. break;
  156. case 9:
  157. keyState = keyState & ~Key::RIGHT_HAND_ACTION;
  158. break;
  159. }
  160. break;
  161. case 1:
  162. // begin action
  163. zRequest->lese(&byte, 1);
  164. switch (byte)
  165. {
  166. case 8:
  167. keyState = keyState | Key::LEFT_HAND_ACTION;
  168. break;
  169. case 9:
  170. keyState = keyState | Key::RIGHT_HAND_ACTION;
  171. break;
  172. }
  173. break;
  174. case 2:
  175. // set movement
  176. {
  177. MovementFrame frame;
  178. zRequest->lese((char*)&frame.direction.x, 4);
  179. zRequest->lese((char*)&frame.direction.y, 4);
  180. zRequest->lese((char*)&frame.direction.z, 4);
  181. zRequest->lese((char*)&frame.targetPosition.x, 4);
  182. zRequest->lese((char*)&frame.targetPosition.y, 4);
  183. zRequest->lese((char*)&frame.targetPosition.z, 4);
  184. zRequest->lese((char*)&frame.movementFlags, 4);
  185. zRequest->lese((char*)&frame.duration, 8);
  186. addMovementFrame(frame);
  187. calculateTarget(frame.targetPosition, frame.direction);
  188. break;
  189. }
  190. case 3:
  191. { // switch item bar position
  192. zRequest->lese((char*)&leftHandPosition, 4);
  193. leftHandPosition = leftHandPosition % itemBar.getEintragAnzahl();
  194. NetworkMessage* msg = new NetworkMessage();
  195. msg->addressGui("gui_item_bar");
  196. char* message = new char[5];
  197. message[0] = 3; // set selected slot
  198. *(int*)(message + 1) = leftHandPosition;
  199. msg->setMessage(message, 5);
  200. Game::INSTANCE->sendMessage(msg, this);
  201. break;
  202. }
  203. case 4:
  204. {
  205. // open inventory
  206. zResponse->openDialog("player_inventory");
  207. Text uiml = getInventoryUIML();
  208. int msgSize = 4 + uiml.getLength();
  209. char* msg = new char[msgSize];
  210. *(int*)msg = uiml.getLength();
  211. memcpy(msg + 4, uiml.getText(), uiml.getLength());
  212. zResponse->setMessage(msg, msgSize);
  213. break;
  214. }
  215. case 5:
  216. {
  217. // request gui
  218. Text uiml = getPlayerGUI();
  219. int msgSize = 6 + uiml.getLength();
  220. char* msg = new char[msgSize];
  221. msg[0] = 2; // gui message
  222. msg[1] = 2; // set gui
  223. *(int*)(msg + 2) = uiml.getLength();
  224. memcpy(msg + 6, uiml.getText(), uiml.getLength());
  225. zResponse->setMessage(msg, msgSize);
  226. break;
  227. }
  228. case 6:
  229. { // inventory transaction
  230. bool isEntity;
  231. zRequest->lese((char*)&isEntity, 1);
  232. Inventory* source;
  233. if (isEntity)
  234. {
  235. int id;
  236. zRequest->lese((char*)&id, 4);
  237. source = Game::INSTANCE->zEntity(id, getCurrentDimensionId());
  238. }
  239. else
  240. {
  241. int dim;
  242. Framework::Vec3<int> pos;
  243. zRequest->lese((char*)&dim, 4);
  244. zRequest->lese((char*)&pos.x, 4);
  245. zRequest->lese((char*)&pos.y, 4);
  246. zRequest->lese((char*)&pos.z, 4);
  247. source = Game::INSTANCE->zBlockAt(pos, dim);
  248. }
  249. int sourceSlotId;
  250. zRequest->lese((char*)&sourceSlotId, 4);
  251. zRequest->lese((char*)&isEntity, 1);
  252. Inventory* target;
  253. if (isEntity)
  254. {
  255. int id;
  256. zRequest->lese((char*)&id, 4);
  257. target = Game::INSTANCE->zEntity(id, getCurrentDimensionId());
  258. }
  259. else
  260. {
  261. int dim;
  262. Framework::Vec3<int> pos;
  263. zRequest->lese((char*)&dim, 4);
  264. zRequest->lese((char*)&pos.x, 4);
  265. zRequest->lese((char*)&pos.y, 4);
  266. zRequest->lese((char*)&pos.z, 4);
  267. target = Game::INSTANCE->zBlockAt(pos, dim);
  268. }
  269. if (source && target)
  270. {
  271. int targetSlotId;
  272. zRequest->lese((char*)&targetSlotId, 4);
  273. SpecificSlotFilter filter(sourceSlotId, targetSlotId);
  274. source->interactWith(target, Direction::NO_DIRECTION).pushItems(source->zSlot(sourceSlotId)->getNumberOfItems(), &filter);
  275. }
  276. break;
  277. }
  278. case 7: // craft action
  279. {
  280. bool isEntity;
  281. zRequest->lese((char*)&isEntity, 1);
  282. BasicShapedCrafter* target;
  283. if (isEntity)
  284. {
  285. int id;
  286. zRequest->lese((char*)&id, 4);
  287. target = dynamic_cast<BasicShapedCrafter*>(Game::INSTANCE->zEntity(id, getCurrentDimensionId()));
  288. }
  289. else
  290. {
  291. int dim;
  292. Framework::Vec3<int> pos;
  293. zRequest->lese((char*)&dim, 4);
  294. zRequest->lese((char*)&pos.x, 4);
  295. zRequest->lese((char*)&pos.y, 4);
  296. zRequest->lese((char*)&pos.z, 4);
  297. target = dynamic_cast<BasicShapedCrafter*>(Game::INSTANCE->zRealBlockInstance(pos, dim));
  298. }
  299. if (target)
  300. target->applyCurrentRecipie();
  301. break;
  302. }
  303. }
  304. }
  305. void Player::onFall(float collisionSpeed)
  306. {
  307. Entity::onFall(collisionSpeed);
  308. gravityMultiplier = 1.f;
  309. jumping = 0;
  310. }
  311. PlayerEntityType::PlayerEntityType()
  312. : EntityType(ID, ModelInfo("player", "player.ltdb/player.png", 6))
  313. {}
  314. void PlayerEntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader* zReader) const
  315. {
  316. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  317. if (!zPlayer)
  318. throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
  319. zReader->lese((char*)&zPlayer->leftHandPosition, 4);
  320. char len;
  321. zReader->lese(&len, 1);
  322. char* name = new char[(int)len + 1];
  323. zReader->lese(name, (int)len);
  324. name[(int)len] = 0;
  325. zPlayer->name = name;
  326. delete[] name;
  327. EntityType::loadSuperEntity(zPlayer, zReader);
  328. }
  329. void PlayerEntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWriter) const
  330. {
  331. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  332. if (!zPlayer)
  333. throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
  334. zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
  335. char len = (char)textLength(zPlayer->getName());
  336. zWriter->schreibe(&len, 1);
  337. zWriter->schreibe(zPlayer->getName(), (int)len);
  338. EntityType::saveSuperEntity(zEntity, zWriter);
  339. }
  340. Entity* PlayerEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  341. {
  342. return new Player(position, dimensionId, entityId);
  343. }