CBattleSiegeController.cpp 11 KB

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