BattleProjectileController.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * BattleProjectileController.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 "BattleProjectileController.h"
  12. #include "BattleInterface.h"
  13. #include "BattleSiegeController.h"
  14. #include "BattleStacksController.h"
  15. #include "CreatureAnimation.h"
  16. #include "../render/Canvas.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../CGameInfo.h"
  19. #include "../../lib/CStack.h"
  20. #include "../../lib/mapObjects/CGTownInstance.h"
  21. static double calculateCatapultParabolaY(const Point & from, const Point & dest, int x)
  22. {
  23. double facA = 0.005; // seems to be constant
  24. // system of 2 linear equations, solutions of which are missing coefficients
  25. // for quadratic equation a*x*x + b*x + c
  26. double eq[2][3] = {
  27. { static_cast<double>(from.x), 1.0, from.y - facA*from.x*from.x },
  28. { static_cast<double>(dest.x), 1.0, dest.y - facA*dest.x*dest.x }
  29. };
  30. // solve system via determinants
  31. double det = eq[0][0] *eq[1][1] - eq[1][0] *eq[0][1];
  32. double detB = eq[0][2] *eq[1][1] - eq[1][2] *eq[0][1];
  33. double detC = eq[0][0] *eq[1][2] - eq[1][0] *eq[0][2];
  34. double facB = detB / det;
  35. double facC = detC / det;
  36. return facA *pow(x, 2.0) + facB *x + facC;
  37. }
  38. void ProjectileMissile::show(Canvas & canvas)
  39. {
  40. size_t group = reverse ? 1 : 0;
  41. auto image = animation->getImage(frameNum, group, true);
  42. if(image)
  43. {
  44. Point pos {
  45. vstd::lerp(from.x, dest.x, progress) - image->width() / 2,
  46. vstd::lerp(from.y, dest.y, progress) - image->height() / 2,
  47. };
  48. canvas.draw(image, pos);
  49. }
  50. float timePassed = GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  51. progress += timePassed * speed;
  52. }
  53. void ProjectileAnimatedMissile::show(Canvas & canvas)
  54. {
  55. ProjectileMissile::show(canvas);
  56. frameProgress += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  57. size_t animationSize = animation->size(reverse ? 1 : 0);
  58. while (frameProgress > animationSize)
  59. frameProgress -= animationSize;
  60. frameNum = std::floor(frameProgress);
  61. }
  62. void ProjectileCatapult::show(Canvas & canvas)
  63. {
  64. frameProgress += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  65. int frameCounter = std::floor(frameProgress);
  66. int frameIndex = (frameCounter + 1) % animation->size(0);
  67. auto image = animation->getImage(frameIndex, 0, true);
  68. if(image)
  69. {
  70. int posX = vstd::lerp(from.x, dest.x, progress);
  71. int posY = calculateCatapultParabolaY(from, dest, posX);
  72. Point pos(posX, posY);
  73. canvas.draw(image, pos);
  74. }
  75. float timePassed = GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  76. progress += timePassed * speed;
  77. }
  78. void ProjectileRay::show(Canvas & canvas)
  79. {
  80. Point curr {
  81. vstd::lerp(from.x, dest.x, progress),
  82. vstd::lerp(from.y, dest.y, progress),
  83. };
  84. Point length = curr - from;
  85. //select axis to draw ray on, we want angle to be less than 45 degrees so individual sub-rays won't overlap each other
  86. if (std::abs(length.x) > std::abs(length.y)) // draw in horizontal axis
  87. {
  88. int y1 = from.y - rayConfig.size() / 2;
  89. int y2 = curr.y - rayConfig.size() / 2;
  90. int x1 = from.x;
  91. int x2 = curr.x;
  92. for (size_t i = 0; i < rayConfig.size(); ++i)
  93. {
  94. auto ray = rayConfig[i];
  95. canvas.drawLine(Point(x1, y1 + i), Point(x2, y2+i), ray.start, ray.end);
  96. }
  97. }
  98. else // draw in vertical axis
  99. {
  100. int x1 = from.x - rayConfig.size() / 2;
  101. int x2 = curr.x - rayConfig.size() / 2;
  102. int y1 = from.y;
  103. int y2 = curr.y;
  104. for (size_t i = 0; i < rayConfig.size(); ++i)
  105. {
  106. auto ray = rayConfig[i];
  107. canvas.drawLine(Point(x1 + i, y1), Point(x2 + i, y2), ray.start, ray.end);
  108. }
  109. }
  110. float timePassed = GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  111. progress += timePassed * speed;
  112. }
  113. BattleProjectileController::BattleProjectileController(BattleInterface & owner):
  114. owner(owner)
  115. {}
  116. const CCreature & BattleProjectileController::getShooter(const CStack * stack) const
  117. {
  118. const CCreature * creature = stack->getCreature();
  119. if(creature->getId() == CreatureID::ARROW_TOWERS)
  120. creature = owner.siegeController->getTurretCreature();
  121. if(creature->animation.missleFrameAngles.empty())
  122. {
  123. logAnim->error("Mod error: Creature '%s' on the Archer's tower is not a shooter. Mod should be fixed. Trying to use archer's data instead...", creature->getNameSingularTranslated());
  124. creature = CGI->creh->objects[CreatureID::ARCHER];
  125. }
  126. return *creature;
  127. }
  128. bool BattleProjectileController::stackUsesRayProjectile(const CStack * stack) const
  129. {
  130. return !getShooter(stack).animation.projectileRay.empty();
  131. }
  132. bool BattleProjectileController::stackUsesMissileProjectile(const CStack * stack) const
  133. {
  134. return !getShooter(stack).animation.projectileImageName.empty();
  135. }
  136. void BattleProjectileController::initStackProjectile(const CStack * stack)
  137. {
  138. if (!stackUsesMissileProjectile(stack))
  139. return;
  140. const CCreature & creature = getShooter(stack);
  141. projectilesCache[creature.animation.projectileImageName] = createProjectileImage(creature.animation.projectileImageName);
  142. }
  143. std::shared_ptr<CAnimation> BattleProjectileController::createProjectileImage(const std::string & path )
  144. {
  145. std::shared_ptr<CAnimation> projectile = std::make_shared<CAnimation>(path);
  146. projectile->preload();
  147. if(projectile->size(1) != 0)
  148. logAnim->error("Expected empty group 1 in stack projectile");
  149. else
  150. projectile->createFlippedGroup(0, 1);
  151. return projectile;
  152. }
  153. std::shared_ptr<CAnimation> BattleProjectileController::getProjectileImage(const CStack * stack)
  154. {
  155. const CCreature & creature = getShooter(stack);
  156. std::string imageName = creature.animation.projectileImageName;
  157. if (!projectilesCache.count(imageName))
  158. initStackProjectile(stack);
  159. return projectilesCache[imageName];
  160. }
  161. void BattleProjectileController::emitStackProjectile(const CStack * stack)
  162. {
  163. int stackID = stack ? stack->ID : -1;
  164. for (auto projectile : projectiles)
  165. {
  166. if ( !projectile->playing && projectile->shooterID == stackID)
  167. {
  168. projectile->playing = true;
  169. return;
  170. }
  171. }
  172. }
  173. void BattleProjectileController::showProjectiles(Canvas & canvas)
  174. {
  175. for ( auto projectile: projectiles)
  176. {
  177. if ( projectile->playing )
  178. projectile->show(canvas);
  179. }
  180. vstd::erase_if(projectiles, [&](const std::shared_ptr<ProjectileBase> & projectile){
  181. return projectile->progress > 1.0f;
  182. });
  183. }
  184. bool BattleProjectileController::hasActiveProjectile(const CStack * stack, bool emittedOnly) const
  185. {
  186. int stackID = stack ? stack->ID : -1;
  187. for(auto const & instance : projectiles)
  188. {
  189. if(instance->shooterID == stackID && (instance->playing || !emittedOnly))
  190. {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. float BattleProjectileController::computeProjectileFlightTime( Point from, Point dest, double animSpeed)
  197. {
  198. float distanceSquared = (dest.x - from.x) * (dest.x - from.x) + (dest.y - from.y) * (dest.y - from.y);
  199. float distance = sqrt(distanceSquared);
  200. assert(distance > 1.f);
  201. return animSpeed / std::max( 1.f, distance);
  202. }
  203. int BattleProjectileController::computeProjectileFrameID( Point from, Point dest, const CStack * stack)
  204. {
  205. const CCreature & creature = getShooter(stack);
  206. auto & angles = creature.animation.missleFrameAngles;
  207. auto animation = getProjectileImage(stack);
  208. // only frames below maxFrame are usable: anything higher is either no present or we don't know when it should be used
  209. size_t maxFrame = std::min<size_t>(angles.size(), animation->size(0));
  210. assert(maxFrame > 0);
  211. double projectileAngle = -atan2(dest.y - from.y, std::abs(dest.x - from.x));
  212. // values in angles array indicate position from which this frame was rendered, in degrees.
  213. // possible range is 90 ... -90, where projectile for +90 will be used for shooting upwards, +0 for shots towards right and -90 for downwards shots
  214. // find frame that has closest angle to one that we need for this shot
  215. int bestID = 0;
  216. double bestDiff = fabs( angles[0] / 180 * M_PI - projectileAngle );
  217. for (int i=1; i<maxFrame; i++)
  218. {
  219. double currentDiff = fabs( angles[i] / 180 * M_PI - projectileAngle );
  220. if (currentDiff < bestDiff)
  221. {
  222. bestID = i;
  223. bestDiff = currentDiff;
  224. }
  225. }
  226. return bestID;
  227. }
  228. void BattleProjectileController::createCatapultProjectile(const CStack * shooter, Point from, Point dest)
  229. {
  230. auto catapultProjectile = new ProjectileCatapult();
  231. catapultProjectile->animation = getProjectileImage(shooter);
  232. catapultProjectile->progress = 0;
  233. catapultProjectile->speed = computeProjectileFlightTime(from, dest, AnimationControls::getCatapultSpeed());
  234. catapultProjectile->from = from;
  235. catapultProjectile->dest = dest;
  236. catapultProjectile->shooterID = shooter->ID;
  237. catapultProjectile->playing = false;
  238. catapultProjectile->frameProgress = 0.f;
  239. projectiles.push_back(std::shared_ptr<ProjectileBase>(catapultProjectile));
  240. }
  241. void BattleProjectileController::createProjectile(const CStack * shooter, Point from, Point dest)
  242. {
  243. const CCreature & shooterInfo = getShooter(shooter);
  244. std::shared_ptr<ProjectileBase> projectile;
  245. if (stackUsesRayProjectile(shooter) && stackUsesMissileProjectile(shooter))
  246. {
  247. logAnim->error("Mod error: Creature '%s' has both missile and ray projectiles configured. Mod should be fixed. Using ray projectile configuration...", shooterInfo.getNameSingularTranslated());
  248. }
  249. if (stackUsesRayProjectile(shooter))
  250. {
  251. auto rayProjectile = new ProjectileRay();
  252. projectile.reset(rayProjectile);
  253. rayProjectile->rayConfig = shooterInfo.animation.projectileRay;
  254. rayProjectile->speed = computeProjectileFlightTime(from, dest, AnimationControls::getRayProjectileSpeed());
  255. }
  256. else if (stackUsesMissileProjectile(shooter))
  257. {
  258. auto missileProjectile = new ProjectileMissile();
  259. projectile.reset(missileProjectile);
  260. missileProjectile->animation = getProjectileImage(shooter);
  261. missileProjectile->reverse = !owner.stacksController->facingRight(shooter);
  262. missileProjectile->frameNum = computeProjectileFrameID(from, dest, shooter);
  263. missileProjectile->speed = computeProjectileFlightTime(from, dest, AnimationControls::getProjectileSpeed());
  264. }
  265. projectile->from = from;
  266. projectile->dest = dest;
  267. projectile->shooterID = shooter->ID;
  268. projectile->progress = 0;
  269. projectile->playing = false;
  270. projectiles.push_back(projectile);
  271. }
  272. void BattleProjectileController::createSpellProjectile(const CStack * shooter, Point from, Point dest, const CSpell * spell)
  273. {
  274. double projectileAngle = std::abs(atan2(dest.x - from.x, dest.y - from.y));
  275. std::string animToDisplay = spell->animationInfo.selectProjectile(projectileAngle);
  276. assert(!animToDisplay.empty());
  277. if(!animToDisplay.empty())
  278. {
  279. auto projectile = new ProjectileAnimatedMissile();
  280. projectile->animation = createProjectileImage(animToDisplay);
  281. projectile->frameProgress = 0;
  282. projectile->frameNum = 0;
  283. projectile->reverse = from.x > dest.x;
  284. projectile->from = from;
  285. projectile->dest = dest;
  286. projectile->shooterID = shooter ? shooter->ID : -1;
  287. projectile->progress = 0;
  288. projectile->speed = computeProjectileFlightTime(from, dest, AnimationControls::getProjectileSpeed());
  289. projectile->playing = false;
  290. projectiles.push_back(std::shared_ptr<ProjectileBase>(projectile));
  291. }
  292. }