CBattleInterface.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. #include "CBattleInterface.h"
  2. #include "CGameInfo.h"
  3. #include "hch/CLodHandler.h"
  4. #include "SDL_Extensions.h"
  5. #include "CAdvmapInterface.h"
  6. #include "AdventureMapButton.h"
  7. #include "hch/CObjectHandler.h"
  8. #include "hch/CHeroHandler.h"
  9. #include "hch/CDefHandler.h"
  10. #include "CCursorHandler.h"
  11. #include "CCallback.h"
  12. #include "CGameState.h"
  13. #include "hch/CGeneralTextHandler.h"
  14. #include "hch/CPreGameTextHandler.h"
  15. #include "client/CCreatureAnimation.h"
  16. #include "client/Graphics.h"
  17. #include "client/CSpellWindow.h"
  18. #include <queue>
  19. #include <sstream>
  20. #include "lib/CondSh.h"
  21. #include "lib/NetPacks.h"
  22. #include <boost/assign/list_of.hpp>
  23. #ifndef __GNUC__
  24. const double M_PI = 3.14159265358979323846;
  25. #else
  26. #define _USE_MATH_DEFINES
  27. #include <cmath>
  28. #endif
  29. extern SDL_Surface * screen;
  30. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  31. extern SDL_Color zwykly;
  32. class CMP_stack2
  33. {
  34. public:
  35. bool operator ()(const CStack& a, const CStack& b)
  36. {
  37. return (a.creature->speed)>(b.creature->speed);
  38. }
  39. } cmpst2 ;
  40. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  41. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL), attackingInfo(NULL), myTurn(false), resWindow(NULL), showStackQueue(false), animSpeed(2), printStackRange(true), printMouseShadow(true)
  42. {
  43. strongInterest = true;
  44. givenCommand = new CondSh<BattleAction *>(NULL);
  45. //initializing armies
  46. this->army1 = army1;
  47. this->army2 = army2;
  48. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  49. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  50. {
  51. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  52. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  53. creAnims[b->second.ID]->setType(2);
  54. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  55. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  56. }
  57. //preparing menu background and terrain
  58. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  59. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  60. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  61. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  62. //preparing graphics for displaying amounts of creatures
  63. amountBasic = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  64. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  65. CSDL_Ext::alphaTransform(amountNormal);
  66. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  67. {
  68. if((amountNormal->format->palette->colors+g)->b != 132 &&
  69. (amountNormal->format->palette->colors+g)->g != 231 &&
  70. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  71. {
  72. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  73. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  74. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  75. }
  76. }
  77. ////blitting menu background and terrain
  78. blitAt(background, 0, 0);
  79. blitAt(menu, 0, 556);
  80. CSDL_Ext::update();
  81. //preparing buttons and console
  82. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  83. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  84. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  85. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  86. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  87. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  88. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  89. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  90. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", false, NULL, false);
  91. bConsoleDown->bitmapOffset = 2;
  92. console = new CBattleConsole();
  93. console->pos.x = 211;
  94. console->pos.y = 560;
  95. console->pos.w = 406;
  96. console->pos.h = 38;
  97. //loading hero animations
  98. if(hero1) // attacking hero
  99. {
  100. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner, hero1->tempOwner == LOCPLINT->playerID ? hero1 : NULL);
  101. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  102. }
  103. else
  104. {
  105. attackingHero = NULL;
  106. }
  107. if(hero2) // defending hero
  108. {
  109. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner, hero2->tempOwner == LOCPLINT->playerID ? hero2 : NULL);
  110. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  111. }
  112. else
  113. {
  114. defendingHero = NULL;
  115. }
  116. //preparing cells and hexes
  117. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  118. CSDL_Ext::alphaTransform(cellBorder);
  119. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  120. CSDL_Ext::alphaTransform(cellShade);
  121. for(int h=0; h<187; ++h)
  122. {
  123. bfield[h].myNumber = h;
  124. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  125. int y = 86 + 42 * (h/17);
  126. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  127. bfield[h].accesible = true;
  128. bfield[h].myInterface = this;
  129. }
  130. //locking occupied positions on batlefield
  131. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  132. {
  133. bfield[it->second.position].accesible = false;
  134. }
  135. //loading projectiles for units
  136. for(std::map<int, CStack>::iterator g = stacks.begin(); g != stacks.end(); ++g)
  137. {
  138. if(g->second.creature->isShooting() && CGI->creh->idToProjectile[g->second.creature->idNumber] != std::string())
  139. {
  140. idToProjectile[g->second.creature->idNumber] = CDefHandler::giveDef(CGI->creh->idToProjectile[g->second.creature->idNumber]);
  141. if(idToProjectile[g->second.creature->idNumber]->ourImages.size() > 2) //add symmetric images
  142. {
  143. for(int k = idToProjectile[g->second.creature->idNumber]->ourImages.size()-2; k > 1; --k)
  144. {
  145. Cimage ci;
  146. ci.bitmap = CSDL_Ext::rotate01(idToProjectile[g->second.creature->idNumber]->ourImages[k].bitmap);
  147. ci.groupNumber = 0;
  148. ci.imName = std::string();
  149. idToProjectile[g->second.creature->idNumber]->ourImages.push_back(ci);
  150. }
  151. }
  152. for(int s=0; s<idToProjectile[g->second.creature->idNumber]->ourImages.size(); ++s) //alpha transforming
  153. {
  154. CSDL_Ext::alphaTransform(idToProjectile[g->second.creature->idNumber]->ourImages[s].bitmap);
  155. }
  156. }
  157. }
  158. //prepairing graphic with cell borders
  159. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  160. //copying palette
  161. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  162. {
  163. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  164. }
  165. //palette copied
  166. for(int i=0; i<11; ++i) //rows
  167. {
  168. for(int j=0; j<15; ++j) //columns
  169. {
  170. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  171. int y = 86 + 42 * i;
  172. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  173. {
  174. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  175. {
  176. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  177. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  178. }
  179. }
  180. }
  181. }
  182. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  183. }
  184. CBattleInterface::~CBattleInterface()
  185. {
  186. SDL_FreeSurface(background);
  187. SDL_FreeSurface(menu);
  188. SDL_FreeSurface(amountBasic);
  189. SDL_FreeSurface(amountNormal);
  190. SDL_FreeSurface(cellBorders);
  191. SDL_FreeSurface(backgroundWithHexes);
  192. delete bOptions;
  193. delete bSurrender;
  194. delete bFlee;
  195. delete bAutofight;
  196. delete bSpell;
  197. delete bWait;
  198. delete bDefence;
  199. delete bConsoleUp;
  200. delete bConsoleDown;
  201. delete console;
  202. delete resWindow;
  203. delete givenCommand;
  204. delete attackingHero;
  205. delete defendingHero;
  206. SDL_FreeSurface(cellBorder);
  207. SDL_FreeSurface(cellShade);
  208. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  209. delete g->second;
  210. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  211. delete g->second;
  212. }
  213. void CBattleInterface::setPrintCellBorders(bool set)
  214. {
  215. printCellBorders = set;
  216. redrawBackgroundWithHexes(activeStack);
  217. }
  218. void CBattleInterface::setPrintStackRange(bool set)
  219. {
  220. printStackRange = set;
  221. redrawBackgroundWithHexes(activeStack);
  222. }
  223. void CBattleInterface::setPrintMouseShadow(bool set)
  224. {
  225. printMouseShadow = set;
  226. }
  227. void CBattleInterface::activate()
  228. {
  229. MotionInterested::activate();
  230. subInt = NULL;
  231. bOptions->activate();
  232. bSurrender->activate();
  233. bFlee->activate();
  234. bAutofight->activate();
  235. bSpell->activate();
  236. bWait->activate();
  237. bDefence->activate();
  238. bConsoleUp->activate();
  239. bConsoleDown->activate();
  240. for(int b=0; b<187; ++b)
  241. {
  242. bfield[b].activate();
  243. }
  244. if(attackingHero)
  245. attackingHero->activate();
  246. if(defendingHero)
  247. defendingHero->activate();
  248. }
  249. void CBattleInterface::deactivate()
  250. {
  251. MotionInterested::deactivate();
  252. bOptions->deactivate();
  253. bSurrender->deactivate();
  254. bFlee->deactivate();
  255. bAutofight->deactivate();
  256. bSpell->deactivate();
  257. bWait->deactivate();
  258. bDefence->deactivate();
  259. bConsoleUp->deactivate();
  260. bConsoleDown->deactivate();
  261. for(int b=0; b<187; ++b)
  262. {
  263. bfield[b].deactivate();
  264. }
  265. if(attackingHero)
  266. attackingHero->deactivate();
  267. if(defendingHero)
  268. defendingHero->deactivate();
  269. }
  270. void CBattleInterface::show(SDL_Surface * to)
  271. {
  272. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  273. ++animCount;
  274. if(!to) //"evaluating" to
  275. to = screen;
  276. //printing background and hexes
  277. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //show everything with range
  278. {
  279. blitAt(backgroundWithHexes, 0, 0, to);
  280. }
  281. else
  282. {
  283. //showing background
  284. blitAt(background, 0, 0, to);
  285. if(printCellBorders)
  286. {
  287. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, NULL);
  288. }
  289. }
  290. //printing hovered cell
  291. if(printMouseShadow)
  292. {
  293. for(int b=0; b<187; ++b)
  294. {
  295. if(bfield[b].strictHovered && bfield[b].hovered)
  296. {
  297. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  298. int y = 86 + 42 * (b/17);
  299. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  300. }
  301. }
  302. }
  303. //showing menu background and console
  304. blitAt(menu, 0, 556, to);
  305. console->show(to);
  306. //showing buttons
  307. bOptions->show(to);
  308. bSurrender->show(to);
  309. bFlee->show(to);
  310. bAutofight->show(to);
  311. bSpell->show(to);
  312. bWait->show(to);
  313. bDefence->show(to);
  314. bConsoleUp->show(to);
  315. bConsoleDown->show(to);
  316. //showing hero animations
  317. if(attackingHero)
  318. attackingHero->show(to);
  319. if(defendingHero)
  320. defendingHero->show(to);
  321. ////showing units //a lot of work...
  322. std::vector<int> stackAliveByHex[187];
  323. //double loop because dead stacks should be printed first
  324. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  325. {
  326. if(j->second.alive())
  327. stackAliveByHex[j->second.position].push_back(j->second.ID);
  328. }
  329. std::vector<int> stackDeadByHex[187];
  330. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  331. {
  332. if(!j->second.alive())
  333. stackDeadByHex[j->second.position].push_back(j->second.ID);
  334. }
  335. attackingShowHelper(); // handle attack animation
  336. for(int b=0; b<187; ++b) //showing dead stacks
  337. {
  338. for(int v=0; v<stackDeadByHex[b].size(); ++v)
  339. {
  340. creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x, creAnims[stackDeadByHex[b][v]]->pos.y, creDir[stackDeadByHex[b][v]], (animCount%(4/animSpeed)==0) && creAnims[stackDeadByHex[b][v]]->getType()!=5, stackDeadByHex[b][v]==activeStack); //increment always when moving, never if stack died
  341. //printing amount
  342. if(stacks[stackDeadByHex[b][v]].amount > 0) //don't print if stack is not alive
  343. {
  344. int xAdd = stacks[stackDeadByHex[b][v]].attackerOwned ? 220 : 202;
  345. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackDeadByHex[b][v]]->pos.x + xAdd, creAnims[stackDeadByHex[b][v]]->pos.y + 260));
  346. std::stringstream ss;
  347. ss<<stacks[stackDeadByHex[b][v]].amount;
  348. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackDeadByHex[b][v]]->pos.x + xAdd + 14, creAnims[stackDeadByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  349. }
  350. }
  351. }
  352. for(int b=0; b<187; ++b) //showing alive stacks
  353. {
  354. for(int v=0; v<stackAliveByHex[b].size(); ++v)
  355. {
  356. creAnims[stackAliveByHex[b][v]]->nextFrame(to, creAnims[stackAliveByHex[b][v]]->pos.x, creAnims[stackAliveByHex[b][v]]->pos.y, creDir[stackAliveByHex[b][v]], (animCount%(4/animSpeed)==0) && creAnims[stackAliveByHex[b][v]]->getType()!=0 && creAnims[stackAliveByHex[b][v]]->getType()!=20 && creAnims[stackAliveByHex[b][v]]->getType()!=21, stackAliveByHex[b][v]==activeStack); //increment always when moving, never if stack died
  357. //printing amount
  358. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  359. {
  360. int xAdd = stacks[stackAliveByHex[b][v]].attackerOwned ? 220 : 202;
  361. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackAliveByHex[b][v]]->pos.x + xAdd, creAnims[stackAliveByHex[b][v]]->pos.y + 260));
  362. std::stringstream ss;
  363. ss<<stacks[stackAliveByHex[b][v]].amount;
  364. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackAliveByHex[b][v]]->pos.x + xAdd + 14, creAnims[stackAliveByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  365. }
  366. }
  367. }
  368. //units shown
  369. projectileShowHelper(to);//showing projectiles
  370. //showing queue of stacks
  371. if(showStackQueue)
  372. {
  373. int xPos = 400 - ( stacks.size() * 42 )/2;
  374. int yPos = 10;
  375. std::vector<CStack> stacksSorted;
  376. for(int v=0; v<stacks.size(); ++v)
  377. {
  378. if(stacks[v].alive()) //we don't want dead stacks to be there
  379. stacksSorted.push_back(stacks[v]);
  380. }
  381. std::stable_sort(stacksSorted.begin(), stacksSorted.end(), cmpst2);
  382. int startFrom = -1;
  383. for(int n=0; n<stacksSorted.size(); ++n)
  384. {
  385. if(stacksSorted[n].ID == activeStack)
  386. {
  387. startFrom = n;
  388. break;
  389. }
  390. }
  391. if(startFrom != -1)
  392. {
  393. for(int b=startFrom; b<stacksSorted.size()+startFrom; ++b)
  394. {
  395. SDL_BlitSurface(graphics->smallImgs[-2], NULL, to, &genRect(32, 32, xPos, yPos));
  396. //printing colored border
  397. for(int xFrom = xPos-1; xFrom<xPos+33; ++xFrom)
  398. {
  399. for(int yFrom = yPos-1; yFrom<yPos+33; ++yFrom)
  400. {
  401. if(xFrom == xPos-1 || xFrom == xPos+32 || yFrom == yPos-1 || yFrom == yPos+32)
  402. {
  403. SDL_Color pc;
  404. if(stacksSorted[b % stacksSorted.size()].owner != 255)
  405. {
  406. pc = graphics->playerColors[stacksSorted[b % stacksSorted.size()].owner];
  407. }
  408. else
  409. {
  410. pc = *graphics->neutralColor;
  411. }
  412. CSDL_Ext::SDL_PutPixel(to, xFrom, yFrom, pc.r, pc.g, pc.b);
  413. }
  414. }
  415. }
  416. //colored border printed
  417. SDL_BlitSurface(graphics->smallImgs[stacksSorted[b % stacksSorted.size()].creature->idNumber], NULL, to, &genRect(32, 32, xPos, yPos));
  418. xPos += 42;
  419. }
  420. }
  421. }
  422. //showing window with result of battle
  423. if(resWindow)
  424. {
  425. resWindow->show(to);
  426. }
  427. }
  428. void CBattleInterface::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  429. {
  430. if(activeStack>=0)
  431. {
  432. int myNumber = -1; //number of hovered tile
  433. for(int g=0; g<187; ++g)
  434. {
  435. if(bfield[g].hovered && bfield[g].strictHovered)
  436. {
  437. myNumber = g;
  438. break;
  439. }
  440. }
  441. if(myNumber == -1)
  442. {
  443. CGI->curh->changeGraphic(1, 6);
  444. }
  445. else
  446. {
  447. if(std::find(shadedHexes.begin(),shadedHexes.end(),myNumber) == shadedHexes.end())
  448. {
  449. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  450. if(shere)
  451. {
  452. if(shere->owner == LOCPLINT->playerID) //our stack
  453. CGI->curh->changeGraphic(1,5);
  454. else if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isShooting()) //we can shoot enemy
  455. CGI->curh->changeGraphic(1,3);
  456. else if(isTileAttackable(myNumber)) //available enemy (melee attackable)
  457. {
  458. int fromHex = -1;
  459. for(int b=0; b<187; ++b)
  460. if(bfield[b].hovered && !bfield[b].strictHovered)
  461. {
  462. fromHex = b;
  463. break;
  464. }
  465. if(fromHex!=-1 && fromHex%17!=0 && fromHex%17!=16 && vstd::contains(shadedHexes, fromHex))
  466. {
  467. switch(BattleInfo::mutualPosition(fromHex, myNumber))
  468. {
  469. case 0:
  470. CGI->curh->changeGraphic(1,12);
  471. break;
  472. case 1:
  473. CGI->curh->changeGraphic(1,7);
  474. break;
  475. case 2:
  476. CGI->curh->changeGraphic(1,8);
  477. break;
  478. case 3:
  479. CGI->curh->changeGraphic(1,9);
  480. break;
  481. case 4:
  482. CGI->curh->changeGraphic(1,10);
  483. break;
  484. case 5:
  485. CGI->curh->changeGraphic(1,11);
  486. break;
  487. }
  488. }
  489. }
  490. else //unavailable enemy
  491. CGI->curh->changeGraphic(1,0);
  492. }
  493. else //empty unavailable tile
  494. CGI->curh->changeGraphic(1,0);
  495. }
  496. else //available tile
  497. {
  498. if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isFlying())
  499. CGI->curh->changeGraphic(1,2);
  500. else
  501. CGI->curh->changeGraphic(1,1);
  502. }
  503. }
  504. }
  505. }
  506. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  507. {
  508. if(creAnims[number]==NULL)
  509. return false; //there is no such creature
  510. creAnims[number]->setType(8);
  511. int firstFrame = creAnims[number]->getFrame();
  512. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(8) + firstFrame - 1; ++g)
  513. {
  514. show();
  515. CSDL_Ext::update();
  516. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  517. if((animCount+1)%(4/animSpeed)==0)
  518. creAnims[number]->incrementFrame();
  519. }
  520. creDir[number] = !creDir[number];
  521. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  522. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  523. creAnims[number]->pos.x = coords.first;
  524. //creAnims[number]->pos.y = coords.second;
  525. if(wideTrick && curs.creature->isDoubleWide())
  526. {
  527. if(!creDir[number])
  528. creAnims[number]->pos.x -= 44;
  529. }
  530. creAnims[number]->setType(7);
  531. firstFrame = creAnims[number]->getFrame();
  532. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(7) + firstFrame - 1; ++g)
  533. {
  534. show();
  535. CSDL_Ext::update();
  536. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  537. }
  538. creAnims[number]->setType(2);
  539. return true;
  540. }
  541. void CBattleInterface::bOptionsf()
  542. {
  543. CGI->curh->changeGraphic(0,0);
  544. deactivate();
  545. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  546. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  547. optionsWin->activate();
  548. LOCPLINT->objsToBlit.push_back(optionsWin);
  549. }
  550. void CBattleInterface::bSurrenderf()
  551. {
  552. }
  553. void CBattleInterface::bFleef()
  554. {
  555. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  556. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  557. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  558. }
  559. void CBattleInterface::reallyFlee()
  560. {
  561. giveCommand(4,0,0);
  562. CGI->curh->changeGraphic(0, 0);
  563. }
  564. void CBattleInterface::bAutofightf()
  565. {
  566. }
  567. void CBattleInterface::bSpellf()
  568. {
  569. CGI->curh->changeGraphic(0,0);
  570. deactivate();
  571. const CGHeroInstance * chi = NULL;
  572. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  573. chi = attackingHeroInstance;
  574. else
  575. chi = defendingHeroInstance;
  576. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), chi);
  577. spellWindow->activate();
  578. LOCPLINT->objsToBlit.push_back(spellWindow);
  579. }
  580. void CBattleInterface::bWaitf()
  581. {
  582. }
  583. void CBattleInterface::bDefencef()
  584. {
  585. giveCommand(3,0,activeStack);
  586. }
  587. void CBattleInterface::bConsoleUpf()
  588. {
  589. console->scrollUp();
  590. }
  591. void CBattleInterface::bConsoleDownf()
  592. {
  593. console->scrollDown();
  594. }
  595. void CBattleInterface::newStack(CStack stack)
  596. {
  597. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  598. creAnims[stack.ID]->setType(2);
  599. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  600. }
  601. void CBattleInterface::stackRemoved(CStack stack)
  602. {
  603. delete creAnims[stack.ID];
  604. creAnims.erase(stack.ID);
  605. }
  606. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  607. {
  608. if(creAnims[ID]->getType() != 2)
  609. {
  610. return; //something went wrong
  611. }
  612. if(byShooting) //delay hit animation
  613. {
  614. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  615. while(true)
  616. {
  617. bool found = false;
  618. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  619. {
  620. if(it->creID == attacker.creature->idNumber)
  621. {
  622. found = true;
  623. break;
  624. }
  625. }
  626. if(!found)
  627. break;
  628. else
  629. {
  630. show();
  631. CSDL_Ext::update();
  632. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  633. }
  634. }
  635. }
  636. creAnims[ID]->setType(5); //death
  637. int firstFrame = creAnims[ID]->getFrame();
  638. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(5) + firstFrame - 1; ++i)
  639. {
  640. if((animCount%(4/animSpeed))==0)
  641. creAnims[ID]->incrementFrame();
  642. show();
  643. CSDL_Ext::update();
  644. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  645. }
  646. printConsoleAttacked(ID, dmg, killed, IDby);
  647. }
  648. void CBattleInterface::stackActivated(int number)
  649. {
  650. //givenCommand = NULL;
  651. activeStack = number;
  652. myTurn = true;
  653. redrawBackgroundWithHexes(number);
  654. }
  655. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving)
  656. {
  657. bool startMoving = creAnims[number]->type==20;
  658. //a few useful variables
  659. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  660. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  661. int hexWbase = 44, hexHbase = 42;
  662. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  663. deactivate();
  664. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  665. {
  666. CGI->curh->hide();
  667. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  668. {
  669. show();
  670. CSDL_Ext::update();
  671. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  672. if((animCount+1)%(4/animSpeed)==0)
  673. creAnims[number]->incrementFrame();
  674. }
  675. }
  676. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  677. {
  678. switch(mutPos) //reverse unit if necessary
  679. {
  680. case 0: case 4: case 5:
  681. if(creDir[number] == true)
  682. reverseCreature(number, curStackPos, twoTiles);
  683. break;
  684. case 1: case 2: case 3:
  685. if(creDir[number] == false)
  686. reverseCreature(number, curStackPos, twoTiles);
  687. break;
  688. }
  689. //moving instructions
  690. creAnims[number]->setType(0);
  691. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  692. for(int i=0; i<steps; ++i)
  693. {
  694. switch(mutPos)
  695. {
  696. case 0:
  697. posX -= ((float)hexWbase)/(2.0f*steps);
  698. creAnims[number]->pos.x = posX;
  699. posY -= ((float)hexHbase)/((float)steps);
  700. creAnims[number]->pos.y = posY;
  701. break;
  702. case 1:
  703. posX += ((float)hexWbase)/(2.0f*steps);
  704. creAnims[number]->pos.x = posX;
  705. posY -= ((float)hexHbase)/((float)steps);
  706. creAnims[number]->pos.y = posY;
  707. break;
  708. case 2:
  709. posX += ((float)hexWbase)/((float)steps);
  710. creAnims[number]->pos.x = posX;
  711. break;
  712. case 3:
  713. posX += ((float)hexWbase)/(2.0f*steps);
  714. creAnims[number]->pos.x = posX;
  715. posY += ((float)hexHbase)/((float)steps);
  716. creAnims[number]->pos.y = posY;
  717. break;
  718. case 4:
  719. posX -= ((float)hexWbase)/(2.0f*steps);
  720. creAnims[number]->pos.x = posX;
  721. posY += ((float)hexHbase)/((float)steps);
  722. creAnims[number]->pos.y = posY;
  723. break;
  724. case 5:
  725. posX -= ((float)hexWbase)/((float)steps);
  726. creAnims[number]->pos.x = posX;
  727. break;
  728. }
  729. show();
  730. CSDL_Ext::update();
  731. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  732. if((animCount+1)%(4/animSpeed)==0)
  733. creAnims[number]->incrementFrame();
  734. }
  735. }
  736. if(endMoving) //animation of ending move
  737. {
  738. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  739. {
  740. creAnims[number]->setType(21);
  741. for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  742. {
  743. show();
  744. CSDL_Ext::update();
  745. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  746. if((animCount+1)%(4/animSpeed)==0)
  747. creAnims[number]->incrementFrame();
  748. }
  749. }
  750. creAnims[number]->setType(2); //resetting to default
  751. CGI->curh->show();
  752. }
  753. activate();
  754. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  755. if(endMoving) //resetting to default
  756. {
  757. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  758. reverseCreature(number, destHex, twoTiles);
  759. }
  760. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  761. creAnims[number]->pos.x = coords.first;
  762. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  763. creAnims[number]->pos.x -= 44;
  764. creAnims[number]->pos.y = coords.second;
  765. }
  766. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  767. {
  768. while(creAnims[ID]->getType() != 2 || (attackingInfo && attackingInfo->IDby == IDby))
  769. {
  770. show();
  771. CSDL_Ext::update();
  772. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  773. }
  774. if(byShooting) //delay hit animation
  775. {
  776. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  777. while(true)
  778. {
  779. bool found = false;
  780. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  781. {
  782. if(it->creID == attacker.creature->idNumber)
  783. {
  784. found = true;
  785. break;
  786. }
  787. }
  788. if(!found)
  789. break;
  790. else
  791. {
  792. show();
  793. CSDL_Ext::update();
  794. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  795. }
  796. }
  797. }
  798. creAnims[ID]->setType(3); //getting hit
  799. int firstFrame = creAnims[ID]->getFrame();
  800. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(3) + firstFrame - 1; ++i)
  801. {
  802. show();
  803. CSDL_Ext::update();
  804. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  805. /*if((animCount+1)%(4/animSpeed)==0)
  806. creAnims[ID]->incrementFrame();*/
  807. }
  808. creAnims[ID]->setType(2);
  809. printConsoleAttacked(ID, dmg, killed, IDby);
  810. }
  811. void CBattleInterface::stackAttacking(int ID, int dest)
  812. {
  813. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  814. {
  815. show();
  816. CSDL_Ext::update();
  817. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  818. }
  819. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  820. if(aStack.attackerOwned)
  821. {
  822. if(aStack.creature->isDoubleWide())
  823. {
  824. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  825. {
  826. case 0:
  827. //reverseCreature(ID, aStack.position, true);
  828. break;
  829. case 1:
  830. break;
  831. case 2:
  832. break;
  833. case 3:
  834. break;
  835. case 4:
  836. //reverseCreature(ID, aStack.position, true);
  837. break;
  838. case 5:
  839. reverseCreature(ID, aStack.position, true);
  840. break;
  841. }
  842. }
  843. else //else for if(aStack.creature->isDoubleWide())
  844. {
  845. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  846. {
  847. case 0:
  848. reverseCreature(ID, aStack.position, true);
  849. break;
  850. case 1:
  851. break;
  852. case 2:
  853. break;
  854. case 3:
  855. break;
  856. case 4:
  857. reverseCreature(ID, aStack.position, true);
  858. break;
  859. case 5:
  860. reverseCreature(ID, aStack.position, true);
  861. break;
  862. }
  863. }
  864. }
  865. else //if(aStack.attackerOwned)
  866. {
  867. if(aStack.creature->isDoubleWide())
  868. {
  869. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  870. {
  871. case 0:
  872. //reverseCreature(ID, aStack.position, true);
  873. break;
  874. case 1:
  875. break;
  876. case 2:
  877. reverseCreature(ID, aStack.position, true);
  878. break;
  879. case 3:
  880. break;
  881. case 4:
  882. //reverseCreature(ID, aStack.position, true);
  883. break;
  884. case 5:
  885. //reverseCreature(ID, aStack.position, true);
  886. break;
  887. }
  888. }
  889. else //else for if(aStack.creature->isDoubleWide())
  890. {
  891. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  892. {
  893. case 0:
  894. //reverseCreature(ID, aStack.position, true);
  895. break;
  896. case 1:
  897. reverseCreature(ID, aStack.position, true);
  898. break;
  899. case 2:
  900. reverseCreature(ID, aStack.position, true);
  901. break;
  902. case 3:
  903. reverseCreature(ID, aStack.position, true);
  904. break;
  905. case 4:
  906. //reverseCreature(ID, aStack.position, true);
  907. break;
  908. case 5:
  909. //reverseCreature(ID, aStack.position, true);
  910. break;
  911. }
  912. }
  913. }
  914. attackingInfo = new CAttHelper;
  915. attackingInfo->dest = dest;
  916. attackingInfo->frame = 0;
  917. attackingInfo->hitCount = 0;
  918. attackingInfo->ID = ID;
  919. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  920. attackingInfo->reversing = false;
  921. attackingInfo->shooting = false;
  922. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  923. {
  924. case 0:
  925. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  926. break;
  927. case 1:
  928. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  929. break;
  930. case 2:
  931. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  932. break;
  933. case 3:
  934. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  935. break;
  936. case 4:
  937. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  938. break;
  939. case 5:
  940. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  941. break;
  942. default:
  943. tlog1<<"Critical Error! Wrong dest in stackAttacking!"<<std::endl;
  944. }
  945. }
  946. void CBattleInterface::newRound(int number)
  947. {
  948. console->addText(CGI->generaltexth->allTexts[412]);
  949. }
  950. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  951. {
  952. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  953. ba->actionType = action;
  954. ba->destinationTile = tile;
  955. ba->stackNumber = stack;
  956. ba->additionalInfo = additional;
  957. givenCommand->setn(ba);
  958. myTurn = false;
  959. activeStack = -1;
  960. }
  961. bool CBattleInterface::isTileAttackable(int number)
  962. {
  963. for(int b=0; b<shadedHexes.size(); ++b)
  964. {
  965. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  966. return true;
  967. }
  968. return false;
  969. }
  970. void CBattleInterface::hexLclicked(int whichOne)
  971. {
  972. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  973. {
  974. if(!myTurn)
  975. return; //we are not permit to do anything
  976. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  977. if(!dest || !dest->alive()) //no creature at that tile
  978. {
  979. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  980. giveCommand(2,whichOne,activeStack);
  981. }
  982. else if(dest->owner != attackingHeroInstance->tempOwner
  983. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)
  984. && BattleInfo::mutualPosition(LOCPLINT->cb->battleGetPos(activeStack),whichOne) < 0 ) //shooting
  985. {
  986. giveCommand(7,whichOne,activeStack);
  987. }
  988. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  989. {
  990. //std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  991. //for(int i=0;i<n.size();i++)
  992. //{
  993. // //TODO: now we are using first available tile, but in the future we should add possibility of choosing from which tile we want to attack
  994. // if(vstd::contains(shadedHexes,n[i]))
  995. // {
  996. // giveCommand(6,n[i],activeStack,whichOne);
  997. // return;
  998. // }
  999. //}
  1000. switch(CGI->curh->number)
  1001. {
  1002. case 12:
  1003. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1004. break;
  1005. case 7:
  1006. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1007. break;
  1008. case 8:
  1009. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1010. break;
  1011. case 9:
  1012. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1013. break;
  1014. case 10:
  1015. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1016. break;
  1017. case 11:
  1018. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. void CBattleInterface::stackIsShooting(int ID, int dest)
  1025. {
  1026. if(attackingInfo != NULL)
  1027. {
  1028. return; //something went wrong
  1029. }
  1030. //projectile
  1031. float projectileAngle; //in radians; if positive, projectiles goes up
  1032. float straightAngle = 0.2f; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  1033. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1034. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1035. if(fromHex < dest)
  1036. projectileAngle = -projectileAngle;
  1037. SProjectileInfo spi;
  1038. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1039. spi.step = 0;
  1040. spi.frameNum = 0;
  1041. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1042. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1043. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1044. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1045. if(projectileAngle > straightAngle) //upper shot
  1046. {
  1047. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1048. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1049. }
  1050. else if(projectileAngle < -straightAngle) //lower shot
  1051. {
  1052. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1053. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1054. }
  1055. else //straight shot
  1056. {
  1057. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1058. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1059. }
  1060. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1061. if(spi.lastStep == 0)
  1062. spi.lastStep = 1;
  1063. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1064. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1065. //set starting frame
  1066. if(spi.spin)
  1067. {
  1068. spi.frameNum = 0;
  1069. }
  1070. else
  1071. {
  1072. spi.frameNum = ((M_PI/2.0f - projectileAngle) / (2.0f *M_PI) + 1/((float)(2*(idToProjectile[spi.creID]->ourImages.size()-1)))) * (idToProjectile[spi.creID]->ourImages.size()-1);
  1073. }
  1074. //set delay
  1075. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1076. projectiles.push_back(spi);
  1077. //attack aniamtion
  1078. attackingInfo = new CAttHelper;
  1079. attackingInfo->dest = dest;
  1080. attackingInfo->frame = 0;
  1081. attackingInfo->hitCount = 0;
  1082. attackingInfo->ID = ID;
  1083. attackingInfo->reversing = false;
  1084. attackingInfo->shooting = true;
  1085. if(projectileAngle > straightAngle) //upper shot
  1086. attackingInfo->shootingGroup = 14;
  1087. else if(projectileAngle < -straightAngle) //lower shot
  1088. attackingInfo->shootingGroup = 16;
  1089. else //straight shot
  1090. attackingInfo->shootingGroup = 15;
  1091. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1092. }
  1093. void CBattleInterface::battleFinished(const BattleResult& br)
  1094. {
  1095. deactivate();
  1096. CGI->curh->changeGraphic(0,0);
  1097. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1098. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1099. resWindow->activate();
  1100. }
  1101. void CBattleInterface::setAnimSpeed(int set)
  1102. {
  1103. animSpeed = set;
  1104. }
  1105. int CBattleInterface::getAnimSpeed() const
  1106. {
  1107. return animSpeed;
  1108. }
  1109. float CBattleInterface::getAnimSpeedMultiplier() const
  1110. {
  1111. switch(animSpeed)
  1112. {
  1113. case 1:
  1114. return 3.5f;
  1115. case 2:
  1116. return 2.2f;
  1117. case 4:
  1118. return 1.0f;
  1119. default:
  1120. return 0.0f;
  1121. }
  1122. }
  1123. void CBattleInterface::attackingShowHelper()
  1124. {
  1125. if(attackingInfo && !attackingInfo->reversing)
  1126. {
  1127. if(attackingInfo->frame == 0)
  1128. {
  1129. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1130. if(attackingInfo->shooting)
  1131. {
  1132. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1133. }
  1134. else
  1135. {
  1136. if(aStack.creature->isDoubleWide())
  1137. {
  1138. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1139. {
  1140. case 0:
  1141. creAnims[attackingInfo->ID]->setType(11);
  1142. break;
  1143. case 1:
  1144. creAnims[attackingInfo->ID]->setType(11);
  1145. break;
  1146. case 2:
  1147. creAnims[attackingInfo->ID]->setType(12);
  1148. break;
  1149. case 3:
  1150. creAnims[attackingInfo->ID]->setType(13);
  1151. break;
  1152. case 4:
  1153. creAnims[attackingInfo->ID]->setType(13);
  1154. break;
  1155. case 5:
  1156. creAnims[attackingInfo->ID]->setType(12);
  1157. break;
  1158. }
  1159. }
  1160. else //else for if(aStack.creature->isDoubleWide())
  1161. {
  1162. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1163. {
  1164. case 0:
  1165. creAnims[attackingInfo->ID]->setType(11);
  1166. break;
  1167. case 1:
  1168. creAnims[attackingInfo->ID]->setType(11);
  1169. break;
  1170. case 2:
  1171. creAnims[attackingInfo->ID]->setType(12);
  1172. break;
  1173. case 3:
  1174. creAnims[attackingInfo->ID]->setType(13);
  1175. break;
  1176. case 4:
  1177. creAnims[attackingInfo->ID]->setType(13);
  1178. break;
  1179. case 5:
  1180. creAnims[attackingInfo->ID]->setType(12);
  1181. break;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1187. {
  1188. attackingInfo->reversing = true;
  1189. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1190. if(aStackp == NULL)
  1191. return;
  1192. CStack aStack = *aStackp;
  1193. if(aStack.attackerOwned)
  1194. {
  1195. if(aStack.creature->isDoubleWide())
  1196. {
  1197. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1198. {
  1199. case 0:
  1200. //reverseCreature(ID, aStack.position, true);
  1201. break;
  1202. case 1:
  1203. break;
  1204. case 2:
  1205. break;
  1206. case 3:
  1207. break;
  1208. case 4:
  1209. //reverseCreature(ID, aStack.position, true);
  1210. break;
  1211. case 5:
  1212. reverseCreature(attackingInfo->ID, aStack.position, true);
  1213. break;
  1214. }
  1215. }
  1216. else //else for if(aStack.creature->isDoubleWide())
  1217. {
  1218. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1219. {
  1220. case 0:
  1221. reverseCreature(attackingInfo->ID, aStack.position, true);
  1222. break;
  1223. case 1:
  1224. break;
  1225. case 2:
  1226. break;
  1227. case 3:
  1228. break;
  1229. case 4:
  1230. reverseCreature(attackingInfo->ID, aStack.position, true);
  1231. break;
  1232. case 5:
  1233. reverseCreature(attackingInfo->ID, aStack.position, true);
  1234. break;
  1235. }
  1236. }
  1237. }
  1238. else //if(aStack.attackerOwned)
  1239. {
  1240. if(aStack.creature->isDoubleWide())
  1241. {
  1242. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1243. {
  1244. case 0:
  1245. //reverseCreature(ID, aStack.position, true);
  1246. break;
  1247. case 1:
  1248. break;
  1249. case 2:
  1250. reverseCreature(attackingInfo->ID, aStack.position, true);
  1251. break;
  1252. case 3:
  1253. break;
  1254. case 4:
  1255. //reverseCreature(ID, aStack.position, true);
  1256. break;
  1257. case 5:
  1258. //reverseCreature(ID, aStack.position, true);
  1259. break;
  1260. }
  1261. }
  1262. else //else for if(aStack.creature->isDoubleWide())
  1263. {
  1264. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1265. {
  1266. case 0:
  1267. //reverseCreature(ID, aStack.position, true);
  1268. break;
  1269. case 1:
  1270. reverseCreature(attackingInfo->ID, aStack.position, true);
  1271. break;
  1272. case 2:
  1273. reverseCreature(attackingInfo->ID, aStack.position, true);
  1274. break;
  1275. case 3:
  1276. reverseCreature(attackingInfo->ID, aStack.position, true);
  1277. break;
  1278. case 4:
  1279. //reverseCreature(ID, aStack.position, true);
  1280. break;
  1281. case 5:
  1282. //reverseCreature(ID, aStack.position, true);
  1283. break;
  1284. }
  1285. }
  1286. }
  1287. attackingInfo->reversing = false;
  1288. creAnims[attackingInfo->ID]->setType(2);
  1289. delete attackingInfo;
  1290. attackingInfo = NULL;
  1291. }
  1292. if(attackingInfo)
  1293. {
  1294. attackingInfo->hitCount++;
  1295. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1296. attackingInfo->frame++;
  1297. }
  1298. }
  1299. }
  1300. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1301. {
  1302. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1303. //preparating background graphic with hexes and shaded hexes
  1304. blitAt(background, 0, 0, backgroundWithHexes);
  1305. if(printCellBorders)
  1306. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1307. if(printStackRange)
  1308. {
  1309. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1310. {
  1311. int i = shadedHexes[m]/17; //row
  1312. int j = shadedHexes[m]%17-1; //column
  1313. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1314. int y = 86 + 42 * i;
  1315. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1316. }
  1317. }
  1318. }
  1319. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1320. {
  1321. char tabh[200];
  1322. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1323. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1324. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1325. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1326. dmg);
  1327. if(killed > 0)
  1328. {
  1329. if(killed > 1)
  1330. {
  1331. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1332. }
  1333. else //killed == 1
  1334. {
  1335. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1336. }
  1337. }
  1338. console->addText(std::string(tabh));
  1339. }
  1340. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1341. {
  1342. if(to == NULL)
  1343. to = screen;
  1344. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1345. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1346. {
  1347. if(it->animStartDelay>0)
  1348. {
  1349. --(it->animStartDelay);
  1350. continue;
  1351. }
  1352. SDL_Rect dst;
  1353. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1354. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1355. dst.x = it->x;
  1356. dst.y = it->y;
  1357. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1358. //actualizing projectile
  1359. ++it->step;
  1360. if(it->step == it->lastStep)
  1361. {
  1362. toBeDeleted.insert(toBeDeleted.end(), it);
  1363. }
  1364. else
  1365. {
  1366. it->x += it->dx;
  1367. it->y += it->dy;
  1368. if(it->spin)
  1369. {
  1370. ++(it->frameNum);
  1371. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1372. }
  1373. }
  1374. }
  1375. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1376. {
  1377. projectiles.erase(*it);
  1378. }
  1379. }
  1380. void CBattleHero::show(SDL_Surface *to)
  1381. {
  1382. //animation of flag
  1383. if(flip)
  1384. {
  1385. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1386. }
  1387. else
  1388. {
  1389. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1390. }
  1391. ++flagAnimCount;
  1392. if(flagAnimCount%4==0)
  1393. {
  1394. ++flagAnim;
  1395. flagAnim %= flag->ourImages.size();
  1396. }
  1397. //animation of hero
  1398. int tick=-1;
  1399. for(int i=0; i<dh->ourImages.size(); ++i)
  1400. {
  1401. if(dh->ourImages[i].groupNumber==phase)
  1402. ++tick;
  1403. if(tick==image)
  1404. {
  1405. SDL_Rect posb = pos;
  1406. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1407. ++image;
  1408. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1409. {
  1410. image = 0;
  1411. }
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. void CBattleHero::activate()
  1417. {
  1418. ClickableL::activate();
  1419. }
  1420. void CBattleHero::deactivate()
  1421. {
  1422. ClickableL::deactivate();
  1423. }
  1424. void CBattleHero::clickLeft(boost::logic::tribool down)
  1425. {
  1426. if(!down && myHero)
  1427. {
  1428. CGI->curh->changeGraphic(0,0);
  1429. LOCPLINT->curint->deactivate();
  1430. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1431. spellWindow->activate();
  1432. LOCPLINT->objsToBlit.push_back(spellWindow);
  1433. }
  1434. }
  1435. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, unsigned char player, const CGHeroInstance * hero): phase(phaseG), image(imageG), flip(flipG), flagAnim(0), myHero(hero)
  1436. {
  1437. dh = CDefHandler::giveDef( defName );
  1438. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1439. {
  1440. if(flip)
  1441. {
  1442. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1443. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1444. dh->ourImages[i].bitmap = hlp;
  1445. }
  1446. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1447. }
  1448. dh->alphaTransformed = true;
  1449. if(flip)
  1450. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1451. else
  1452. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1453. //coloring flag and adding transparency
  1454. for(int i=0; i<flag->ourImages.size(); ++i)
  1455. {
  1456. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1457. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1458. }
  1459. }
  1460. CBattleHero::~CBattleHero()
  1461. {
  1462. delete dh;
  1463. delete flag;
  1464. }
  1465. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1466. {
  1467. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1468. ret.second = -139 + 42 * (hexNum/17); //counting y
  1469. //counting x
  1470. if(attacker)
  1471. {
  1472. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1473. }
  1474. else
  1475. {
  1476. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1477. }
  1478. //shifting position for double - hex creatures
  1479. if(creature->isDoubleWide())
  1480. {
  1481. if(attacker)
  1482. {
  1483. ret.first -= 42;
  1484. }
  1485. else
  1486. {
  1487. ret.first += 42;
  1488. }
  1489. }
  1490. //returning
  1491. return ret;
  1492. }
  1493. void CBattleHex::activate()
  1494. {
  1495. Hoverable::activate();
  1496. MotionInterested::activate();
  1497. ClickableL::activate();
  1498. ClickableR::activate();
  1499. }
  1500. void CBattleHex::deactivate()
  1501. {
  1502. Hoverable::deactivate();
  1503. MotionInterested::deactivate();
  1504. ClickableL::deactivate();
  1505. ClickableR::deactivate();
  1506. }
  1507. void CBattleHex::hover(bool on)
  1508. {
  1509. hovered = on;
  1510. Hoverable::hover(on);
  1511. if(!on && setAlterText)
  1512. {
  1513. myInterface->console->alterTxt = std::string();
  1514. setAlterText = false;
  1515. }
  1516. }
  1517. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1518. {
  1519. }
  1520. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1521. {
  1522. if(myInterface->cellShade)
  1523. {
  1524. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1525. {
  1526. strictHovered = false;
  1527. }
  1528. else //hovered pixel is inside hex
  1529. {
  1530. strictHovered = true;
  1531. }
  1532. }
  1533. if(hovered && strictHovered) //print attacked creature to console
  1534. {
  1535. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1536. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1537. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1538. {
  1539. char tabh[160];
  1540. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1541. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1542. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1543. myInterface->console->alterTxt = std::string(tabh);
  1544. setAlterText = true;
  1545. }
  1546. }
  1547. else if(setAlterText)
  1548. {
  1549. myInterface->console->alterTxt = std::string();
  1550. setAlterText = false;
  1551. }
  1552. }
  1553. void CBattleHex::clickLeft(boost::logic::tribool down)
  1554. {
  1555. if(!down && hovered && strictHovered) //we've been really clicked!
  1556. {
  1557. myInterface->hexLclicked(myNumber);
  1558. }
  1559. }
  1560. void CBattleHex::clickRight(boost::logic::tribool down)
  1561. {
  1562. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1563. if(hovered && strictHovered && stID!=-1)
  1564. {
  1565. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1566. if(!myst.alive()) return;
  1567. StackState *pom = NULL;
  1568. if(down)
  1569. {
  1570. pom = new StackState();
  1571. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1572. if(h)
  1573. {
  1574. pom->attackBonus = h->primSkills[0];
  1575. pom->defenseBonus = h->primSkills[1];
  1576. pom->luck = h->getCurrentLuck();
  1577. pom->morale = h->getCurrentMorale();
  1578. pom->currentHealth = myst.firstHPleft;
  1579. }
  1580. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1581. ->activate();
  1582. }
  1583. delete pom;
  1584. }
  1585. }
  1586. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1587. {
  1588. }
  1589. CBattleConsole::~CBattleConsole()
  1590. {
  1591. texts.clear();
  1592. }
  1593. void CBattleConsole::show(SDL_Surface * to)
  1594. {
  1595. if(alterTxt.size())
  1596. {
  1597. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1598. }
  1599. else if(texts.size())
  1600. {
  1601. if(texts.size()==1)
  1602. {
  1603. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1604. }
  1605. else
  1606. {
  1607. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1608. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1609. }
  1610. }
  1611. }
  1612. bool CBattleConsole::addText(const std::string & text)
  1613. {
  1614. if(text.size()>70)
  1615. return false; //text too long!
  1616. int firstInToken = 0;
  1617. for(int i=0; i<text.size(); ++i) //tokenize
  1618. {
  1619. if(text[i] == 10)
  1620. {
  1621. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1622. firstInToken = i+1;
  1623. }
  1624. }
  1625. texts.push_back( text.substr(firstInToken, text.size()) );
  1626. lastShown = texts.size()-1;
  1627. return true;
  1628. }
  1629. void CBattleConsole::eraseText(unsigned int pos)
  1630. {
  1631. if(pos < texts.size())
  1632. {
  1633. texts.erase(texts.begin() + pos);
  1634. if(lastShown == texts.size())
  1635. --lastShown;
  1636. }
  1637. }
  1638. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  1639. {
  1640. if(pos >= texts.size()) //no such pos
  1641. return;
  1642. texts[pos] = text;
  1643. }
  1644. void CBattleConsole::scrollUp(unsigned int by)
  1645. {
  1646. if(lastShown > by)
  1647. lastShown -= by;
  1648. }
  1649. void CBattleConsole::scrollDown(unsigned int by)
  1650. {
  1651. if(lastShown + by < texts.size())
  1652. lastShown += by;
  1653. }
  1654. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1655. {
  1656. this->pos = pos;
  1657. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1658. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1659. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1660. SDL_FreeSurface(background);
  1661. background = pom;
  1662. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1663. if(br.winner==0) //attacker won
  1664. {
  1665. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1666. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1667. }
  1668. else //if(br.winner==1)
  1669. {
  1670. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1671. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1672. }
  1673. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1674. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1675. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1676. std::string attackerName, defenderName;
  1677. if(owner->attackingHeroInstance) //a hero attacked
  1678. {
  1679. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1680. //setting attackerName
  1681. attackerName = owner->attackingHeroInstance->name;
  1682. }
  1683. else //a monster attacked
  1684. {
  1685. int bestMonsterID = -1;
  1686. int bestPower = 0;
  1687. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1688. {
  1689. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1690. {
  1691. bestPower = CGI->creh->creatures[it->first].AIValue;
  1692. bestMonsterID = it->first;
  1693. }
  1694. }
  1695. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1696. //setting attackerName
  1697. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1698. }
  1699. if(owner->defendingHeroInstance) //a hero defended
  1700. {
  1701. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1702. //setting defenderName
  1703. defenderName = owner->defendingHeroInstance->name;
  1704. }
  1705. else //a monster defended
  1706. {
  1707. int bestMonsterID = -1;
  1708. int bestPower = 0;
  1709. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1710. {
  1711. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1712. {
  1713. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1714. bestMonsterID = it->second.first;
  1715. }
  1716. }
  1717. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1718. //setting defenderName
  1719. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1720. }
  1721. //printing attacker and defender's names
  1722. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1723. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1724. //printing casualities
  1725. for(int step = 0; step < 2; ++step)
  1726. {
  1727. if(br.casualties[step].size()==0)
  1728. {
  1729. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  1730. }
  1731. else
  1732. {
  1733. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  1734. int yPos = 344 + step*97;
  1735. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  1736. {
  1737. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1738. std::stringstream amount;
  1739. amount<<it->second;
  1740. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1741. xPos += 42;
  1742. }
  1743. }
  1744. }
  1745. //printing result description
  1746. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  1747. switch(br.result)
  1748. {
  1749. case 0: //normal victory
  1750. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1751. {
  1752. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  1753. }
  1754. else
  1755. {
  1756. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  1757. }
  1758. break;
  1759. case 1: //flee
  1760. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1761. {
  1762. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  1763. }
  1764. else
  1765. {
  1766. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  1767. }
  1768. break;
  1769. case 2: //surrender
  1770. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1771. {
  1772. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  1773. }
  1774. else
  1775. {
  1776. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  1777. }
  1778. break;
  1779. }
  1780. }
  1781. CBattleReslutWindow::~CBattleReslutWindow()
  1782. {
  1783. SDL_FreeSurface(background);
  1784. }
  1785. void CBattleReslutWindow::activate()
  1786. {
  1787. LOCPLINT->showingDialog->set(true);
  1788. exit->activate();
  1789. }
  1790. void CBattleReslutWindow::deactivate()
  1791. {
  1792. exit->deactivate();
  1793. }
  1794. void CBattleReslutWindow::show(SDL_Surface *to)
  1795. {
  1796. //evaluating to
  1797. if(!to)
  1798. to = screen;
  1799. SDL_BlitSurface(background, NULL, to, &pos);
  1800. exit->show(to);
  1801. }
  1802. void CBattleReslutWindow::bExitf()
  1803. {
  1804. LOCPLINT->battleResultQuited();
  1805. }
  1806. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  1807. {
  1808. pos = position;
  1809. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  1810. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1811. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->preth->zelp[427].first)(3,CGI->preth->zelp[427].first), CGI->preth->zelp[427].second, false, "sysopchk.def", NULL, 185, 140, false);
  1812. viewGrid->select(owner->printCellBorders);
  1813. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->preth->zelp[428].first)(3,CGI->preth->zelp[428].first), CGI->preth->zelp[428].second, false, "sysopchk.def", NULL, 185, 173, false);
  1814. movementShadow->select(owner->printStackRange);
  1815. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->preth->zelp[429].first)(3,CGI->preth->zelp[429].first), CGI->preth->zelp[429].second, false, "sysopchk.def", NULL, 185, 207, false);
  1816. mouseShadow->select(owner->printMouseShadow);
  1817. animSpeeds = new CHighlightableButtonsGroup(0);
  1818. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[422].first),CGI->preth->zelp[422].second, "sysopb9.def",188, 309, 1);
  1819. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[423].first),CGI->preth->zelp[423].second, "sysob10.def",252, 309, 2);
  1820. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[424].first),CGI->preth->zelp[424].second, "sysob11.def",315, 309, 4);
  1821. animSpeeds->select(owner->getAnimSpeed(), 1);
  1822. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  1823. setToDefault = new AdventureMapButton (CGI->preth->zelp[392].first, CGI->preth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def", false, NULL, false);
  1824. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  1825. exit = new AdventureMapButton (CGI->preth->zelp[393].first, CGI->preth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def", false, NULL, false);
  1826. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  1827. //printing texts to background
  1828. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  1829. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  1830. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  1831. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  1832. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  1833. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  1834. //auto - combat options
  1835. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  1836. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  1837. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  1838. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  1839. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  1840. //creature info
  1841. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  1842. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  1843. //general options
  1844. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  1845. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  1846. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  1847. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  1848. //texts printed
  1849. }
  1850. CBattleOptionsWindow::~CBattleOptionsWindow()
  1851. {
  1852. SDL_FreeSurface(background);
  1853. delete setToDefault;
  1854. delete exit;
  1855. delete viewGrid;
  1856. delete movementShadow;
  1857. delete animSpeeds;
  1858. delete mouseShadow;
  1859. }
  1860. void CBattleOptionsWindow::activate()
  1861. {
  1862. setToDefault->activate();
  1863. exit->activate();
  1864. viewGrid->activate();
  1865. movementShadow->activate();
  1866. animSpeeds->activate();
  1867. mouseShadow->activate();
  1868. }
  1869. void CBattleOptionsWindow::deactivate()
  1870. {
  1871. setToDefault->deactivate();
  1872. exit->deactivate();
  1873. viewGrid->deactivate();
  1874. movementShadow->deactivate();
  1875. animSpeeds->deactivate();
  1876. mouseShadow->deactivate();
  1877. }
  1878. void CBattleOptionsWindow::show(SDL_Surface *to)
  1879. {
  1880. if(!to) //"evaluating" to
  1881. to = screen;
  1882. SDL_BlitSurface(background, NULL, to, &pos);
  1883. setToDefault->show(to);
  1884. exit->show(to);
  1885. viewGrid->show(to);
  1886. movementShadow->show(to);
  1887. animSpeeds->show(to);
  1888. mouseShadow->show(to);
  1889. }
  1890. void CBattleOptionsWindow::bDefaultf()
  1891. {
  1892. }
  1893. void CBattleOptionsWindow::bExitf()
  1894. {
  1895. deactivate();
  1896. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  1897. {
  1898. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  1899. {
  1900. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  1901. break;
  1902. }
  1903. }
  1904. delete this;
  1905. LOCPLINT->curint->activate();
  1906. }