CBattleProjectileController.cpp 9.5 KB

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