Entity.cpp 26 KB

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