Entity.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. {}
  151. void Entity::onDeath()
  152. {
  153. removed = 1;
  154. Game::INSTANCE->requestWorldUpdate(
  155. new EntityRemovedUpdate(id, currentDimensionId, location));
  156. }
  157. void Entity::useItem(int typeId, Item* zItem)
  158. {
  159. if (zItem && zItem->isEatable())
  160. { // TODO: eat item
  161. zItem->applyFoodEffects(this);
  162. }
  163. else if (zItem && zItem->isPlaceable())
  164. { // TODO: place item
  165. cs.lock();
  166. if (target) target->placeBlock(this, zItem);
  167. cs.unlock();
  168. }
  169. else if (!zItem || zItem->isUsable())
  170. { // use item skill
  171. cs.lock();
  172. if (target)
  173. {
  174. ItemSkill* selected = 0;
  175. for (ItemSkill* skill : skills)
  176. {
  177. if (skill->getTypeId() == typeId)
  178. {
  179. selected = skill;
  180. break;
  181. }
  182. }
  183. if (!selected)
  184. {
  185. selected = selected->zSkillType()->createDefaultItemSkill();
  186. skills.add(selected);
  187. }
  188. target->applyItemSkillOnTarget(this, selected, zItem);
  189. }
  190. cs.unlock();
  191. }
  192. }
  193. void Entity::onTargetChange() {}
  194. void Entity::addMovementFrame(MovementFrame& frame)
  195. {
  196. cs.lock();
  197. movements.add(frame);
  198. cs.unlock();
  199. NetworkMessage* message = new NetworkMessage();
  200. message->addressEntity(this);
  201. char* msg = new char[37];
  202. msg[0] = 0;
  203. *(float*)(msg + 1) = frame.direction.x;
  204. *(float*)(msg + 5) = frame.direction.y;
  205. *(float*)(msg + 9) = frame.direction.z;
  206. *(float*)(msg + 13) = frame.targetPosition.x;
  207. *(float*)(msg + 17) = frame.targetPosition.y;
  208. *(float*)(msg + 21) = frame.targetPosition.z;
  209. *(int*)(msg + 25) = frame.movementFlags;
  210. *(double*)(msg + 29) = frame.duration;
  211. message->setMessage(msg, 37);
  212. Game::INSTANCE->broadcastMessage(message);
  213. faceDir = frame.direction;
  214. // TODO implement subscription system to notify only interested clients
  215. }
  216. void Entity::calculateTarget(
  217. Framework::Vec3<float> basePos, Framework::Vec3<float> direction)
  218. {
  219. Vec3<float> headPosition = basePos + faceOffset;
  220. int px = (int)floor(headPosition.x);
  221. int py = (int)floor(headPosition.y);
  222. int pz = (int)floor(headPosition.z);
  223. direction.normalize();
  224. Direction dir = BOTTOM;
  225. while (true)
  226. {
  227. if (getDefaultBlock(Game::INSTANCE->zBlockAt(
  228. Vec3<int>{px, py, pz}, currentDimensionId))
  229. ->isInteractable())
  230. {
  231. if (!target || !target->isBlock({px, py, pz}, dir))
  232. {
  233. cs.lock();
  234. delete target;
  235. target = new ActionTarget({px, py, pz}, dir);
  236. cs.unlock();
  237. onTargetChange();
  238. }
  239. break;
  240. }
  241. // collision to neighbor of current block
  242. if (direction.x > 0)
  243. {
  244. float xt = ((float)px + 1.f - headPosition.x) / direction.x;
  245. Vec3<float> tmp = headPosition + direction * xt;
  246. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  247. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  248. && tmp.z < (float)pz + 1.f)
  249. {
  250. dir = WEST;
  251. px++;
  252. continue;
  253. }
  254. }
  255. if (direction.x < 0)
  256. {
  257. float xt = ((float)px - headPosition.x) / direction.x;
  258. Vec3<float> tmp = headPosition + direction * xt;
  259. if (xt <= targetDistanceLimit && tmp.y >= (float)py
  260. && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
  261. && tmp.z < (float)pz + 1.f)
  262. {
  263. dir = EAST;
  264. px--;
  265. continue;
  266. }
  267. }
  268. if (direction.y > 0)
  269. {
  270. float yt = ((float)py + 1.f - headPosition.y) / direction.y;
  271. Vec3<float> tmp = headPosition + direction * yt;
  272. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  273. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  274. && tmp.z < (float)pz + 1.f)
  275. {
  276. dir = NORTH;
  277. py++;
  278. continue;
  279. }
  280. }
  281. if (direction.y < 0)
  282. {
  283. float yt = ((float)py - headPosition.y) / direction.y;
  284. Vec3<float> tmp = headPosition + direction * yt;
  285. if (yt <= targetDistanceLimit && tmp.x >= (float)px
  286. && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
  287. && tmp.z < (float)pz + 1.f)
  288. {
  289. dir = SOUTH;
  290. py--;
  291. continue;
  292. }
  293. }
  294. if (direction.z > 0)
  295. {
  296. float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
  297. Vec3<float> tmp = headPosition + direction * zt;
  298. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  299. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  300. && tmp.y < (float)py + 1.f)
  301. {
  302. dir = BOTTOM;
  303. pz++;
  304. continue;
  305. }
  306. }
  307. if (direction.z < 0)
  308. {
  309. float zt = ((float)pz - headPosition.z) / direction.z;
  310. Vec3<float> tmp = headPosition + direction * zt;
  311. if (zt <= targetDistanceLimit && tmp.x >= (float)px
  312. && tmp.x < (float)px + 1.f && tmp.y >= (float)py
  313. && tmp.y < (float)py + 1)
  314. {
  315. dir = TOP;
  316. pz--;
  317. continue;
  318. }
  319. }
  320. if (target)
  321. {
  322. cs.lock();
  323. delete target;
  324. target = 0;
  325. cs.unlock();
  326. onTargetChange();
  327. }
  328. break;
  329. }
  330. }
  331. void Entity::prepareTick(const Dimension* zDimension) {}
  332. void Entity::tick(const Dimension* zDimension)
  333. {
  334. if (time.isMeasuring())
  335. {
  336. time.messungEnde();
  337. if (movements.getEintragAnzahl() > 0)
  338. {
  339. MovementFrame currentFrame = movements.get(0);
  340. double seconds = time.getSekunden();
  341. while (seconds > 0)
  342. {
  343. if (currentFrame.duration <= 0)
  344. {
  345. cs.lock();
  346. movements.remove(0);
  347. cs.unlock();
  348. if (movements.getEintragAnzahl() > 0)
  349. currentFrame = movements.get(0);
  350. else
  351. break;
  352. }
  353. double t = MIN(currentFrame.duration, seconds);
  354. // TODO: add collisin detection to reduce cheating capability
  355. location += (currentFrame.targetPosition - location)
  356. * (float)(t / currentFrame.duration);
  357. currentFrame.duration -= t;
  358. seconds -= t;
  359. if (currentFrame.duration <= 0)
  360. {
  361. location = currentFrame.targetPosition;
  362. }
  363. }
  364. if (currentFrame.duration > 0) movements.set(currentFrame, 0);
  365. }
  366. }
  367. time.messungStart();
  368. }
  369. void Entity::api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  370. {}
  371. void Entity::onFall(float collisionSpeed)
  372. {
  373. if (collisionSpeed > 5)
  374. {
  375. // TODO: take damage
  376. }
  377. }
  378. void Entity::setPosition(Framework::Vec3<float> pos)
  379. {
  380. location = pos;
  381. }
  382. float Entity::getMaxHP() const
  383. {
  384. return maxHP;
  385. }
  386. float Entity::getCurrentHP() const
  387. {
  388. return currentHP;
  389. }
  390. float Entity::getStamina() const
  391. {
  392. return stamina;
  393. }
  394. float Entity::getMaxStamina() const
  395. {
  396. return maxStamina;
  397. }
  398. float Entity::getHunger() const
  399. {
  400. return hunger;
  401. }
  402. float Entity::getMaxHunger() const
  403. {
  404. return maxHunger;
  405. }
  406. float Entity::getThirst() const
  407. {
  408. return thirst;
  409. }
  410. float Entity::getMaxThirst() const
  411. {
  412. return maxThirst;
  413. }
  414. Framework::Vec3<float> Entity::getSpeed() const
  415. {
  416. return speed;
  417. }
  418. Framework::Vec3<float> Entity::getFaceDir() const
  419. {
  420. return faceDir;
  421. }
  422. Framework::Vec3<float> Entity::getPosition() const
  423. {
  424. return location;
  425. }
  426. float Entity::getGravityMultiplier() const
  427. {
  428. return gravityMultiplier;
  429. }
  430. int Entity::getCurrentDimensionId() const
  431. {
  432. return currentDimensionId;
  433. }
  434. bool Entity::isRemoved() const
  435. {
  436. return removed;
  437. }
  438. const EntityType* Entity::zType() const
  439. {
  440. return StaticRegistry<EntityType>::INSTANCE.zElement(typeId);
  441. }
  442. const ActionTarget* Entity::zTarget() const
  443. {
  444. return target;
  445. }
  446. int Entity::getId() const
  447. {
  448. return id;
  449. }
  450. bool Entity::hasDefaultModel() const
  451. {
  452. return 1;
  453. }
  454. ModelInfo Entity::getSpecialModel() const
  455. {
  456. return ModelInfo("", "", 0);
  457. }
  458. float Entity::getMaxSpeed() const
  459. {
  460. return maxMovementSpeed;
  461. }
  462. bool Entity::isMoving() const
  463. {
  464. return movements.getEintragAnzahl() > 0;
  465. }