Entity.cpp 45 KB

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