Entity.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #include "Entity.h"
  2. #include "BlockType.h"
  3. #include "Dimension.h"
  4. #include "EntityRemovedUpdate.h"
  5. #include "Game.h"
  6. #include "ItemSkill.h"
  7. #include "NoBlock.h"
  8. ActionTarget::ActionTarget(Vec3<int> blockPos, Direction blockSide)
  9. : blockPos(blockPos),
  10. targetBlockSide(blockSide),
  11. entityId(-1)
  12. {}
  13. ActionTarget::ActionTarget(int entityId)
  14. : entityId(entityId)
  15. {}
  16. bool ActionTarget::isBlock(
  17. Framework::Vec3<int> blockPos, Direction blockSide) const
  18. {
  19. return this->entityId == -1 && this->blockPos == blockPos
  20. && this->targetBlockSide == targetBlockSide;
  21. }
  22. bool ActionTarget::isEntity(int entityId) const
  23. {
  24. return this->entityId == entityId;
  25. }
  26. void ActionTarget::applyItemSkillOnTarget(
  27. Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem)
  28. {
  29. if (entityId >= 0)
  30. {
  31. // TODO: get entity from game and apply skill
  32. }
  33. else
  34. {
  35. Block* block = Game::INSTANCE->zRealBlockInstance(
  36. blockPos, zActor->getCurrentDimensionId());
  37. if (block) zItemSkill->use(zActor, zUsedItem, block);
  38. }
  39. }
  40. void ActionTarget::placeBlock(Entity* zActor, Item* zItem)
  41. {
  42. // TODO: check stamina of actor
  43. auto zB = Game::INSTANCE->zBlockAt(blockPos + getDirection(targetBlockSide),
  44. zActor->getCurrentDimensionId());
  45. if ((zB.isA() && zB.getA()->zBlockType()->getId() == BlockTypeEnum::AIR)
  46. || (zB.isB() && zB.isB() == BlockTypeEnum::AIR))
  47. {
  48. Block* block = zItem->zPlacedBlockType()->createBlockAt(
  49. blockPos + getDirection(targetBlockSide), zItem);
  50. if (block)
  51. {
  52. Game::INSTANCE->zDimension(zActor->getCurrentDimensionId())
  53. ->placeBlock(block->getPos(), block);
  54. zItem->onPlaced();
  55. // TODO: decrese stamina of actor
  56. }
  57. }
  58. }
  59. void ActionTarget::toMessage(const ActionTarget* zTarget, NetworkMessage* zMsg)
  60. {
  61. if (zTarget)
  62. {
  63. if (zTarget->entityId >= 0)
  64. {
  65. char* message = new char[6];
  66. message[0] = 3;
  67. message[1] = 1;
  68. *(int*)(message + 2) = zTarget->entityId;
  69. zMsg->setMessage(message, 6);
  70. }
  71. else
  72. {
  73. char* message = new char[18];
  74. message[0] = 3;
  75. message[1] = 2;
  76. *(int*)(message + 2) = zTarget->blockPos.x;
  77. *(int*)(message + 6) = zTarget->blockPos.y;
  78. *(int*)(message + 10) = zTarget->blockPos.z;
  79. *(int*)(message + 14) = zTarget->targetBlockSide;
  80. zMsg->setMessage(message, 18);
  81. }
  82. }
  83. else
  84. {
  85. char* message = new char[2];
  86. message[0] = 3;
  87. message[1] = 0;
  88. zMsg->setMessage(message, 2);
  89. }
  90. }
  91. void ActionTarget::save(ActionTarget* zTarget, Framework::StreamWriter* zWriter)
  92. {
  93. if (zTarget)
  94. {
  95. if (zTarget->entityId >= 0)
  96. {
  97. char b = 1;
  98. zWriter->schreibe(&b, 1);
  99. zWriter->schreibe((char*)&zTarget->entityId, 4);
  100. }
  101. else
  102. {
  103. char b = 2;
  104. zWriter->schreibe(&b, 1);
  105. zWriter->schreibe((char*)&zTarget->blockPos.x, 4);
  106. zWriter->schreibe((char*)&zTarget->blockPos.y, 4);
  107. zWriter->schreibe((char*)&zTarget->blockPos.z, 4);
  108. zWriter->schreibe((char*)&zTarget->targetBlockSide, 4);
  109. }
  110. }
  111. else
  112. {
  113. char b = 0;
  114. zWriter->schreibe(&b, 1);
  115. }
  116. }
  117. ActionTarget* ActionTarget::load(Framework::StreamReader* zReader)
  118. {
  119. char b;
  120. zReader->lese(&b, 1);
  121. if (b == 1)
  122. {
  123. int id;
  124. zReader->lese((char*)&id, 4);
  125. return new ActionTarget(id);
  126. }
  127. else if (b == 2)
  128. {
  129. Framework::Vec3<int> pos;
  130. Direction side;
  131. zReader->lese((char*)&pos.x, 4);
  132. zReader->lese((char*)&pos.y, 4);
  133. zReader->lese((char*)&pos.z, 4);
  134. zReader->lese((char*)&side, 4);
  135. return new ActionTarget(pos, side);
  136. }
  137. return 0;
  138. }
  139. Entity::Entity(
  140. int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
  141. : Inventory(location, true),
  142. speed(0, 0, 0),
  143. faceDir(1, 0, 0),
  144. target(0),
  145. typeId(typeId),
  146. currentDimensionId(dimensionId),
  147. removed(0),
  148. gravityMultiplier(1.f),
  149. id(entityId),
  150. placeBlockCooldown(0)
  151. {}
  152. void Entity::onDeath()
  153. {
  154. removed = 1;
  155. Game::INSTANCE->requestWorldUpdate(
  156. new EntityRemovedUpdate(id, currentDimensionId, location));
  157. }
  158. void Entity::useItem(int typeId, Item* zItem)
  159. {
  160. if (zItem && zItem->isEatable())
  161. { // TODO: eat item
  162. zItem->applyFoodEffects(this);
  163. }
  164. else if (zItem && zItem->isPlaceable())
  165. { // TODO: place item
  166. cs.lock();
  167. if (target) target->placeBlock(this, zItem);
  168. cs.unlock();
  169. }
  170. else if (!zItem || zItem->isUsable())
  171. { // use item skill
  172. cs.lock();
  173. if (target)
  174. {
  175. ItemSkill* selected = 0;
  176. for (ItemSkill* skill : skills)
  177. {
  178. if (skill->getTypeId() == typeId)
  179. {
  180. selected = skill;
  181. break;
  182. }
  183. }
  184. if (!selected)
  185. {
  186. selected = StaticRegistry<ItemType>::INSTANCE.zElement(typeId)
  187. ->createDefaultItemSkill();
  188. skills.add(selected);
  189. }
  190. target->applyItemSkillOnTarget(this, selected, zItem);
  191. }
  192. cs.unlock();
  193. }
  194. }
  195. void Entity::onTargetChange() {}
  196. void Entity::addMovementFrame(MovementFrame& frame)
  197. {
  198. cs.lock();
  199. movements.add(frame);
  200. cs.unlock();
  201. NetworkMessage* message = new NetworkMessage();
  202. message->addressEntity(this);
  203. char* msg = new char[37];
  204. msg[0] = 0;
  205. *(float*)(msg + 1) = frame.direction.x;
  206. *(float*)(msg + 5) = frame.direction.y;
  207. *(float*)(msg + 9) = frame.direction.z;
  208. *(float*)(msg + 13) = frame.targetPosition.x;
  209. *(float*)(msg + 17) = frame.targetPosition.y;
  210. *(float*)(msg + 21) = frame.targetPosition.z;
  211. *(int*)(msg + 25) = frame.movementFlags;
  212. *(double*)(msg + 29) = frame.duration;
  213. message->setMessage(msg, 37);
  214. Game::INSTANCE->broadcastMessage(message);
  215. faceDir = frame.direction;
  216. // TODO implement subscription system to notify only interested clients
  217. }
  218. void Entity::calculateTarget(
  219. Framework::Vec3<float> basePos, Framework::Vec3<float> direction)
  220. {
  221. Vec3<float> headPosition = basePos + faceOffset;
  222. int px = (int)floor(headPosition.x);
  223. int py = (int)floor(headPosition.y);
  224. int pz = (int)floor(headPosition.z);
  225. direction.normalize();
  226. Direction dir = BOTTOM;
  227. while (true)
  228. {
  229. if (getDefaultBlock(Game::INSTANCE->zBlockAt(
  230. Vec3<int>{px, py, pz}, currentDimensionId))
  231. ->isInteractable())
  232. {
  233. if (!target || !target->isBlock({px, py, pz}, dir))
  234. {
  235. cs.lock();
  236. delete target;
  237. target = new ActionTarget({px, py, pz}, dir);
  238. cs.unlock();
  239. onTargetChange();
  240. }
  241. break;
  242. }
  243. // collision to neighbor of current block
  244. if (direction.x > 0)
  245. {
  246. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  247. Vec3<float> tmp = headPosition + direction * xt;
  248. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  249. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  250. && tmp.z < (float)pz + 1.f)
  251. {
  252. dir = WEST;
  253. px++;
  254. continue;
  255. }
  256. }
  257. if (direction.x < 0)
  258. {
  259. float xt = ((float)px - headPosition.x) / direction.x;
  260. Vec3<float> tmp = headPosition + direction * xt;
  261. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  262. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  263. && tmp.z < (float)pz + 1.f)
  264. {
  265. dir = EAST;
  266. px--;
  267. continue;
  268. }
  269. }
  270. if (direction.y > 0)
  271. {
  272. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  273. Vec3<float> tmp = headPosition + direction * yt;
  274. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  275. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  276. && tmp.z < (float)pz + 1.f)
  277. {
  278. dir = NORTH;
  279. py++;
  280. continue;
  281. }
  282. }
  283. if (direction.y < 0)
  284. {
  285. float yt = ((float)py - headPosition.y) / direction.y;
  286. Vec3<float> tmp = headPosition + direction * yt;
  287. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  288. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  289. && tmp.z < (float)pz + 1.f)
  290. {
  291. dir = SOUTH;
  292. py--;
  293. continue;
  294. }
  295. }
  296. if (direction.z > 0)
  297. {
  298. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  299. Vec3<float> tmp = headPosition + direction * zt;
  300. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  301. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  302. && tmp.y < (float)py + 1.f)
  303. {
  304. dir = BOTTOM;
  305. pz++;
  306. continue;
  307. }
  308. }
  309. if (direction.z < 0)
  310. {
  311. float zt = ((float)pz - headPosition.z) / direction.z;
  312. Vec3<float> tmp = headPosition + direction * zt;
  313. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  314. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  315. && tmp.y < (float)py + 1)
  316. {
  317. dir = TOP;
  318. pz--;
  319. continue;
  320. }
  321. }
  322. if (target)
  323. {
  324. cs.lock();
  325. delete target;
  326. target = 0;
  327. cs.unlock();
  328. onTargetChange();
  329. }
  330. break;
  331. }
  332. }
  333. void Entity::prepareTick(const Dimension* zDimension) {}
  334. void Entity::tick(const Dimension* zDimension)
  335. {
  336. if (time.isMeasuring())
  337. {
  338. time.messungEnde();
  339. if (movements.getEintragAnzahl() > 0)
  340. {
  341. MovementFrame currentFrame = movements.get(0);
  342. double seconds = time.getSekunden();
  343. while (seconds > 0)
  344. {
  345. if (currentFrame.duration <= 0)
  346. {
  347. cs.lock();
  348. movements.remove(0);
  349. cs.unlock();
  350. if (movements.getEintragAnzahl() > 0)
  351. currentFrame = movements.get(0);
  352. else
  353. break;
  354. }
  355. double t = MIN(currentFrame.duration, seconds);
  356. // TODO: add collisin detection to reduce cheating capability
  357. location += (currentFrame.targetPosition - location)
  358. * (float)(t / currentFrame.duration);
  359. currentFrame.duration -= t;
  360. seconds -= t;
  361. if (currentFrame.duration <= 0)
  362. {
  363. location = currentFrame.targetPosition;
  364. }
  365. }
  366. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  367. }
  368. }
  369. time.messungStart();
  370. }
  371. void Entity::api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  372. {}
  373. void Entity::onFall(float collisionSpeed)
  374. {
  375. if (collisionSpeed > 5)
  376. {
  377. // TODO: take damage
  378. }
  379. }
  380. void Entity::setPosition(Framework::Vec3<float> pos)
  381. {
  382. location = pos;
  383. }
  384. float Entity::getMaxHP() const
  385. {
  386. return maxHP;
  387. }
  388. float Entity::getCurrentHP() const
  389. {
  390. return currentHP;
  391. }
  392. float Entity::getStamina() const
  393. {
  394. return stamina;
  395. }
  396. float Entity::getMaxStamina() const
  397. {
  398. return maxStamina;
  399. }
  400. float Entity::getHunger() const
  401. {
  402. return hunger;
  403. }
  404. float Entity::getMaxHunger() const
  405. {
  406. return maxHunger;
  407. }
  408. float Entity::getThirst() const
  409. {
  410. return thirst;
  411. }
  412. float Entity::getMaxThirst() const
  413. {
  414. return maxThirst;
  415. }
  416. Framework::Vec3<float> Entity::getSpeed() const
  417. {
  418. return speed;
  419. }
  420. Framework::Vec3<float> Entity::getFaceDir() const
  421. {
  422. return faceDir;
  423. }
  424. Framework::Vec3<float> Entity::getPosition() const
  425. {
  426. return location;
  427. }
  428. float Entity::getGravityMultiplier() const
  429. {
  430. return gravityMultiplier;
  431. }
  432. int Entity::getCurrentDimensionId() const
  433. {
  434. return currentDimensionId;
  435. }
  436. bool Entity::isRemoved() const
  437. {
  438. return removed;
  439. }
  440. const EntityType* Entity::zType() const
  441. {
  442. return StaticRegistry<EntityType>::INSTANCE.zElement(typeId);
  443. }
  444. const ActionTarget* Entity::zTarget() const
  445. {
  446. return target;
  447. }
  448. int Entity::getId() const
  449. {
  450. return id;
  451. }
  452. bool Entity::hasDefaultModel() const
  453. {
  454. return 1;
  455. }
  456. ModelInfo Entity::getSpecialModel() const
  457. {
  458. return ModelInfo("", "", 0);
  459. }
  460. float Entity::getMaxSpeed() const
  461. {
  462. return maxMovementSpeed;
  463. }
  464. bool Entity::isMoving() const
  465. {
  466. return movements.getEintragAnzahl() > 0;
  467. }