Player.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. }
  34. void Player::onTargetChange()
  35. {
  36. NetworkMessage msg;
  37. ActionTarget::toMessage(zTarget(), msg);
  38. Game::INSTANCE->sendMessage(&msg, this);
  39. }
  40. Framework::Text Player::getInventoryUIML()
  41. {
  42. Framework::Text result = "<dialog id=\"player_inventory\" title=\"Inventory\" width=\"610\" height=\"450\">";
  43. 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=\"";
  44. result += getId();
  45. result += "\"/>";
  46. 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=\"";
  47. result += getId();
  48. result += "\"/>";
  49. 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=\"";
  50. result += getId();
  51. result += "\"/>";
  52. result += "</dialog>";
  53. return result;
  54. }
  55. Framework::Text Player::getPlayerGUI()
  56. {
  57. Framework::Text result = "<gui id=\"player_gui\">";
  58. result += "<itemBar id=\"gui_item_bar\" margin-bottom=\"9\" align-bottom=\"end\" align-left=\"center\" width=\"592\" height=\"52\" rowSize=\"10\" slotNameFilter=\"ItemBar\" target=\"";
  59. result += getId();
  60. result += "\"/>";
  61. result += "</gui>";
  62. return result;
  63. }
  64. void Player::useItemSlot(ItemSlot* zSlot)
  65. {
  66. if (zSlot->zStack())
  67. {
  68. ItemStack* stack = takeItemsOut(zSlot, 1, NO_DIRECTION);
  69. if (stack)
  70. {
  71. Item* item = stack->extractFromStack();
  72. Entity::useItem(item->zItemType(), item);
  73. if (item->getDurability() > 0)
  74. { // put used item back
  75. stack->addToStack(item);
  76. // TODO: use inventory wrapper to update the cache of the inventory
  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 : itemBar)
  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. speed = { 0, 0, speed.z };
  137. if ((keyState | Key::MOVE_FRONT) == keyState)
  138. speed += {faceDir.x, faceDir.y, 0};
  139. if ((keyState | Key::MOVE_BACK) == keyState)
  140. speed += {-faceDir.x, -faceDir.y, 0};
  141. if ((keyState | Key::MOVE_RIGHT) == keyState)
  142. {
  143. Vec2<float> norm = { faceDir.x, faceDir.y };
  144. norm.CW90().normalize();
  145. speed += {norm.x, norm.y, 0};
  146. }
  147. if ((keyState | Key::MOVE_LEFT) == keyState)
  148. {
  149. Vec2<float> norm = { faceDir.x, faceDir.y };
  150. norm.CCW90().normalize();
  151. speed += {norm.x, norm.y, 0};
  152. }
  153. Vec2<float> norm = { speed.x, speed.y };
  154. if (norm.getLengthSq() != 0)
  155. {
  156. norm.normalize();
  157. speed.x = norm.x * 4.f; // 4 blocks per second movement speed
  158. speed.y = norm.y * 4.f;
  159. }
  160. if ((keyState | Key::MOVE_DOWN) == keyState && gravityMultiplier == 0.f)
  161. speed.z = -4.f;
  162. if ((keyState | Key::LEFT_HAND_ACTION) == keyState)
  163. useItemSlot(itemBar.get(leftHandPosition));
  164. if ((keyState | Key::RIGHT_HAND_ACTION) == keyState)
  165. useItemSlot(itemBar.get((leftHandPosition + 1) % itemBar.getEintragAnzahl()));
  166. return Entity::tick(zDimension);
  167. }
  168. void Player::playerApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  169. {
  170. char byte;
  171. zRequest->lese(&byte, 1);
  172. switch (byte)
  173. {
  174. case 0:
  175. // stop action
  176. zRequest->lese(&byte, 1);
  177. switch (byte)
  178. {
  179. case 0:
  180. keyState = keyState & ~Key::MOVE_FRONT;
  181. break;
  182. case 1:
  183. keyState = keyState & ~Key::MOVE_LEFT;
  184. break;
  185. case 2:
  186. keyState = keyState & ~Key::MOVE_BACK;
  187. break;
  188. case 3:
  189. keyState = keyState & ~Key::MOVE_RIGHT;
  190. break;
  191. case 4:
  192. if (gravityMultiplier == 0.f)
  193. speed.z = 0;
  194. keyState = keyState & ~Key::MOVE_DOWN;
  195. break;
  196. case 5:
  197. keyState = keyState & ~Key::ROTATE_LEFT;
  198. break;
  199. case 6:
  200. keyState = keyState & ~Key::ROTATE_RIGHT;
  201. break;
  202. case 7:
  203. if (gravityMultiplier == 0.f)
  204. speed.z = 0;
  205. keyState = keyState & ~Key::MOVE_UP;
  206. break;
  207. case 8:
  208. keyState = keyState & ~Key::LEFT_HAND_ACTION;
  209. break;
  210. case 9:
  211. keyState = keyState & ~Key::RIGHT_HAND_ACTION;
  212. break;
  213. }
  214. break;
  215. case 1:
  216. // begin action
  217. zRequest->lese(&byte, 1);
  218. switch (byte)
  219. {
  220. case 0:
  221. keyState = keyState | Key::MOVE_FRONT;
  222. break;
  223. case 1:
  224. keyState = keyState | Key::MOVE_LEFT;
  225. break;
  226. case 2:
  227. keyState = keyState | Key::MOVE_BACK;
  228. break;
  229. case 3:
  230. keyState = keyState | Key::MOVE_RIGHT;
  231. break;
  232. case 4:
  233. keyState = keyState | Key::MOVE_DOWN;
  234. break;
  235. case 5:
  236. keyState = keyState | Key::ROTATE_LEFT;
  237. break;
  238. case 6:
  239. keyState = keyState | Key::ROTATE_RIGHT;
  240. break;
  241. case 7:
  242. if ((keyState | Key::MOVE_UP) != keyState)
  243. {
  244. if (gravityMultiplier > 0)
  245. {
  246. if (jumping)
  247. {
  248. // TODO: check if flight is enabled
  249. gravityMultiplier = 0;
  250. jumping = 0;
  251. speed.z = 1.5f;
  252. }
  253. else
  254. {
  255. jumping = 1;
  256. speed.z = 5.f;
  257. }
  258. }
  259. else
  260. speed.z = 1.5f;
  261. }
  262. keyState = keyState | Key::MOVE_UP;
  263. break;
  264. case 8:
  265. keyState = keyState | Key::LEFT_HAND_ACTION;
  266. break;
  267. case 9:
  268. keyState = keyState | Key::RIGHT_HAND_ACTION;
  269. break;
  270. }
  271. break;
  272. case 2:
  273. // switch target direction
  274. zRequest->lese((char*)&faceDir.x, 4);
  275. zRequest->lese((char*)&faceDir.y, 4);
  276. zRequest->lese((char*)&faceDir.z, 4);
  277. break;
  278. case 3:
  279. { // switch item bar position
  280. zRequest->lese((char*)&leftHandPosition, 4);
  281. leftHandPosition = leftHandPosition % itemBar.getEintragAnzahl();
  282. NetworkMessage msg;
  283. msg.adressGui("gui_item_bar");
  284. char message[5];
  285. message[0] = 3; // set selected slot
  286. *(int*)(message + 1) = leftHandPosition;
  287. msg.setMessage(message, 5, 0);
  288. Game::INSTANCE->sendMessage(&msg, this);
  289. break;
  290. }
  291. case 4:
  292. {
  293. // open inventory
  294. zResponse->openDialog("player_inventory");
  295. Text uiml = getInventoryUIML();
  296. int msgSize = 4 + uiml.getLength();
  297. char* msg = new char[msgSize];
  298. *(int*)msg = uiml.getLength();
  299. memcpy(msg + 4, uiml.getText(), uiml.getLength());
  300. zResponse->setMessage(msg, msgSize, 1);
  301. break;
  302. }
  303. case 5:
  304. {
  305. // request gui
  306. Text uiml = getPlayerGUI();
  307. int msgSize = 6 + uiml.getLength();
  308. char* msg = new char[msgSize];
  309. msg[0] = 2; // gui message
  310. msg[1] = 2; // set gui
  311. *(int*)(msg + 2) = uiml.getLength();
  312. memcpy(msg + 6, uiml.getText(), uiml.getLength());
  313. zResponse->setMessage(msg, msgSize, 1);
  314. break;
  315. }
  316. case 6:
  317. { // inventory transaction
  318. bool isEntity;
  319. zRequest->lese((char*)&isEntity, 1);
  320. Inventory* source;
  321. if (isEntity)
  322. {
  323. int id;
  324. zRequest->lese((char*)&id, 4);
  325. source = Game::INSTANCE->zEntity(id, getCurrentDimensionId());
  326. }
  327. else
  328. {
  329. int dim;
  330. Framework::Vec3<int> pos;
  331. zRequest->lese((char*)&dim, 4);
  332. zRequest->lese((char*)&pos.x, 4);
  333. zRequest->lese((char*)&pos.y, 4);
  334. zRequest->lese((char*)&pos.z, 4);
  335. source = Game::INSTANCE->zBlockAt(pos, dim);
  336. }
  337. int sourceSlotId;
  338. zRequest->lese((char*)&sourceSlotId, 4);
  339. zRequest->lese((char*)&isEntity, 1);
  340. Inventory* target;
  341. if (isEntity)
  342. {
  343. int id;
  344. zRequest->lese((char*)&id, 4);
  345. target = Game::INSTANCE->zEntity(id, getCurrentDimensionId());
  346. }
  347. else
  348. {
  349. int dim;
  350. Framework::Vec3<int> pos;
  351. zRequest->lese((char*)&dim, 4);
  352. zRequest->lese((char*)&pos.x, 4);
  353. zRequest->lese((char*)&pos.y, 4);
  354. zRequest->lese((char*)&pos.z, 4);
  355. target = Game::INSTANCE->zBlockAt(pos, dim);
  356. }
  357. if (source && target)
  358. {
  359. int targetSlotId;
  360. zRequest->lese((char*)&targetSlotId, 4);
  361. SpecificSlotFilter filter(sourceSlotId, targetSlotId);
  362. source->interactWith(target, Direction::NO_DIRECTION).pushItems(source->zSlot(sourceSlotId)->getNumberOfItems(), &filter);
  363. }
  364. break;
  365. }
  366. case 7: // craft action
  367. {
  368. bool isEntity;
  369. zRequest->lese((char*)&isEntity, 1);
  370. BasicShapedCrafter* target;
  371. if (isEntity)
  372. {
  373. int id;
  374. zRequest->lese((char*)&id, 4);
  375. target = dynamic_cast<BasicShapedCrafter*>(Game::INSTANCE->zEntity(id, getCurrentDimensionId()));
  376. }
  377. else
  378. {
  379. int dim;
  380. Framework::Vec3<int> pos;
  381. zRequest->lese((char*)&dim, 4);
  382. zRequest->lese((char*)&pos.x, 4);
  383. zRequest->lese((char*)&pos.y, 4);
  384. zRequest->lese((char*)&pos.z, 4);
  385. target = dynamic_cast<BasicShapedCrafter*>(Game::INSTANCE->zRealBlockInstance(pos, dim));
  386. }
  387. if (target)
  388. target->applyCurrentRecipie();
  389. break;
  390. }
  391. }
  392. }
  393. void Player::onFall(float collisionSpeed)
  394. {
  395. Entity::onFall(collisionSpeed);
  396. gravityMultiplier = 1.f;
  397. jumping = 0;
  398. }
  399. PlayerEntityType::PlayerEntityType()
  400. : EntityType(ID, ModelInfo("player", "player.ltdb/player.png", 6))
  401. {}
  402. void PlayerEntityType::loadSuperEntity(Entity* zEntity, Framework::StreamReader* zReader) const
  403. {
  404. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  405. if (!zPlayer)
  406. throw "PlayerEntityType::loadSuperEntity was called with an entity witch is not an instance of Player";
  407. zReader->lese((char*)&zPlayer->leftHandPosition, 4);
  408. EntityType::loadSuperEntity(zPlayer, zReader);
  409. }
  410. void PlayerEntityType::saveSuperEntity(Entity* zEntity, Framework::StreamWriter* zWriter) const
  411. {
  412. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  413. if (!zPlayer)
  414. throw "PlayerEntityType::saveSuperEntity was called with an entity witch is not an instance of Player";
  415. zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
  416. EntityType::saveSuperEntity(zEntity, zWriter);
  417. }
  418. Entity* PlayerEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  419. {
  420. return new Player(position, dimensionId, entityId);
  421. }