Entity.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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
  132. = Game::INSTANCE->zBlockType(block.getB())->getTargetUIML();
  133. }
  134. char* message = new char[18 + targetUIML.getLength() + 2];
  135. message[0] = 3;
  136. message[1] = 2;
  137. *(int*)(message + 2) = zTarget->blockPos.x;
  138. *(int*)(message + 6) = zTarget->blockPos.y;
  139. *(int*)(message + 10) = zTarget->blockPos.z;
  140. *(int*)(message + 14) = zTarget->targetBlockSide;
  141. short len = (short)targetUIML.getLength();
  142. *(short*)(message + 18) = len;
  143. memcpy(message + 20, targetUIML.getText(), len);
  144. zMsg->setMessage(message, 18 + len + 2);
  145. }
  146. }
  147. else
  148. {
  149. char* message = new char[2];
  150. message[0] = 3;
  151. message[1] = 0;
  152. zMsg->setMessage(message, 2);
  153. }
  154. }
  155. void ActionTarget::save(ActionTarget* zTarget, Framework::StreamWriter* zWriter)
  156. {
  157. if (zTarget)
  158. {
  159. if (zTarget->entityId >= 0)
  160. {
  161. char b = 1;
  162. zWriter->schreibe(&b, 1);
  163. zWriter->schreibe((char*)&zTarget->entityId, 4);
  164. }
  165. else
  166. {
  167. char b = 2;
  168. zWriter->schreibe(&b, 1);
  169. zWriter->schreibe((char*)&zTarget->blockPos.x, 4);
  170. zWriter->schreibe((char*)&zTarget->blockPos.y, 4);
  171. zWriter->schreibe((char*)&zTarget->blockPos.z, 4);
  172. zWriter->schreibe((char*)&zTarget->targetBlockSide, 4);
  173. }
  174. }
  175. else
  176. {
  177. char b = 0;
  178. zWriter->schreibe(&b, 1);
  179. }
  180. }
  181. ActionTarget* ActionTarget::load(Framework::StreamReader* zReader)
  182. {
  183. char b;
  184. zReader->lese(&b, 1);
  185. if (b == 1)
  186. {
  187. int id;
  188. zReader->lese((char*)&id, 4);
  189. return new ActionTarget(id);
  190. }
  191. else if (b == 2)
  192. {
  193. Framework::Vec3<int> pos;
  194. Direction side;
  195. zReader->lese((char*)&pos.x, 4);
  196. zReader->lese((char*)&pos.y, 4);
  197. zReader->lese((char*)&pos.z, 4);
  198. zReader->lese((char*)&side, 4);
  199. return new ActionTarget(pos, side);
  200. }
  201. return 0;
  202. }
  203. Entity::Entity(
  204. int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
  205. : Inventory(location, dimensionId, true),
  206. chatSecurityLevel(0),
  207. speed(0, 0, 0),
  208. faceDir(1, 0, 0),
  209. target(0),
  210. typeId(typeId),
  211. removed(0),
  212. gravityMultiplier(1.f),
  213. id(entityId),
  214. placeBlockCooldown(0)
  215. {}
  216. void Entity::onDeath()
  217. {
  218. if (!removed)
  219. {
  220. Game::INSTANCE->requestWorldUpdate(
  221. new EntityRemovedUpdate(id, dimensionId, location));
  222. removed = 1;
  223. }
  224. }
  225. bool Entity::useItem(int typeId, ItemStack* zStack, bool left)
  226. {
  227. if (left)
  228. {
  229. if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
  230. {
  231. cs.lock();
  232. if (target)
  233. {
  234. ItemSkill* selected = zSkill(typeId);
  235. if (!selected)
  236. {
  237. selected = Game::INSTANCE->zItemType(typeId)
  238. ->createDefaultItemSkill();
  239. selected->setItemTypeId(typeId);
  240. if (selected) skills.add(selected);
  241. }
  242. if (!selected)
  243. {
  244. selected = zSkill(ItemTypeEnum::PLAYER_HAND);
  245. selected->setItemTypeId(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(
  283. location, 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 = Game::INSTANCE->zItemType(typeId)
  337. ->createDefaultItemSkill();
  338. selected->setItemTypeId(typeId);
  339. if (selected) skills.add(selected);
  340. }
  341. if (!selected)
  342. {
  343. selected = zSkill(ItemTypeEnum::PLAYER_HAND);
  344. selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
  345. }
  346. bool result = target->interactItemSkillOnTarget(this,
  347. selected,
  348. !zStack || zStack->getSize() > 1 ? 0
  349. : (Item*)zStack->zItem());
  350. cs.unlock();
  351. return result;
  352. }
  353. cs.unlock();
  354. }
  355. else
  356. {
  357. useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
  358. }
  359. }
  360. return 0;
  361. }
  362. void Entity::onTargetChange() {}
  363. bool Entity::interact(Item* zItem, Entity* zActor)
  364. {
  365. return false;
  366. }
  367. void Entity::addMovementFrame(MovementFrame& frame)
  368. {
  369. cs.lock();
  370. movements.add(frame);
  371. cs.unlock();
  372. NetworkMessage* message = new NetworkMessage();
  373. message->addressEntity(this);
  374. char* msg = new char[37];
  375. msg[0] = 0;
  376. *(float*)(msg + 1) = frame.direction.x;
  377. *(float*)(msg + 5) = frame.direction.y;
  378. *(float*)(msg + 9) = frame.direction.z;
  379. *(float*)(msg + 13) = frame.targetPosition.x;
  380. *(float*)(msg + 17) = frame.targetPosition.y;
  381. *(float*)(msg + 21) = frame.targetPosition.z;
  382. *(int*)(msg + 25) = frame.movementFlags;
  383. *(double*)(msg + 29) = frame.duration;
  384. message->setMessage(msg, 37);
  385. Game::INSTANCE->broadcastMessage(message);
  386. faceDir = frame.direction;
  387. // TODO implement subscription system to notify only interested clients
  388. }
  389. void Entity::calculateTarget(Framework::Vec3<float> basePos,
  390. Framework::Vec3<float> direction,
  391. const Item* zItem)
  392. {
  393. Vec3<float> headPosition = basePos + faceOffset;
  394. int px = (int)floor(headPosition.x);
  395. int py = (int)floor(headPosition.y);
  396. int pz = (int)floor(headPosition.z);
  397. direction.normalize();
  398. Direction dir = BOTTOM;
  399. while (true)
  400. {
  401. if (getDefaultBlock(
  402. Game::INSTANCE->zBlockAt(Vec3<int>{px, py, pz}, dimensionId))
  403. ->isInteractable(zItem))
  404. {
  405. if (!target || !target->isBlock({px, py, pz}, dir))
  406. {
  407. cs.lock();
  408. delete target;
  409. target = new ActionTarget({px, py, pz}, dir);
  410. cs.unlock();
  411. onTargetChange();
  412. }
  413. break;
  414. }
  415. // collision to neighbor of current block
  416. if (direction.x > 0)
  417. {
  418. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  419. Vec3<float> tmp = headPosition + direction * xt;
  420. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  421. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  422. && tmp.z < (float)pz + 1.f)
  423. {
  424. dir = WEST;
  425. px++;
  426. continue;
  427. }
  428. }
  429. if (direction.x < 0)
  430. {
  431. float xt = ((float)px - headPosition.x) / direction.x;
  432. Vec3<float> tmp = headPosition + direction * xt;
  433. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  434. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  435. && tmp.z < (float)pz + 1.f)
  436. {
  437. dir = EAST;
  438. px--;
  439. continue;
  440. }
  441. }
  442. if (direction.y > 0)
  443. {
  444. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  445. Vec3<float> tmp = headPosition + direction * yt;
  446. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  447. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  448. && tmp.z < (float)pz + 1.f)
  449. {
  450. dir = NORTH;
  451. py++;
  452. continue;
  453. }
  454. }
  455. if (direction.y < 0)
  456. {
  457. float yt = ((float)py - headPosition.y) / direction.y;
  458. Vec3<float> tmp = headPosition + direction * yt;
  459. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  460. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  461. && tmp.z < (float)pz + 1.f)
  462. {
  463. dir = SOUTH;
  464. py--;
  465. continue;
  466. }
  467. }
  468. if (direction.z > 0)
  469. {
  470. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  471. Vec3<float> tmp = headPosition + direction * zt;
  472. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  473. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  474. && tmp.y < (float)py + 1.f)
  475. {
  476. dir = BOTTOM;
  477. pz++;
  478. continue;
  479. }
  480. }
  481. if (direction.z < 0)
  482. {
  483. float zt = ((float)pz - headPosition.z) / direction.z;
  484. Vec3<float> tmp = headPosition + direction * zt;
  485. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  486. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  487. && tmp.y < (float)py + 1)
  488. {
  489. dir = TOP;
  490. pz--;
  491. continue;
  492. }
  493. }
  494. if (target)
  495. {
  496. cs.lock();
  497. delete target;
  498. target = 0;
  499. cs.unlock();
  500. onTargetChange();
  501. }
  502. break;
  503. }
  504. }
  505. void Entity::removeStatusBarObserver(Entity* zSource, Framework::Text id)
  506. {
  507. cs.lock();
  508. int index = 0;
  509. for (auto observer : statusBarObservers)
  510. {
  511. if (observer.getFirst() == zSource->getId()
  512. && observer.getSecond().istGleich(id))
  513. {
  514. statusBarObservers.remove(index);
  515. break;
  516. }
  517. index++;
  518. }
  519. cs.unlock();
  520. }
  521. void Entity::addStatusBarObserver(Entity* zSource, Framework::Text id)
  522. {
  523. cs.lock();
  524. for (auto observer : statusBarObservers)
  525. {
  526. if (observer.getFirst() == zSource->getId()
  527. && observer.getSecond().istGleich(id))
  528. {
  529. cs.unlock();
  530. return;
  531. }
  532. }
  533. statusBarObservers.add(ImmutablePair<int, Text>(zSource->getId(), id));
  534. cs.unlock();
  535. }
  536. void Entity::notifyStatusBarObservers(NetworkMessage* msg)
  537. {
  538. cs.lock();
  539. int index = 0;
  540. Array<int> toDelete;
  541. for (auto observer : statusBarObservers)
  542. {
  543. Entity* e = Game::INSTANCE->zEntity(observer.getFirst());
  544. if (e)
  545. {
  546. msg->addressUIElement(observer.getSecond());
  547. Game::INSTANCE->sendMessage(msg->clone(), e);
  548. }
  549. else
  550. toDelete.add(index, 0);
  551. index++;
  552. }
  553. for (int i : toDelete)
  554. statusBarObservers.remove(i);
  555. cs.unlock();
  556. msg->release();
  557. }
  558. ItemSkill* Entity::zSkill(int itemType)
  559. {
  560. for (ItemSkill* skill : skills)
  561. {
  562. if (skill->getItemTypeId() == typeId)
  563. {
  564. return skill;
  565. }
  566. }
  567. return 0;
  568. }
  569. void Entity::prepareTick(const Dimension* zDimension) {}
  570. void Entity::tick(const Dimension* zDimension)
  571. {
  572. if (placeBlockCooldown > 0)
  573. {
  574. placeBlockCooldown--;
  575. }
  576. placeBlockCooldown--;
  577. if (time.isMeasuring())
  578. {
  579. time.messungEnde();
  580. if (movements.getEintragAnzahl() > 0)
  581. {
  582. MovementFrame currentFrame = movements.get(0);
  583. double seconds = time.getSekunden();
  584. while (seconds > 0)
  585. {
  586. if (currentFrame.duration <= 0)
  587. {
  588. cs.lock();
  589. movements.remove(0);
  590. cs.unlock();
  591. if (movements.getEintragAnzahl() > 0)
  592. currentFrame = movements.get(0);
  593. else
  594. break;
  595. }
  596. double t = MIN(currentFrame.duration, seconds);
  597. // TODO: add collision detection to reduce cheating capability
  598. location += (currentFrame.targetPosition - location)
  599. * (float)(t / currentFrame.duration);
  600. currentFrame.duration -= t;
  601. seconds -= t;
  602. if (currentFrame.duration <= 0)
  603. {
  604. location = currentFrame.targetPosition;
  605. }
  606. }
  607. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  608. if (getStamina() <= getMaxStamina() - 0.0025f)
  609. {
  610. if (getThirst() > 0 && getHunger() > 0)
  611. {
  612. setStamina(getStamina() + 0.0025f);
  613. setHunger(getHunger() - 0.0005f);
  614. setThirst(getThirst() - 0.0015f);
  615. }
  616. }
  617. }
  618. else
  619. {
  620. if (getStamina() <= getMaxStamina() - 0.005f)
  621. {
  622. if (getThirst() > 0 && getHunger() > 0)
  623. {
  624. setStamina(getStamina() + 0.005f);
  625. setHunger(getHunger() - 0.001f);
  626. setThirst(getThirst() - 0.003f);
  627. }
  628. }
  629. }
  630. }
  631. time.messungStart();
  632. }
  633. void Entity::api(Framework::StreamReader* zRequest,
  634. NetworkMessage* zResponse,
  635. Entity* zSource)
  636. {
  637. char type;
  638. zRequest->lese(&type, 1);
  639. switch (type)
  640. {
  641. case 0: // request status bar state
  642. {
  643. char len;
  644. zRequest->lese(&len, 1);
  645. char* guiId = new char[(int)len + 1];
  646. zRequest->lese(guiId, len);
  647. guiId[(int)len] = 0;
  648. zResponse->addressUIElement(guiId);
  649. addStatusBarObserver(zSource, guiId);
  650. char* msg = new char[33];
  651. msg[0] = 0;
  652. *(float*)(msg + 1) = getMaxHP();
  653. *(float*)(msg + 5) = getCurrentHP();
  654. *(float*)(msg + 9) = getMaxStamina();
  655. *(float*)(msg + 13) = getStamina();
  656. *(float*)(msg + 17) = getMaxHunger();
  657. *(float*)(msg + 21) = getHunger();
  658. *(float*)(msg + 25) = getMaxThirst();
  659. *(float*)(msg + 29) = getThirst();
  660. zResponse->setMessage(msg, 33);
  661. delete[] guiId;
  662. break;
  663. }
  664. case 1: // remove status bar observer
  665. {
  666. char len;
  667. zRequest->lese(&len, 1);
  668. char* guiId = new char[(int)len + 1];
  669. zRequest->lese(guiId, len);
  670. guiId[(int)len] = 0;
  671. removeStatusBarObserver(zSource, guiId);
  672. delete[] guiId;
  673. break;
  674. }
  675. }
  676. }
  677. void Entity::onFall(float collisionSpeed)
  678. {
  679. if (collisionSpeed > 5)
  680. {
  681. // TODO: take damage
  682. }
  683. }
  684. void Entity::setChatSecurityLevel(int level)
  685. {
  686. chatSecurityLevel = level;
  687. }
  688. void Entity::setPosition(Framework::Vec3<float> pos)
  689. {
  690. location = pos;
  691. }
  692. void Entity::setHP(float hp)
  693. {
  694. currentHP = MIN(MAX(hp, 0), maxHP);
  695. NetworkMessage* msg = new NetworkMessage();
  696. char* message = new char[9];
  697. message[0] = 1;
  698. *(float*)(message + 1) = getMaxHP();
  699. *(float*)(message + 5) = getCurrentHP();
  700. msg->setMessage(message, 9);
  701. notifyStatusBarObservers(msg);
  702. }
  703. void Entity::setStamina(float stamina)
  704. {
  705. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  706. NetworkMessage* msg = new NetworkMessage();
  707. char* message = new char[9];
  708. message[0] = 2;
  709. *(float*)(message + 1) = getMaxStamina();
  710. *(float*)(message + 5) = getStamina();
  711. msg->setMessage(message, 9);
  712. notifyStatusBarObservers(msg);
  713. }
  714. void Entity::setHunger(float hunger)
  715. {
  716. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  717. NetworkMessage* msg = new NetworkMessage();
  718. char* message = new char[9];
  719. message[0] = 3;
  720. *(float*)(message + 1) = getMaxHunger();
  721. *(float*)(message + 5) = getHunger();
  722. msg->setMessage(message, 9);
  723. notifyStatusBarObservers(msg);
  724. }
  725. void Entity::setThirst(float thirst)
  726. {
  727. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  728. NetworkMessage* msg = new NetworkMessage();
  729. char* message = new char[9];
  730. message[0] = 4;
  731. *(float*)(message + 1) = getMaxThirst();
  732. *(float*)(message + 5) = getThirst();
  733. msg->setMessage(message, 9);
  734. notifyStatusBarObservers(msg);
  735. }
  736. float Entity::getMaxHP() const
  737. {
  738. return maxHP;
  739. }
  740. float Entity::getCurrentHP() const
  741. {
  742. return currentHP;
  743. }
  744. float Entity::getStamina() const
  745. {
  746. return stamina;
  747. }
  748. float Entity::getMaxStamina() const
  749. {
  750. return maxStamina;
  751. }
  752. float Entity::getHunger() const
  753. {
  754. return hunger;
  755. }
  756. float Entity::getMaxHunger() const
  757. {
  758. return maxHunger;
  759. }
  760. float Entity::getThirst() const
  761. {
  762. return thirst;
  763. }
  764. float Entity::getMaxThirst() const
  765. {
  766. return maxThirst;
  767. }
  768. Framework::Vec3<float> Entity::getSpeed() const
  769. {
  770. return speed;
  771. }
  772. Framework::Vec3<float> Entity::getFaceDir() const
  773. {
  774. return faceDir;
  775. }
  776. Framework::Vec3<float> Entity::getPosition() const
  777. {
  778. return location;
  779. }
  780. float Entity::getGravityMultiplier() const
  781. {
  782. return gravityMultiplier;
  783. }
  784. bool Entity::isRemoved() const
  785. {
  786. return removed;
  787. }
  788. const EntityType* Entity::zType() const
  789. {
  790. return Game::INSTANCE->zEntityType(typeId);
  791. }
  792. const ActionTarget* Entity::zTarget() const
  793. {
  794. return target;
  795. }
  796. int Entity::getId() const
  797. {
  798. return id;
  799. }
  800. bool Entity::hasDefaultModel() const
  801. {
  802. return 1;
  803. }
  804. ModelInfo* Entity::zSpecialModel() const
  805. {
  806. return 0;
  807. }
  808. float Entity::getMaxSpeed() const
  809. {
  810. return maxMovementSpeed;
  811. }
  812. bool Entity::isMoving() const
  813. {
  814. return movements.getEintragAnzahl() > 0;
  815. }
  816. int Entity::getChatSecurityLevel() const
  817. {
  818. return chatSecurityLevel;
  819. }