CreatureAnimation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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_);
  170. reverse = GH.renderHandler().loadAnimation(name_);
  171. //todo: optimize
  172. forward->preload();
  173. reverse->preload();
  174. // if necessary, add one frame into vcmi-only group DEAD
  175. if(forward->size(size_t(ECreatureAnimType::DEAD)) == 0)
  176. {
  177. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), forward->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  178. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), reverse->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  179. }
  180. if(forward->size(size_t(ECreatureAnimType::DEAD_RANGED)) == 0 && forward->size(size_t(ECreatureAnimType::DEATH_RANGED)) != 0)
  181. {
  182. forward->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), forward->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  183. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), reverse->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  184. }
  185. if(forward->size(size_t(ECreatureAnimType::FROZEN)) == 0)
  186. {
  187. forward->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  188. reverse->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  189. }
  190. if(forward->size(size_t(ECreatureAnimType::RESURRECTION)) == 0)
  191. {
  192. for (size_t i = 0; i < forward->size(size_t(ECreatureAnimType::DEATH)); ++i)
  193. {
  194. size_t current = forward->size(size_t(ECreatureAnimType::DEATH)) - 1 - i;
  195. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  196. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  197. }
  198. }
  199. //TODO: get dimensions form CAnimation
  200. auto first = forward->getImage(0, size_t(type), true);
  201. if(!first)
  202. {
  203. fullWidth = 0;
  204. fullHeight = 0;
  205. return;
  206. }
  207. fullWidth = first->width();
  208. fullHeight = first->height();
  209. reverse->verticalFlip();
  210. speed = speedController(this, type);
  211. }
  212. void CreatureAnimation::endAnimation()
  213. {
  214. once = false;
  215. auto copy = onAnimationReset;
  216. onAnimationReset.clear();
  217. copy();
  218. }
  219. bool CreatureAnimation::incrementFrame(float timePassed)
  220. {
  221. elapsedTime += timePassed;
  222. currentFrame += timePassed * speed;
  223. if (animationEnd >= 0)
  224. currentFrame = std::min(currentFrame, animationEnd);
  225. const auto framesNumber = framesInGroup(type);
  226. if(framesNumber <= 0)
  227. {
  228. endAnimation();
  229. }
  230. else if(currentFrame >= float(framesNumber))
  231. {
  232. // just in case of extremely low fps (or insanely high speed)
  233. while(currentFrame >= float(framesNumber))
  234. currentFrame -= framesNumber;
  235. if(once)
  236. setType(ECreatureAnimType::HOLDING);
  237. endAnimation();
  238. return true;
  239. }
  240. return false;
  241. }
  242. void CreatureAnimation::setBorderColor(ColorRGBA palette)
  243. {
  244. border = palette;
  245. }
  246. int CreatureAnimation::getWidth() const
  247. {
  248. return fullWidth;
  249. }
  250. int CreatureAnimation::getHeight() const
  251. {
  252. return fullHeight;
  253. }
  254. float CreatureAnimation::getCurrentFrame() const
  255. {
  256. return currentFrame;
  257. }
  258. void CreatureAnimation::playOnce( ECreatureAnimType type )
  259. {
  260. setType(type);
  261. once = true;
  262. }
  263. inline int getBorderStrength(float time)
  264. {
  265. float borderStrength = fabs(std::round(time) - time) * 2; // generate value in range 0-1
  266. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  267. }
  268. static ColorRGBA genBorderColor(ui8 alpha, const ColorRGBA & base)
  269. {
  270. return ColorRGBA(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  271. }
  272. static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
  273. {
  274. return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
  275. }
  276. static ColorRGBA addColors(const ColorRGBA & base, const ColorRGBA & over)
  277. {
  278. return ColorRGBA(
  279. mixChannels(over.r, base.r, over.a, base.a),
  280. mixChannels(over.g, base.g, over.a, base.a),
  281. mixChannels(over.b, base.b, over.a, base.a),
  282. ui8(over.a + base.a * (255 - over.a) / 256)
  283. );
  284. }
  285. void CreatureAnimation::genSpecialPalette(IImage::SpecialPalette & target)
  286. {
  287. target.resize(8);
  288. target[0] = genShadow(0);
  289. target[1] = genShadow(shadowAlpha / 2);
  290. // colors 2 & 3 are not used in creatures
  291. target[4] = genShadow(shadowAlpha);
  292. target[5] = genBorderColor(getBorderStrength(elapsedTime), border);
  293. target[6] = addColors(genShadow(shadowAlpha), genBorderColor(getBorderStrength(elapsedTime), border));
  294. target[7] = addColors(genShadow(shadowAlpha / 2), genBorderColor(getBorderStrength(elapsedTime), border));
  295. }
  296. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight)
  297. {
  298. ColorRGBA shadowTest = shifter.shiftColor(genShadow(128));
  299. shadowAlpha = shadowTest.a;
  300. size_t frame = static_cast<size_t>(floor(currentFrame));
  301. std::shared_ptr<IImage> image;
  302. if(facingRight)
  303. image = forward->getImage(frame, size_t(type));
  304. else
  305. image = reverse->getImage(frame, size_t(type));
  306. if(image)
  307. {
  308. IImage::SpecialPalette SpecialPalette;
  309. genSpecialPalette(SpecialPalette);
  310. image->setSpecialPalette(SpecialPalette, IImage::SPECIAL_PALETTE_MASK_CREATURES);
  311. image->adjustPalette(shifter, IImage::SPECIAL_PALETTE_MASK_CREATURES);
  312. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  313. }
  314. }
  315. void CreatureAnimation::playUntil(size_t frameIndex)
  316. {
  317. animationEnd = frameIndex;
  318. }
  319. int CreatureAnimation::framesInGroup(ECreatureAnimType group) const
  320. {
  321. return static_cast<int>(forward->size(size_t(group)));
  322. }
  323. bool CreatureAnimation::isDead() const
  324. {
  325. return getType() == ECreatureAnimType::DEAD
  326. || getType() == ECreatureAnimType::DEAD_RANGED;
  327. }
  328. bool CreatureAnimation::isDying() const
  329. {
  330. return getType() == ECreatureAnimType::DEATH
  331. || getType() == ECreatureAnimType::DEATH_RANGED;
  332. }
  333. bool CreatureAnimation::isDeadOrDying() const
  334. {
  335. return getType() == ECreatureAnimType::DEAD
  336. || getType() == ECreatureAnimType::DEATH
  337. || getType() == ECreatureAnimType::DEAD_RANGED
  338. || getType() == ECreatureAnimType::DEATH_RANGED;
  339. }
  340. bool CreatureAnimation::isIdle() const
  341. {
  342. return getType() == ECreatureAnimType::HOLDING
  343. || getType() == ECreatureAnimType::MOUSEON;
  344. }
  345. bool CreatureAnimation::isMoving() const
  346. {
  347. return getType() == ECreatureAnimType::MOVE_START
  348. || getType() == ECreatureAnimType::MOVING
  349. || getType() == ECreatureAnimType::MOVE_END
  350. || getType() == ECreatureAnimType::TURN_L
  351. || getType() == ECreatureAnimType::TURN_R;
  352. }
  353. bool CreatureAnimation::isShooting() const
  354. {
  355. return getType() == ECreatureAnimType::SHOOT_UP
  356. || getType() == ECreatureAnimType::SHOOT_FRONT
  357. || getType() == ECreatureAnimType::SHOOT_DOWN;
  358. }