Entity.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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, 0);
  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(
  404. Game::INSTANCE->zBlockAt(
  405. Framework::Vec3<int>{px, py, pz}, dimensionId, 0))
  406. ->isInteractable(zItem))
  407. {
  408. if (!target || !target->isBlock({px, py, pz}, dir))
  409. {
  410. cs.lock();
  411. delete target;
  412. target = new ActionTarget({px, py, pz}, dir);
  413. cs.unlock();
  414. onTargetChange();
  415. }
  416. break;
  417. }
  418. // collision to neighbor of current block
  419. if (direction.x > 0)
  420. {
  421. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  422. Framework::Vec3<float> tmp = headPosition + direction * xt;
  423. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  424. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  425. && tmp.z < (float)pz + 1.f)
  426. {
  427. dir = WEST;
  428. px++;
  429. continue;
  430. }
  431. }
  432. if (direction.x < 0)
  433. {
  434. float xt = ((float)px - headPosition.x) / direction.x;
  435. Framework::Vec3<float> tmp = headPosition + direction * xt;
  436. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  437. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  438. && tmp.z < (float)pz + 1.f)
  439. {
  440. dir = EAST;
  441. px--;
  442. continue;
  443. }
  444. }
  445. if (direction.y > 0)
  446. {
  447. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  448. Framework::Vec3<float> tmp = headPosition + direction * yt;
  449. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  450. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  451. && tmp.z < (float)pz + 1.f)
  452. {
  453. dir = NORTH;
  454. py++;
  455. continue;
  456. }
  457. }
  458. if (direction.y < 0)
  459. {
  460. float yt = ((float)py - headPosition.y) / direction.y;
  461. Framework::Vec3<float> tmp = headPosition + direction * yt;
  462. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  463. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  464. && tmp.z < (float)pz + 1.f)
  465. {
  466. dir = SOUTH;
  467. py--;
  468. continue;
  469. }
  470. }
  471. if (direction.z > 0)
  472. {
  473. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  474. Framework::Vec3<float> tmp = headPosition + direction * zt;
  475. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  476. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  477. && tmp.y < (float)py + 1.f)
  478. {
  479. dir = BOTTOM;
  480. pz++;
  481. continue;
  482. }
  483. }
  484. if (direction.z < 0)
  485. {
  486. float zt = ((float)pz - headPosition.z) / direction.z;
  487. Framework::Vec3<float> tmp = headPosition + direction * zt;
  488. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  489. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  490. && tmp.y < (float)py + 1)
  491. {
  492. dir = TOP;
  493. pz--;
  494. continue;
  495. }
  496. }
  497. if (target)
  498. {
  499. cs.lock();
  500. delete target;
  501. target = 0;
  502. cs.unlock();
  503. onTargetChange();
  504. }
  505. break;
  506. }
  507. }
  508. void Entity::removeStatusBarObserver(Entity* zSource, Framework::Text id)
  509. {
  510. cs.lock();
  511. int index = 0;
  512. for (auto observer : statusBarObservers)
  513. {
  514. if (observer.getFirst() == zSource->getId()
  515. && observer.getSecond().istGleich(id))
  516. {
  517. statusBarObservers.remove(index);
  518. break;
  519. }
  520. index++;
  521. }
  522. cs.unlock();
  523. }
  524. void Entity::addStatusBarObserver(Entity* zSource, Framework::Text id)
  525. {
  526. cs.lock();
  527. for (auto observer : statusBarObservers)
  528. {
  529. if (observer.getFirst() == zSource->getId()
  530. && observer.getSecond().istGleich(id))
  531. {
  532. cs.unlock();
  533. return;
  534. }
  535. }
  536. statusBarObservers.add(
  537. Framework::ImmutablePair<int, Framework::Text>(zSource->getId(), id));
  538. cs.unlock();
  539. }
  540. void Entity::notifyStatusBarObservers(NetworkMessage* msg)
  541. {
  542. cs.lock();
  543. int index = 0;
  544. Framework::Array<int> toDelete;
  545. for (auto observer : statusBarObservers)
  546. {
  547. Entity* e = Game::INSTANCE->zEntity(observer.getFirst());
  548. if (e)
  549. {
  550. msg->addressUIElement(observer.getSecond());
  551. Game::INSTANCE->sendMessage(msg->clone(), e);
  552. }
  553. else
  554. toDelete.add(index, 0);
  555. index++;
  556. }
  557. for (int i : toDelete)
  558. statusBarObservers.remove(i);
  559. cs.unlock();
  560. msg->release();
  561. }
  562. ItemSkill* Entity::zSkill(int itemType)
  563. {
  564. for (ItemSkill* skill : skills)
  565. {
  566. if (skill->getItemTypeId() == itemType)
  567. {
  568. return skill;
  569. }
  570. }
  571. return 0;
  572. }
  573. void Entity::prepareTick(const Dimension* zDimension) {}
  574. void Entity::tick(const Dimension* zDimension)
  575. {
  576. if (placeBlockCooldown > 0)
  577. {
  578. placeBlockCooldown--;
  579. }
  580. placeBlockCooldown--;
  581. if (time.isMeasuring())
  582. {
  583. time.messungEnde();
  584. if (movements.getEintragAnzahl() > 0)
  585. {
  586. MovementFrame currentFrame = movements.get(0);
  587. double seconds = time.getSekunden();
  588. while (seconds > 0)
  589. {
  590. if (currentFrame.duration <= 0)
  591. {
  592. cs.lock();
  593. movements.remove(0);
  594. cs.unlock();
  595. if (movements.getEintragAnzahl() > 0)
  596. currentFrame = movements.get(0);
  597. else
  598. break;
  599. }
  600. double t = MIN(currentFrame.duration, seconds);
  601. // TODO: add collision detection to reduce cheating capability
  602. location += (currentFrame.targetPosition - location)
  603. * (float)(t / currentFrame.duration);
  604. currentFrame.duration -= t;
  605. seconds -= t;
  606. if (currentFrame.duration <= 0)
  607. {
  608. location = currentFrame.targetPosition;
  609. }
  610. }
  611. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  612. if (getStamina() <= getMaxStamina() - 0.0025f)
  613. {
  614. if (getThirst() > 0 && getHunger() > 0)
  615. {
  616. setStamina(getStamina() + 0.0025f);
  617. setHunger(getHunger() - 0.0005f);
  618. setThirst(getThirst() - 0.0015f);
  619. }
  620. }
  621. }
  622. else
  623. {
  624. if (getStamina() <= getMaxStamina() - 0.005f)
  625. {
  626. if (getThirst() > 0 && getHunger() > 0)
  627. {
  628. setStamina(getStamina() + 0.005f);
  629. setHunger(getHunger() - 0.001f);
  630. setThirst(getThirst() - 0.003f);
  631. }
  632. }
  633. }
  634. }
  635. time.messungStart();
  636. }
  637. void Entity::api(Framework::StreamReader* zRequest,
  638. NetworkMessage* zResponse,
  639. Entity* zSource)
  640. {
  641. char type;
  642. zRequest->lese(&type, 1);
  643. switch (type)
  644. {
  645. case 0: // request status bar state
  646. {
  647. char len;
  648. zRequest->lese(&len, 1);
  649. char* guiId = new char[(int)len + 1];
  650. zRequest->lese(guiId, len);
  651. guiId[(int)len] = 0;
  652. zResponse->addressUIElement(guiId);
  653. addStatusBarObserver(zSource, guiId);
  654. char* msg = new char[33];
  655. msg[0] = 0;
  656. *(float*)(msg + 1) = getMaxHP();
  657. *(float*)(msg + 5) = getCurrentHP();
  658. *(float*)(msg + 9) = getMaxStamina();
  659. *(float*)(msg + 13) = getStamina();
  660. *(float*)(msg + 17) = getMaxHunger();
  661. *(float*)(msg + 21) = getHunger();
  662. *(float*)(msg + 25) = getMaxThirst();
  663. *(float*)(msg + 29) = getThirst();
  664. zResponse->setMessage(msg, 33);
  665. delete[] guiId;
  666. break;
  667. }
  668. case 1: // remove status bar observer
  669. {
  670. char len;
  671. zRequest->lese(&len, 1);
  672. char* guiId = new char[(int)len + 1];
  673. zRequest->lese(guiId, len);
  674. guiId[(int)len] = 0;
  675. removeStatusBarObserver(zSource, guiId);
  676. delete[] guiId;
  677. break;
  678. }
  679. }
  680. }
  681. void Entity::onFall(float collisionSpeed)
  682. {
  683. if (collisionSpeed > 10)
  684. {
  685. setHP(getCurrentHP() - (collisionSpeed - 10.f) / 2.5f);
  686. }
  687. }
  688. void Entity::setChatSecurityLevel(int level)
  689. {
  690. chatSecurityLevel = level;
  691. }
  692. void Entity::setPosition(Framework::Vec3<float> pos)
  693. {
  694. location = pos;
  695. }
  696. void Entity::takeDamage(Entity* zSource, float damage)
  697. {
  698. // TODO: implement this
  699. }
  700. void Entity::setHP(float hp)
  701. {
  702. currentHP = MIN(MAX(hp, 0), maxHP);
  703. NetworkMessage* msg = new NetworkMessage();
  704. char* message = new char[9];
  705. message[0] = 1;
  706. *(float*)(message + 1) = getMaxHP();
  707. *(float*)(message + 5) = getCurrentHP();
  708. msg->setMessage(message, 9);
  709. notifyStatusBarObservers(msg);
  710. if (currentHP == 0)
  711. {
  712. onDeath();
  713. }
  714. }
  715. void Entity::setStamina(float stamina)
  716. {
  717. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  718. NetworkMessage* msg = new NetworkMessage();
  719. char* message = new char[9];
  720. message[0] = 2;
  721. *(float*)(message + 1) = getMaxStamina();
  722. *(float*)(message + 5) = getStamina();
  723. msg->setMessage(message, 9);
  724. notifyStatusBarObservers(msg);
  725. }
  726. void Entity::setHunger(float hunger)
  727. {
  728. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  729. NetworkMessage* msg = new NetworkMessage();
  730. char* message = new char[9];
  731. message[0] = 3;
  732. *(float*)(message + 1) = getMaxHunger();
  733. *(float*)(message + 5) = getHunger();
  734. msg->setMessage(message, 9);
  735. notifyStatusBarObservers(msg);
  736. }
  737. void Entity::setThirst(float thirst)
  738. {
  739. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  740. NetworkMessage* msg = new NetworkMessage();
  741. char* message = new char[9];
  742. message[0] = 4;
  743. *(float*)(message + 1) = getMaxThirst();
  744. *(float*)(message + 5) = getThirst();
  745. msg->setMessage(message, 9);
  746. notifyStatusBarObservers(msg);
  747. }
  748. void Entity::setGravityMultiplier(float multiplier)
  749. {
  750. gravityMultiplier = multiplier;
  751. }
  752. float Entity::getMaxHP() const
  753. {
  754. return maxHP;
  755. }
  756. float Entity::getCurrentHP() const
  757. {
  758. return currentHP;
  759. }
  760. float Entity::getStamina() const
  761. {
  762. return stamina;
  763. }
  764. float Entity::getMaxStamina() const
  765. {
  766. return maxStamina;
  767. }
  768. float Entity::getHunger() const
  769. {
  770. return hunger;
  771. }
  772. float Entity::getMaxHunger() const
  773. {
  774. return maxHunger;
  775. }
  776. float Entity::getThirst() const
  777. {
  778. return thirst;
  779. }
  780. float Entity::getMaxThirst() const
  781. {
  782. return maxThirst;
  783. }
  784. Framework::Vec3<float> Entity::getSpeed() const
  785. {
  786. return speed;
  787. }
  788. Framework::Vec3<float> Entity::getFaceDir() const
  789. {
  790. return faceDir;
  791. }
  792. Framework::Vec3<float> Entity::getPosition() const
  793. {
  794. return location;
  795. }
  796. float Entity::getGravityMultiplier() const
  797. {
  798. return gravityMultiplier;
  799. }
  800. float Entity::getJumpSpeed() const
  801. {
  802. return jumpSpeed;
  803. }
  804. void Entity::setJumpSpeed(float speed)
  805. {
  806. jumpSpeed = speed;
  807. }
  808. bool Entity::isRemoved() const
  809. {
  810. return removed;
  811. }
  812. const EntityType* Entity::zType() const
  813. {
  814. return Game::INSTANCE->zEntityType(typeId);
  815. }
  816. const ActionTarget* Entity::zTarget() const
  817. {
  818. return target;
  819. }
  820. int Entity::getId() const
  821. {
  822. return id;
  823. }
  824. bool Entity::hasDefaultModel() const
  825. {
  826. return 1;
  827. }
  828. ModelInfo* Entity::zSpecialModel() const
  829. {
  830. return 0;
  831. }
  832. float Entity::getMaxSpeed() const
  833. {
  834. return maxMovementSpeed;
  835. }
  836. bool Entity::isMoving() const
  837. {
  838. return movements.getEintragAnzahl() > 0;
  839. }
  840. int Entity::getChatSecurityLevel() const
  841. {
  842. return chatSecurityLevel;
  843. }