Entity.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include "Entity.h"
  2. #include <Text.h>
  3. #include "BlockType.h"
  4. #include "Dimension.h"
  5. #include "EntityRemovedUpdate.h"
  6. #include "Game.h"
  7. #include "ItemSkill.h"
  8. #include "NoBlock.h"
  9. ActionTarget::ActionTarget(Vec3<int> blockPos, Direction blockSide)
  10. : blockPos(blockPos),
  11. targetBlockSide(blockSide),
  12. entityId(-1)
  13. {}
  14. ActionTarget::ActionTarget(int entityId)
  15. : entityId(entityId)
  16. {}
  17. bool ActionTarget::isBlock(
  18. Framework::Vec3<int> blockPos, Direction blockSide) const
  19. {
  20. return this->entityId == -1 && this->blockPos == blockPos
  21. && (this->targetBlockSide == targetBlockSide
  22. || blockSide == NO_DIRECTION);
  23. }
  24. bool ActionTarget::isEntity(int entityId) const
  25. {
  26. return this->entityId == entityId;
  27. }
  28. void ActionTarget::applyItemSkillOnTarget(
  29. Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem)
  30. {
  31. if (entityId >= 0)
  32. {
  33. Entity* target = Game::INSTANCE->zEntity(entityId);
  34. if (target) zItemSkill->use(zActor, zUsedItem, target);
  35. }
  36. else
  37. {
  38. Block* block = Game::INSTANCE->zRealBlockInstance(
  39. blockPos, zActor->getCurrentDimensionId());
  40. if (block) zItemSkill->use(zActor, zUsedItem, block);
  41. }
  42. }
  43. void ActionTarget::placeBlock(Entity* zActor, Item* zItem)
  44. {
  45. if (zActor->getStamina() > 0.2f)
  46. {
  47. if (zItem->canBePlacedAt(zActor->getCurrentDimensionId(),
  48. blockPos + getDirection(targetBlockSide)))
  49. {
  50. Block* block = zItem->zPlacedBlockType()->createBlockAt(
  51. blockPos + getDirection(targetBlockSide), zItem);
  52. if (block)
  53. {
  54. Game::INSTANCE->zDimension(zActor->getCurrentDimensionId())
  55. ->placeBlock(block->getPos(), block);
  56. zItem->onPlaced();
  57. zActor->setStamina(zActor->getStamina() - 0.2f);
  58. }
  59. }
  60. }
  61. }
  62. void ActionTarget::toMessage(
  63. const ActionTarget* zTarget, int dimensionId, NetworkMessage* zMsg)
  64. {
  65. if (zTarget)
  66. {
  67. if (zTarget->entityId >= 0)
  68. {
  69. char* message = new char[6];
  70. message[0] = 3;
  71. message[1] = 1;
  72. *(int*)(message + 2) = zTarget->entityId;
  73. zMsg->setMessage(message, 6);
  74. }
  75. else
  76. {
  77. Framework::Text targetUIML = "";
  78. auto block
  79. = Game::INSTANCE->zBlockAt(zTarget->blockPos, dimensionId);
  80. if (block.isA())
  81. {
  82. targetUIML = block.getA()->getTargetUIML();
  83. }
  84. else if (block.isB())
  85. {
  86. targetUIML = StaticRegistry<BlockType>::INSTANCE
  87. .zElement(block.getB())
  88. ->getTargetUIML();
  89. }
  90. char* message = new char[18 + targetUIML.getLength() + 2];
  91. message[0] = 3;
  92. message[1] = 2;
  93. *(int*)(message + 2) = zTarget->blockPos.x;
  94. *(int*)(message + 6) = zTarget->blockPos.y;
  95. *(int*)(message + 10) = zTarget->blockPos.z;
  96. *(int*)(message + 14) = zTarget->targetBlockSide;
  97. short len = (short)targetUIML.getLength();
  98. *(short*)(message + 18) = len;
  99. memcpy(message + 20, targetUIML.getText(), len);
  100. zMsg->setMessage(message, 18 + len + 2);
  101. }
  102. }
  103. else
  104. {
  105. char* message = new char[2];
  106. message[0] = 3;
  107. message[1] = 0;
  108. zMsg->setMessage(message, 2);
  109. }
  110. }
  111. void ActionTarget::save(ActionTarget* zTarget, Framework::StreamWriter* zWriter)
  112. {
  113. if (zTarget)
  114. {
  115. if (zTarget->entityId >= 0)
  116. {
  117. char b = 1;
  118. zWriter->schreibe(&b, 1);
  119. zWriter->schreibe((char*)&zTarget->entityId, 4);
  120. }
  121. else
  122. {
  123. char b = 2;
  124. zWriter->schreibe(&b, 1);
  125. zWriter->schreibe((char*)&zTarget->blockPos.x, 4);
  126. zWriter->schreibe((char*)&zTarget->blockPos.y, 4);
  127. zWriter->schreibe((char*)&zTarget->blockPos.z, 4);
  128. zWriter->schreibe((char*)&zTarget->targetBlockSide, 4);
  129. }
  130. }
  131. else
  132. {
  133. char b = 0;
  134. zWriter->schreibe(&b, 1);
  135. }
  136. }
  137. ActionTarget* ActionTarget::load(Framework::StreamReader* zReader)
  138. {
  139. char b;
  140. zReader->lese(&b, 1);
  141. if (b == 1)
  142. {
  143. int id;
  144. zReader->lese((char*)&id, 4);
  145. return new ActionTarget(id);
  146. }
  147. else if (b == 2)
  148. {
  149. Framework::Vec3<int> pos;
  150. Direction side;
  151. zReader->lese((char*)&pos.x, 4);
  152. zReader->lese((char*)&pos.y, 4);
  153. zReader->lese((char*)&pos.z, 4);
  154. zReader->lese((char*)&side, 4);
  155. return new ActionTarget(pos, side);
  156. }
  157. return 0;
  158. }
  159. Entity::Entity(
  160. int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
  161. : Inventory(location, true),
  162. chatSecurityLevel(0),
  163. speed(0, 0, 0),
  164. faceDir(1, 0, 0),
  165. target(0),
  166. typeId(typeId),
  167. currentDimensionId(dimensionId),
  168. removed(0),
  169. gravityMultiplier(1.f),
  170. id(entityId),
  171. placeBlockCooldown(0)
  172. {}
  173. void Entity::onDeath()
  174. {
  175. removed = 1;
  176. Game::INSTANCE->requestWorldUpdate(
  177. new EntityRemovedUpdate(id, currentDimensionId, location));
  178. }
  179. void Entity::useItem(int typeId, Item* zItem)
  180. {
  181. if (zItem && zItem->isEatable())
  182. { // eat item
  183. zItem->applyFoodEffects(this);
  184. }
  185. else if (zItem && zItem->isPlaceable())
  186. { // place item
  187. if (placeBlockCooldown <= 0)
  188. {
  189. cs.lock();
  190. if (target)
  191. {
  192. target->placeBlock(this, zItem);
  193. placeBlockCooldown = 15;
  194. }
  195. cs.unlock();
  196. }
  197. }
  198. else if (!zItem || zItem->isUsable())
  199. { // use item skill
  200. cs.lock();
  201. if (target)
  202. {
  203. ItemSkill* selected = 0;
  204. for (ItemSkill* skill : skills)
  205. {
  206. if (skill->getTypeId() == typeId)
  207. {
  208. selected = skill;
  209. break;
  210. }
  211. }
  212. if (!selected)
  213. {
  214. selected = StaticRegistry<ItemType>::INSTANCE.zElement(typeId)
  215. ->createDefaultItemSkill();
  216. skills.add(selected);
  217. }
  218. target->applyItemSkillOnTarget(this, selected, zItem);
  219. }
  220. cs.unlock();
  221. }
  222. }
  223. void Entity::onTargetChange() {}
  224. void Entity::addMovementFrame(MovementFrame& frame)
  225. {
  226. cs.lock();
  227. movements.add(frame);
  228. cs.unlock();
  229. NetworkMessage* message = new NetworkMessage();
  230. message->addressEntity(this);
  231. char* msg = new char[37];
  232. msg[0] = 0;
  233. *(float*)(msg + 1) = frame.direction.x;
  234. *(float*)(msg + 5) = frame.direction.y;
  235. *(float*)(msg + 9) = frame.direction.z;
  236. *(float*)(msg + 13) = frame.targetPosition.x;
  237. *(float*)(msg + 17) = frame.targetPosition.y;
  238. *(float*)(msg + 21) = frame.targetPosition.z;
  239. *(int*)(msg + 25) = frame.movementFlags;
  240. *(double*)(msg + 29) = frame.duration;
  241. message->setMessage(msg, 37);
  242. Game::INSTANCE->broadcastMessage(message);
  243. faceDir = frame.direction;
  244. // TODO implement subscription system to notify only interested clients
  245. }
  246. void Entity::calculateTarget(
  247. Framework::Vec3<float> basePos, Framework::Vec3<float> direction)
  248. {
  249. Vec3<float> headPosition = basePos + faceOffset;
  250. int px = (int)floor(headPosition.x);
  251. int py = (int)floor(headPosition.y);
  252. int pz = (int)floor(headPosition.z);
  253. direction.normalize();
  254. Direction dir = BOTTOM;
  255. while (true)
  256. {
  257. if (getDefaultBlock(Game::INSTANCE->zBlockAt(
  258. Vec3<int>{px, py, pz}, currentDimensionId))
  259. ->isInteractable())
  260. {
  261. if (!target || !target->isBlock({px, py, pz}, dir))
  262. {
  263. cs.lock();
  264. delete target;
  265. target = new ActionTarget({px, py, pz}, dir);
  266. cs.unlock();
  267. onTargetChange();
  268. }
  269. break;
  270. }
  271. // collision to neighbor of current block
  272. if (direction.x > 0)
  273. {
  274. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  275. Vec3<float> tmp = headPosition + direction * xt;
  276. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  277. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  278. && tmp.z < (float)pz + 1.f)
  279. {
  280. dir = WEST;
  281. px++;
  282. continue;
  283. }
  284. }
  285. if (direction.x < 0)
  286. {
  287. float xt = ((float)px - headPosition.x) / direction.x;
  288. Vec3<float> tmp = headPosition + direction * xt;
  289. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  290. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  291. && tmp.z < (float)pz + 1.f)
  292. {
  293. dir = EAST;
  294. px--;
  295. continue;
  296. }
  297. }
  298. if (direction.y > 0)
  299. {
  300. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  301. Vec3<float> tmp = headPosition + direction * yt;
  302. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  303. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  304. && tmp.z < (float)pz + 1.f)
  305. {
  306. dir = NORTH;
  307. py++;
  308. continue;
  309. }
  310. }
  311. if (direction.y < 0)
  312. {
  313. float yt = ((float)py - headPosition.y) / direction.y;
  314. Vec3<float> tmp = headPosition + direction * yt;
  315. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  316. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  317. && tmp.z < (float)pz + 1.f)
  318. {
  319. dir = SOUTH;
  320. py--;
  321. continue;
  322. }
  323. }
  324. if (direction.z > 0)
  325. {
  326. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  327. Vec3<float> tmp = headPosition + direction * zt;
  328. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  329. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  330. && tmp.y < (float)py + 1.f)
  331. {
  332. dir = BOTTOM;
  333. pz++;
  334. continue;
  335. }
  336. }
  337. if (direction.z < 0)
  338. {
  339. float zt = ((float)pz - headPosition.z) / direction.z;
  340. Vec3<float> tmp = headPosition + direction * zt;
  341. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  342. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  343. && tmp.y < (float)py + 1)
  344. {
  345. dir = TOP;
  346. pz--;
  347. continue;
  348. }
  349. }
  350. if (target)
  351. {
  352. cs.lock();
  353. delete target;
  354. target = 0;
  355. cs.unlock();
  356. onTargetChange();
  357. }
  358. break;
  359. }
  360. }
  361. void Entity::removeStatusBarObserver(Entity* zSource, Framework::Text id)
  362. {
  363. cs.lock();
  364. int index = 0;
  365. for (auto observer : statusBarObservers)
  366. {
  367. if (observer.getFirst() == zSource->getId()
  368. && observer.getSecond().istGleich(id))
  369. {
  370. statusBarObservers.remove(index);
  371. break;
  372. }
  373. index++;
  374. }
  375. cs.unlock();
  376. }
  377. void Entity::addStatusBarObserver(Entity* zSource, Framework::Text id)
  378. {
  379. cs.lock();
  380. for (auto observer : statusBarObservers)
  381. {
  382. if (observer.getFirst() == zSource->getId()
  383. && observer.getSecond().istGleich(id))
  384. {
  385. cs.unlock();
  386. return;
  387. }
  388. }
  389. statusBarObservers.add(ImmutablePair<int, Text>(zSource->getId(), id));
  390. cs.unlock();
  391. }
  392. void Entity::notifyStatusBarObservers(NetworkMessage* msg)
  393. {
  394. cs.lock();
  395. int index = 0;
  396. Array<int> toDelete;
  397. for (auto observer : statusBarObservers)
  398. {
  399. Entity* e = Game::INSTANCE->zEntity(observer.getFirst());
  400. if (e)
  401. {
  402. msg->addressGui(observer.getSecond());
  403. Game::INSTANCE->sendMessage(msg->clone(), e);
  404. }
  405. else
  406. toDelete.add(index, 0);
  407. index++;
  408. }
  409. for (int i : toDelete)
  410. statusBarObservers.remove(i);
  411. cs.unlock();
  412. msg->release();
  413. }
  414. void Entity::prepareTick(const Dimension* zDimension) {}
  415. void Entity::tick(const Dimension* zDimension)
  416. {
  417. if (placeBlockCooldown > 0)
  418. {
  419. placeBlockCooldown--;
  420. }
  421. placeBlockCooldown--;
  422. if (time.isMeasuring())
  423. {
  424. time.messungEnde();
  425. if (movements.getEintragAnzahl() > 0)
  426. {
  427. MovementFrame currentFrame = movements.get(0);
  428. double seconds = time.getSekunden();
  429. while (seconds > 0)
  430. {
  431. if (currentFrame.duration <= 0)
  432. {
  433. cs.lock();
  434. movements.remove(0);
  435. cs.unlock();
  436. if (movements.getEintragAnzahl() > 0)
  437. currentFrame = movements.get(0);
  438. else
  439. break;
  440. }
  441. double t = MIN(currentFrame.duration, seconds);
  442. // TODO: add collisin detection to reduce cheating capability
  443. location += (currentFrame.targetPosition - location)
  444. * (float)(t / currentFrame.duration);
  445. currentFrame.duration -= t;
  446. seconds -= t;
  447. if (currentFrame.duration <= 0)
  448. {
  449. location = currentFrame.targetPosition;
  450. }
  451. }
  452. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  453. if (getStamina() <= getMaxStamina() - 0.0025f)
  454. {
  455. if (getThirst() > 0 && getHunger() > 0)
  456. {
  457. setStamina(getStamina() + 0.0025f);
  458. setHunger(getHunger() - 0.0005f);
  459. setThirst(getThirst() - 0.0015f);
  460. }
  461. }
  462. }
  463. else
  464. {
  465. if (getStamina() <= getMaxStamina() - 0.005f)
  466. {
  467. if (getThirst() > 0 && getHunger() > 0)
  468. {
  469. setStamina(getStamina() + 0.005f);
  470. setHunger(getHunger() - 0.001f);
  471. setThirst(getThirst() - 0.003f);
  472. }
  473. }
  474. }
  475. }
  476. time.messungStart();
  477. }
  478. void Entity::api(Framework::StreamReader* zRequest,
  479. NetworkMessage* zResponse,
  480. Entity* zSource)
  481. {
  482. char type;
  483. zRequest->lese(&type, 1);
  484. switch (type)
  485. {
  486. case 0: // request status bar state
  487. {
  488. char len;
  489. zRequest->lese(&len, 1);
  490. char* guiId = new char[(int)len + 1];
  491. zRequest->lese(guiId, len);
  492. guiId[(int)len] = 0;
  493. zResponse->addressGui(guiId);
  494. addStatusBarObserver(zSource, guiId);
  495. char* msg = new char[33];
  496. msg[0] = 0;
  497. *(float*)(msg + 1) = getMaxHP();
  498. *(float*)(msg + 5) = getCurrentHP();
  499. *(float*)(msg + 9) = getMaxStamina();
  500. *(float*)(msg + 13) = getStamina();
  501. *(float*)(msg + 17) = getMaxHunger();
  502. *(float*)(msg + 21) = getHunger();
  503. *(float*)(msg + 25) = getMaxThirst();
  504. *(float*)(msg + 29) = getThirst();
  505. zResponse->setMessage(msg, 33);
  506. delete[] guiId;
  507. break;
  508. }
  509. case 1: // remove status bar observer
  510. {
  511. char len;
  512. zRequest->lese(&len, 1);
  513. char* guiId = new char[(int)len + 1];
  514. zRequest->lese(guiId, len);
  515. guiId[(int)len] = 0;
  516. removeStatusBarObserver(zSource, guiId);
  517. delete[] guiId;
  518. break;
  519. }
  520. }
  521. }
  522. void Entity::onFall(float collisionSpeed)
  523. {
  524. if (collisionSpeed > 5)
  525. {
  526. // TODO: take damage
  527. }
  528. }
  529. void Entity::setChatSecurityLevel(int level)
  530. {
  531. chatSecurityLevel = level;
  532. }
  533. void Entity::setPosition(Framework::Vec3<float> pos)
  534. {
  535. location = pos;
  536. }
  537. void Entity::setHP(float hp)
  538. {
  539. currentHP = MIN(MAX(hp, 0), maxHP);
  540. NetworkMessage* msg = new NetworkMessage();
  541. char* message = new char[9];
  542. message[0] = 1;
  543. *(float*)(message + 1) = getMaxHP();
  544. *(float*)(message + 5) = getCurrentHP();
  545. msg->setMessage(message, 9);
  546. notifyStatusBarObservers(msg);
  547. }
  548. void Entity::setStamina(float stamina)
  549. {
  550. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  551. NetworkMessage* msg = new NetworkMessage();
  552. char* message = new char[9];
  553. message[0] = 2;
  554. *(float*)(message + 1) = getMaxStamina();
  555. *(float*)(message + 5) = getStamina();
  556. msg->setMessage(message, 9);
  557. notifyStatusBarObservers(msg);
  558. }
  559. void Entity::setHunger(float hunger)
  560. {
  561. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  562. NetworkMessage* msg = new NetworkMessage();
  563. char* message = new char[9];
  564. message[0] = 3;
  565. *(float*)(message + 1) = getMaxHunger();
  566. *(float*)(message + 5) = getHunger();
  567. msg->setMessage(message, 9);
  568. notifyStatusBarObservers(msg);
  569. }
  570. void Entity::setThirst(float thirst)
  571. {
  572. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  573. NetworkMessage* msg = new NetworkMessage();
  574. char* message = new char[9];
  575. message[0] = 4;
  576. *(float*)(message + 1) = getMaxThirst();
  577. *(float*)(message + 5) = getThirst();
  578. msg->setMessage(message, 9);
  579. notifyStatusBarObservers(msg);
  580. }
  581. float Entity::getMaxHP() const
  582. {
  583. return maxHP;
  584. }
  585. float Entity::getCurrentHP() const
  586. {
  587. return currentHP;
  588. }
  589. float Entity::getStamina() const
  590. {
  591. return stamina;
  592. }
  593. float Entity::getMaxStamina() const
  594. {
  595. return maxStamina;
  596. }
  597. float Entity::getHunger() const
  598. {
  599. return hunger;
  600. }
  601. float Entity::getMaxHunger() const
  602. {
  603. return maxHunger;
  604. }
  605. float Entity::getThirst() const
  606. {
  607. return thirst;
  608. }
  609. float Entity::getMaxThirst() const
  610. {
  611. return maxThirst;
  612. }
  613. Framework::Vec3<float> Entity::getSpeed() const
  614. {
  615. return speed;
  616. }
  617. Framework::Vec3<float> Entity::getFaceDir() const
  618. {
  619. return faceDir;
  620. }
  621. Framework::Vec3<float> Entity::getPosition() const
  622. {
  623. return location;
  624. }
  625. float Entity::getGravityMultiplier() const
  626. {
  627. return gravityMultiplier;
  628. }
  629. int Entity::getCurrentDimensionId() const
  630. {
  631. return currentDimensionId;
  632. }
  633. bool Entity::isRemoved() const
  634. {
  635. return removed;
  636. }
  637. const EntityType* Entity::zType() const
  638. {
  639. return StaticRegistry<EntityType>::INSTANCE.zElement(typeId);
  640. }
  641. const ActionTarget* Entity::zTarget() const
  642. {
  643. return target;
  644. }
  645. int Entity::getId() const
  646. {
  647. return id;
  648. }
  649. bool Entity::hasDefaultModel() const
  650. {
  651. return 1;
  652. }
  653. ModelInfo Entity::getSpecialModel() const
  654. {
  655. return ModelInfo("", "", 0);
  656. }
  657. float Entity::getMaxSpeed() const
  658. {
  659. return maxMovementSpeed;
  660. }
  661. bool Entity::isMoving() const
  662. {
  663. return movements.getEintragAnzahl() > 0;
  664. }
  665. int Entity::getChatSecurityLevel() const
  666. {
  667. return chatSecurityLevel;
  668. }