Entity.cpp 43 KB

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