Entity.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 "NoBlock.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. 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 (placeBlockCooldown > 0)
  552. {
  553. placeBlockCooldown--;
  554. }
  555. placeBlockCooldown--;
  556. if (time.isMeasuring())
  557. {
  558. time.messungEnde();
  559. if (movements.getEintragAnzahl() > 0)
  560. {
  561. MovementFrame currentFrame = movements.get(0);
  562. double seconds = time.getSekunden();
  563. while (seconds > 0)
  564. {
  565. if (currentFrame.duration <= 0)
  566. {
  567. cs.lock();
  568. movements.remove(0);
  569. cs.unlock();
  570. if (movements.getEintragAnzahl() > 0)
  571. currentFrame = movements.get(0);
  572. else
  573. break;
  574. }
  575. double t = MIN(currentFrame.duration, seconds);
  576. // TODO: add collision detection to reduce cheating capability
  577. location += (currentFrame.targetPosition - location)
  578. * (float)(t / currentFrame.duration);
  579. currentFrame.duration -= t;
  580. seconds -= t;
  581. if (currentFrame.duration <= 0)
  582. {
  583. location = currentFrame.targetPosition;
  584. }
  585. }
  586. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  587. if (getStamina() <= getMaxStamina() - 0.0025f)
  588. {
  589. if (getThirst() > 0 && getHunger() > 0)
  590. {
  591. setStamina(getStamina() + 0.0025f);
  592. setHunger(getHunger() - 0.0005f);
  593. setThirst(getThirst() - 0.0015f);
  594. }
  595. }
  596. }
  597. else
  598. {
  599. if (getStamina() <= getMaxStamina() - 0.005f)
  600. {
  601. if (getThirst() > 0 && getHunger() > 0)
  602. {
  603. setStamina(getStamina() + 0.005f);
  604. setHunger(getHunger() - 0.001f);
  605. setThirst(getThirst() - 0.003f);
  606. }
  607. }
  608. }
  609. }
  610. time.messungStart();
  611. Framework::Punkt chunkCenter
  612. = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
  613. if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
  614. {
  615. Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
  616. Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
  617. Chunk* zCurrentChunk
  618. = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
  619. Chunk* zLastChunk
  620. = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
  621. if (lastDimensionId != -1)
  622. {
  623. if (zLastChunk)
  624. {
  625. zLastChunk->onEntityLeaves(
  626. this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
  627. }
  628. }
  629. if (zCurrentChunk)
  630. {
  631. zCurrentChunk->onEntityEnters(
  632. this, lastDimensionId == dimensionId ? zLastChunk : 0);
  633. }
  634. lastDimensionId = dimensionId;
  635. lastChunkCenter = chunkCenter;
  636. }
  637. }
  638. void Entity::api(Framework::StreamReader* zRequest,
  639. NetworkMessage* zResponse,
  640. Entity* zSource)
  641. {
  642. char type;
  643. zRequest->lese(&type, 1);
  644. switch (type)
  645. {
  646. case 0: // request status bar state
  647. {
  648. char len;
  649. zRequest->lese(&len, 1);
  650. char* guiId = new char[(int)len + 1];
  651. zRequest->lese(guiId, len);
  652. guiId[(int)len] = 0;
  653. int processor;
  654. zRequest->lese((char*)&processor, 4);
  655. zResponse->addressUIElement(guiId, processor);
  656. statusBarObservable.addObserver(zSource, guiId, processor);
  657. char* msg = new char[33];
  658. msg[0] = 0;
  659. *(float*)(msg + 1) = getMaxHP();
  660. *(float*)(msg + 5) = getCurrentHP();
  661. *(float*)(msg + 9) = getMaxStamina();
  662. *(float*)(msg + 13) = getStamina();
  663. *(float*)(msg + 17) = getMaxHunger();
  664. *(float*)(msg + 21) = getHunger();
  665. *(float*)(msg + 25) = getMaxThirst();
  666. *(float*)(msg + 29) = getThirst();
  667. zResponse->setMessage(msg, 33);
  668. delete[] guiId;
  669. break;
  670. }
  671. case 1: // remove status bar observer
  672. {
  673. char len;
  674. zRequest->lese(&len, 1);
  675. char* guiId = new char[(int)len + 1];
  676. zRequest->lese(guiId, len);
  677. guiId[(int)len] = 0;
  678. int processor;
  679. zRequest->lese((char*)&processor, 4);
  680. statusBarObservable.removeObserver(zSource, guiId, processor);
  681. delete[] guiId;
  682. break;
  683. }
  684. }
  685. }
  686. void Entity::onFall(float collisionSpeed)
  687. {
  688. if (collisionSpeed > 10)
  689. {
  690. setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 10.f) / 2.5f);
  691. }
  692. }
  693. void Entity::setChatSecurityLevel(int level)
  694. {
  695. chatSecurityLevel = level;
  696. }
  697. void Entity::setPosition(Framework::Vec3<float> pos)
  698. {
  699. location = pos;
  700. }
  701. void Entity::takeDamage(Entity* zSource, float damage)
  702. {
  703. // TODO: implement this
  704. }
  705. void Entity::setHP(
  706. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  707. {
  708. currentHP = MIN(MAX(hp, 0), maxHP);
  709. NetworkMessage* msg = new NetworkMessage();
  710. char* message = new char[9];
  711. message[0] = 1;
  712. *(float*)(message + 1) = getMaxHP();
  713. *(float*)(message + 5) = getCurrentHP();
  714. msg->setMessage(message, 9);
  715. notifyStatusBarObservers(msg);
  716. if (currentHP == 0)
  717. {
  718. onDeath(zActor, zUsedItem, zUsedSkill);
  719. }
  720. }
  721. void Entity::setStamina(float stamina)
  722. {
  723. this->stamina = MIN(MAX(stamina, 0), maxStamina);
  724. NetworkMessage* msg = new NetworkMessage();
  725. char* message = new char[9];
  726. message[0] = 2;
  727. *(float*)(message + 1) = getMaxStamina();
  728. *(float*)(message + 5) = getStamina();
  729. msg->setMessage(message, 9);
  730. notifyStatusBarObservers(msg);
  731. }
  732. void Entity::setHunger(float hunger)
  733. {
  734. this->hunger = MIN(MAX(hunger, 0), maxHunger);
  735. NetworkMessage* msg = new NetworkMessage();
  736. char* message = new char[9];
  737. message[0] = 3;
  738. *(float*)(message + 1) = getMaxHunger();
  739. *(float*)(message + 5) = getHunger();
  740. msg->setMessage(message, 9);
  741. notifyStatusBarObservers(msg);
  742. }
  743. void Entity::setThirst(float thirst)
  744. {
  745. this->thirst = MIN(MAX(thirst, 0), maxThirst);
  746. NetworkMessage* msg = new NetworkMessage();
  747. char* message = new char[9];
  748. message[0] = 4;
  749. *(float*)(message + 1) = getMaxThirst();
  750. *(float*)(message + 5) = getThirst();
  751. msg->setMessage(message, 9);
  752. notifyStatusBarObservers(msg);
  753. }
  754. void Entity::setGravityMultiplier(float multiplier)
  755. {
  756. gravityMultiplier = multiplier;
  757. }
  758. float Entity::getMaxHP() const
  759. {
  760. return maxHP;
  761. }
  762. float Entity::getCurrentHP() const
  763. {
  764. return currentHP;
  765. }
  766. float Entity::getStamina() const
  767. {
  768. return stamina;
  769. }
  770. float Entity::getMaxStamina() const
  771. {
  772. return maxStamina;
  773. }
  774. float Entity::getHunger() const
  775. {
  776. return hunger;
  777. }
  778. float Entity::getMaxHunger() const
  779. {
  780. return maxHunger;
  781. }
  782. float Entity::getThirst() const
  783. {
  784. return thirst;
  785. }
  786. float Entity::getMaxThirst() const
  787. {
  788. return maxThirst;
  789. }
  790. Framework::Vec3<float> Entity::getSpeed() const
  791. {
  792. return speed;
  793. }
  794. Framework::Vec3<float> Entity::getFaceDir() const
  795. {
  796. return faceDir;
  797. }
  798. Framework::Vec3<float> Entity::getPosition() const
  799. {
  800. return location;
  801. }
  802. float Entity::getGravityMultiplier() const
  803. {
  804. return gravityMultiplier;
  805. }
  806. float Entity::getJumpSpeed() const
  807. {
  808. return jumpSpeed;
  809. }
  810. void Entity::setJumpSpeed(float speed)
  811. {
  812. jumpSpeed = speed;
  813. }
  814. bool Entity::isRemoved() const
  815. {
  816. return removed;
  817. }
  818. const EntityType* Entity::zType() const
  819. {
  820. return Game::INSTANCE->zEntityType(typeId);
  821. }
  822. const ActionTarget* Entity::zTarget() const
  823. {
  824. return target;
  825. }
  826. int Entity::getId() const
  827. {
  828. return id;
  829. }
  830. bool Entity::hasDefaultModel() const
  831. {
  832. return 1;
  833. }
  834. ModelInfo* Entity::zSpecialModel() const
  835. {
  836. return 0;
  837. }
  838. float Entity::getMaxSpeed() const
  839. {
  840. return maxMovementSpeed;
  841. }
  842. bool Entity::isMoving() const
  843. {
  844. return movements.getEintragAnzahl() > 0;
  845. }
  846. int Entity::getChatSecurityLevel() const
  847. {
  848. return chatSecurityLevel;
  849. }