Entity.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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. Framework::Vec3<int> minXYI(
  588. (int)floor(minXY.x), (int)floor(minXY.y), (int)floor(minXY.z));
  589. Framework::Vec3<int> maxXYI(
  590. (int)floor(maxXY.x), (int)floor(maxXY.y), (int)floor(maxXY.z));
  591. Framework::Vec3<int> minXmaxYI(
  592. (int)floor(minXmaxY.x), (int)floor(minXmaxY.y), (int)floor(minXmaxY.z));
  593. Framework::Vec3<int> maxXminYI(
  594. (int)floor(maxXminY.x), (int)floor(maxXminY.y), (int)floor(maxXminY.z));
  595. x = minXYI.x;
  596. y = minXYI.y;
  597. if (x > minXmaxYI.x) x = minXmaxYI.x;
  598. if (x > maxXminYI.x) x = maxXminYI.x;
  599. if (x > maxXYI.x) x = maxXYI.x;
  600. if (y > minXmaxYI.y) y = minXmaxYI.y;
  601. if (y > maxXminYI.y) y = maxXminYI.y;
  602. if (y > maxXYI.y) y = maxXYI.y;
  603. xl = minXYI.x;
  604. if (xl < minXmaxYI.x) xl = minXmaxYI.x;
  605. if (xl < maxXminYI.x) xl = maxXminYI.x;
  606. if (xl < maxXYI.x) xl = maxXYI.x;
  607. yl = minXYI.y;
  608. if (yl < minXmaxYI.y) yl = minXmaxYI.y;
  609. if (yl < maxXminYI.y) yl = maxXminYI.y;
  610. if (yl < maxXYI.y) yl = maxXYI.y;
  611. xl = xl - x + 1;
  612. yl = yl - y + 1;
  613. if (collisionMapLength < xl * yl)
  614. {
  615. delete[] collisionMap;
  616. collisionMap = new bool[xl * yl];
  617. collisionMapLength = xl * yl;
  618. }
  619. memset(collisionMap, 0, xl * yl * sizeof(bool));
  620. int xi = minXYI.x - x;
  621. int yi = minXYI.y - y;
  622. collisionMap[xi + yi * xl] = 1;
  623. xi = maxXYI.x - x;
  624. yi = maxXYI.y - y;
  625. collisionMap[xi + yi * xl] = 1;
  626. xi = minXmaxYI.x - x;
  627. yi = minXmaxYI.y - y;
  628. collisionMap[xi + yi * xl] = 1;
  629. xi = maxXminYI.x - x;
  630. yi = maxXminYI.y - y;
  631. collisionMap[xi + yi * xl] = 1;
  632. auto dir = minXmaxY - minXY;
  633. float m = 0;
  634. if (abs(dir.x) >= abs(dir.y))
  635. {
  636. if (dir.x != 0 && dir.y != 0)
  637. {
  638. m = dir.y / dir.x;
  639. }
  640. float yy = minXY.y;
  641. for (int xx = (int)floor(minXY.x); xx < (int)floor(minXmaxY.x); xx++)
  642. {
  643. int xi = xx - x;
  644. int yi = (int)floor(yy) - y;
  645. if (xi >= xl || yi >= yl)
  646. {
  647. continue;
  648. }
  649. collisionMap[xi + yi * xl] = 1;
  650. yy += m;
  651. }
  652. }
  653. else
  654. {
  655. if (dir.x != 0 && dir.y != 0)
  656. {
  657. m = dir.x / dir.y;
  658. }
  659. float xx = minXY.x;
  660. for (int yy = (int)floor(minXY.y); yy < (int)floor(minXmaxY.y); yy++)
  661. {
  662. int xi = (int)floor(xx) - x;
  663. int yi = yy - y;
  664. if (xi >= xl || yi >= yl)
  665. {
  666. continue;
  667. }
  668. collisionMap[xi + yi * xl] = 1;
  669. xx += m;
  670. }
  671. }
  672. dir = maxXminY - minXY;
  673. m = 0;
  674. if (abs(dir.x) >= abs(dir.y))
  675. {
  676. if (dir.x != 0 && dir.y != 0)
  677. {
  678. m = dir.y / dir.x;
  679. }
  680. float yy = minXY.y;
  681. for (int xx = (int)floor(minXY.x); xx < (int)floor(maxXminY.x); xx++)
  682. {
  683. int xi = xx - x;
  684. int yi = (int)floor(yy) - y;
  685. if (xi >= xl || yi >= yl)
  686. {
  687. continue;
  688. }
  689. collisionMap[xi + yi * xl] = 1;
  690. yy += m;
  691. }
  692. }
  693. else
  694. {
  695. if (dir.x != 0 && dir.y != 0)
  696. {
  697. m = dir.x / dir.y;
  698. }
  699. float xx = minXY.x;
  700. for (int yy = (int)floor(minXY.y); yy < (int)floor(maxXminY.y); yy++)
  701. {
  702. int xi = (int)floor(xx) - x;
  703. int yi = yy - y;
  704. if (xi >= xl || yi >= yl)
  705. {
  706. continue;
  707. }
  708. collisionMap[xi + yi * xl] = 1;
  709. xx += m;
  710. }
  711. }
  712. dir = maxXY - maxXminY;
  713. m = 0;
  714. if (abs(dir.x) >= abs(dir.y))
  715. {
  716. if (dir.x != 0 && dir.y != 0)
  717. {
  718. m = dir.y / dir.x;
  719. }
  720. float yy = maxXminY.y;
  721. for (int xx = (int)floor(maxXminY.x); xx < (int)floor(maxXY.x); xx++)
  722. {
  723. int xi = xx - x;
  724. int yi = (int)floor(yy) - y;
  725. if (xi >= xl || yi >= yl)
  726. {
  727. continue;
  728. }
  729. collisionMap[xi + yi * xl] = 1;
  730. yy += m;
  731. }
  732. }
  733. else
  734. {
  735. if (dir.x != 0 && dir.y != 0)
  736. {
  737. m = dir.x / dir.y;
  738. }
  739. float xx = maxXminY.x;
  740. for (int yy = (int)floor(maxXminY.y); yy < (int)floor(maxXY.y); yy++)
  741. {
  742. int xi = (int)floor(xx) - x;
  743. int yi = yy - y;
  744. if (xi >= xl || yi >= yl)
  745. {
  746. continue;
  747. }
  748. collisionMap[xi + yi * xl] = 1;
  749. xx += m;
  750. }
  751. }
  752. dir = maxXY - minXmaxY;
  753. m = 0;
  754. if (abs(dir.x) >= abs(dir.y))
  755. {
  756. if (dir.x != 0 && dir.y != 0)
  757. {
  758. m = dir.y / dir.x;
  759. }
  760. float yy = minXmaxY.y;
  761. for (int xx = (int)floor(minXmaxY.x); xx < (int)floor(maxXY.x); xx++)
  762. {
  763. int xi = xx - x;
  764. int yi = (int)floor(yy) - y;
  765. if (xi >= xl || yi >= yl)
  766. {
  767. continue;
  768. }
  769. collisionMap[xi + yi * xl] = 1;
  770. yy += m;
  771. }
  772. }
  773. else
  774. {
  775. if (dir.x != 0 && dir.y != 0)
  776. {
  777. m = dir.x / dir.y;
  778. }
  779. float xx = minXmaxY.x;
  780. for (int yy = (int)floor(minXmaxY.y); yy < (int)floor(maxXY.y); yy++)
  781. {
  782. int xi = (int)floor(xx) - x;
  783. int yi = yy - y;
  784. if (xi >= xl || yi >= yl)
  785. {
  786. continue;
  787. }
  788. collisionMap[xi + yi * xl] = 1;
  789. xx += m;
  790. }
  791. }
  792. for (int yy = 0; yy < yl; yy++)
  793. {
  794. int min = xl;
  795. int max = -1;
  796. for (int xx = 0; xx < xl; xx++)
  797. {
  798. if (collisionMap[xx + yy * xl])
  799. {
  800. if (xx < min) min = xx;
  801. if (xx > max) max = xx;
  802. }
  803. }
  804. for (int xx = min; xx <= max; xx++)
  805. {
  806. collisionMap[xx + yy * xl] = 1;
  807. }
  808. }
  809. }
  810. bool Entity::isCollidingWithBlock(const Dimension* zDimension)
  811. {
  812. int x, y, xl, yl;
  813. calcBlockCollision(x, y, xl, yl);
  814. bool result = isCollidingWithBlock(zDimension, x, y, xl, yl);
  815. return result;
  816. }
  817. bool Entity::isCollidingWithBlock(
  818. const Dimension* zDimension, int x, int y, int xl, int yl)
  819. {
  820. for (int yy = 0; yy < yl; yy++)
  821. {
  822. for (int xx = 0; xx < xl; xx++)
  823. {
  824. if (collisionMap[xx + yy * xl])
  825. {
  826. for (int zz = (int)(location.z - boundingBox.z);
  827. zz <= (int)(location.z + boundingBox.z);
  828. zz++)
  829. {
  830. const Block* block = zDimension->zBlockOrDefault(
  831. Framework::Vec3<int>(x + xx, y + yy, zz));
  832. if (!block->isPassable())
  833. {
  834. return true;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. return false;
  841. }
  842. void Entity::prepareTick(const Dimension* zDimension, double seconds) {}
  843. void Entity::tick(const Dimension* zDimension, double seconds)
  844. {
  845. if (removed) return;
  846. if (placeBlockCooldown > 0)
  847. {
  848. placeBlockCooldown -= seconds;
  849. }
  850. bool changed = 0;
  851. float rotSpeed = 0.f;
  852. if (movementFlags & MovementFlags::ROTATE_LEFT)
  853. {
  854. rotSpeed += 3.f * (float)seconds;
  855. }
  856. if (movementFlags & MovementFlags::ROTATE_RIGHT)
  857. {
  858. rotSpeed -= 3.f * (float)seconds;
  859. }
  860. if (movementFlags & MovementFlags::ROTATE_TO_FACE)
  861. {
  862. float rot = Framework::Vec2<float>(0, -1).angle({faceDir.x, faceDir.y});
  863. rotSpeed = rot - rotation;
  864. while (rotSpeed >= 2.f * (float)PI)
  865. {
  866. rotSpeed -= 2.f * (float)PI;
  867. }
  868. while (rotSpeed <= -2.f * (float)PI)
  869. {
  870. rotSpeed += 2.f * (float)PI;
  871. }
  872. }
  873. if (rotSpeed != 0.f)
  874. {
  875. float last = rotation;
  876. rotation += rotSpeed;
  877. while (rotation >= 2.f * (float)PI)
  878. {
  879. rotation -= 2.f * (float)PI;
  880. }
  881. while (rotation <= -2.f * (float)PI)
  882. {
  883. rotation += 2.f * (float)PI;
  884. }
  885. if (last != rotation)
  886. {
  887. if (isCollidingWithBlock(zDimension))
  888. {
  889. rotation -= rotSpeed;
  890. }
  891. else
  892. {
  893. changed = 1;
  894. }
  895. }
  896. }
  897. Framework::Vec3<float> moveDir(0.f, 0.f, 0.f);
  898. if (movementFlags & MovementFlags::WALK_FORWARD)
  899. {
  900. moveDir.x += cosf(rotation - (float)PI / 2.f);
  901. moveDir.y += sinf(rotation - (float)PI / 2.f);
  902. }
  903. if (movementFlags & MovementFlags::WALK_BACKWARD)
  904. {
  905. moveDir.x -= cosf(rotation - (float)PI / 2.f);
  906. moveDir.y -= sinf(rotation - (float)PI / 2.f);
  907. }
  908. if (movementFlags & MovementFlags::WALK_LEFT)
  909. {
  910. moveDir.x += cosf(rotation + (float)PI);
  911. moveDir.y += sinf(rotation + (float)PI);
  912. }
  913. if (movementFlags & MovementFlags::WALK_RIGHT)
  914. {
  915. moveDir.x += cosf(rotation);
  916. moveDir.y += sinf(rotation);
  917. }
  918. if (movementFlags & MovementFlags::FLYING
  919. && movementFlags & MovementFlags::JUMPING)
  920. {
  921. moveDir.z += 1.f;
  922. movementFlags &= ~MovementFlags::GROUND_CONTACT;
  923. }
  924. if (movementFlags & MovementFlags::FLYING
  925. && movementFlags & MovementFlags::SNEAKING)
  926. {
  927. moveDir.z -= 1.f;
  928. }
  929. float staminaCost = 0.f;
  930. if (moveDir.x != 0.f || moveDir.y != 0.f)
  931. {
  932. moveDir.normalize();
  933. moveDir *= maxMovementSpeed * (float)seconds;
  934. if (movementFlags & MovementFlags::SPRINTING)
  935. {
  936. moveDir *= 2.f;
  937. }
  938. if (movementFlags & MovementFlags::SNEAKING
  939. && !(movementFlags & MovementFlags::FLYING))
  940. {
  941. moveDir *= 0.5f;
  942. }
  943. staminaCost = moveDir.getLength() / (maxMovementSpeed * 10);
  944. if (stamina < staminaCost)
  945. {
  946. staminaCost = stamina;
  947. moveDir *= stamina / staminaCost;
  948. }
  949. }
  950. if (!(movementFlags & MovementFlags::FLYING)
  951. && movementFlags & MovementFlags::JUMPING
  952. && movementFlags & MovementFlags::GROUND_CONTACT)
  953. {
  954. if (stamina > staminaCost + 0.02f)
  955. {
  956. staminaCost += 0.02f;
  957. speed.z += jumpSpeed;
  958. movementFlags &= ~MovementFlags::GROUND_CONTACT;
  959. }
  960. }
  961. if ((!(movementFlags & MovementFlags::FLYING)
  962. && (speed.x != 0.f || speed.y != 0.f || speed.z != 0.f
  963. || moveDir.x != 0.f || moveDir.y != 0.f || moveDir.z != 0.f))
  964. || !(movementFlags & MovementFlags::GROUND_CONTACT))
  965. {
  966. speed.z
  967. -= zDimension->getGravity() * gravityMultiplier * (float)seconds;
  968. }
  969. moveDir += speed * (float)seconds;
  970. bool staminaChanged = staminaCost != 0.f;
  971. stamina -= staminaCost;
  972. if (stamina <= maxStamina - (float)seconds)
  973. {
  974. if (getThirst() > 0 && getHunger() > 0)
  975. { // TODO: modify regen rate based on hunger/thirst
  976. stamina += (float)seconds;
  977. staminaChanged = 1;
  978. setHunger(hunger - (float)seconds / 5.f);
  979. setThirst(thirst - (float)seconds / 2.5f);
  980. }
  981. }
  982. if (staminaChanged)
  983. {
  984. setStamina(stamina); // notify observers
  985. }
  986. if (moveDir.x != 0.f || moveDir.y != 0.f || moveDir.z != 0.f)
  987. {
  988. while (abs(moveDir.x) > 1.f)
  989. {
  990. if (moveDir.x > 0.f)
  991. {
  992. location.x += 1.f;
  993. if (isCollidingWithBlock(zDimension))
  994. {
  995. moveDir.x = 0.f;
  996. speed.x = 0.f;
  997. location.x -= 1.f;
  998. }
  999. else
  1000. {
  1001. moveDir.x -= 1.f;
  1002. changed = 1;
  1003. }
  1004. }
  1005. else
  1006. {
  1007. location.x -= 1.f;
  1008. if (isCollidingWithBlock(zDimension))
  1009. {
  1010. moveDir.x = 0.f;
  1011. speed.x = 0.f;
  1012. location.x += 1.f;
  1013. }
  1014. else
  1015. {
  1016. moveDir.x += 1.f;
  1017. changed = 1;
  1018. }
  1019. }
  1020. }
  1021. if (moveDir.x != 0.f)
  1022. {
  1023. location.x += moveDir.x;
  1024. if (isCollidingWithBlock(zDimension))
  1025. {
  1026. location.x -= moveDir.x;
  1027. speed.x = 0.f;
  1028. }
  1029. else
  1030. {
  1031. changed = 1;
  1032. }
  1033. }
  1034. while (abs(moveDir.y) > 1.f)
  1035. {
  1036. if (moveDir.y > 0.f)
  1037. {
  1038. location.y += 1.f;
  1039. if (isCollidingWithBlock(zDimension))
  1040. {
  1041. moveDir.y = 0.f;
  1042. location.y -= 1.f;
  1043. speed.y = 0.f;
  1044. }
  1045. else
  1046. {
  1047. moveDir.y -= 1.f;
  1048. changed = 1;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. location.y -= 1.f;
  1054. if (isCollidingWithBlock(zDimension))
  1055. {
  1056. moveDir.y = 0.f;
  1057. location.y += 1.f;
  1058. speed.y = 0.f;
  1059. }
  1060. else
  1061. {
  1062. moveDir.y += 1.f;
  1063. changed = 1;
  1064. }
  1065. }
  1066. }
  1067. if (moveDir.y != 0.f)
  1068. {
  1069. location.y += moveDir.y;
  1070. if (isCollidingWithBlock(zDimension))
  1071. {
  1072. location.y -= moveDir.y;
  1073. speed.y = 0.f;
  1074. }
  1075. else
  1076. {
  1077. changed = 1;
  1078. }
  1079. }
  1080. while (abs(moveDir.z) > 1.f)
  1081. {
  1082. if (moveDir.z > 0.f)
  1083. {
  1084. location.z += 1.f;
  1085. if (isCollidingWithBlock(zDimension))
  1086. {
  1087. moveDir.z = 0.f;
  1088. location.z -= 1.f;
  1089. speed.z = 0.f;
  1090. }
  1091. else
  1092. {
  1093. moveDir.z -= 1.f;
  1094. changed = 1;
  1095. }
  1096. }
  1097. else
  1098. {
  1099. location.z -= 1.f;
  1100. if (isCollidingWithBlock(zDimension))
  1101. {
  1102. moveDir.z = 0.f;
  1103. location.z += 1.f;
  1104. onFall(speed.z);
  1105. speed.z = 0.f;
  1106. movementFlags |= MovementFlags::GROUND_CONTACT;
  1107. }
  1108. else
  1109. {
  1110. moveDir.z += 1.f;
  1111. changed = 1;
  1112. }
  1113. }
  1114. }
  1115. if (moveDir.z != 0.f)
  1116. {
  1117. location.z += moveDir.z;
  1118. if (isCollidingWithBlock(zDimension))
  1119. {
  1120. if (speed.z < 0.f)
  1121. {
  1122. onFall(speed.z);
  1123. speed.z = 0.f;
  1124. movementFlags |= MovementFlags::GROUND_CONTACT;
  1125. }
  1126. else
  1127. {
  1128. changed = 1;
  1129. }
  1130. location.z -= moveDir.z;
  1131. }
  1132. }
  1133. }
  1134. if (movementFlags & MovementFlags::GROUND_CONTACT)
  1135. {
  1136. if (speed.x != 0)
  1137. {
  1138. if (speed.x > (float)seconds)
  1139. {
  1140. speed.x -= (float)seconds;
  1141. }
  1142. else if (speed.x < -(float)seconds)
  1143. {
  1144. speed.x += (float)seconds;
  1145. }
  1146. else
  1147. {
  1148. speed.x = 0;
  1149. }
  1150. }
  1151. if (speed.y != 0)
  1152. {
  1153. if (speed.y > (float)seconds)
  1154. {
  1155. speed.y -= (float)seconds;
  1156. }
  1157. else if (speed.y < -(float)seconds)
  1158. {
  1159. speed.y += (float)seconds;
  1160. }
  1161. else
  1162. {
  1163. speed.y = 0;
  1164. }
  1165. }
  1166. }
  1167. Framework::Punkt chunkCenter
  1168. = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
  1169. Chunk* zCurrentChunk = 0;
  1170. if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
  1171. {
  1172. Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
  1173. Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
  1174. zCurrentChunk
  1175. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1176. Chunk* zLastChunk
  1177. = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
  1178. if (lastDimensionId != -1)
  1179. {
  1180. if (zLastChunk)
  1181. {
  1182. zLastChunk->onEntityLeaves(
  1183. this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
  1184. }
  1185. }
  1186. if (zCurrentChunk)
  1187. {
  1188. zCurrentChunk->onEntityEnters(
  1189. this, lastDimensionId == dimensionId ? zLastChunk : 0);
  1190. }
  1191. lastDimensionId = dimensionId;
  1192. lastChunkCenter = chunkCenter;
  1193. }
  1194. if (changed)
  1195. {
  1196. if (!zCurrentChunk)
  1197. {
  1198. Dimension* currentDimension
  1199. = Game::INSTANCE->zDimension(dimensionId);
  1200. zCurrentChunk
  1201. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1202. }
  1203. if (zCurrentChunk)
  1204. {
  1205. NetworkMessage* msg = new NetworkMessage();
  1206. msg->sendEntityMovement(this, (float)seconds);
  1207. zCurrentChunk->notifyObservers(msg);
  1208. }
  1209. }
  1210. }
  1211. void Entity::api(Framework::StreamReader* zRequest,
  1212. NetworkMessage* zResponse,
  1213. Entity* zSource)
  1214. {
  1215. char type;
  1216. zRequest->lese(&type, 1);
  1217. switch (type)
  1218. {
  1219. case 0: // request status bar state
  1220. {
  1221. char len;
  1222. zRequest->lese(&len, 1);
  1223. char* guiId = new char[(int)len + 1];
  1224. zRequest->lese(guiId, len);
  1225. guiId[(int)len] = 0;
  1226. int processor;
  1227. zRequest->lese((char*)&processor, 4);
  1228. zResponse->addressUIElement(guiId, processor);
  1229. statusBarObservable.addObserver(zSource, guiId, processor);
  1230. char* msg = new char[33];
  1231. msg[0] = 0;
  1232. *(float*)(msg + 1) = getMaxHP();
  1233. *(float*)(msg + 5) = getCurrentHP();
  1234. *(float*)(msg + 9) = getMaxStamina();
  1235. *(float*)(msg + 13) = getStamina();
  1236. *(float*)(msg + 17) = getMaxHunger();
  1237. *(float*)(msg + 21) = getHunger();
  1238. *(float*)(msg + 25) = getMaxThirst();
  1239. *(float*)(msg + 29) = getThirst();
  1240. zResponse->setMessage(msg, 33);
  1241. delete[] guiId;
  1242. break;
  1243. }
  1244. case 1: // remove status bar observer
  1245. {
  1246. char len;
  1247. zRequest->lese(&len, 1);
  1248. char* guiId = new char[(int)len + 1];
  1249. zRequest->lese(guiId, len);
  1250. guiId[(int)len] = 0;
  1251. int processor;
  1252. zRequest->lese((char*)&processor, 4);
  1253. statusBarObservable.removeObserver(zSource, guiId, processor);
  1254. delete[] guiId;
  1255. break;
  1256. }
  1257. case 2: // TODO: component request
  1258. break;
  1259. }
  1260. }
  1261. void Entity::onFall(float collisionSpeed)
  1262. {
  1263. if (collisionSpeed > 20)
  1264. {
  1265. setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 20.f) / 2.5f);
  1266. }
  1267. }
  1268. Framework::XML::Element* Entity::getTargetUIML() const
  1269. {
  1270. return new Framework::XML::Element(
  1271. Framework::Text(
  1272. "<targetInfo><text id=\"type\" width=\"auto\" height=\"auto\">")
  1273. + Game::INSTANCE->zEntityType(typeId)->getName()
  1274. + "</text></targetInfo>");
  1275. }
  1276. void Entity::setChatSecurityLevel(int level)
  1277. {
  1278. chatSecurityLevel = level;
  1279. }
  1280. void Entity::setPosition(Framework::Vec3<float> pos)
  1281. {
  1282. location = pos;
  1283. }
  1284. void Entity::takeDamage(
  1285. Entity* zSource, Item* zUsedItem, ItemSkill* zUsedSkill, float damage)
  1286. {
  1287. currentHP -= damage;
  1288. if (currentHP <= 0)
  1289. {
  1290. currentHP = 0;
  1291. onDeath(zSource, zUsedItem, zUsedSkill);
  1292. }
  1293. }
  1294. void Entity::setHP(
  1295. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  1296. {
  1297. currentHP = MIN(MAX(hp, 0), maxHP);
  1298. NetworkMessage* msg = new NetworkMessage();
  1299. char* message = new char[9];
  1300. message[0] = 1;
  1301. *(float*)(message + 1) = getMaxHP();
  1302. *(float*)(message + 5) = getCurrentHP();
  1303. msg->setMessage(message, 9);
  1304. notifyStatusBarObservers(msg);
  1305. if (currentHP == 0)
  1306. {
  1307. onDeath(zActor, zUsedItem, zUsedSkill);
  1308. }
  1309. }
  1310. void Entity::setStamina(float stamina)
  1311. {
  1312. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  1313. NetworkMessage* msg = new NetworkMessage();
  1314. char* message = new char[9];
  1315. message[0] = 2;
  1316. *(float*)(message + 1) = getMaxStamina();
  1317. *(float*)(message + 5) = getStamina();
  1318. msg->setMessage(message, 9);
  1319. notifyStatusBarObservers(msg);
  1320. }
  1321. void Entity::setHunger(float hunger)
  1322. {
  1323. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  1324. NetworkMessage* msg = new NetworkMessage();
  1325. char* message = new char[9];
  1326. message[0] = 3;
  1327. *(float*)(message + 1) = getMaxHunger();
  1328. *(float*)(message + 5) = getHunger();
  1329. msg->setMessage(message, 9);
  1330. notifyStatusBarObservers(msg);
  1331. }
  1332. void Entity::setThirst(float thirst)
  1333. {
  1334. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  1335. NetworkMessage* msg = new NetworkMessage();
  1336. char* message = new char[9];
  1337. message[0] = 4;
  1338. *(float*)(message + 1) = getMaxThirst();
  1339. *(float*)(message + 5) = getThirst();
  1340. msg->setMessage(message, 9);
  1341. notifyStatusBarObservers(msg);
  1342. }
  1343. void Entity::setGravityMultiplier(float multiplier)
  1344. {
  1345. gravityMultiplier = multiplier;
  1346. }
  1347. float Entity::getMaxHP() const
  1348. {
  1349. return maxHP;
  1350. }
  1351. float Entity::getCurrentHP() const
  1352. {
  1353. return currentHP;
  1354. }
  1355. float Entity::getStamina() const
  1356. {
  1357. return stamina;
  1358. }
  1359. float Entity::getMaxStamina() const
  1360. {
  1361. return maxStamina;
  1362. }
  1363. float Entity::getHunger() const
  1364. {
  1365. return hunger;
  1366. }
  1367. float Entity::getMaxHunger() const
  1368. {
  1369. return maxHunger;
  1370. }
  1371. float Entity::getThirst() const
  1372. {
  1373. return thirst;
  1374. }
  1375. float Entity::getMaxThirst() const
  1376. {
  1377. return maxThirst;
  1378. }
  1379. Framework::Vec3<float> Entity::getSpeed() const
  1380. {
  1381. return speed;
  1382. }
  1383. Framework::Vec3<float> Entity::getFaceDir() const
  1384. {
  1385. return faceDir;
  1386. }
  1387. Framework::Vec3<float> Entity::getPosition() const
  1388. {
  1389. return location;
  1390. }
  1391. float Entity::getGravityMultiplier() const
  1392. {
  1393. return gravityMultiplier;
  1394. }
  1395. float Entity::getJumpSpeed() const
  1396. {
  1397. return jumpSpeed;
  1398. }
  1399. void Entity::setJumpSpeed(float speed)
  1400. {
  1401. jumpSpeed = speed;
  1402. }
  1403. bool Entity::isRemoved() const
  1404. {
  1405. return removed;
  1406. }
  1407. const EntityType* Entity::zType() const
  1408. {
  1409. return Game::INSTANCE->zEntityType(typeId);
  1410. }
  1411. const ActionTarget* Entity::zTarget() const
  1412. {
  1413. return target;
  1414. }
  1415. int Entity::getId() const
  1416. {
  1417. return id;
  1418. }
  1419. bool Entity::hasDefaultModel() const
  1420. {
  1421. return 1;
  1422. }
  1423. ModelInfo* Entity::zSpecialModel() const
  1424. {
  1425. return 0;
  1426. }
  1427. float Entity::getMaxSpeed() const
  1428. {
  1429. return maxMovementSpeed;
  1430. }
  1431. bool Entity::isMoving() const
  1432. {
  1433. return movementFlags > 0;
  1434. }
  1435. int Entity::getChatSecurityLevel() const
  1436. {
  1437. return chatSecurityLevel;
  1438. }
  1439. Framework::Maybe<Framework::Punkt> Entity::getLastSavedChunkCenter() const
  1440. {
  1441. return lastSavedChunkCenter;
  1442. }
  1443. void Entity::setLastSavedChunkCenter(Framework::Punkt pos)
  1444. {
  1445. lastSavedChunkCenter = Framework::Maybe<Framework::Punkt>::of(pos);
  1446. }
  1447. void Entity::setRemoved()
  1448. {
  1449. removed = true;
  1450. }
  1451. double Entity::getHitDistance(
  1452. Framework::Vec3<float> rayOrigin, Framework::Vec3<float> rayDirection) const
  1453. {
  1454. Framework::Vec3<float> rotatedRayOrigin
  1455. = Framework::Vec3<float>(rayOrigin).rotateZ(-rotation);
  1456. Framework::Vec3<float> rotatedRayDirection
  1457. = Framework::Vec3<float>(rayDirection).rotateZ(-rotation);
  1458. rotatedRayDirection.normalize();
  1459. if (rotatedRayDirection.x != 0)
  1460. {
  1461. float d;
  1462. if (rotatedRayDirection.x > 0)
  1463. {
  1464. float border = getPosition().x - boundingBox.x;
  1465. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1466. }
  1467. else if (rotatedRayDirection.x < 0)
  1468. {
  1469. float border = getPosition().x + boundingBox.x;
  1470. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1471. }
  1472. if (d > 0)
  1473. {
  1474. Framework::Vec3<float> hitPoint
  1475. = rotatedRayOrigin + rotatedRayDirection * d;
  1476. if (hitPoint.y >= getPosition().y - boundingBox.y
  1477. && hitPoint.y <= getPosition().y + boundingBox.y
  1478. && hitPoint.z >= getPosition().z - boundingBox.z
  1479. && hitPoint.z <= getPosition().z + boundingBox.z)
  1480. {
  1481. return d;
  1482. }
  1483. }
  1484. }
  1485. if (rotatedRayDirection.y != 0)
  1486. {
  1487. float d;
  1488. if (rotatedRayDirection.y > 0)
  1489. {
  1490. float border = getPosition().y - boundingBox.y;
  1491. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1492. }
  1493. else if (rotatedRayDirection.y < 0)
  1494. {
  1495. float border = getPosition().y + boundingBox.y;
  1496. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1497. }
  1498. if (d > 0)
  1499. {
  1500. Framework::Vec3<float> hitPoint
  1501. = rotatedRayOrigin + rotatedRayDirection * d;
  1502. if (hitPoint.x >= getPosition().x - boundingBox.x
  1503. && hitPoint.x <= getPosition().x + boundingBox.x
  1504. && hitPoint.z >= getPosition().z - boundingBox.z
  1505. && hitPoint.z <= getPosition().z + boundingBox.z)
  1506. {
  1507. return d;
  1508. }
  1509. }
  1510. }
  1511. if (rotatedRayDirection.z != 0)
  1512. {
  1513. float d;
  1514. if (rotatedRayDirection.z > 0)
  1515. {
  1516. float border = getPosition().z - boundingBox.z;
  1517. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1518. }
  1519. else if (rotatedRayDirection.z < 0)
  1520. {
  1521. float border = getPosition().z + boundingBox.z;
  1522. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1523. }
  1524. if (d > 0)
  1525. {
  1526. Framework::Vec3<float> hitPoint
  1527. = rotatedRayOrigin + rotatedRayDirection * d;
  1528. if (hitPoint.x >= getPosition().x - boundingBox.x
  1529. && hitPoint.x <= getPosition().x + boundingBox.x
  1530. && hitPoint.y >= getPosition().y - boundingBox.y
  1531. && hitPoint.y <= getPosition().y + boundingBox.y)
  1532. {
  1533. return d;
  1534. }
  1535. }
  1536. }
  1537. return NAN;
  1538. }
  1539. float Entity::getRotation() const
  1540. {
  1541. return rotation;
  1542. }