CBattleSiegeController.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * CBattleSiegeController.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 "CBattleSiegeController.h"
  12. #include "../CMusicHandler.h"
  13. #include "../../lib/NetPacks.h"
  14. #include "../CGameInfo.h"
  15. #include "../../CCallback.h"
  16. #include "../CBitmapHandler.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../../lib/CStack.h"
  19. #include "../../lib/mapObjects/CGTownInstance.h"
  20. #include "CBattleInterface.h"
  21. #include "CBattleAnimations.h"
  22. #include "CBattleInterfaceClasses.h"
  23. #include "CBattleStacksController.h"
  24. CBattleSiegeController::~CBattleSiegeController()
  25. {
  26. auto gateState = owner->curInt->cb->battleGetGateState();
  27. for (int g = 0; g < ARRAY_COUNT(walls); ++g)
  28. {
  29. if (g != EWallVisual::GATE || (gateState != EGateState::NONE && gateState != EGateState::CLOSED && gateState != EGateState::BLOCKED))
  30. SDL_FreeSurface(walls[g]);
  31. }
  32. SDL_FreeSurface(moatSurface);
  33. SDL_FreeSurface(mlipSurface);
  34. }
  35. std::string CBattleSiegeController::getSiegeName(ui16 what) const
  36. {
  37. return getSiegeName(what, EWallState::INTACT);
  38. }
  39. std::string CBattleSiegeController::getSiegeName(ui16 what, int state) const
  40. {
  41. auto getImageIndex = [&]() -> int
  42. {
  43. switch (state)
  44. {
  45. case EWallState::INTACT : return 1;
  46. case EWallState::DAMAGED :
  47. if(what == 2 || what == 3 || what == 8) // towers don't have separate image here - INTACT and DAMAGED is 1, DESTROYED is 2
  48. return 1;
  49. else
  50. return 2;
  51. case EWallState::DESTROYED :
  52. if (what == 2 || what == 3 || what == 8)
  53. return 2;
  54. else
  55. return 3;
  56. }
  57. return 1;
  58. };
  59. const std::string & prefix = town->town->clientInfo.siegePrefix;
  60. std::string addit = boost::lexical_cast<std::string>(getImageIndex());
  61. switch(what)
  62. {
  63. case EWallVisual::BACKGROUND:
  64. return prefix + "BACK.BMP";
  65. case EWallVisual::BACKGROUND_WALL:
  66. {
  67. switch(town->town->faction->index)
  68. {
  69. case ETownType::RAMPART:
  70. case ETownType::NECROPOLIS:
  71. case ETownType::DUNGEON:
  72. case ETownType::STRONGHOLD:
  73. return prefix + "TPW1.BMP";
  74. default:
  75. return prefix + "TPWL.BMP";
  76. }
  77. }
  78. case EWallVisual::KEEP:
  79. return prefix + "MAN" + addit + ".BMP";
  80. case EWallVisual::BOTTOM_TOWER:
  81. return prefix + "TW1" + addit + ".BMP";
  82. case EWallVisual::BOTTOM_WALL:
  83. return prefix + "WA1" + addit + ".BMP";
  84. case EWallVisual::WALL_BELLOW_GATE:
  85. return prefix + "WA3" + addit + ".BMP";
  86. case EWallVisual::WALL_OVER_GATE:
  87. return prefix + "WA4" + addit + ".BMP";
  88. case EWallVisual::UPPER_WALL:
  89. return prefix + "WA6" + addit + ".BMP";
  90. case EWallVisual::UPPER_TOWER:
  91. return prefix + "TW2" + addit + ".BMP";
  92. case EWallVisual::GATE:
  93. return prefix + "DRW" + addit + ".BMP";
  94. case EWallVisual::GATE_ARCH:
  95. return prefix + "ARCH.BMP";
  96. case EWallVisual::BOTTOM_STATIC_WALL:
  97. return prefix + "WA2.BMP";
  98. case EWallVisual::UPPER_STATIC_WALL:
  99. return prefix + "WA5.BMP";
  100. case EWallVisual::MOAT:
  101. return prefix + "MOAT.BMP";
  102. case EWallVisual::BACKGROUND_MOAT:
  103. return prefix + "MLIP.BMP";
  104. case EWallVisual::KEEP_BATTLEMENT:
  105. return prefix + "MANC.BMP";
  106. case EWallVisual::BOTTOM_BATTLEMENT:
  107. return prefix + "TW1C.BMP";
  108. case EWallVisual::UPPER_BATTLEMENT:
  109. return prefix + "TW2C.BMP";
  110. default:
  111. return "";
  112. }
  113. }
  114. void CBattleSiegeController::printPartOfWall(SDL_Surface *to, int what)
  115. {
  116. Point pos = Point(-1, -1);
  117. auto & ci = town->town->clientInfo;
  118. if (vstd::iswithin(what, 1, 17))
  119. {
  120. pos.x = ci.siegePositions[what].x + owner->pos.x;
  121. pos.y = ci.siegePositions[what].y + owner->pos.y;
  122. }
  123. if (town->town->faction->index == ETownType::TOWER
  124. && (what == EWallVisual::MOAT || what == EWallVisual::BACKGROUND_MOAT))
  125. return; // no moat in Tower. TODO: remove hardcode somehow?
  126. if (pos.x != -1)
  127. {
  128. //gate have no displayed bitmap when drawbridge is raised
  129. if (what == EWallVisual::GATE)
  130. {
  131. auto gateState = owner->curInt->cb->battleGetGateState();
  132. if (gateState != EGateState::OPENED && gateState != EGateState::DESTROYED)
  133. return;
  134. }
  135. blitAt(walls[what], pos.x, pos.y, to);
  136. }
  137. }
  138. std::string CBattleSiegeController::getBattleBackgroundName()
  139. {
  140. return getSiegeName(0);
  141. }
  142. CBattleSiegeController::CBattleSiegeController(CBattleInterface * owner, const CGTownInstance *siegeTown):
  143. owner(owner),
  144. town(siegeTown)
  145. {
  146. assert(owner->fieldController.get() == nullptr); // must be created after this
  147. moatSurface = BitmapHandler::loadBitmap( getSiegeName(13) );
  148. mlipSurface = BitmapHandler::loadBitmap( getSiegeName(14) );
  149. for (int g = 0; g < ARRAY_COUNT(walls); ++g)
  150. {
  151. if (g != EWallVisual::GATE)
  152. walls[g] = BitmapHandler::loadBitmap(getSiegeName(g));
  153. }
  154. }
  155. const CCreature *CBattleSiegeController::turretCreature()
  156. {
  157. return CGI->creh->objects[town->town->clientInfo.siegeShooter];
  158. }
  159. Point CBattleSiegeController::turretCreaturePosition( BattleHex position )
  160. {
  161. // Turret positions are read out of the config/wall_pos.txt
  162. int posID = 0;
  163. switch (position)
  164. {
  165. case BattleHex::CASTLE_CENTRAL_TOWER: // keep creature
  166. posID = 18;
  167. break;
  168. case BattleHex::CASTLE_BOTTOM_TOWER: // bottom creature
  169. posID = 19;
  170. break;
  171. case BattleHex::CASTLE_UPPER_TOWER: // upper creature
  172. posID = 20;
  173. break;
  174. }
  175. if (posID != 0)
  176. {
  177. Point result = owner->pos.topLeft();
  178. result.x += town->town->clientInfo.siegePositions[posID].x;
  179. result.y += town->town->clientInfo.siegePositions[posID].y;
  180. return result;
  181. }
  182. assert(0);
  183. return Point(0,0);
  184. }
  185. void CBattleSiegeController::gateStateChanged(const EGateState state)
  186. {
  187. auto oldState = owner->curInt->cb->battleGetGateState();
  188. bool playSound = false;
  189. int stateId = EWallState::NONE;
  190. switch(state)
  191. {
  192. case EGateState::CLOSED:
  193. if (oldState != EGateState::BLOCKED)
  194. playSound = true;
  195. break;
  196. case EGateState::BLOCKED:
  197. if (oldState != EGateState::CLOSED)
  198. playSound = true;
  199. break;
  200. case EGateState::OPENED:
  201. playSound = true;
  202. stateId = EWallState::DAMAGED;
  203. break;
  204. case EGateState::DESTROYED:
  205. stateId = EWallState::DESTROYED;
  206. break;
  207. }
  208. if (oldState != EGateState::NONE && oldState != EGateState::CLOSED && oldState != EGateState::BLOCKED)
  209. SDL_FreeSurface(walls[EWallVisual::GATE]);
  210. if (stateId != EWallState::NONE)
  211. walls[EWallVisual::GATE] = BitmapHandler::loadBitmap(getSiegeName(EWallVisual::GATE, stateId));
  212. if (playSound)
  213. CCS->soundh->playSound(soundBase::DRAWBRG);
  214. }
  215. void CBattleSiegeController::showAbsoluteObstacles(SDL_Surface * to)
  216. {
  217. ui8 siegeLevel = owner->curInt->cb->battleGetSiegeLevel();
  218. if (siegeLevel >= 2) //citadel or castle
  219. {
  220. //print moat/mlip
  221. auto & info = town->town->clientInfo;
  222. Point moatPos(info.siegePositions[13].x, info.siegePositions[13].y);
  223. Point mlipPos(info.siegePositions[14].x, info.siegePositions[14].y);
  224. if (moatSurface) //eg. tower has no moat
  225. blitAt(moatSurface, moatPos.x + owner->pos.x, moatPos.y + owner->pos.y, to);
  226. if (mlipSurface) //eg. tower has no mlip
  227. blitAt(mlipSurface, mlipPos.x + owner->pos.x, mlipPos.y + owner->pos.y, to);
  228. printPartOfWall(to, EWallVisual::BACKGROUND_MOAT);
  229. }
  230. }
  231. void CBattleSiegeController::sortObjectsByHex(BattleObjectsByHex & sorted)
  232. {
  233. sorted.beforeAll.walls.push_back(EWallVisual::BACKGROUND_WALL);
  234. sorted.hex[135].walls.push_back(EWallVisual::KEEP);
  235. sorted.afterAll.walls.push_back(EWallVisual::BOTTOM_TOWER);
  236. sorted.hex[182].walls.push_back(EWallVisual::BOTTOM_WALL);
  237. sorted.hex[130].walls.push_back(EWallVisual::WALL_BELLOW_GATE);
  238. sorted.hex[78].walls.push_back(EWallVisual::WALL_OVER_GATE);
  239. sorted.hex[12].walls.push_back(EWallVisual::UPPER_WALL);
  240. sorted.beforeAll.walls.push_back(EWallVisual::UPPER_TOWER);
  241. sorted.hex[94].walls.push_back(EWallVisual::GATE);
  242. sorted.hex[112].walls.push_back(EWallVisual::GATE_ARCH);
  243. sorted.hex[165].walls.push_back(EWallVisual::BOTTOM_STATIC_WALL);
  244. sorted.hex[45].walls.push_back(EWallVisual::UPPER_STATIC_WALL);
  245. if (town->hasBuilt(BuildingID::CITADEL))
  246. {
  247. sorted.beforeAll.walls.push_back(EWallVisual::MOAT);
  248. //sorted.beforeAll.walls.push_back(EWallVisual::BACKGROUND_MOAT); // blit as absolute obstacle
  249. sorted.hex[135].walls.push_back(EWallVisual::KEEP_BATTLEMENT);
  250. }
  251. if (town->hasBuilt(BuildingID::CASTLE))
  252. {
  253. sorted.afterAll.walls.push_back(EWallVisual::BOTTOM_BATTLEMENT);
  254. sorted.beforeAll.walls.push_back(EWallVisual::UPPER_BATTLEMENT);
  255. }
  256. }
  257. void CBattleSiegeController::showPiecesOfWall(SDL_Surface *to, std::vector<int> pieces)
  258. {
  259. for (auto piece : pieces)
  260. {
  261. if (piece < 15) // not a tower - just print
  262. printPartOfWall(to, piece);
  263. else // tower. find if tower is built and not destroyed - stack is present
  264. {
  265. // PieceID StackID
  266. // 15 = keep, -2
  267. // 16 = lower, -3
  268. // 17 = upper, -4
  269. // tower. check if tower is alive - stack is found
  270. int stackPos = 13 - piece;
  271. const CStack *turret = nullptr;
  272. for (auto & stack : owner->curInt->cb->battleGetAllStacks(true))
  273. {
  274. if(stack->initialPosition == stackPos)
  275. {
  276. turret = stack;
  277. break;
  278. }
  279. }
  280. if (turret)
  281. {
  282. std::vector<const CStack *> stackList(1, turret);
  283. owner->stacksController->showStacks(to, stackList);
  284. printPartOfWall(to, piece);
  285. }
  286. }
  287. }
  288. }
  289. bool CBattleSiegeController::isCatapultAttackable(BattleHex hex) const
  290. {
  291. if (owner->tacticsMode)
  292. return false;
  293. auto wallPart = owner->curInt->cb->battleHexToWallPart(hex);
  294. if (!owner->curInt->cb->isWallPartPotentiallyAttackable(wallPart))
  295. return false;
  296. auto state = owner->curInt->cb->battleGetWallState(static_cast<int>(wallPart));
  297. return state != EWallState::DESTROYED && state != EWallState::NONE;
  298. }
  299. void CBattleSiegeController::stackIsCatapulting(const CatapultAttack & ca)
  300. {
  301. if (ca.attacker != -1)
  302. {
  303. const CStack *stack = owner->curInt->cb->battleGetStackByID(ca.attacker);
  304. for (auto attackInfo : ca.attackedParts)
  305. {
  306. owner->stacksController->addNewAnim(new CShootingAnimation(owner, stack, attackInfo.destinationTile, nullptr, true, attackInfo.damageDealt));
  307. }
  308. }
  309. else
  310. {
  311. //no attacker stack, assume spell-related (earthquake) - only hit animation
  312. for (auto attackInfo : ca.attackedParts)
  313. {
  314. Point destPos = CClickableHex::getXYUnitAnim(attackInfo.destinationTile, nullptr, owner) + Point(99, 120);
  315. owner->stacksController->addNewAnim(new CEffectAnimation(owner, "SGEXPL.DEF", destPos.x, destPos.y));
  316. }
  317. }
  318. owner->waitForAnims();
  319. for (auto attackInfo : ca.attackedParts)
  320. {
  321. int wallId = attackInfo.attackedPart + 2;
  322. //gate state changing handled separately
  323. if (wallId == EWallVisual::GATE)
  324. continue;
  325. SDL_FreeSurface(walls[wallId]);
  326. walls[wallId] = BitmapHandler::loadBitmap(
  327. getSiegeName(wallId, owner->curInt->cb->battleGetWallState(attackInfo.attackedPart)));
  328. }
  329. }