Player.cpp 16 KB

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