Entity.cpp 14 KB

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