Entity.cpp 45 KB

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