CreatureAnimation.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * CCreatureAnimation.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CreatureAnimation.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../../lib/CCreatureHandler.h"
  14. #include "../render/Canvas.h"
  15. #include "../render/ColorFilter.h"
  16. #include "../renderSDL/SDL_Extensions.h"
  17. static const SDL_Color creatureBlueBorder = { 0, 255, 255, 255 };
  18. static const SDL_Color creatureGoldBorder = { 255, 255, 0, 255 };
  19. static const SDL_Color creatureNoBorder = { 0, 0, 0, 0 };
  20. static SDL_Color genShadow(ui8 alpha)
  21. {
  22. return CSDL_Ext::makeColor(0, 0, 0, alpha);
  23. }
  24. SDL_Color AnimationControls::getBlueBorder()
  25. {
  26. return creatureBlueBorder;
  27. }
  28. SDL_Color AnimationControls::getGoldBorder()
  29. {
  30. return creatureGoldBorder;
  31. }
  32. SDL_Color AnimationControls::getNoBorder()
  33. {
  34. return creatureNoBorder;
  35. }
  36. std::shared_ptr<CreatureAnimation> AnimationControls::getAnimation(const CCreature * creature)
  37. {
  38. auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
  39. return std::make_shared<CreatureAnimation>(creature->animDefName, func);
  40. }
  41. float AnimationControls::getAnimationSpeedFactor()
  42. {
  43. // according to testing, H3 ratios between slow/medium/fast might actually be 36/60/100 (x1.666)
  44. // exact value is hard to tell due to large rounding errors
  45. // however we will assume them to be 33/66/100 since these values are better for standard 60 fps displays:
  46. // with these numbers, base frame display duration will be 100/66/33 ms - exactly 6/4/2 frames
  47. return settings["battle"]["speedFactor"].Float();
  48. }
  49. float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, ECreatureAnimType type)
  50. {
  51. assert(creature->animation.walkAnimationTime != 0);
  52. assert(creature->animation.attackAnimationTime != 0);
  53. assert(anim->framesInGroup(type) != 0);
  54. // possible new fields for creature format:
  55. //split "Attack time" into "Shoot Time" and "Cast Time"
  56. // base speed for all H3 animations on slow speed is 10 frames per second (or 100ms per frame)
  57. const float baseSpeed = 10.f;
  58. const float speed = baseSpeed * getAnimationSpeedFactor();
  59. switch (type)
  60. {
  61. case ECreatureAnimType::MOVING:
  62. return speed / creature->animation.walkAnimationTime;
  63. case ECreatureAnimType::MOUSEON:
  64. return baseSpeed;
  65. case ECreatureAnimType::HOLDING:
  66. if ( creature->animation.idleAnimationTime > 0.01)
  67. return speed / creature->animation.idleAnimationTime;
  68. else
  69. return 0.f; // this animation is disabled for current creature
  70. case ECreatureAnimType::SHOOT_UP:
  71. case ECreatureAnimType::SHOOT_FRONT:
  72. case ECreatureAnimType::SHOOT_DOWN:
  73. case ECreatureAnimType::SPECIAL_UP:
  74. case ECreatureAnimType::SPECIAL_FRONT:
  75. case ECreatureAnimType::SPECIAL_DOWN:
  76. case ECreatureAnimType::CAST_DOWN:
  77. case ECreatureAnimType::CAST_FRONT:
  78. case ECreatureAnimType::CAST_UP:
  79. return speed / creature->animation.attackAnimationTime;
  80. // as strange as it looks like "attackAnimationTime" does not affects melee attacks
  81. // necessary because length of these animations must be same for all creatures for synchronization
  82. case ECreatureAnimType::ATTACK_UP:
  83. case ECreatureAnimType::ATTACK_FRONT:
  84. case ECreatureAnimType::ATTACK_DOWN:
  85. case ECreatureAnimType::HITTED:
  86. case ECreatureAnimType::DEFENCE:
  87. case ECreatureAnimType::DEATH:
  88. case ECreatureAnimType::DEATH_RANGED:
  89. case ECreatureAnimType::RESURRECTION:
  90. case ECreatureAnimType::GROUP_ATTACK_DOWN:
  91. case ECreatureAnimType::GROUP_ATTACK_FRONT:
  92. case ECreatureAnimType::GROUP_ATTACK_UP:
  93. return speed;
  94. case ECreatureAnimType::TURN_L:
  95. case ECreatureAnimType::TURN_R:
  96. return speed;
  97. case ECreatureAnimType::MOVE_START:
  98. case ECreatureAnimType::MOVE_END:
  99. return speed;
  100. case ECreatureAnimType::DEAD:
  101. case ECreatureAnimType::DEAD_RANGED:
  102. return speed;
  103. default:
  104. return speed;
  105. }
  106. }
  107. float AnimationControls::getProjectileSpeed()
  108. {
  109. // H3 speed: 1250/2500/3750 pixels per second
  110. return static_cast<float>(getAnimationSpeedFactor() * 1250);
  111. }
  112. float AnimationControls::getRayProjectileSpeed()
  113. {
  114. // H3 speed: 4000/8000/12000 pixels per second
  115. return static_cast<float>(getAnimationSpeedFactor() * 4000);
  116. }
  117. float AnimationControls::getCatapultSpeed()
  118. {
  119. // H3 speed: 200/400/600 pixels per second
  120. return static_cast<float>(getAnimationSpeedFactor() * 200);
  121. }
  122. float AnimationControls::getSpellEffectSpeed()
  123. {
  124. // H3 speed: 10/20/30 frames per second
  125. return static_cast<float>(getAnimationSpeedFactor() * 10);
  126. }
  127. float AnimationControls::getMovementDistance(const CCreature * creature)
  128. {
  129. // H3 speed: 2/4/6 tiles per second
  130. return static_cast<float>( 2.0 * getAnimationSpeedFactor() / creature->animation.walkAnimationTime);
  131. }
  132. float AnimationControls::getFlightDistance(const CCreature * creature)
  133. {
  134. // Note: for whatever reason, H3 uses "Walk Animation Time" here, even though "Flight Animation Distance" also exists
  135. // H3 speed: 250/500/750 pixels per second
  136. return static_cast<float>( 250.0 * getAnimationSpeedFactor() / creature->animation.walkAnimationTime);
  137. }
  138. float AnimationControls::getFadeInDuration()
  139. {
  140. // H3 speed: 500/250/166 ms
  141. return 0.5f / getAnimationSpeedFactor();
  142. }
  143. float AnimationControls::getObstaclesSpeed()
  144. {
  145. // H3 speed: 20 frames per second, irregardless of speed setting.
  146. return 20.f;
  147. }
  148. ECreatureAnimType CreatureAnimation::getType() const
  149. {
  150. return type;
  151. }
  152. void CreatureAnimation::setType(ECreatureAnimType type)
  153. {
  154. this->type = type;
  155. currentFrame = 0;
  156. once = false;
  157. play();
  158. }
  159. CreatureAnimation::CreatureAnimation(const std::string & name_, TSpeedController controller)
  160. : name(name_),
  161. speed(0.1f),
  162. shadowAlpha(128),
  163. currentFrame(0),
  164. elapsedTime(0),
  165. type(ECreatureAnimType::HOLDING),
  166. border(CSDL_Ext::makeColor(0, 0, 0, 0)),
  167. speedController(controller),
  168. once(false)
  169. {
  170. forward = std::make_shared<CAnimation>(name_);
  171. reverse = std::make_shared<CAnimation>(name_);
  172. //todo: optimize
  173. forward->preload();
  174. reverse->preload();
  175. // if necessary, add one frame into vcmi-only group DEAD
  176. if(forward->size(size_t(ECreatureAnimType::DEAD)) == 0)
  177. {
  178. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), forward->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  179. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), reverse->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  180. }
  181. if(forward->size(size_t(ECreatureAnimType::DEAD_RANGED)) == 0 && forward->size(size_t(ECreatureAnimType::DEATH_RANGED)) != 0)
  182. {
  183. forward->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), forward->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  184. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), reverse->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  185. }
  186. if(forward->size(size_t(ECreatureAnimType::FROZEN)) == 0)
  187. {
  188. forward->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  189. reverse->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  190. }
  191. if(forward->size(size_t(ECreatureAnimType::RESURRECTION)) == 0)
  192. {
  193. for (size_t i = 0; i < forward->size(size_t(ECreatureAnimType::DEATH)); ++i)
  194. {
  195. size_t current = forward->size(size_t(ECreatureAnimType::DEATH)) - 1 - i;
  196. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  197. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  198. }
  199. }
  200. //TODO: get dimensions form CAnimation
  201. auto first = forward->getImage(0, size_t(type), true);
  202. if(!first)
  203. {
  204. fullWidth = 0;
  205. fullHeight = 0;
  206. return;
  207. }
  208. fullWidth = first->width();
  209. fullHeight = first->height();
  210. reverse->verticalFlip();
  211. play();
  212. }
  213. void CreatureAnimation::endAnimation()
  214. {
  215. once = false;
  216. auto copy = onAnimationReset;
  217. onAnimationReset.clear();
  218. copy();
  219. }
  220. bool CreatureAnimation::incrementFrame(float timePassed)
  221. {
  222. elapsedTime += timePassed;
  223. currentFrame += timePassed * speed;
  224. const auto framesNumber = framesInGroup(type);
  225. if(framesNumber <= 0)
  226. {
  227. endAnimation();
  228. }
  229. else if(currentFrame >= float(framesNumber))
  230. {
  231. // just in case of extremely low fps (or insanely high speed)
  232. while(currentFrame >= float(framesNumber))
  233. currentFrame -= framesNumber;
  234. if(once)
  235. setType(ECreatureAnimType::HOLDING);
  236. endAnimation();
  237. return true;
  238. }
  239. return false;
  240. }
  241. void CreatureAnimation::setBorderColor(SDL_Color palette)
  242. {
  243. border = palette;
  244. }
  245. int CreatureAnimation::getWidth() const
  246. {
  247. return fullWidth;
  248. }
  249. int CreatureAnimation::getHeight() const
  250. {
  251. return fullHeight;
  252. }
  253. float CreatureAnimation::getCurrentFrame() const
  254. {
  255. return currentFrame;
  256. }
  257. void CreatureAnimation::playOnce( ECreatureAnimType type )
  258. {
  259. setType(type);
  260. once = true;
  261. }
  262. inline int getBorderStrength(float time)
  263. {
  264. float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
  265. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  266. }
  267. static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
  268. {
  269. return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  270. }
  271. static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
  272. {
  273. return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
  274. }
  275. static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
  276. {
  277. return CSDL_Ext::makeColor(
  278. mixChannels(over.r, base.r, over.a, base.a),
  279. mixChannels(over.g, base.g, over.a, base.a),
  280. mixChannels(over.b, base.b, over.a, base.a),
  281. ui8(over.a + base.a * (255 - over.a) / 256)
  282. );
  283. }
  284. void CreatureAnimation::genSpecialPalette(IImage::SpecialPalette & target)
  285. {
  286. target[0] = genShadow(shadowAlpha / 2);
  287. target[1] = genShadow(shadowAlpha / 2);
  288. target[2] = genShadow(shadowAlpha);
  289. target[3] = genShadow(shadowAlpha);
  290. target[4] = genBorderColor(getBorderStrength(elapsedTime), border);
  291. target[5] = addColors(genShadow(shadowAlpha), genBorderColor(getBorderStrength(elapsedTime), border));
  292. target[6] = addColors(genShadow(shadowAlpha / 2), genBorderColor(getBorderStrength(elapsedTime), border));
  293. }
  294. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight)
  295. {
  296. SDL_Color shadowTest = shifter.shiftColor(genShadow(128));
  297. shadowAlpha = shadowTest.a;
  298. size_t frame = static_cast<size_t>(floor(currentFrame));
  299. std::shared_ptr<IImage> image;
  300. if(facingRight)
  301. image = forward->getImage(frame, size_t(type));
  302. else
  303. image = reverse->getImage(frame, size_t(type));
  304. if(image)
  305. {
  306. IImage::SpecialPalette SpecialPalette;
  307. genSpecialPalette(SpecialPalette);
  308. image->setSpecialPallete(SpecialPalette);
  309. image->adjustPalette(shifter, 8);
  310. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  311. }
  312. }
  313. int CreatureAnimation::framesInGroup(ECreatureAnimType group) const
  314. {
  315. return static_cast<int>(forward->size(size_t(group)));
  316. }
  317. bool CreatureAnimation::isDead() const
  318. {
  319. return getType() == ECreatureAnimType::DEAD
  320. || getType() == ECreatureAnimType::DEAD_RANGED;
  321. }
  322. bool CreatureAnimation::isDying() const
  323. {
  324. return getType() == ECreatureAnimType::DEATH
  325. || getType() == ECreatureAnimType::DEATH_RANGED;
  326. }
  327. bool CreatureAnimation::isDeadOrDying() const
  328. {
  329. return getType() == ECreatureAnimType::DEAD
  330. || getType() == ECreatureAnimType::DEATH
  331. || getType() == ECreatureAnimType::DEAD_RANGED
  332. || getType() == ECreatureAnimType::DEATH_RANGED;
  333. }
  334. bool CreatureAnimation::isIdle() const
  335. {
  336. return getType() == ECreatureAnimType::HOLDING
  337. || getType() == ECreatureAnimType::MOUSEON;
  338. }
  339. bool CreatureAnimation::isMoving() const
  340. {
  341. return getType() == ECreatureAnimType::MOVE_START
  342. || getType() == ECreatureAnimType::MOVING
  343. || getType() == ECreatureAnimType::MOVE_END
  344. || getType() == ECreatureAnimType::TURN_L
  345. || getType() == ECreatureAnimType::TURN_R;
  346. }
  347. bool CreatureAnimation::isShooting() const
  348. {
  349. return getType() == ECreatureAnimType::SHOOT_UP
  350. || getType() == ECreatureAnimType::SHOOT_FRONT
  351. || getType() == ECreatureAnimType::SHOOT_DOWN;
  352. }
  353. void CreatureAnimation::pause()
  354. {
  355. speed = 0;
  356. }
  357. void CreatureAnimation::play()
  358. {
  359. //logAnim->trace("Play %s group %d at %d:%d", name, static_cast<int>(getType()), pos.x, pos.y);
  360. speed = speedController(this, type);
  361. }