CreatureAnimation.cpp 12 KB

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