Entity.cpp 26 KB

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