Player.cpp 10.0 KB

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