Entity.cpp 23 KB

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