Entity.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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. Entity* zEntity = Game::INSTANCE->zEntity(zTarget->entityId);
  119. if (zEntity)
  120. {
  121. Framework::XML::Element* targetUIML = zEntity->getTargetUIML();
  122. Framework::Text targetUIMLText
  123. = targetUIML ? targetUIML->toString() : Framework::Text();
  124. targetUIML->release();
  125. char* message = new char[8 + targetUIMLText.getLength()];
  126. message[0] = 3;
  127. message[1] = 1;
  128. *(int*)(message + 2) = zTarget->entityId;
  129. *(short*)(message + 6) = (short)targetUIMLText.getLength();
  130. memcpy(message + 8,
  131. targetUIMLText.getText(),
  132. targetUIMLText.getLength());
  133. zMsg->setMessage(message, 8 + targetUIMLText.getLength());
  134. }
  135. else
  136. {
  137. char* message = new char[2];
  138. message[0] = 3;
  139. message[1] = 0;
  140. zMsg->setMessage(message, 2);
  141. }
  142. }
  143. else
  144. {
  145. Framework::XML::Element* targetUIML = 0;
  146. auto block
  147. = Game::INSTANCE->zBlockAt(zTarget->blockPos, dimensionId, 0);
  148. if (block.isA())
  149. {
  150. targetUIML = block.getA()->getTargetUIML();
  151. }
  152. else if (block.isB())
  153. {
  154. targetUIML
  155. = Game::INSTANCE->zBlockType(block.getB())->getTargetUIML();
  156. }
  157. Framework::Text targetUIMLText
  158. = targetUIML ? targetUIML->toString() : Framework::Text();
  159. char* message = new char[18 + targetUIMLText.getLength() + 2];
  160. message[0] = 3;
  161. message[1] = 2;
  162. *(int*)(message + 2) = zTarget->blockPos.x;
  163. *(int*)(message + 6) = zTarget->blockPos.y;
  164. *(int*)(message + 10) = zTarget->blockPos.z;
  165. *(int*)(message + 14) = zTarget->targetBlockSide;
  166. short len = (short)targetUIMLText.getLength();
  167. *(short*)(message + 18) = len;
  168. memcpy(message + 20, targetUIMLText.getText(), len);
  169. zMsg->setMessage(message, 18 + len + 2);
  170. }
  171. }
  172. else
  173. {
  174. char* message = new char[2];
  175. message[0] = 3;
  176. message[1] = 0;
  177. zMsg->setMessage(message, 2);
  178. }
  179. }
  180. void ActionTarget::save(ActionTarget* zTarget, Framework::StreamWriter* zWriter)
  181. {
  182. if (zTarget)
  183. {
  184. if (zTarget->entityId >= 0)
  185. {
  186. char b = 1;
  187. zWriter->schreibe(&b, 1);
  188. zWriter->schreibe((char*)&zTarget->entityId, 4);
  189. }
  190. else
  191. {
  192. char b = 2;
  193. zWriter->schreibe(&b, 1);
  194. zWriter->schreibe((char*)&zTarget->blockPos.x, 4);
  195. zWriter->schreibe((char*)&zTarget->blockPos.y, 4);
  196. zWriter->schreibe((char*)&zTarget->blockPos.z, 4);
  197. zWriter->schreibe((char*)&zTarget->targetBlockSide, 4);
  198. }
  199. }
  200. else
  201. {
  202. char b = 0;
  203. zWriter->schreibe(&b, 1);
  204. }
  205. }
  206. ActionTarget* ActionTarget::load(Framework::StreamReader* zReader)
  207. {
  208. char b;
  209. zReader->lese(&b, 1);
  210. if (b == 1)
  211. {
  212. int id;
  213. zReader->lese((char*)&id, 4);
  214. return new ActionTarget(id);
  215. }
  216. else if (b == 2)
  217. {
  218. Framework::Vec3<int> pos;
  219. Direction side;
  220. zReader->lese((char*)&pos.x, 4);
  221. zReader->lese((char*)&pos.y, 4);
  222. zReader->lese((char*)&pos.z, 4);
  223. zReader->lese((char*)&side, 4);
  224. return new ActionTarget(pos, side);
  225. }
  226. return 0;
  227. }
  228. Entity::Entity(
  229. int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
  230. : Inventory(location, dimensionId, true),
  231. chatSecurityLevel(0),
  232. lastChunkCenter(0, 0),
  233. lastSavedChunkCenter(Framework::Maybe<Framework::Punkt>::empty()),
  234. lastDimensionId(-1),
  235. maxMovementSpeed(0.f),
  236. speed(0, 0, 0),
  237. faceDir(1, 0, 0),
  238. target(0),
  239. typeId(typeId),
  240. removed(0),
  241. gravityMultiplier(1.f),
  242. jumpSpeed(0.f),
  243. id(entityId),
  244. placeBlockCooldown(0.0),
  245. movementFlags(0)
  246. {}
  247. void Entity::onDeath(Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill)
  248. {
  249. if (!removed)
  250. {
  251. for (DropConfig* config : zType()->getDropConfigs())
  252. {
  253. config->onObjectDestroyed(zActor, zUsedItem, zUsedSkill, this);
  254. }
  255. Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
  256. if (dim)
  257. {
  258. Chunk* chunk = dim->zChunk(lastChunkCenter);
  259. if (chunk)
  260. {
  261. chunk->onEntityLeaves(this, 0);
  262. }
  263. dim->removeEntity(id);
  264. }
  265. removed = 1;
  266. }
  267. }
  268. bool Entity::useItem(int typeId, ItemStack* zStack, bool left)
  269. {
  270. if (left)
  271. {
  272. if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
  273. {
  274. cs.lock();
  275. if (target)
  276. {
  277. ItemSkill* selected = zSkill(typeId);
  278. if (!selected)
  279. {
  280. selected = Game::INSTANCE->zItemType(typeId)
  281. ->createDefaultItemSkill();
  282. selected->setItemTypeId(typeId);
  283. if (selected) skills.add(selected);
  284. }
  285. if (!selected)
  286. {
  287. selected = zSkill(ItemTypeEnum::PLAYER_HAND);
  288. selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
  289. }
  290. bool result = target->useItemSkillOnTarget(this,
  291. selected,
  292. !zStack || zStack->getSize() > 1 ? 0
  293. : (Item*)zStack->zItem());
  294. cs.unlock();
  295. return result;
  296. }
  297. cs.unlock();
  298. }
  299. else
  300. {
  301. useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
  302. }
  303. }
  304. else
  305. {
  306. if (zStack && zStack->zItem() && zStack->zItem()->isPlaceable()
  307. && zStack->getSize() > 0)
  308. { // place item
  309. cs.lock();
  310. if (target)
  311. {
  312. if (placeBlockCooldown <= 0)
  313. {
  314. Item* item = zStack->extractFromStack();
  315. bool result = target->placeBlock(this, item);
  316. if (item->getHp() > 0)
  317. {
  318. if (!zStack->addToStack(
  319. dynamic_cast<Item*>(item->getThis())))
  320. {
  321. ItemStack* newStack = new ItemStack(item, 1);
  322. addItems(newStack, NO_DIRECTION, 0);
  323. if (newStack->getSize())
  324. {
  325. Game::INSTANCE->spawnItem(
  326. location, dimensionId, newStack);
  327. }
  328. }
  329. else
  330. {
  331. item->release();
  332. }
  333. }
  334. else
  335. {
  336. item->release();
  337. }
  338. if (result)
  339. {
  340. placeBlockCooldown = 1.0;
  341. }
  342. cs.unlock();
  343. return result;
  344. }
  345. else
  346. {
  347. cs.unlock();
  348. return 0;
  349. }
  350. }
  351. cs.unlock();
  352. }
  353. if (zStack && zStack->zItem() && zStack->zItem()->isEatable()
  354. && zStack->getSize() > 0)
  355. { // eat item
  356. if (zStack->getSize() == 1)
  357. {
  358. return ((Item*)zStack->zItem())->applyFoodEffects(this);
  359. }
  360. else
  361. {
  362. if (zStack->zItem()->canApplyFoodEffectsFully(this))
  363. {
  364. Item* item = zStack->extractFromStack();
  365. item->applyFoodEffects(this);
  366. item->release();
  367. return 1;
  368. }
  369. }
  370. }
  371. if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
  372. {
  373. cs.lock();
  374. if (target)
  375. {
  376. ItemSkill* selected = zSkill(typeId);
  377. if (!selected)
  378. {
  379. selected = Game::INSTANCE->zItemType(typeId)
  380. ->createDefaultItemSkill();
  381. selected->setItemTypeId(typeId);
  382. if (selected) skills.add(selected);
  383. }
  384. if (!selected)
  385. {
  386. selected = zSkill(ItemTypeEnum::PLAYER_HAND);
  387. selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
  388. }
  389. bool result = target->interactItemSkillOnTarget(this,
  390. selected,
  391. !zStack || zStack->getSize() > 1 ? 0
  392. : (Item*)zStack->zItem());
  393. cs.unlock();
  394. return result;
  395. }
  396. cs.unlock();
  397. }
  398. else
  399. {
  400. useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
  401. }
  402. }
  403. return 0;
  404. }
  405. void Entity::onTargetChange() {}
  406. bool Entity::interact(Item* zItem, Entity* zActor)
  407. {
  408. return false;
  409. }
  410. void Entity::calculateTarget(const Item* zItem)
  411. {
  412. Framework::Vec3<float> headPosition = location + faceOffset;
  413. int px = (int)floor(headPosition.x);
  414. int py = (int)floor(headPosition.y);
  415. int pz = (int)floor(headPosition.z);
  416. Framework::Vec3<float> direction = faceDir;
  417. direction.normalize();
  418. Direction dir = BOTTOM;
  419. bool found = false;
  420. bool changed = false;
  421. while (true)
  422. {
  423. if (getDefaultBlock(
  424. Game::INSTANCE->zBlockAt(
  425. Framework::Vec3<int>{px, py, pz}, dimensionId, 0))
  426. ->isInteractable(zItem))
  427. {
  428. found = true;
  429. if (!target || !target->isBlock({px, py, pz}, dir))
  430. {
  431. changed = true;
  432. }
  433. break;
  434. }
  435. // collision to neighbor of current block
  436. if (direction.x > 0)
  437. {
  438. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  439. Framework::Vec3<float> tmp = headPosition + direction * xt;
  440. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  441. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  442. && tmp.z < (float)pz + 1.f)
  443. {
  444. dir = WEST;
  445. px++;
  446. continue;
  447. }
  448. }
  449. if (direction.x < 0)
  450. {
  451. float xt = ((float)px - headPosition.x) / direction.x;
  452. Framework::Vec3<float> tmp = headPosition + direction * xt;
  453. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  454. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  455. && tmp.z < (float)pz + 1.f)
  456. {
  457. dir = EAST;
  458. px--;
  459. continue;
  460. }
  461. }
  462. if (direction.y > 0)
  463. {
  464. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  465. Framework::Vec3<float> tmp = headPosition + direction * yt;
  466. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  467. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  468. && tmp.z < (float)pz + 1.f)
  469. {
  470. dir = NORTH;
  471. py++;
  472. continue;
  473. }
  474. }
  475. if (direction.y < 0)
  476. {
  477. float yt = ((float)py - headPosition.y) / direction.y;
  478. Framework::Vec3<float> tmp = headPosition + direction * yt;
  479. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  480. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  481. && tmp.z < (float)pz + 1.f)
  482. {
  483. dir = SOUTH;
  484. py--;
  485. continue;
  486. }
  487. }
  488. if (direction.z > 0)
  489. {
  490. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  491. Framework::Vec3<float> tmp = headPosition + direction * zt;
  492. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  493. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  494. && tmp.y < (float)py + 1.f)
  495. {
  496. dir = BOTTOM;
  497. pz++;
  498. continue;
  499. }
  500. }
  501. if (direction.z < 0)
  502. {
  503. float zt = ((float)pz - headPosition.z) / direction.z;
  504. Framework::Vec3<float> tmp = headPosition + direction * zt;
  505. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  506. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  507. && tmp.y < (float)py + 1)
  508. {
  509. dir = TOP;
  510. pz--;
  511. continue;
  512. }
  513. }
  514. if (target)
  515. {
  516. changed = true;
  517. }
  518. break;
  519. }
  520. float distSq = Framework::Vec3<float>((float)px, (float)py, (float)pz)
  521. .abstandSq(headPosition);
  522. Entity* zte = Game::INSTANCE->zDimension(dimensionId)
  523. ->zTarget(headPosition, direction, distSq);
  524. if (zte)
  525. {
  526. if (!target || !target->isEntity(zte->getId()))
  527. {
  528. cs.lock();
  529. delete target;
  530. target = new ActionTarget(zte->getId());
  531. cs.unlock();
  532. onTargetChange();
  533. }
  534. }
  535. else if (changed)
  536. {
  537. if (target && !found)
  538. {
  539. cs.lock();
  540. delete target;
  541. target = 0;
  542. cs.unlock();
  543. onTargetChange();
  544. }
  545. else
  546. {
  547. cs.lock();
  548. delete target;
  549. target = new ActionTarget({px, py, pz}, dir);
  550. cs.unlock();
  551. onTargetChange();
  552. }
  553. }
  554. }
  555. void Entity::notifyStatusBarObservers(NetworkMessage* msg)
  556. {
  557. statusBarObservable.notifyObservers(msg);
  558. }
  559. ItemSkill* Entity::zSkill(int itemType)
  560. {
  561. for (ItemSkill* skill : skills)
  562. {
  563. if (skill->getItemTypeId() == itemType)
  564. {
  565. return skill;
  566. }
  567. }
  568. return 0;
  569. }
  570. void Entity::calcBlockCollision(int& x, int& y, int& xl, int& yl, bool** map)
  571. {
  572. Framework::Vec3<float> minXY = -boundingBox;
  573. Framework::Vec3<float> maxXY = boundingBox;
  574. Framework::Vec3<float> minXmaxY(minXY.x, maxXY.y, minXY.z);
  575. Framework::Vec3<float> maxXminY(maxXY.x, minXY.y, minXY.z);
  576. maxXY.z = minXY.z;
  577. minXY.rotateZ(rotation);
  578. maxXY.rotateZ(rotation);
  579. minXmaxY.rotateZ(rotation);
  580. maxXminY.rotateZ(rotation);
  581. minXY += location;
  582. maxXY += location;
  583. minXmaxY += location;
  584. maxXminY += location;
  585. x = (int)minXY.x;
  586. y = (int)minXY.y;
  587. if (x > (int)minXmaxY.x) x = (int)minXmaxY.x;
  588. if (x > (int)maxXminY.x) x = (int)maxXminY.x;
  589. if (x > (int)maxXY.x) x = (int)maxXY.x;
  590. if (y > (int)minXmaxY.y) y = (int)minXmaxY.y;
  591. if (y > (int)maxXminY.y) y = (int)maxXminY.y;
  592. if (y > (int)maxXY.y) y = (int)maxXY.y;
  593. xl = (int)minXY.x;
  594. if (xl < (int)minXmaxY.x) xl = (int)minXmaxY.x;
  595. if (xl < (int)maxXminY.x) xl = (int)maxXminY.x;
  596. if (xl < (int)maxXY.x) xl = (int)maxXY.x;
  597. yl = (int)minXY.y;
  598. if (yl < (int)minXmaxY.y) yl = (int)minXmaxY.y;
  599. if (yl < (int)maxXminY.y) yl = (int)maxXminY.y;
  600. if (yl < (int)maxXY.y) yl = (int)maxXY.y;
  601. xl = xl - x + 1;
  602. yl = yl - y + 1;
  603. *map = new bool[xl * yl];
  604. memset(*map, 0, xl * yl * sizeof(bool));
  605. int xi = (int)minXY.x - x;
  606. int yi = (int)minXY.y - y;
  607. (*map)[xi + yi * xl] = 1;
  608. xi = (int)maxXY.x - x;
  609. yi = (int)maxXY.y - y;
  610. (*map)[xi + yi * xl] = 1;
  611. xi = (int)minXmaxY.x - x;
  612. yi = (int)minXmaxY.y - y;
  613. (*map)[xi + yi * xl] = 1;
  614. xi = (int)maxXminY.x - x;
  615. yi = (int)maxXminY.y - y;
  616. (*map)[xi + yi * xl] = 1;
  617. auto dir = minXmaxY - minXY;
  618. float m = 0;
  619. if (abs(dir.x) >= abs(dir.y))
  620. {
  621. if (dir.x != 0 && dir.y != 0)
  622. {
  623. m = dir.y / dir.x;
  624. }
  625. float yy = minXY.y;
  626. for (int xx = (int)minXY.x; xx < (int)minXmaxY.x; xx++)
  627. {
  628. int xi = xx - x;
  629. int yi = (int)yy - y;
  630. (*map)[xi + yi * xl] = 1;
  631. yy += m;
  632. }
  633. }
  634. else
  635. {
  636. if (dir.x != 0 && dir.y != 0)
  637. {
  638. m = dir.x / dir.y;
  639. }
  640. float xx = minXY.x;
  641. for (int yy = (int)minXY.y; yy < (int)minXmaxY.y; yy++)
  642. {
  643. int xi = (int)xx - x;
  644. int yi = yy - y;
  645. (*map)[xi + yi * xl] = 1;
  646. xx += m;
  647. }
  648. }
  649. dir = maxXminY - minXY;
  650. m = 0;
  651. if (abs(dir.x) >= abs(dir.y))
  652. {
  653. if (dir.x != 0 && dir.y != 0)
  654. {
  655. m = dir.y / dir.x;
  656. }
  657. float yy = minXY.y;
  658. for (int xx = (int)minXY.x; xx < (int)maxXminY.x; xx++)
  659. {
  660. int xi = xx - x;
  661. int yi = (int)yy - y;
  662. (*map)[xi + yi * xl] = 1;
  663. yy += m;
  664. }
  665. }
  666. else
  667. {
  668. if (dir.x != 0 && dir.y != 0)
  669. {
  670. m = dir.x / dir.y;
  671. }
  672. float xx = minXY.x;
  673. for (int yy = (int)minXY.y; yy < (int)maxXminY.y; yy++)
  674. {
  675. int xi = (int)xx - x;
  676. int yi = yy - y;
  677. (*map)[xi + yi * xl] = 1;
  678. xx += m;
  679. }
  680. }
  681. dir = maxXY - maxXminY;
  682. m = 0;
  683. if (abs(dir.x) >= abs(dir.y))
  684. {
  685. if (dir.x != 0 && dir.y != 0)
  686. {
  687. m = dir.y / dir.x;
  688. }
  689. float yy = maxXminY.y;
  690. for (int xx = (int)maxXminY.x; xx < (int)maxXY.x; xx++)
  691. {
  692. int xi = xx - x;
  693. int yi = (int)yy - y;
  694. (*map)[xi + yi * xl] = 1;
  695. yy += m;
  696. }
  697. }
  698. else
  699. {
  700. if (dir.x != 0 && dir.y != 0)
  701. {
  702. m = dir.x / dir.y;
  703. }
  704. float xx = maxXminY.x;
  705. for (int yy = (int)maxXminY.y; yy < (int)maxXY.y; yy++)
  706. {
  707. int xi = (int)xx - x;
  708. int yi = yy - y;
  709. (*map)[xi + yi * xl] = 1;
  710. xx += m;
  711. }
  712. }
  713. dir = maxXY - minXmaxY;
  714. m = 0;
  715. if (abs(dir.x) >= abs(dir.y))
  716. {
  717. if (dir.x != 0 && dir.y != 0)
  718. {
  719. m = dir.y / dir.x;
  720. }
  721. float yy = minXmaxY.y;
  722. for (int xx = (int)minXmaxY.x; xx < (int)maxXY.x; xx++)
  723. {
  724. int xi = xx - x;
  725. int yi = (int)yy - y;
  726. (*map)[xi + yi * xl] = 1;
  727. yy += m;
  728. }
  729. }
  730. else
  731. {
  732. if (dir.x != 0 && dir.y != 0)
  733. {
  734. m = dir.x / dir.y;
  735. }
  736. float xx = minXmaxY.x;
  737. for (int yy = (int)minXmaxY.y; yy < (int)maxXY.y; yy++)
  738. {
  739. int xi = (int)xx - x;
  740. int yi = yy - y;
  741. (*map)[xi + yi * xl] = 1;
  742. xx += m;
  743. }
  744. }
  745. for (int yy = 0; yy < yl; yy++)
  746. {
  747. int min = xl;
  748. int max = -1;
  749. for (int xx = 0; xx < xl; xx++)
  750. {
  751. if ((*map)[xx + yy * xl])
  752. {
  753. if (xx < min) min = xx;
  754. if (xx > max) max = xx;
  755. }
  756. }
  757. for (int xx = min; xx <= max; xx++)
  758. {
  759. (*map)[xx + yy * xl] = 1;
  760. }
  761. }
  762. }
  763. bool Entity::isCollidingWithBlock(const Dimension* zDimension)
  764. {
  765. int x, y, xl, yl;
  766. bool* map;
  767. calcBlockCollision(x, y, xl, yl, &map);
  768. bool result = isCollidingWithBlock(zDimension, x, y, xl, yl, map);
  769. delete[] map;
  770. return result;
  771. }
  772. bool Entity::isCollidingWithBlock(
  773. const Dimension* zDimension, int x, int y, int xl, int yl, bool* map)
  774. {
  775. for (int yy = 0; yy < yl; yy++)
  776. {
  777. for (int xx = 0; xx < xl; xx++)
  778. {
  779. if (map[xx + yy * xl])
  780. {
  781. for (int zz = (int)(location.z - boundingBox.z);
  782. zz <= (int)(location.z + boundingBox.z);
  783. zz++)
  784. {
  785. const Block* block = zDimension->zBlockOrDefault(
  786. Framework::Vec3<int>(x + xx, y + yy, zz));
  787. if (!block->isPassable())
  788. {
  789. return true;
  790. }
  791. }
  792. }
  793. }
  794. }
  795. return false;
  796. }
  797. void Entity::prepareTick(const Dimension* zDimension, double seconds) {}
  798. void Entity::tick(const Dimension* zDimension, double seconds)
  799. {
  800. if (removed) return;
  801. if (placeBlockCooldown > 0)
  802. {
  803. placeBlockCooldown -= seconds;
  804. }
  805. bool changed = 0;
  806. float rotSpeed = 0.f;
  807. if (movementFlags & MovementFlags::ROTATE_LEFT)
  808. {
  809. rotSpeed += 3.f * (float)seconds;
  810. }
  811. if (movementFlags & MovementFlags::ROTATE_RIGHT)
  812. {
  813. rotSpeed -= 3.f * (float)seconds;
  814. }
  815. if (movementFlags & MovementFlags::ROTATE_TO_FACE)
  816. {
  817. float rot = Framework::Vec2<float>(0, -1).angle({faceDir.x, faceDir.y});
  818. rotSpeed = rot - rotation;
  819. }
  820. if (rotSpeed != 0.f)
  821. {
  822. rotation += rotSpeed;
  823. if (isCollidingWithBlock(zDimension))
  824. {
  825. rotation -= rotSpeed;
  826. }
  827. else
  828. {
  829. changed = 1;
  830. }
  831. while (rotation > 2.f * (float)PI)
  832. {
  833. rotation -= 2.f * (float)PI;
  834. }
  835. while (rotation < 0.f)
  836. {
  837. rotation += 2.f * (float)PI;
  838. }
  839. }
  840. Framework::Vec3<float> moveDir(0.f, 0.f, 0.f);
  841. if (movementFlags & MovementFlags::WALK_FORWARD)
  842. {
  843. moveDir.x += cosf(rotation - (float)PI / 2.f);
  844. moveDir.y += sinf(rotation - (float)PI / 2.f);
  845. }
  846. if (movementFlags & MovementFlags::WALK_BACKWARD)
  847. {
  848. moveDir.x -= cosf(rotation - (float)PI / 2.f);
  849. moveDir.y -= sinf(rotation - (float)PI / 2.f);
  850. }
  851. if (movementFlags & MovementFlags::WALK_LEFT)
  852. {
  853. moveDir.x += cosf(rotation + (float)PI);
  854. moveDir.y += sinf(rotation + (float)PI);
  855. }
  856. if (movementFlags & MovementFlags::WALK_RIGHT)
  857. {
  858. moveDir.x += cosf(rotation);
  859. moveDir.y += sinf(rotation);
  860. }
  861. if (movementFlags & MovementFlags::FLYING
  862. && movementFlags & MovementFlags::JUMPING)
  863. {
  864. moveDir.z += 1.f;
  865. movementFlags &= ~MovementFlags::GROUND_CONTACT;
  866. }
  867. if (movementFlags & MovementFlags::FLYING
  868. && movementFlags & MovementFlags::SNEAKING)
  869. {
  870. moveDir.z -= 1.f;
  871. }
  872. float staminaCost = 0.f;
  873. if (moveDir.x != 0.f || moveDir.y != 0.f)
  874. {
  875. moveDir.normalize();
  876. moveDir *= maxMovementSpeed * (float)seconds;
  877. if (movementFlags & MovementFlags::SPRINTING)
  878. {
  879. moveDir *= 2.f;
  880. }
  881. if (movementFlags & MovementFlags::SNEAKING
  882. && !(movementFlags & MovementFlags::FLYING))
  883. {
  884. moveDir *= 0.5f;
  885. }
  886. staminaCost = moveDir.getLength() / (maxMovementSpeed * 10);
  887. if (stamina < staminaCost)
  888. {
  889. staminaCost = stamina;
  890. moveDir *= stamina / staminaCost;
  891. }
  892. }
  893. if (!(movementFlags & MovementFlags::FLYING)
  894. && movementFlags & MovementFlags::JUMPING
  895. && movementFlags & MovementFlags::GROUND_CONTACT)
  896. {
  897. if (stamina > staminaCost + 0.02f)
  898. {
  899. staminaCost += 0.02f;
  900. speed.z += jumpSpeed;
  901. movementFlags &= ~MovementFlags::GROUND_CONTACT;
  902. }
  903. }
  904. if (!(movementFlags & MovementFlags::FLYING))
  905. {
  906. speed.z
  907. -= zDimension->getGravity() * gravityMultiplier * (float)seconds;
  908. }
  909. moveDir += speed * (float)seconds;
  910. bool staminaChanged = staminaCost != 0.f;
  911. stamina -= staminaCost;
  912. if (stamina <= maxStamina - (float)seconds)
  913. {
  914. if (getThirst() > 0 && getHunger() > 0)
  915. { // TODO: modify regen rate based on hunger/thirst
  916. stamina += (float)seconds;
  917. staminaChanged = 1;
  918. setHunger(hunger - (float)seconds / 5.f);
  919. setThirst(thirst - (float)seconds / 2.5f);
  920. }
  921. }
  922. if (staminaChanged)
  923. {
  924. setStamina(stamina); // notify observers
  925. }
  926. if (moveDir.x != 0.f || moveDir.y != 0.f || moveDir.z != 0.f)
  927. {
  928. while (abs(moveDir.x) > 1.f)
  929. {
  930. if (moveDir.x > 0.f)
  931. {
  932. location.x += 1.f;
  933. if (isCollidingWithBlock(zDimension))
  934. {
  935. moveDir.x = 0.f;
  936. speed.x = 0.f;
  937. location.x -= 1.f;
  938. }
  939. else
  940. {
  941. moveDir.x -= 1.f;
  942. changed = 1;
  943. }
  944. }
  945. else
  946. {
  947. location.x -= 1.f;
  948. if (isCollidingWithBlock(zDimension))
  949. {
  950. moveDir.x = 0.f;
  951. speed.x = 0.f;
  952. location.x += 1.f;
  953. }
  954. else
  955. {
  956. moveDir.x += 1.f;
  957. changed = 1;
  958. }
  959. }
  960. }
  961. if (moveDir.x != 0.f)
  962. {
  963. location.x += moveDir.x;
  964. if (isCollidingWithBlock(zDimension))
  965. {
  966. location.x -= moveDir.x;
  967. speed.x = 0.f;
  968. }
  969. else
  970. {
  971. changed = 1;
  972. }
  973. }
  974. while (abs(moveDir.y) > 1.f)
  975. {
  976. if (moveDir.y > 0.f)
  977. {
  978. location.y += 1.f;
  979. if (isCollidingWithBlock(zDimension))
  980. {
  981. moveDir.y = 0.f;
  982. location.y -= 1.f;
  983. speed.y = 0.f;
  984. }
  985. else
  986. {
  987. moveDir.y -= 1.f;
  988. changed = 1;
  989. }
  990. }
  991. else
  992. {
  993. location.y -= 1.f;
  994. if (isCollidingWithBlock(zDimension))
  995. {
  996. moveDir.y = 0.f;
  997. location.y += 1.f;
  998. speed.y = 0.f;
  999. }
  1000. else
  1001. {
  1002. moveDir.y += 1.f;
  1003. changed = 1;
  1004. }
  1005. }
  1006. }
  1007. if (moveDir.y != 0.f)
  1008. {
  1009. location.y += moveDir.y;
  1010. if (isCollidingWithBlock(zDimension))
  1011. {
  1012. location.y -= moveDir.y;
  1013. speed.y = 0.f;
  1014. }
  1015. else
  1016. {
  1017. changed = 1;
  1018. }
  1019. }
  1020. while (abs(moveDir.z) > 1.f)
  1021. {
  1022. if (moveDir.z > 0.f)
  1023. {
  1024. location.z += 1.f;
  1025. if (isCollidingWithBlock(zDimension))
  1026. {
  1027. moveDir.z = 0.f;
  1028. location.z -= 1.f;
  1029. speed.z = 0.f;
  1030. }
  1031. else
  1032. {
  1033. moveDir.z -= 1.f;
  1034. changed = 1;
  1035. }
  1036. }
  1037. else
  1038. {
  1039. location.z -= 1.f;
  1040. if (isCollidingWithBlock(zDimension))
  1041. {
  1042. moveDir.z = 0.f;
  1043. location.z += 1.f;
  1044. onFall(speed.z);
  1045. speed.z = 0.f;
  1046. movementFlags |= MovementFlags::GROUND_CONTACT;
  1047. }
  1048. else
  1049. {
  1050. moveDir.z += 1.f;
  1051. changed = 1;
  1052. }
  1053. }
  1054. }
  1055. if (moveDir.z != 0.f)
  1056. {
  1057. location.z += moveDir.z;
  1058. if (isCollidingWithBlock(zDimension))
  1059. {
  1060. if (speed.z < 0.f)
  1061. {
  1062. onFall(speed.z);
  1063. speed.z = 0.f;
  1064. movementFlags |= MovementFlags::GROUND_CONTACT;
  1065. }
  1066. else
  1067. {
  1068. changed = 1;
  1069. }
  1070. location.z -= moveDir.z;
  1071. }
  1072. }
  1073. }
  1074. if (movementFlags & MovementFlags::GROUND_CONTACT)
  1075. {
  1076. if (speed.x != 0)
  1077. {
  1078. if (speed.x > (float)seconds)
  1079. {
  1080. speed.x -= (float)seconds;
  1081. }
  1082. else if (speed.x < -(float)seconds)
  1083. {
  1084. speed.x += (float)seconds;
  1085. }
  1086. else
  1087. {
  1088. speed.x = 0;
  1089. }
  1090. }
  1091. if (speed.y != 0)
  1092. {
  1093. if (speed.y > (float)seconds)
  1094. {
  1095. speed.y -= (float)seconds;
  1096. }
  1097. else if (speed.y < -(float)seconds)
  1098. {
  1099. speed.y += (float)seconds;
  1100. }
  1101. else
  1102. {
  1103. speed.y = 0;
  1104. }
  1105. }
  1106. }
  1107. Framework::Punkt chunkCenter
  1108. = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
  1109. Chunk* zCurrentChunk = 0;
  1110. if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
  1111. {
  1112. Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
  1113. Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
  1114. zCurrentChunk
  1115. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1116. Chunk* zLastChunk
  1117. = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
  1118. if (lastDimensionId != -1)
  1119. {
  1120. if (zLastChunk)
  1121. {
  1122. zLastChunk->onEntityLeaves(
  1123. this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
  1124. }
  1125. }
  1126. if (zCurrentChunk)
  1127. {
  1128. zCurrentChunk->onEntityEnters(
  1129. this, lastDimensionId == dimensionId ? zLastChunk : 0);
  1130. }
  1131. lastDimensionId = dimensionId;
  1132. lastChunkCenter = chunkCenter;
  1133. }
  1134. if (changed)
  1135. {
  1136. if (!zCurrentChunk)
  1137. {
  1138. Dimension* currentDimension
  1139. = Game::INSTANCE->zDimension(dimensionId);
  1140. zCurrentChunk
  1141. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1142. }
  1143. if (zCurrentChunk)
  1144. {
  1145. NetworkMessage* msg = new NetworkMessage();
  1146. msg->sendEntityMovement(this, (float)seconds);
  1147. zCurrentChunk->notifyObservers(msg);
  1148. }
  1149. }
  1150. }
  1151. void Entity::api(Framework::StreamReader* zRequest,
  1152. NetworkMessage* zResponse,
  1153. Entity* zSource)
  1154. {
  1155. char type;
  1156. zRequest->lese(&type, 1);
  1157. switch (type)
  1158. {
  1159. case 0: // request status bar state
  1160. {
  1161. char len;
  1162. zRequest->lese(&len, 1);
  1163. char* guiId = new char[(int)len + 1];
  1164. zRequest->lese(guiId, len);
  1165. guiId[(int)len] = 0;
  1166. int processor;
  1167. zRequest->lese((char*)&processor, 4);
  1168. zResponse->addressUIElement(guiId, processor);
  1169. statusBarObservable.addObserver(zSource, guiId, processor);
  1170. char* msg = new char[33];
  1171. msg[0] = 0;
  1172. *(float*)(msg + 1) = getMaxHP();
  1173. *(float*)(msg + 5) = getCurrentHP();
  1174. *(float*)(msg + 9) = getMaxStamina();
  1175. *(float*)(msg + 13) = getStamina();
  1176. *(float*)(msg + 17) = getMaxHunger();
  1177. *(float*)(msg + 21) = getHunger();
  1178. *(float*)(msg + 25) = getMaxThirst();
  1179. *(float*)(msg + 29) = getThirst();
  1180. zResponse->setMessage(msg, 33);
  1181. delete[] guiId;
  1182. break;
  1183. }
  1184. case 1: // remove status bar observer
  1185. {
  1186. char len;
  1187. zRequest->lese(&len, 1);
  1188. char* guiId = new char[(int)len + 1];
  1189. zRequest->lese(guiId, len);
  1190. guiId[(int)len] = 0;
  1191. int processor;
  1192. zRequest->lese((char*)&processor, 4);
  1193. statusBarObservable.removeObserver(zSource, guiId, processor);
  1194. delete[] guiId;
  1195. break;
  1196. }
  1197. case 2: // TODO: component request
  1198. break;
  1199. }
  1200. }
  1201. void Entity::onFall(float collisionSpeed)
  1202. {
  1203. if (collisionSpeed > 20)
  1204. {
  1205. setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 20.f) / 2.5f);
  1206. }
  1207. }
  1208. Framework::XML::Element* Entity::getTargetUIML() const
  1209. {
  1210. return new Framework::XML::Element(
  1211. Framework::Text(
  1212. "<targetInfo><text id=\"type\" width=\"auto\" height=\"auto\">")
  1213. + Game::INSTANCE->zEntityType(typeId)->getName()
  1214. + "</text></targetInfo>");
  1215. }
  1216. void Entity::setChatSecurityLevel(int level)
  1217. {
  1218. chatSecurityLevel = level;
  1219. }
  1220. void Entity::setPosition(Framework::Vec3<float> pos)
  1221. {
  1222. location = pos;
  1223. }
  1224. void Entity::takeDamage(
  1225. Entity* zSource, Item* zUsedItem, ItemSkill* zUsedSkill, float damage)
  1226. {
  1227. currentHP -= damage;
  1228. if (currentHP <= 0)
  1229. {
  1230. currentHP = 0;
  1231. onDeath(zSource, zUsedItem, zUsedSkill);
  1232. }
  1233. }
  1234. void Entity::setHP(
  1235. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  1236. {
  1237. currentHP = MIN(MAX(hp, 0), maxHP);
  1238. NetworkMessage* msg = new NetworkMessage();
  1239. char* message = new char[9];
  1240. message[0] = 1;
  1241. *(float*)(message + 1) = getMaxHP();
  1242. *(float*)(message + 5) = getCurrentHP();
  1243. msg->setMessage(message, 9);
  1244. notifyStatusBarObservers(msg);
  1245. if (currentHP == 0)
  1246. {
  1247. onDeath(zActor, zUsedItem, zUsedSkill);
  1248. }
  1249. }
  1250. void Entity::setStamina(float stamina)
  1251. {
  1252. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  1253. NetworkMessage* msg = new NetworkMessage();
  1254. char* message = new char[9];
  1255. message[0] = 2;
  1256. *(float*)(message + 1) = getMaxStamina();
  1257. *(float*)(message + 5) = getStamina();
  1258. msg->setMessage(message, 9);
  1259. notifyStatusBarObservers(msg);
  1260. }
  1261. void Entity::setHunger(float hunger)
  1262. {
  1263. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  1264. NetworkMessage* msg = new NetworkMessage();
  1265. char* message = new char[9];
  1266. message[0] = 3;
  1267. *(float*)(message + 1) = getMaxHunger();
  1268. *(float*)(message + 5) = getHunger();
  1269. msg->setMessage(message, 9);
  1270. notifyStatusBarObservers(msg);
  1271. }
  1272. void Entity::setThirst(float thirst)
  1273. {
  1274. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  1275. NetworkMessage* msg = new NetworkMessage();
  1276. char* message = new char[9];
  1277. message[0] = 4;
  1278. *(float*)(message + 1) = getMaxThirst();
  1279. *(float*)(message + 5) = getThirst();
  1280. msg->setMessage(message, 9);
  1281. notifyStatusBarObservers(msg);
  1282. }
  1283. void Entity::setGravityMultiplier(float multiplier)
  1284. {
  1285. gravityMultiplier = multiplier;
  1286. }
  1287. float Entity::getMaxHP() const
  1288. {
  1289. return maxHP;
  1290. }
  1291. float Entity::getCurrentHP() const
  1292. {
  1293. return currentHP;
  1294. }
  1295. float Entity::getStamina() const
  1296. {
  1297. return stamina;
  1298. }
  1299. float Entity::getMaxStamina() const
  1300. {
  1301. return maxStamina;
  1302. }
  1303. float Entity::getHunger() const
  1304. {
  1305. return hunger;
  1306. }
  1307. float Entity::getMaxHunger() const
  1308. {
  1309. return maxHunger;
  1310. }
  1311. float Entity::getThirst() const
  1312. {
  1313. return thirst;
  1314. }
  1315. float Entity::getMaxThirst() const
  1316. {
  1317. return maxThirst;
  1318. }
  1319. Framework::Vec3<float> Entity::getSpeed() const
  1320. {
  1321. return speed;
  1322. }
  1323. Framework::Vec3<float> Entity::getFaceDir() const
  1324. {
  1325. return faceDir;
  1326. }
  1327. Framework::Vec3<float> Entity::getPosition() const
  1328. {
  1329. return location;
  1330. }
  1331. float Entity::getGravityMultiplier() const
  1332. {
  1333. return gravityMultiplier;
  1334. }
  1335. float Entity::getJumpSpeed() const
  1336. {
  1337. return jumpSpeed;
  1338. }
  1339. void Entity::setJumpSpeed(float speed)
  1340. {
  1341. jumpSpeed = speed;
  1342. }
  1343. bool Entity::isRemoved() const
  1344. {
  1345. return removed;
  1346. }
  1347. const EntityType* Entity::zType() const
  1348. {
  1349. return Game::INSTANCE->zEntityType(typeId);
  1350. }
  1351. const ActionTarget* Entity::zTarget() const
  1352. {
  1353. return target;
  1354. }
  1355. int Entity::getId() const
  1356. {
  1357. return id;
  1358. }
  1359. bool Entity::hasDefaultModel() const
  1360. {
  1361. return 1;
  1362. }
  1363. ModelInfo* Entity::zSpecialModel() const
  1364. {
  1365. return 0;
  1366. }
  1367. float Entity::getMaxSpeed() const
  1368. {
  1369. return maxMovementSpeed;
  1370. }
  1371. bool Entity::isMoving() const
  1372. {
  1373. return movementFlags > 0;
  1374. }
  1375. int Entity::getChatSecurityLevel() const
  1376. {
  1377. return chatSecurityLevel;
  1378. }
  1379. Framework::Maybe<Framework::Punkt> Entity::getLastSavedChunkCenter() const
  1380. {
  1381. return lastSavedChunkCenter;
  1382. }
  1383. void Entity::setLastSavedChunkCenter(Framework::Punkt pos)
  1384. {
  1385. lastSavedChunkCenter = Framework::Maybe<Framework::Punkt>::of(pos);
  1386. }
  1387. void Entity::setRemoved()
  1388. {
  1389. removed = true;
  1390. }
  1391. double Entity::getHitDistance(
  1392. Framework::Vec3<float> rayOrigin, Framework::Vec3<float> rayDirection) const
  1393. {
  1394. Framework::Vec3<float> rotatedRayOrigin
  1395. = Framework::Vec3<float>(rayOrigin).rotateZ(-rotation);
  1396. Framework::Vec3<float> rotatedRayDirection
  1397. = Framework::Vec3<float>(rayDirection).rotateZ(-rotation);
  1398. rotatedRayDirection.normalize();
  1399. if (rotatedRayDirection.x != 0)
  1400. {
  1401. float d;
  1402. if (rotatedRayDirection.x > 0)
  1403. {
  1404. float border = getPosition().x - boundingBox.x;
  1405. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1406. }
  1407. else if (rotatedRayDirection.x < 0)
  1408. {
  1409. float border = getPosition().x + boundingBox.x;
  1410. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1411. }
  1412. if (d > 0)
  1413. {
  1414. Framework::Vec3<float> hitPoint
  1415. = rotatedRayOrigin + rotatedRayDirection * d;
  1416. if (hitPoint.y >= getPosition().y - boundingBox.y
  1417. && hitPoint.y <= getPosition().y + boundingBox.y
  1418. && hitPoint.z >= getPosition().z - boundingBox.z
  1419. && hitPoint.z <= getPosition().z + boundingBox.z)
  1420. {
  1421. return d;
  1422. }
  1423. }
  1424. }
  1425. if (rotatedRayDirection.y != 0)
  1426. {
  1427. float d;
  1428. if (rotatedRayDirection.y > 0)
  1429. {
  1430. float border = getPosition().y - boundingBox.y;
  1431. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1432. }
  1433. else if (rotatedRayDirection.y < 0)
  1434. {
  1435. float border = getPosition().y + boundingBox.y;
  1436. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1437. }
  1438. if (d > 0)
  1439. {
  1440. Framework::Vec3<float> hitPoint
  1441. = rotatedRayOrigin + rotatedRayDirection * d;
  1442. if (hitPoint.x >= getPosition().x - boundingBox.x
  1443. && hitPoint.x <= getPosition().x + boundingBox.x
  1444. && hitPoint.z >= getPosition().z - boundingBox.z
  1445. && hitPoint.z <= getPosition().z + boundingBox.z)
  1446. {
  1447. return d;
  1448. }
  1449. }
  1450. }
  1451. if (rotatedRayDirection.z != 0)
  1452. {
  1453. float d;
  1454. if (rotatedRayDirection.z > 0)
  1455. {
  1456. float border = getPosition().z - boundingBox.z;
  1457. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1458. }
  1459. else if (rotatedRayDirection.z < 0)
  1460. {
  1461. float border = getPosition().z + boundingBox.z;
  1462. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1463. }
  1464. if (d > 0)
  1465. {
  1466. Framework::Vec3<float> hitPoint
  1467. = rotatedRayOrigin + rotatedRayDirection * d;
  1468. if (hitPoint.x >= getPosition().x - boundingBox.x
  1469. && hitPoint.x <= getPosition().x + boundingBox.x
  1470. && hitPoint.y >= getPosition().y - boundingBox.y
  1471. && hitPoint.y <= getPosition().y + boundingBox.y)
  1472. {
  1473. return d;
  1474. }
  1475. }
  1476. }
  1477. return NAN;
  1478. }
  1479. float Entity::getRotation() const
  1480. {
  1481. return rotation;
  1482. }