Entity.cpp 24 KB

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