Entity.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. else
  1133. {
  1134. changed = 1;
  1135. }
  1136. }
  1137. }
  1138. if (movementFlags & MovementFlags::GROUND_CONTACT)
  1139. {
  1140. if (speed.x != 0)
  1141. {
  1142. if (speed.x > (float)seconds)
  1143. {
  1144. speed.x -= (float)seconds;
  1145. }
  1146. else if (speed.x < -(float)seconds)
  1147. {
  1148. speed.x += (float)seconds;
  1149. }
  1150. else
  1151. {
  1152. speed.x = 0;
  1153. }
  1154. }
  1155. if (speed.y != 0)
  1156. {
  1157. if (speed.y > (float)seconds)
  1158. {
  1159. speed.y -= (float)seconds;
  1160. }
  1161. else if (speed.y < -(float)seconds)
  1162. {
  1163. speed.y += (float)seconds;
  1164. }
  1165. else
  1166. {
  1167. speed.y = 0;
  1168. }
  1169. }
  1170. }
  1171. Framework::Punkt chunkCenter
  1172. = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
  1173. Chunk* zCurrentChunk = 0;
  1174. if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
  1175. {
  1176. Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
  1177. Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
  1178. zCurrentChunk
  1179. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1180. Chunk* zLastChunk
  1181. = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
  1182. if (lastDimensionId != -1)
  1183. {
  1184. if (zLastChunk)
  1185. {
  1186. zLastChunk->onEntityLeaves(
  1187. this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
  1188. }
  1189. }
  1190. if (zCurrentChunk)
  1191. {
  1192. zCurrentChunk->onEntityEnters(
  1193. this, lastDimensionId == dimensionId ? zLastChunk : 0);
  1194. }
  1195. lastDimensionId = dimensionId;
  1196. lastChunkCenter = chunkCenter;
  1197. }
  1198. if (changed)
  1199. {
  1200. if (!zCurrentChunk)
  1201. {
  1202. Dimension* currentDimension
  1203. = Game::INSTANCE->zDimension(dimensionId);
  1204. zCurrentChunk
  1205. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1206. }
  1207. if (zCurrentChunk)
  1208. {
  1209. NetworkMessage* msg = new NetworkMessage();
  1210. msg->sendEntityMovement(this, (float)seconds);
  1211. zCurrentChunk->notifyObservers(msg);
  1212. }
  1213. }
  1214. }
  1215. void Entity::api(Framework::StreamReader* zRequest,
  1216. NetworkMessage* zResponse,
  1217. Entity* zSource)
  1218. {
  1219. char type;
  1220. zRequest->lese(&type, 1);
  1221. switch (type)
  1222. {
  1223. case 0: // request status bar state
  1224. {
  1225. char len;
  1226. zRequest->lese(&len, 1);
  1227. char* guiId = new char[(int)len + 1];
  1228. zRequest->lese(guiId, len);
  1229. guiId[(int)len] = 0;
  1230. int processor;
  1231. zRequest->lese((char*)&processor, 4);
  1232. zResponse->addressUIElement(guiId, processor);
  1233. statusBarObservable.addObserver(zSource, guiId, processor);
  1234. char* msg = new char[33];
  1235. msg[0] = 0;
  1236. *(float*)(msg + 1) = getMaxHP();
  1237. *(float*)(msg + 5) = getCurrentHP();
  1238. *(float*)(msg + 9) = getMaxStamina();
  1239. *(float*)(msg + 13) = getStamina();
  1240. *(float*)(msg + 17) = getMaxHunger();
  1241. *(float*)(msg + 21) = getHunger();
  1242. *(float*)(msg + 25) = getMaxThirst();
  1243. *(float*)(msg + 29) = getThirst();
  1244. zResponse->setMessage(msg, 33);
  1245. delete[] guiId;
  1246. break;
  1247. }
  1248. case 1: // remove status bar observer
  1249. {
  1250. char len;
  1251. zRequest->lese(&len, 1);
  1252. char* guiId = new char[(int)len + 1];
  1253. zRequest->lese(guiId, len);
  1254. guiId[(int)len] = 0;
  1255. int processor;
  1256. zRequest->lese((char*)&processor, 4);
  1257. statusBarObservable.removeObserver(zSource, guiId, processor);
  1258. delete[] guiId;
  1259. break;
  1260. }
  1261. case 2: // TODO: component request
  1262. break;
  1263. }
  1264. }
  1265. void Entity::onFall(float collisionSpeed)
  1266. {
  1267. if (collisionSpeed > 20)
  1268. {
  1269. setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 20.f) / 2.5f);
  1270. }
  1271. }
  1272. Framework::XML::Element* Entity::getTargetUIML() const
  1273. {
  1274. return new Framework::XML::Element(
  1275. Framework::Text(
  1276. "<targetInfo><text id=\"type\" width=\"auto\" height=\"auto\">")
  1277. + Game::INSTANCE->zEntityType(typeId)->getName()
  1278. + "</text></targetInfo>");
  1279. }
  1280. void Entity::setChatSecurityLevel(int level)
  1281. {
  1282. chatSecurityLevel = level;
  1283. }
  1284. void Entity::setPosition(Framework::Vec3<float> pos)
  1285. {
  1286. location = pos;
  1287. }
  1288. void Entity::takeDamage(
  1289. Entity* zSource, Item* zUsedItem, ItemSkill* zUsedSkill, float damage)
  1290. {
  1291. currentHP -= damage;
  1292. if (currentHP <= 0)
  1293. {
  1294. currentHP = 0;
  1295. onDeath(zSource, zUsedItem, zUsedSkill);
  1296. }
  1297. }
  1298. void Entity::setHP(
  1299. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  1300. {
  1301. currentHP = MIN(MAX(hp, 0), maxHP);
  1302. NetworkMessage* msg = new NetworkMessage();
  1303. char* message = new char[9];
  1304. message[0] = 1;
  1305. *(float*)(message + 1) = getMaxHP();
  1306. *(float*)(message + 5) = getCurrentHP();
  1307. msg->setMessage(message, 9);
  1308. notifyStatusBarObservers(msg);
  1309. if (currentHP == 0)
  1310. {
  1311. onDeath(zActor, zUsedItem, zUsedSkill);
  1312. }
  1313. }
  1314. void Entity::setStamina(float stamina)
  1315. {
  1316. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  1317. NetworkMessage* msg = new NetworkMessage();
  1318. char* message = new char[9];
  1319. message[0] = 2;
  1320. *(float*)(message + 1) = getMaxStamina();
  1321. *(float*)(message + 5) = getStamina();
  1322. msg->setMessage(message, 9);
  1323. notifyStatusBarObservers(msg);
  1324. }
  1325. void Entity::setHunger(float hunger)
  1326. {
  1327. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  1328. NetworkMessage* msg = new NetworkMessage();
  1329. char* message = new char[9];
  1330. message[0] = 3;
  1331. *(float*)(message + 1) = getMaxHunger();
  1332. *(float*)(message + 5) = getHunger();
  1333. msg->setMessage(message, 9);
  1334. notifyStatusBarObservers(msg);
  1335. }
  1336. void Entity::setThirst(float thirst)
  1337. {
  1338. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  1339. NetworkMessage* msg = new NetworkMessage();
  1340. char* message = new char[9];
  1341. message[0] = 4;
  1342. *(float*)(message + 1) = getMaxThirst();
  1343. *(float*)(message + 5) = getThirst();
  1344. msg->setMessage(message, 9);
  1345. notifyStatusBarObservers(msg);
  1346. }
  1347. void Entity::setGravityMultiplier(float multiplier)
  1348. {
  1349. gravityMultiplier = multiplier;
  1350. }
  1351. float Entity::getMaxHP() const
  1352. {
  1353. return maxHP;
  1354. }
  1355. float Entity::getCurrentHP() const
  1356. {
  1357. return currentHP;
  1358. }
  1359. float Entity::getStamina() const
  1360. {
  1361. return stamina;
  1362. }
  1363. float Entity::getMaxStamina() const
  1364. {
  1365. return maxStamina;
  1366. }
  1367. float Entity::getHunger() const
  1368. {
  1369. return hunger;
  1370. }
  1371. float Entity::getMaxHunger() const
  1372. {
  1373. return maxHunger;
  1374. }
  1375. float Entity::getThirst() const
  1376. {
  1377. return thirst;
  1378. }
  1379. float Entity::getMaxThirst() const
  1380. {
  1381. return maxThirst;
  1382. }
  1383. Framework::Vec3<float> Entity::getSpeed() const
  1384. {
  1385. return speed;
  1386. }
  1387. Framework::Vec3<float> Entity::getFaceDir() const
  1388. {
  1389. return faceDir;
  1390. }
  1391. Framework::Vec3<float> Entity::getPosition() const
  1392. {
  1393. return location;
  1394. }
  1395. float Entity::getGravityMultiplier() const
  1396. {
  1397. return gravityMultiplier;
  1398. }
  1399. float Entity::getJumpSpeed() const
  1400. {
  1401. return jumpSpeed;
  1402. }
  1403. void Entity::setJumpSpeed(float speed)
  1404. {
  1405. jumpSpeed = speed;
  1406. }
  1407. bool Entity::isRemoved() const
  1408. {
  1409. return removed;
  1410. }
  1411. const EntityType* Entity::zType() const
  1412. {
  1413. return Game::INSTANCE->zEntityType(typeId);
  1414. }
  1415. const ActionTarget* Entity::zTarget() const
  1416. {
  1417. return target;
  1418. }
  1419. int Entity::getId() const
  1420. {
  1421. return id;
  1422. }
  1423. bool Entity::hasDefaultModel() const
  1424. {
  1425. return 1;
  1426. }
  1427. ModelInfo* Entity::zSpecialModel() const
  1428. {
  1429. return 0;
  1430. }
  1431. float Entity::getMaxSpeed() const
  1432. {
  1433. return maxMovementSpeed;
  1434. }
  1435. bool Entity::isMoving() const
  1436. {
  1437. return movementFlags > 0;
  1438. }
  1439. int Entity::getChatSecurityLevel() const
  1440. {
  1441. return chatSecurityLevel;
  1442. }
  1443. Framework::Maybe<Framework::Punkt> Entity::getLastSavedChunkCenter() const
  1444. {
  1445. return lastSavedChunkCenter;
  1446. }
  1447. void Entity::setLastSavedChunkCenter(Framework::Punkt pos)
  1448. {
  1449. lastSavedChunkCenter = Framework::Maybe<Framework::Punkt>::of(pos);
  1450. }
  1451. void Entity::setRemoved()
  1452. {
  1453. removed = true;
  1454. }
  1455. double Entity::getHitDistance(
  1456. Framework::Vec3<float> rayOrigin, Framework::Vec3<float> rayDirection) const
  1457. {
  1458. Framework::Vec3<float> rotatedRayOrigin
  1459. = Framework::Vec3<float>(rayOrigin).rotateZ(-rotation);
  1460. Framework::Vec3<float> rotatedRayDirection
  1461. = Framework::Vec3<float>(rayDirection).rotateZ(-rotation);
  1462. rotatedRayDirection.normalize();
  1463. if (rotatedRayDirection.x != 0)
  1464. {
  1465. float d;
  1466. if (rotatedRayDirection.x > 0)
  1467. {
  1468. float border = getPosition().x - boundingBox.x;
  1469. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1470. }
  1471. else if (rotatedRayDirection.x < 0)
  1472. {
  1473. float border = getPosition().x + boundingBox.x;
  1474. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1475. }
  1476. if (d > 0)
  1477. {
  1478. Framework::Vec3<float> hitPoint
  1479. = rotatedRayOrigin + rotatedRayDirection * d;
  1480. if (hitPoint.y >= getPosition().y - boundingBox.y
  1481. && hitPoint.y <= getPosition().y + boundingBox.y
  1482. && hitPoint.z >= getPosition().z - boundingBox.z
  1483. && hitPoint.z <= getPosition().z + boundingBox.z)
  1484. {
  1485. return d;
  1486. }
  1487. }
  1488. }
  1489. if (rotatedRayDirection.y != 0)
  1490. {
  1491. float d;
  1492. if (rotatedRayDirection.y > 0)
  1493. {
  1494. float border = getPosition().y - boundingBox.y;
  1495. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1496. }
  1497. else if (rotatedRayDirection.y < 0)
  1498. {
  1499. float border = getPosition().y + boundingBox.y;
  1500. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1501. }
  1502. if (d > 0)
  1503. {
  1504. Framework::Vec3<float> hitPoint
  1505. = rotatedRayOrigin + rotatedRayDirection * d;
  1506. if (hitPoint.x >= getPosition().x - boundingBox.x
  1507. && hitPoint.x <= getPosition().x + boundingBox.x
  1508. && hitPoint.z >= getPosition().z - boundingBox.z
  1509. && hitPoint.z <= getPosition().z + boundingBox.z)
  1510. {
  1511. return d;
  1512. }
  1513. }
  1514. }
  1515. if (rotatedRayDirection.z != 0)
  1516. {
  1517. float d;
  1518. if (rotatedRayDirection.z > 0)
  1519. {
  1520. float border = getPosition().z - boundingBox.z;
  1521. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1522. }
  1523. else if (rotatedRayDirection.z < 0)
  1524. {
  1525. float border = getPosition().z + boundingBox.z;
  1526. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1527. }
  1528. if (d > 0)
  1529. {
  1530. Framework::Vec3<float> hitPoint
  1531. = rotatedRayOrigin + rotatedRayDirection * d;
  1532. if (hitPoint.x >= getPosition().x - boundingBox.x
  1533. && hitPoint.x <= getPosition().x + boundingBox.x
  1534. && hitPoint.y >= getPosition().y - boundingBox.y
  1535. && hitPoint.y <= getPosition().y + boundingBox.y)
  1536. {
  1537. return d;
  1538. }
  1539. }
  1540. }
  1541. return NAN;
  1542. }
  1543. float Entity::getRotation() const
  1544. {
  1545. return rotation;
  1546. }