Player.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. Game::INSTANCE->zQuestManager()->processEvent(
  252. new QuestEventOpenDialog(
  253. dynamic_cast<Entity*>(getThis()), "player_inventory"));
  254. break;
  255. }
  256. case 5:
  257. {
  258. // request gui
  259. Text uiml = getPlayerGUI();
  260. int msgSize = 6 + uiml.getLength();
  261. char* msg = new char[msgSize];
  262. msg[0] = 2; // gui message
  263. msg[1] = 2; // set gui
  264. *(int*)(msg + 2) = uiml.getLength();
  265. memcpy(msg + 6, uiml.getText(), uiml.getLength());
  266. zResponse->setMessage(msg, msgSize);
  267. break;
  268. }
  269. case 6:
  270. { // inventory transaction
  271. bool isEntity;
  272. zRequest->lese((char*)&isEntity, 1);
  273. Inventory* source;
  274. if (isEntity)
  275. {
  276. int id;
  277. zRequest->lese((char*)&id, 4);
  278. source = Game::INSTANCE->zEntity(id, dimensionId);
  279. }
  280. else
  281. {
  282. int dim;
  283. Framework::Vec3<int> pos;
  284. zRequest->lese((char*)&dim, 4);
  285. zRequest->lese((char*)&pos.x, 4);
  286. zRequest->lese((char*)&pos.y, 4);
  287. zRequest->lese((char*)&pos.z, 4);
  288. source = Game::INSTANCE->zBlockAt(pos, dim);
  289. }
  290. int sourceSlotId;
  291. zRequest->lese((char*)&sourceSlotId, 4);
  292. zRequest->lese((char*)&isEntity, 1);
  293. Inventory* target;
  294. if (isEntity)
  295. {
  296. int id;
  297. zRequest->lese((char*)&id, 4);
  298. target = Game::INSTANCE->zEntity(id, dimensionId);
  299. }
  300. else
  301. {
  302. int dim;
  303. Framework::Vec3<int> pos;
  304. zRequest->lese((char*)&dim, 4);
  305. zRequest->lese((char*)&pos.x, 4);
  306. zRequest->lese((char*)&pos.y, 4);
  307. zRequest->lese((char*)&pos.z, 4);
  308. target = Game::INSTANCE->zBlockAt(pos, dim);
  309. }
  310. if (source && target)
  311. {
  312. int targetSlotId;
  313. zRequest->lese((char*)&targetSlotId, 4);
  314. SpecificSlotFilter filter(sourceSlotId, targetSlotId);
  315. source->interactWith(target, Direction::NO_DIRECTION)
  316. .pushItems(source->zSlot(sourceSlotId)->getNumberOfItems(),
  317. &filter);
  318. }
  319. break;
  320. }
  321. case 7: // craft action
  322. {
  323. bool isEntity;
  324. zRequest->lese((char*)&isEntity, 1);
  325. BasicShapedCrafter* target;
  326. if (isEntity)
  327. {
  328. int id;
  329. zRequest->lese((char*)&id, 4);
  330. target = dynamic_cast<BasicShapedCrafter*>(
  331. Game::INSTANCE->zEntity(id, dimensionId));
  332. }
  333. else
  334. {
  335. int dim;
  336. Framework::Vec3<int> pos;
  337. zRequest->lese((char*)&dim, 4);
  338. zRequest->lese((char*)&pos.x, 4);
  339. zRequest->lese((char*)&pos.y, 4);
  340. zRequest->lese((char*)&pos.z, 4);
  341. target = dynamic_cast<BasicShapedCrafter*>(
  342. Game::INSTANCE->zRealBlockInstance(pos, dim));
  343. }
  344. if (target) target->applyCurrentRecipie();
  345. break;
  346. }
  347. case 8: // request left hand position
  348. {
  349. NetworkMessage* msg = new NetworkMessage();
  350. msg->addressGui("gui_item_bar");
  351. char* message = new char[5];
  352. message[0] = 3; // set selected slot
  353. *(int*)(message + 1) = leftHandPosition;
  354. msg->setMessage(message, 5);
  355. Game::INSTANCE->sendMessage(msg, this);
  356. break;
  357. }
  358. case 9: // open quest dialog
  359. {
  360. // TODO: open quest dialog
  361. }
  362. }
  363. }
  364. void Player::onFall(float collisionSpeed)
  365. {
  366. Entity::onFall(collisionSpeed);
  367. gravityMultiplier = 1.f;
  368. jumping = 0;
  369. }
  370. PlayerEntityType::PlayerEntityType()
  371. : EntityType(EntityTypeEnum::PLAYER,
  372. ModelInfo("player", "player.ltdb/player.png", 6))
  373. {}
  374. void PlayerEntityType::loadSuperEntity(
  375. Entity* zEntity, Framework::StreamReader* zReader) const
  376. {
  377. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  378. if (!zPlayer)
  379. throw "PlayerEntityType::loadSuperEntity was called with an entity "
  380. "witch is not an instance of Player";
  381. zReader->lese((char*)&zPlayer->leftHandPosition, 4);
  382. char len;
  383. zReader->lese(&len, 1);
  384. char* name = new char[(int)len + 1];
  385. zReader->lese(name, (int)len);
  386. name[(int)len] = 0;
  387. zPlayer->name = name;
  388. delete[] name;
  389. EntityType::loadSuperEntity(zPlayer, zReader);
  390. }
  391. void PlayerEntityType::saveSuperEntity(
  392. Entity* zEntity, Framework::StreamWriter* zWriter) const
  393. {
  394. Player* zPlayer = dynamic_cast<Player*>(zEntity);
  395. if (!zPlayer)
  396. throw "PlayerEntityType::saveSuperEntity was called with an entity "
  397. "witch is not an instance of Player";
  398. zWriter->schreibe((char*)&zPlayer->leftHandPosition, 4);
  399. char len = (char)textLength(zPlayer->getName());
  400. zWriter->schreibe(&len, 1);
  401. zWriter->schreibe(zPlayer->getName(), (int)len);
  402. EntityType::saveSuperEntity(zEntity, zWriter);
  403. }
  404. Entity* PlayerEntityType::createEntity(
  405. Framework::Vec3<float> position, int dimensionId, int entityId) const
  406. {
  407. return new Player(position, dimensionId, entityId);
  408. }