CBattleProjectileController.cpp 9.8 KB

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