Entity.cpp 24 KB

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