Entity.cpp 26 KB

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