Entity.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. bool collided = isCollidingWithBlock(zDimension);
  1006. while (abs(moveDir.x) > 1.f)
  1007. {
  1008. if (moveDir.x > 0.f)
  1009. {
  1010. location.x += 1.f;
  1011. if (!collided && isCollidingWithBlock(zDimension))
  1012. {
  1013. moveDir.x = 0.f;
  1014. speed.x = 0.f;
  1015. location.x -= 1.f;
  1016. }
  1017. else
  1018. {
  1019. moveDir.x -= 1.f;
  1020. changed = 1;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. location.x -= 1.f;
  1026. if (!collided && isCollidingWithBlock(zDimension))
  1027. {
  1028. moveDir.x = 0.f;
  1029. speed.x = 0.f;
  1030. location.x += 1.f;
  1031. }
  1032. else
  1033. {
  1034. moveDir.x += 1.f;
  1035. changed = 1;
  1036. }
  1037. }
  1038. }
  1039. if (moveDir.x != 0.f)
  1040. {
  1041. location.x += moveDir.x;
  1042. if (!collided && isCollidingWithBlock(zDimension))
  1043. {
  1044. location.x -= moveDir.x;
  1045. speed.x = 0.f;
  1046. }
  1047. else
  1048. {
  1049. changed = 1;
  1050. }
  1051. }
  1052. while (abs(moveDir.y) > 1.f)
  1053. {
  1054. if (moveDir.y > 0.f)
  1055. {
  1056. location.y += 1.f;
  1057. if (!collided && isCollidingWithBlock(zDimension))
  1058. {
  1059. moveDir.y = 0.f;
  1060. location.y -= 1.f;
  1061. speed.y = 0.f;
  1062. }
  1063. else
  1064. {
  1065. moveDir.y -= 1.f;
  1066. changed = 1;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. location.y -= 1.f;
  1072. if (!collided && isCollidingWithBlock(zDimension))
  1073. {
  1074. moveDir.y = 0.f;
  1075. location.y += 1.f;
  1076. speed.y = 0.f;
  1077. }
  1078. else
  1079. {
  1080. moveDir.y += 1.f;
  1081. changed = 1;
  1082. }
  1083. }
  1084. }
  1085. if (moveDir.y != 0.f)
  1086. {
  1087. location.y += moveDir.y;
  1088. if (!collided && isCollidingWithBlock(zDimension))
  1089. {
  1090. location.y -= moveDir.y;
  1091. speed.y = 0.f;
  1092. }
  1093. else
  1094. {
  1095. changed = 1;
  1096. }
  1097. }
  1098. while (abs(moveDir.z) > 1.f)
  1099. {
  1100. if (moveDir.z > 0.f)
  1101. {
  1102. location.z += 1.f;
  1103. if (isCollidingWithBlock(zDimension))
  1104. {
  1105. moveDir.z = 0.f;
  1106. location.z -= 1.f;
  1107. speed.z = 0.f;
  1108. }
  1109. else
  1110. {
  1111. moveDir.z -= 1.f;
  1112. changed = 1;
  1113. }
  1114. }
  1115. else
  1116. {
  1117. location.z -= 1.f;
  1118. if (isCollidingWithBlock(zDimension))
  1119. {
  1120. moveDir.z = 0.f;
  1121. location.z += 1.f;
  1122. onFall(speed.z);
  1123. speed.z = 0.f;
  1124. movementFlags |= MovementFlags::GROUND_CONTACT;
  1125. }
  1126. else
  1127. {
  1128. moveDir.z += 1.f;
  1129. changed = 1;
  1130. }
  1131. }
  1132. }
  1133. if (moveDir.z != 0.f)
  1134. {
  1135. location.z += moveDir.z;
  1136. if (isCollidingWithBlock(zDimension))
  1137. {
  1138. if (speed.z < 0.f)
  1139. {
  1140. onFall(speed.z);
  1141. speed.z = 0.f;
  1142. movementFlags |= MovementFlags::GROUND_CONTACT;
  1143. }
  1144. else
  1145. {
  1146. changed = 1;
  1147. }
  1148. location.z -= moveDir.z;
  1149. }
  1150. else
  1151. {
  1152. changed = 1;
  1153. }
  1154. }
  1155. }
  1156. if (movementFlags & MovementFlags::GROUND_CONTACT)
  1157. {
  1158. if (speed.x != 0)
  1159. {
  1160. if (speed.x > (float)seconds)
  1161. {
  1162. speed.x -= (float)seconds;
  1163. }
  1164. else if (speed.x < -(float)seconds)
  1165. {
  1166. speed.x += (float)seconds;
  1167. }
  1168. else
  1169. {
  1170. speed.x = 0;
  1171. }
  1172. }
  1173. if (speed.y != 0)
  1174. {
  1175. if (speed.y > (float)seconds)
  1176. {
  1177. speed.y -= (float)seconds;
  1178. }
  1179. else if (speed.y < -(float)seconds)
  1180. {
  1181. speed.y += (float)seconds;
  1182. }
  1183. else
  1184. {
  1185. speed.y = 0;
  1186. }
  1187. }
  1188. }
  1189. Framework::Punkt chunkCenter
  1190. = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
  1191. Chunk* zCurrentChunk = 0;
  1192. if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
  1193. {
  1194. Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
  1195. Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
  1196. zCurrentChunk
  1197. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1198. Chunk* zLastChunk
  1199. = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
  1200. if (lastDimensionId != -1)
  1201. {
  1202. if (zLastChunk)
  1203. {
  1204. zLastChunk->onEntityLeaves(
  1205. this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
  1206. }
  1207. }
  1208. if (zCurrentChunk)
  1209. {
  1210. zCurrentChunk->onEntityEnters(
  1211. this, lastDimensionId == dimensionId ? zLastChunk : 0);
  1212. }
  1213. lastDimensionId = dimensionId;
  1214. lastChunkCenter = chunkCenter;
  1215. }
  1216. if (changed)
  1217. {
  1218. if (!zCurrentChunk)
  1219. {
  1220. Dimension* currentDimension
  1221. = Game::INSTANCE->zDimension(dimensionId);
  1222. zCurrentChunk
  1223. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  1224. }
  1225. if (zCurrentChunk)
  1226. {
  1227. NetworkMessage* msg = new NetworkMessage();
  1228. msg->sendEntityMovement(this, (float)seconds);
  1229. zCurrentChunk->notifyObservers(msg);
  1230. }
  1231. }
  1232. }
  1233. void Entity::api(Framework::StreamReader* zRequest,
  1234. NetworkMessage* zResponse,
  1235. Entity* zSource)
  1236. {
  1237. char type;
  1238. zRequest->lese(&type, 1);
  1239. switch (type)
  1240. {
  1241. case 0: // request status bar state
  1242. {
  1243. char len;
  1244. zRequest->lese(&len, 1);
  1245. char* guiId = new char[(int)len + 1];
  1246. zRequest->lese(guiId, len);
  1247. guiId[(int)len] = 0;
  1248. int processor;
  1249. zRequest->lese((char*)&processor, 4);
  1250. zResponse->addressUIElement(guiId, processor);
  1251. statusBarObservable.addObserver(zSource, guiId, processor);
  1252. char* msg = new char[33];
  1253. msg[0] = 0;
  1254. *(float*)(msg + 1) = getMaxHP();
  1255. *(float*)(msg + 5) = getCurrentHP();
  1256. *(float*)(msg + 9) = getMaxStamina();
  1257. *(float*)(msg + 13) = getStamina();
  1258. *(float*)(msg + 17) = getMaxHunger();
  1259. *(float*)(msg + 21) = getHunger();
  1260. *(float*)(msg + 25) = getMaxThirst();
  1261. *(float*)(msg + 29) = getThirst();
  1262. zResponse->setMessage(msg, 33);
  1263. delete[] guiId;
  1264. break;
  1265. }
  1266. case 1: // remove status bar observer
  1267. {
  1268. char len;
  1269. zRequest->lese(&len, 1);
  1270. char* guiId = new char[(int)len + 1];
  1271. zRequest->lese(guiId, len);
  1272. guiId[(int)len] = 0;
  1273. int processor;
  1274. zRequest->lese((char*)&processor, 4);
  1275. statusBarObservable.removeObserver(zSource, guiId, processor);
  1276. delete[] guiId;
  1277. break;
  1278. }
  1279. case 2: // TODO: component request
  1280. break;
  1281. }
  1282. }
  1283. void Entity::onFall(float collisionSpeed)
  1284. {
  1285. if (collisionSpeed > 20)
  1286. {
  1287. setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 20.f) / 2.5f);
  1288. }
  1289. }
  1290. Framework::XML::Element* Entity::getTargetUIML() const
  1291. {
  1292. return UIMLBuilder::createContainer()
  1293. ->addChild(UIMLBuilder::createTextAuto(
  1294. Game::INSTANCE->zEntityType(typeId)->getName())
  1295. ->setID("type")
  1296. ->build())
  1297. ->build();
  1298. }
  1299. void Entity::setChatSecurityLevel(int level)
  1300. {
  1301. chatSecurityLevel = level;
  1302. }
  1303. void Entity::setPosition(Framework::Vec3<float> pos)
  1304. {
  1305. location = pos;
  1306. }
  1307. void Entity::takeDamage(
  1308. Entity* zSource, Item* zUsedItem, ItemSkill* zUsedSkill, float damage)
  1309. {
  1310. currentHP -= damage;
  1311. if (currentHP <= 0)
  1312. {
  1313. currentHP = 0;
  1314. onDeath(zSource, zUsedItem, zUsedSkill);
  1315. }
  1316. }
  1317. void Entity::setHP(
  1318. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  1319. {
  1320. currentHP = MIN(MAX(hp, 0), maxHP);
  1321. NetworkMessage* msg = new NetworkMessage();
  1322. char* message = new char[9];
  1323. message[0] = 1;
  1324. *(float*)(message + 1) = getMaxHP();
  1325. *(float*)(message + 5) = getCurrentHP();
  1326. msg->setMessage(message, 9);
  1327. notifyStatusBarObservers(msg);
  1328. if (currentHP == 0)
  1329. {
  1330. onDeath(zActor, zUsedItem, zUsedSkill);
  1331. }
  1332. }
  1333. void Entity::setStamina(float stamina)
  1334. {
  1335. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  1336. NetworkMessage* msg = new NetworkMessage();
  1337. char* message = new char[9];
  1338. message[0] = 2;
  1339. *(float*)(message + 1) = getMaxStamina();
  1340. *(float*)(message + 5) = getStamina();
  1341. msg->setMessage(message, 9);
  1342. notifyStatusBarObservers(msg);
  1343. }
  1344. void Entity::setHunger(float hunger)
  1345. {
  1346. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  1347. NetworkMessage* msg = new NetworkMessage();
  1348. char* message = new char[9];
  1349. message[0] = 3;
  1350. *(float*)(message + 1) = getMaxHunger();
  1351. *(float*)(message + 5) = getHunger();
  1352. msg->setMessage(message, 9);
  1353. notifyStatusBarObservers(msg);
  1354. }
  1355. void Entity::setThirst(float thirst)
  1356. {
  1357. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  1358. NetworkMessage* msg = new NetworkMessage();
  1359. char* message = new char[9];
  1360. message[0] = 4;
  1361. *(float*)(message + 1) = getMaxThirst();
  1362. *(float*)(message + 5) = getThirst();
  1363. msg->setMessage(message, 9);
  1364. notifyStatusBarObservers(msg);
  1365. }
  1366. void Entity::setGravityMultiplier(float multiplier)
  1367. {
  1368. gravityMultiplier = multiplier;
  1369. }
  1370. float Entity::getMaxHP() const
  1371. {
  1372. return maxHP;
  1373. }
  1374. float Entity::getCurrentHP() const
  1375. {
  1376. return currentHP;
  1377. }
  1378. float Entity::getStamina() const
  1379. {
  1380. return stamina;
  1381. }
  1382. float Entity::getMaxStamina() const
  1383. {
  1384. return maxStamina;
  1385. }
  1386. float Entity::getHunger() const
  1387. {
  1388. return hunger;
  1389. }
  1390. float Entity::getMaxHunger() const
  1391. {
  1392. return maxHunger;
  1393. }
  1394. float Entity::getThirst() const
  1395. {
  1396. return thirst;
  1397. }
  1398. float Entity::getMaxThirst() const
  1399. {
  1400. return maxThirst;
  1401. }
  1402. Framework::Vec3<float> Entity::getSpeed() const
  1403. {
  1404. return speed;
  1405. }
  1406. Framework::Vec3<float> Entity::getFaceDir() const
  1407. {
  1408. return faceDir;
  1409. }
  1410. Framework::Vec3<float> Entity::getPosition() const
  1411. {
  1412. return location;
  1413. }
  1414. float Entity::getGravityMultiplier() const
  1415. {
  1416. return gravityMultiplier;
  1417. }
  1418. float Entity::getJumpSpeed() const
  1419. {
  1420. return jumpSpeed;
  1421. }
  1422. void Entity::setJumpSpeed(float speed)
  1423. {
  1424. jumpSpeed = speed;
  1425. }
  1426. bool Entity::isRemoved() const
  1427. {
  1428. return removed;
  1429. }
  1430. const EntityType* Entity::zType() const
  1431. {
  1432. return Game::INSTANCE->zEntityType(typeId);
  1433. }
  1434. const ActionTarget* Entity::zTarget() const
  1435. {
  1436. return target;
  1437. }
  1438. int Entity::getId() const
  1439. {
  1440. return id;
  1441. }
  1442. bool Entity::hasDefaultModel() const
  1443. {
  1444. return 1;
  1445. }
  1446. ModelInfo* Entity::zSpecialModel() const
  1447. {
  1448. return 0;
  1449. }
  1450. float Entity::getMaxSpeed() const
  1451. {
  1452. return maxMovementSpeed;
  1453. }
  1454. bool Entity::isMoving() const
  1455. {
  1456. return movementFlags > 0;
  1457. }
  1458. int Entity::getChatSecurityLevel() const
  1459. {
  1460. return chatSecurityLevel;
  1461. }
  1462. Framework::Maybe<Framework::Punkt> Entity::getLastSavedChunkCenter() const
  1463. {
  1464. return lastSavedChunkCenter;
  1465. }
  1466. void Entity::setLastSavedChunkCenter(Framework::Punkt pos)
  1467. {
  1468. lastSavedChunkCenter = Framework::Maybe<Framework::Punkt>::of(pos);
  1469. }
  1470. void Entity::setLastChunk(int dimensionId, Framework::Punkt chankCenter)
  1471. {
  1472. lastDimensionId = dimensionId;
  1473. lastChunkCenter = chankCenter;
  1474. }
  1475. void Entity::setRemoved()
  1476. {
  1477. removed = true;
  1478. }
  1479. double Entity::getHitDistance(
  1480. Framework::Vec3<float> rayOrigin, Framework::Vec3<float> rayDirection) const
  1481. {
  1482. Framework::Vec3<float> rotatedRayOrigin
  1483. = Framework::Vec3<float>(rayOrigin).rotateZ(-rotation);
  1484. Framework::Vec3<float> rotatedRayDirection
  1485. = Framework::Vec3<float>(rayDirection).rotateZ(-rotation);
  1486. rotatedRayDirection.normalize();
  1487. if (rotatedRayDirection.x != 0)
  1488. {
  1489. float d;
  1490. if (rotatedRayDirection.x > 0)
  1491. {
  1492. float border = getPosition().x - boundingBox.x;
  1493. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1494. }
  1495. else if (rotatedRayDirection.x < 0)
  1496. {
  1497. float border = getPosition().x + boundingBox.x;
  1498. d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
  1499. }
  1500. if (d > 0)
  1501. {
  1502. Framework::Vec3<float> hitPoint
  1503. = rotatedRayOrigin + rotatedRayDirection * d;
  1504. if (hitPoint.y >= getPosition().y - boundingBox.y
  1505. && hitPoint.y <= getPosition().y + boundingBox.y
  1506. && hitPoint.z >= getPosition().z - boundingBox.z
  1507. && hitPoint.z <= getPosition().z + boundingBox.z)
  1508. {
  1509. return d;
  1510. }
  1511. }
  1512. }
  1513. if (rotatedRayDirection.y != 0)
  1514. {
  1515. float d;
  1516. if (rotatedRayDirection.y > 0)
  1517. {
  1518. float border = getPosition().y - boundingBox.y;
  1519. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1520. }
  1521. else if (rotatedRayDirection.y < 0)
  1522. {
  1523. float border = getPosition().y + boundingBox.y;
  1524. d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
  1525. }
  1526. if (d > 0)
  1527. {
  1528. Framework::Vec3<float> hitPoint
  1529. = rotatedRayOrigin + rotatedRayDirection * d;
  1530. if (hitPoint.x >= getPosition().x - boundingBox.x
  1531. && hitPoint.x <= getPosition().x + boundingBox.x
  1532. && hitPoint.z >= getPosition().z - boundingBox.z
  1533. && hitPoint.z <= getPosition().z + boundingBox.z)
  1534. {
  1535. return d;
  1536. }
  1537. }
  1538. }
  1539. if (rotatedRayDirection.z != 0)
  1540. {
  1541. float d;
  1542. if (rotatedRayDirection.z > 0)
  1543. {
  1544. float border = getPosition().z - boundingBox.z;
  1545. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1546. }
  1547. else if (rotatedRayDirection.z < 0)
  1548. {
  1549. float border = getPosition().z + boundingBox.z;
  1550. d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
  1551. }
  1552. if (d > 0)
  1553. {
  1554. Framework::Vec3<float> hitPoint
  1555. = rotatedRayOrigin + rotatedRayDirection * d;
  1556. if (hitPoint.x >= getPosition().x - boundingBox.x
  1557. && hitPoint.x <= getPosition().x + boundingBox.x
  1558. && hitPoint.y >= getPosition().y - boundingBox.y
  1559. && hitPoint.y <= getPosition().y + boundingBox.y)
  1560. {
  1561. return d;
  1562. }
  1563. }
  1564. }
  1565. return NAN;
  1566. }
  1567. float Entity::getRotation() const
  1568. {
  1569. return rotation;
  1570. }