Entity.cpp 25 KB

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