Player.cpp 14 KB

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