Player.cpp 13 KB

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