Player.cpp 14 KB

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