Player.cpp 13 KB

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