Entity.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. #include "Entity.h"
  2. #include <Text.h>
  3. #include "BlockType.h"
  4. #include "Dimension.h"
  5. #include "EntityType.h"
  6. #include "Game.h"
  7. #include "ItemSkill.h"
  8. #include "ItemStack.h"
  9. #include "ItemType.h"
  10. ActionTarget::ActionTarget(Framework::Vec3<int> blockPos, Direction blockSide)
  11. : blockPos(blockPos),
  12. targetBlockSide(blockSide),
  13. entityId(-1)
  14. {}
  15. ActionTarget::ActionTarget(int entityId)
  16. : entityId(entityId)
  17. {}
  18. bool ActionTarget::isBlock(
  19. Framework::Vec3<int> blockPos, Direction blockSide) const
  20. {
  21. return this->entityId == -1 && this->blockPos == blockPos
  22. && (this->targetBlockSide == targetBlockSide
  23. || blockSide == NO_DIRECTION);
  24. }
  25. bool ActionTarget::isEntity(int entityId) const
  26. {
  27. return this->entityId == entityId;
  28. }
  29. bool ActionTarget::useItemSkillOnTarget(
  30. Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem)
  31. {
  32. if (entityId >= 0)
  33. {
  34. Entity* target = Game::INSTANCE->zEntity(entityId);
  35. if (target)
  36. {
  37. return zItemSkill->use(zActor, zUsedItem, target);
  38. }
  39. }
  40. else
  41. {
  42. Block* block = Game::INSTANCE->zRealBlockInstance(
  43. blockPos, zActor->getDimensionId());
  44. if (block)
  45. {
  46. return zItemSkill->use(zActor, zUsedItem, block);
  47. }
  48. }
  49. return 0;
  50. }
  51. bool ActionTarget::interactItemSkillOnTarget(
  52. Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem)
  53. {
  54. if (zItemSkill)
  55. {
  56. if (entityId >= 0)
  57. {
  58. Entity* target = Game::INSTANCE->zEntity(entityId);
  59. if (target) return zItemSkill->interact(zActor, zUsedItem, target);
  60. }
  61. else
  62. {
  63. Block* block = Game::INSTANCE->zRealBlockInstance(
  64. blockPos, zActor->getDimensionId());
  65. if (block) return zItemSkill->interact(zActor, zUsedItem, block);
  66. }
  67. }
  68. else
  69. {
  70. bool itemChanged = 0;
  71. if (entityId >= 0)
  72. {
  73. Block* block = Game::INSTANCE->zRealBlockInstance(
  74. blockPos, zActor->getDimensionId());
  75. if (block) block->interact(zUsedItem, zActor, itemChanged);
  76. }
  77. else
  78. {
  79. Block* block = Game::INSTANCE->zRealBlockInstance(
  80. blockPos, zActor->getDimensionId());
  81. if (block) block->interact(zUsedItem, zActor, itemChanged);
  82. }
  83. return itemChanged;
  84. }
  85. return 0;
  86. }
  87. bool ActionTarget::placeBlock(Entity* zActor, Item* zItem)
  88. {
  89. if (zActor->getStamina() > 0.2f)
  90. {
  91. if (zItem->canBePlacedAt(zActor->getDimensionId(),
  92. blockPos + getDirection(targetBlockSide)))
  93. {
  94. Block* block = zItem->zPlacedBlockType()->createBlockAt(
  95. blockPos + getDirection(targetBlockSide),
  96. zActor->getDimensionId(),
  97. zItem);
  98. if (block)
  99. {
  100. Game::INSTANCE->zDimension(zActor->getDimensionId())
  101. ->placeBlock(block->getPos(), block);
  102. zItem->onPlaced();
  103. zActor->setStamina(zActor->getStamina() - 0.2f);
  104. return 1;
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. void ActionTarget::toMessage(
  111. const ActionTarget* zTarget, int dimensionId, NetworkMessage* zMsg)
  112. {
  113. if (zTarget)
  114. {
  115. if (zTarget->entityId >= 0)
  116. {
  117. char* message = new char[6];
  118. message[0] = 3;
  119. message[1] = 1;
  120. *(int*)(message + 2) = zTarget->entityId;
  121. zMsg->setMessage(message, 6);
  122. }
  123. else
  124. {
  125. Framework::XML::Element* targetUIML = 0;
  126. auto block
  127. = Game::INSTANCE->zBlockAt(zTarget->blockPos, dimensionId, 0);
  128. if (block.isA())
  129. {
  130. targetUIML = block.getA()->getTargetUIML();
  131. }
  132. else if (block.isB())
  133. {
  134. targetUIML
  135. = Game::INSTANCE->zBlockType(block.getB())->getTargetUIML();
  136. }
  137. Framework::Text targetUIMLText
  138. = targetUIML ? targetUIML->toString() : Framework::Text();
  139. char* message = new char[18 + targetUIMLText.getLength() + 2];
  140. message[0] = 3;
  141. message[1] = 2;
  142. *(int*)(message + 2) = zTarget->blockPos.x;
  143. *(int*)(message + 6) = zTarget->blockPos.y;
  144. *(int*)(message + 10) = zTarget->blockPos.z;
  145. *(int*)(message + 14) = zTarget->targetBlockSide;
  146. short len = (short)targetUIMLText.getLength();
  147. *(short*)(message + 18) = len;
  148. memcpy(message + 20, targetUIMLText.getText(), len);
  149. zMsg->setMessage(message, 18 + len + 2);
  150. }
  151. }
  152. else
  153. {
  154. char* message = new char[2];
  155. message[0] = 3;
  156. message[1] = 0;
  157. zMsg->setMessage(message, 2);
  158. }
  159. }
  160. void ActionTarget::save(ActionTarget* zTarget, Framework::StreamWriter* zWriter)
  161. {
  162. if (zTarget)
  163. {
  164. if (zTarget->entityId >= 0)
  165. {
  166. char b = 1;
  167. zWriter->schreibe(&b, 1);
  168. zWriter->schreibe((char*)&zTarget->entityId, 4);
  169. }
  170. else
  171. {
  172. char b = 2;
  173. zWriter->schreibe(&b, 1);
  174. zWriter->schreibe((char*)&zTarget->blockPos.x, 4);
  175. zWriter->schreibe((char*)&zTarget->blockPos.y, 4);
  176. zWriter->schreibe((char*)&zTarget->blockPos.z, 4);
  177. zWriter->schreibe((char*)&zTarget->targetBlockSide, 4);
  178. }
  179. }
  180. else
  181. {
  182. char b = 0;
  183. zWriter->schreibe(&b, 1);
  184. }
  185. }
  186. ActionTarget* ActionTarget::load(Framework::StreamReader* zReader)
  187. {
  188. char b;
  189. zReader->lese(&b, 1);
  190. if (b == 1)
  191. {
  192. int id;
  193. zReader->lese((char*)&id, 4);
  194. return new ActionTarget(id);
  195. }
  196. else if (b == 2)
  197. {
  198. Framework::Vec3<int> pos;
  199. Direction side;
  200. zReader->lese((char*)&pos.x, 4);
  201. zReader->lese((char*)&pos.y, 4);
  202. zReader->lese((char*)&pos.z, 4);
  203. zReader->lese((char*)&side, 4);
  204. return new ActionTarget(pos, side);
  205. }
  206. return 0;
  207. }
  208. Entity::Entity(
  209. int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
  210. : Inventory(location, dimensionId, true),
  211. chatSecurityLevel(0),
  212. lastChunkCenter(0, 0),
  213. lastSavedChunkCenter(Framework::Maybe<Framework::Punkt>::empty()),
  214. lastDimensionId(-1),
  215. speed(0, 0, 0),
  216. faceDir(1, 0, 0),
  217. target(0),
  218. typeId(typeId),
  219. removed(0),
  220. gravityMultiplier(1.f),
  221. jumpSpeed(0.f),
  222. id(entityId),
  223. placeBlockCooldown(0)
  224. {}
  225. void Entity::onDeath(Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill)
  226. {
  227. if (!removed)
  228. {
  229. for (DropConfig* config : zType()->getDropConfigs())
  230. {
  231. config->onObjectDestroyed(zActor, zUsedItem, zUsedSkill, this);
  232. }
  233. Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
  234. if (dim)
  235. {
  236. Chunk* chunk = dim->zChunk(lastChunkCenter);
  237. if (chunk)
  238. {
  239. chunk->onEntityLeaves(this, 0);
  240. }
  241. dim->removeEntity(id);
  242. }
  243. removed = 1;
  244. }
  245. }
  246. bool Entity::useItem(int typeId, ItemStack* zStack, bool left)
  247. {
  248. if (left)
  249. {
  250. if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
  251. {
  252. cs.lock();
  253. if (target)
  254. {
  255. ItemSkill* selected = zSkill(typeId);
  256. if (!selected)
  257. {
  258. selected = Game::INSTANCE->zItemType(typeId)
  259. ->createDefaultItemSkill();
  260. selected->setItemTypeId(typeId);
  261. if (selected) skills.add(selected);
  262. }
  263. if (!selected)
  264. {
  265. selected = zSkill(ItemTypeEnum::PLAYER_HAND);
  266. selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
  267. }
  268. bool result = target->useItemSkillOnTarget(this,
  269. selected,
  270. !zStack || zStack->getSize() > 1 ? 0
  271. : (Item*)zStack->zItem());
  272. cs.unlock();
  273. return result;
  274. }
  275. cs.unlock();
  276. }
  277. else
  278. {
  279. useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
  280. }
  281. }
  282. else
  283. {
  284. if (zStack && zStack->zItem() && zStack->zItem()->isPlaceable()
  285. && zStack->getSize() > 0)
  286. { // place item
  287. cs.lock();
  288. if (target)
  289. {
  290. if (placeBlockCooldown <= 0)
  291. {
  292. Item* item = zStack->extractFromStack();
  293. bool result = target->placeBlock(this, item);
  294. if (item->getHp() > 0)
  295. {
  296. if (!zStack->addToStack(
  297. dynamic_cast<Item*>(item->getThis())))
  298. {
  299. ItemStack* newStack = new ItemStack(item, 1);
  300. addItems(newStack, NO_DIRECTION, 0);
  301. if (newStack->getSize())
  302. {
  303. Game::INSTANCE->spawnItem(
  304. location, dimensionId, newStack);
  305. }
  306. }
  307. else
  308. {
  309. item->release();
  310. }
  311. }
  312. else
  313. {
  314. item->release();
  315. }
  316. if (result)
  317. {
  318. placeBlockCooldown = 15;
  319. }
  320. cs.unlock();
  321. return result;
  322. }
  323. else
  324. {
  325. cs.unlock();
  326. return 0;
  327. }
  328. }
  329. cs.unlock();
  330. }
  331. if (zStack && zStack->zItem() && zStack->zItem()->isEatable()
  332. && zStack->getSize() > 0)
  333. { // eat item
  334. if (zStack->getSize() == 1)
  335. {
  336. return ((Item*)zStack->zItem())->applyFoodEffects(this);
  337. }
  338. else
  339. {
  340. if (zStack->zItem()->canApplyFoodEffectsFully(this))
  341. {
  342. Item* item = zStack->extractFromStack();
  343. item->applyFoodEffects(this);
  344. item->release();
  345. return 1;
  346. }
  347. }
  348. }
  349. if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
  350. {
  351. cs.lock();
  352. if (target)
  353. {
  354. ItemSkill* selected = zSkill(typeId);
  355. if (!selected)
  356. {
  357. selected = Game::INSTANCE->zItemType(typeId)
  358. ->createDefaultItemSkill();
  359. selected->setItemTypeId(typeId);
  360. if (selected) skills.add(selected);
  361. }
  362. if (!selected)
  363. {
  364. selected = zSkill(ItemTypeEnum::PLAYER_HAND);
  365. selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
  366. }
  367. bool result = target->interactItemSkillOnTarget(this,
  368. selected,
  369. !zStack || zStack->getSize() > 1 ? 0
  370. : (Item*)zStack->zItem());
  371. cs.unlock();
  372. return result;
  373. }
  374. cs.unlock();
  375. }
  376. else
  377. {
  378. useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
  379. }
  380. }
  381. return 0;
  382. }
  383. void Entity::onTargetChange() {}
  384. bool Entity::interact(Item* zItem, Entity* zActor)
  385. {
  386. return false;
  387. }
  388. void Entity::addMovementFrame(MovementFrame& frame)
  389. {
  390. cs.lock();
  391. movements.add(frame);
  392. cs.unlock();
  393. Dimension* dim = Game::INSTANCE->zDimension(lastDimensionId);
  394. if (dim)
  395. {
  396. Chunk* chunk = dim->zChunk(lastChunkCenter);
  397. if (chunk)
  398. {
  399. NetworkMessage* message = new NetworkMessage();
  400. message->addressEntity(this);
  401. char* msg = new char[37];
  402. msg[0] = 0;
  403. *(float*)(msg + 1) = frame.direction.x;
  404. *(float*)(msg + 5) = frame.direction.y;
  405. *(float*)(msg + 9) = frame.direction.z;
  406. *(float*)(msg + 13) = frame.targetPosition.x;
  407. *(float*)(msg + 17) = frame.targetPosition.y;
  408. *(float*)(msg + 21) = frame.targetPosition.z;
  409. *(int*)(msg + 25) = frame.movementFlags;
  410. *(double*)(msg + 29) = frame.duration;
  411. message->setMessage(msg, 37);
  412. chunk->notifyObservers(message);
  413. }
  414. }
  415. faceDir = frame.direction;
  416. }
  417. void Entity::calculateTarget(Framework::Vec3<float> basePos,
  418. Framework::Vec3<float> direction,
  419. const Item* zItem)
  420. {
  421. Framework::Vec3<float> headPosition = basePos + faceOffset;
  422. int px = (int)floor(headPosition.x);
  423. int py = (int)floor(headPosition.y);
  424. int pz = (int)floor(headPosition.z);
  425. direction.normalize();
  426. Direction dir = BOTTOM;
  427. while (true)
  428. {
  429. if (getDefaultBlock(
  430. Game::INSTANCE->zBlockAt(
  431. Framework::Vec3<int>{px, py, pz}, dimensionId, 0))
  432. ->isInteractable(zItem))
  433. {
  434. if (!target || !target->isBlock({px, py, pz}, dir))
  435. {
  436. cs.lock();
  437. delete target;
  438. target = new ActionTarget({px, py, pz}, dir);
  439. cs.unlock();
  440. onTargetChange();
  441. }
  442. break;
  443. }
  444. // collision to neighbor of current block
  445. if (direction.x > 0)
  446. {
  447. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  448. Framework::Vec3<float> tmp = headPosition + direction * xt;
  449. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  450. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  451. && tmp.z < (float)pz + 1.f)
  452. {
  453. dir = WEST;
  454. px++;
  455. continue;
  456. }
  457. }
  458. if (direction.x < 0)
  459. {
  460. float xt = ((float)px - headPosition.x) / direction.x;
  461. Framework::Vec3<float> tmp = headPosition + direction * xt;
  462. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  463. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  464. && tmp.z < (float)pz + 1.f)
  465. {
  466. dir = EAST;
  467. px--;
  468. continue;
  469. }
  470. }
  471. if (direction.y > 0)
  472. {
  473. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  474. Framework::Vec3<float> tmp = headPosition + direction * yt;
  475. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  476. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  477. && tmp.z < (float)pz + 1.f)
  478. {
  479. dir = NORTH;
  480. py++;
  481. continue;
  482. }
  483. }
  484. if (direction.y < 0)
  485. {
  486. float yt = ((float)py - headPosition.y) / direction.y;
  487. Framework::Vec3<float> tmp = headPosition + direction * yt;
  488. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  489. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  490. && tmp.z < (float)pz + 1.f)
  491. {
  492. dir = SOUTH;
  493. py--;
  494. continue;
  495. }
  496. }
  497. if (direction.z > 0)
  498. {
  499. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  500. Framework::Vec3<float> tmp = headPosition + direction * zt;
  501. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  502. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  503. && tmp.y < (float)py + 1.f)
  504. {
  505. dir = BOTTOM;
  506. pz++;
  507. continue;
  508. }
  509. }
  510. if (direction.z < 0)
  511. {
  512. float zt = ((float)pz - headPosition.z) / direction.z;
  513. Framework::Vec3<float> tmp = headPosition + direction * zt;
  514. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  515. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  516. && tmp.y < (float)py + 1)
  517. {
  518. dir = TOP;
  519. pz--;
  520. continue;
  521. }
  522. }
  523. if (target)
  524. {
  525. cs.lock();
  526. delete target;
  527. target = 0;
  528. cs.unlock();
  529. onTargetChange();
  530. }
  531. break;
  532. }
  533. }
  534. void Entity::notifyStatusBarObservers(NetworkMessage* msg)
  535. {
  536. statusBarObservable.notifyObservers(msg);
  537. }
  538. ItemSkill* Entity::zSkill(int itemType)
  539. {
  540. for (ItemSkill* skill : skills)
  541. {
  542. if (skill->getItemTypeId() == itemType)
  543. {
  544. return skill;
  545. }
  546. }
  547. return 0;
  548. }
  549. void Entity::prepareTick(const Dimension* zDimension) {}
  550. void Entity::tick(const Dimension* zDimension)
  551. {
  552. if (removed) return;
  553. if (placeBlockCooldown > 0)
  554. {
  555. placeBlockCooldown--;
  556. }
  557. placeBlockCooldown--;
  558. if (time.isMeasuring())
  559. {
  560. time.messungEnde();
  561. if (movements.getEintragAnzahl() > 0)
  562. {
  563. MovementFrame currentFrame = movements.get(0);
  564. double seconds = time.getSekunden();
  565. while (seconds > 0)
  566. {
  567. if (currentFrame.duration <= 0)
  568. {
  569. cs.lock();
  570. movements.remove(0);
  571. cs.unlock();
  572. if (movements.getEintragAnzahl() > 0)
  573. currentFrame = movements.get(0);
  574. else
  575. break;
  576. }
  577. double t = MIN(currentFrame.duration, seconds);
  578. // TODO: add collision detection to reduce cheating capability
  579. location += (currentFrame.targetPosition - location)
  580. * (float)(t / currentFrame.duration);
  581. currentFrame.duration -= t;
  582. seconds -= t;
  583. if (currentFrame.duration <= 0)
  584. {
  585. location = currentFrame.targetPosition;
  586. }
  587. }
  588. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  589. if (getStamina() <= getMaxStamina() - 0.0025f)
  590. {
  591. if (getThirst() > 0 && getHunger() > 0)
  592. {
  593. setStamina(getStamina() + 0.0025f);
  594. setHunger(getHunger() - 0.0005f);
  595. setThirst(getThirst() - 0.0015f);
  596. }
  597. }
  598. }
  599. else
  600. {
  601. if (getStamina() <= getMaxStamina() - 0.005f)
  602. {
  603. if (getThirst() > 0 && getHunger() > 0)
  604. {
  605. setStamina(getStamina() + 0.005f);
  606. setHunger(getHunger() - 0.001f);
  607. setThirst(getThirst() - 0.003f);
  608. }
  609. }
  610. }
  611. }
  612. time.messungStart();
  613. Framework::Punkt chunkCenter
  614. = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
  615. if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
  616. {
  617. Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
  618. Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
  619. Chunk* zCurrentChunk
  620. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  621. Chunk* zLastChunk
  622. = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
  623. if (lastDimensionId != -1)
  624. {
  625. if (zLastChunk)
  626. {
  627. zLastChunk->onEntityLeaves(
  628. this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
  629. }
  630. }
  631. if (zCurrentChunk)
  632. {
  633. zCurrentChunk->onEntityEnters(
  634. this, lastDimensionId == dimensionId ? zLastChunk : 0);
  635. }
  636. lastDimensionId = dimensionId;
  637. lastChunkCenter = chunkCenter;
  638. }
  639. }
  640. void Entity::api(Framework::StreamReader* zRequest,
  641. NetworkMessage* zResponse,
  642. Entity* zSource)
  643. {
  644. char type;
  645. zRequest->lese(&type, 1);
  646. switch (type)
  647. {
  648. case 0: // request status bar state
  649. {
  650. char len;
  651. zRequest->lese(&len, 1);
  652. char* guiId = new char[(int)len + 1];
  653. zRequest->lese(guiId, len);
  654. guiId[(int)len] = 0;
  655. int processor;
  656. zRequest->lese((char*)&processor, 4);
  657. zResponse->addressUIElement(guiId, processor);
  658. statusBarObservable.addObserver(zSource, guiId, processor);
  659. char* msg = new char[33];
  660. msg[0] = 0;
  661. *(float*)(msg + 1) = getMaxHP();
  662. *(float*)(msg + 5) = getCurrentHP();
  663. *(float*)(msg + 9) = getMaxStamina();
  664. *(float*)(msg + 13) = getStamina();
  665. *(float*)(msg + 17) = getMaxHunger();
  666. *(float*)(msg + 21) = getHunger();
  667. *(float*)(msg + 25) = getMaxThirst();
  668. *(float*)(msg + 29) = getThirst();
  669. zResponse->setMessage(msg, 33);
  670. delete[] guiId;
  671. break;
  672. }
  673. case 1: // remove status bar observer
  674. {
  675. char len;
  676. zRequest->lese(&len, 1);
  677. char* guiId = new char[(int)len + 1];
  678. zRequest->lese(guiId, len);
  679. guiId[(int)len] = 0;
  680. int processor;
  681. zRequest->lese((char*)&processor, 4);
  682. statusBarObservable.removeObserver(zSource, guiId, processor);
  683. delete[] guiId;
  684. break;
  685. }
  686. case 2: // TODO: component request
  687. break;
  688. }
  689. }
  690. void Entity::onFall(float collisionSpeed)
  691. {
  692. if (collisionSpeed > 10)
  693. {
  694. setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 10.f) / 2.5f);
  695. }
  696. }
  697. void Entity::setChatSecurityLevel(int level)
  698. {
  699. chatSecurityLevel = level;
  700. }
  701. void Entity::setPosition(Framework::Vec3<float> pos)
  702. {
  703. location = pos;
  704. }
  705. void Entity::takeDamage(Entity* zSource, float damage)
  706. {
  707. // TODO: implement this
  708. }
  709. void Entity::setHP(
  710. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  711. {
  712. currentHP = MIN(MAX(hp, 0), maxHP);
  713. NetworkMessage* msg = new NetworkMessage();
  714. char* message = new char[9];
  715. message[0] = 1;
  716. *(float*)(message + 1) = getMaxHP();
  717. *(float*)(message + 5) = getCurrentHP();
  718. msg->setMessage(message, 9);
  719. notifyStatusBarObservers(msg);
  720. if (currentHP == 0)
  721. {
  722. onDeath(zActor, zUsedItem, zUsedSkill);
  723. }
  724. }
  725. void Entity::setStamina(float stamina)
  726. {
  727. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  728. NetworkMessage* msg = new NetworkMessage();
  729. char* message = new char[9];
  730. message[0] = 2;
  731. *(float*)(message + 1) = getMaxStamina();
  732. *(float*)(message + 5) = getStamina();
  733. msg->setMessage(message, 9);
  734. notifyStatusBarObservers(msg);
  735. }
  736. void Entity::setHunger(float hunger)
  737. {
  738. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  739. NetworkMessage* msg = new NetworkMessage();
  740. char* message = new char[9];
  741. message[0] = 3;
  742. *(float*)(message + 1) = getMaxHunger();
  743. *(float*)(message + 5) = getHunger();
  744. msg->setMessage(message, 9);
  745. notifyStatusBarObservers(msg);
  746. }
  747. void Entity::setThirst(float thirst)
  748. {
  749. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  750. NetworkMessage* msg = new NetworkMessage();
  751. char* message = new char[9];
  752. message[0] = 4;
  753. *(float*)(message + 1) = getMaxThirst();
  754. *(float*)(message + 5) = getThirst();
  755. msg->setMessage(message, 9);
  756. notifyStatusBarObservers(msg);
  757. }
  758. void Entity::setGravityMultiplier(float multiplier)
  759. {
  760. gravityMultiplier = multiplier;
  761. }
  762. float Entity::getMaxHP() const
  763. {
  764. return maxHP;
  765. }
  766. float Entity::getCurrentHP() const
  767. {
  768. return currentHP;
  769. }
  770. float Entity::getStamina() const
  771. {
  772. return stamina;
  773. }
  774. float Entity::getMaxStamina() const
  775. {
  776. return maxStamina;
  777. }
  778. float Entity::getHunger() const
  779. {
  780. return hunger;
  781. }
  782. float Entity::getMaxHunger() const
  783. {
  784. return maxHunger;
  785. }
  786. float Entity::getThirst() const
  787. {
  788. return thirst;
  789. }
  790. float Entity::getMaxThirst() const
  791. {
  792. return maxThirst;
  793. }
  794. Framework::Vec3<float> Entity::getSpeed() const
  795. {
  796. return speed;
  797. }
  798. Framework::Vec3<float> Entity::getFaceDir() const
  799. {
  800. return faceDir;
  801. }
  802. Framework::Vec3<float> Entity::getPosition() const
  803. {
  804. return location;
  805. }
  806. float Entity::getGravityMultiplier() const
  807. {
  808. return gravityMultiplier;
  809. }
  810. float Entity::getJumpSpeed() const
  811. {
  812. return jumpSpeed;
  813. }
  814. void Entity::setJumpSpeed(float speed)
  815. {
  816. jumpSpeed = speed;
  817. }
  818. bool Entity::isRemoved() const
  819. {
  820. return removed;
  821. }
  822. const EntityType* Entity::zType() const
  823. {
  824. return Game::INSTANCE->zEntityType(typeId);
  825. }
  826. const ActionTarget* Entity::zTarget() const
  827. {
  828. return target;
  829. }
  830. int Entity::getId() const
  831. {
  832. return id;
  833. }
  834. bool Entity::hasDefaultModel() const
  835. {
  836. return 1;
  837. }
  838. ModelInfo* Entity::zSpecialModel() const
  839. {
  840. return 0;
  841. }
  842. float Entity::getMaxSpeed() const
  843. {
  844. return maxMovementSpeed;
  845. }
  846. bool Entity::isMoving() const
  847. {
  848. return movements.getEintragAnzahl() > 0;
  849. }
  850. int Entity::getChatSecurityLevel() const
  851. {
  852. return chatSecurityLevel;
  853. }
  854. Framework::Maybe<Framework::Punkt> Entity::getLastSavedChunkCenter() const
  855. {
  856. return lastSavedChunkCenter;
  857. }
  858. void Entity::setLastSavedChunkCenter(Framework::Punkt pos)
  859. {
  860. lastSavedChunkCenter = Framework::Maybe<Framework::Punkt>::of(pos);
  861. }
  862. void Entity::setRemoved()
  863. {
  864. removed = true;
  865. }