CBattleProjectileController.cpp 12 KB

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