Entity.cpp 29 KB

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