Player.cpp 15 KB

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