Entity.cpp 45 KB

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