BattleProjectileController.cpp 11 KB

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