BattleProjectileController.cpp 11 KB

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