CBattleInterface.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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 "CCallback.h"
  11. #include "CGameState.h"
  12. #include "hch/CGeneralTextHandler.h"
  13. #include "client/CCreatureAnimation.h"
  14. #include "client/Graphics.h"
  15. #include <queue>
  16. #include <sstream>
  17. extern SDL_Surface * screen;
  18. extern TTF_Font * GEOR13;
  19. extern SDL_Color zwykly;
  20. SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
  21. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  22. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL), attackingInfo(NULL)
  23. {
  24. //initializing armies
  25. this->army1 = army1;
  26. this->army2 = army2;
  27. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  28. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  29. {
  30. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  31. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  32. creAnims[b->second.ID]->setType(2);
  33. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  34. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  35. }
  36. //preparing menu background and terrain
  37. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  38. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  39. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  40. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  41. //preparing graphics for displaying amounts of creatures
  42. amountBasic = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  43. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  44. CSDL_Ext::alphaTransform(amountNormal);
  45. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  46. {
  47. if((amountNormal->format->palette->colors+g)->b != 132 &&
  48. (amountNormal->format->palette->colors+g)->g != 231 &&
  49. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  50. {
  51. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  52. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  53. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  54. }
  55. }
  56. ////blitting menu background and terrain
  57. blitAt(background, 0, 0);
  58. blitAt(menu, 0, 556);
  59. CSDL_Ext::update();
  60. //preparing buttons and console
  61. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  62. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  63. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  64. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  65. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  66. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  67. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  68. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  69. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", false, NULL, false);
  70. bConsoleDown->bitmapOffset = 2;
  71. console = new CBattleConsole();
  72. console->pos.x = 211;
  73. console->pos.y = 560;
  74. console->pos.w = 406;
  75. console->pos.h = 38;
  76. //loading hero animations
  77. if(hero1) // attacking hero
  78. {
  79. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  80. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  81. }
  82. else
  83. {
  84. attackingHero = NULL;
  85. }
  86. if(hero2) // defending hero
  87. {
  88. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  89. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  90. }
  91. else
  92. {
  93. defendingHero = NULL;
  94. }
  95. //preparing cells and hexes
  96. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  97. CSDL_Ext::alphaTransform(cellBorder);
  98. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  99. CSDL_Ext::alphaTransform(cellShade);
  100. for(int h=0; h<187; ++h)
  101. {
  102. bfield[h].myNumber = h;
  103. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  104. int y = 86 + 42 * (h/17);
  105. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  106. bfield[h].accesible = true;
  107. bfield[h].myInterface = this;
  108. }
  109. //locking occupied positions on batlefield
  110. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  111. {
  112. bfield[it->second.position].accesible = false;
  113. }
  114. }
  115. CBattleInterface::~CBattleInterface()
  116. {
  117. SDL_FreeSurface(background);
  118. SDL_FreeSurface(menu);
  119. SDL_FreeSurface(amountBasic);
  120. SDL_FreeSurface(amountNormal);
  121. delete bOptions;
  122. delete bSurrender;
  123. delete bFlee;
  124. delete bAutofight;
  125. delete bSpell;
  126. delete bWait;
  127. delete bDefence;
  128. delete bConsoleUp;
  129. delete bConsoleDown;
  130. delete console;
  131. delete attackingHero;
  132. delete defendingHero;
  133. SDL_FreeSurface(cellBorder);
  134. SDL_FreeSurface(cellShade);
  135. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  136. delete g->second;
  137. }
  138. void CBattleInterface::activate()
  139. {
  140. bOptions->activate();
  141. bSurrender->activate();
  142. bFlee->activate();
  143. bAutofight->activate();
  144. bSpell->activate();
  145. bWait->activate();
  146. bDefence->activate();
  147. bConsoleUp->activate();
  148. bConsoleDown->activate();
  149. for(int b=0; b<187; ++b)
  150. {
  151. bfield[b].activate();
  152. }
  153. }
  154. void CBattleInterface::deactivate()
  155. {
  156. bOptions->deactivate();
  157. bSurrender->deactivate();
  158. bFlee->deactivate();
  159. bAutofight->deactivate();
  160. bSpell->deactivate();
  161. bWait->deactivate();
  162. bDefence->deactivate();
  163. bConsoleUp->deactivate();
  164. bConsoleDown->deactivate();
  165. for(int b=0; b<187; ++b)
  166. {
  167. bfield[b].deactivate();
  168. }
  169. }
  170. void CBattleInterface::show(SDL_Surface * to)
  171. {
  172. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  173. ++animCount;
  174. if(!to) //"evaluating" to
  175. to = screen;
  176. //showing background
  177. blitAt(background, 0, 0, to);
  178. if(printCellBorders) //printing cell borders
  179. {
  180. for(int i=0; i<11; ++i) //rows
  181. {
  182. for(int j=0; j<15; ++j) //columns
  183. {
  184. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  185. int y = 86 + 42 * i;
  186. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorder, NULL, to, &genRect(cellBorder->h, cellBorder->w, x, y));
  187. }
  188. }
  189. }
  190. //printing hovered cell
  191. for(int b=0; b<187; ++b)
  192. {
  193. if(bfield[b].strictHovered && bfield[b].hovered)
  194. {
  195. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  196. int y = 86 + 42 * (b/17);
  197. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  198. }
  199. }
  200. //showing selected unit's range
  201. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //don't show if unit is moving
  202. {
  203. showRange(to, activeStack);
  204. }
  205. //showing menu background and console
  206. blitAt(menu, 0, 556, to);
  207. console->show(to);
  208. //showing buttons
  209. bOptions->show(to);
  210. bSurrender->show(to);
  211. bFlee->show(to);
  212. bAutofight->show(to);
  213. bSpell->show(to);
  214. bWait->show(to);
  215. bDefence->show(to);
  216. bConsoleUp->show(to);
  217. bConsoleDown->show(to);
  218. //showing hero animations
  219. if(attackingHero)
  220. attackingHero->show(to);
  221. if(defendingHero)
  222. defendingHero->show(to);
  223. //showing units //a lot of work...
  224. std::vector<int> stackAliveByHex[187];
  225. //double loop because dead stacks should be printed first
  226. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  227. {
  228. if(j->second.alive)
  229. stackAliveByHex[j->second.position].push_back(j->second.ID);
  230. }
  231. std::vector<int> stackDeadByHex[187];
  232. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  233. {
  234. if(!j->second.alive)
  235. stackDeadByHex[j->second.position].push_back(j->second.ID);
  236. }
  237. attackingShowHelper(); // handle attack animation
  238. for(int b=0; b<187; ++b) //showing dead stacks
  239. {
  240. for(int v=0; v<stackDeadByHex[b].size(); ++v)
  241. {
  242. creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x, creAnims[stackDeadByHex[b][v]]->pos.y, creDir[stackDeadByHex[b][v]], (animCount%2==0 || creAnims[stackDeadByHex[b][v]]->getType()!=2) && stacks[stackDeadByHex[b][v]].alive, stackDeadByHex[b][v]==activeStack); //increment always when moving, never if stack died
  243. //printing amount
  244. if(stacks[stackDeadByHex[b][v]].amount > 0) //don't print if stack is not alive
  245. {
  246. if(stacks[stackDeadByHex[b][v]].attackerOwned)
  247. {
  248. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackDeadByHex[b][v]]->pos.x + 220, creAnims[stackDeadByHex[b][v]]->pos.y + 260));
  249. std::stringstream ss;
  250. ss<<stacks[stackDeadByHex[b][v]].amount;
  251. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackDeadByHex[b][v]]->pos.x + 220 + 14, creAnims[stackDeadByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  252. }
  253. else
  254. {
  255. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackDeadByHex[b][v]]->pos.x + 202, creAnims[stackDeadByHex[b][v]]->pos.y + 260));
  256. std::stringstream ss;
  257. ss<<stacks[stackDeadByHex[b][v]].amount;
  258. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackDeadByHex[b][v]]->pos.x + 202 + 14, creAnims[stackDeadByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  259. }
  260. }
  261. }
  262. }
  263. for(int b=0; b<187; ++b) //showing alive stacks
  264. {
  265. for(int v=0; v<stackAliveByHex[b].size(); ++v)
  266. {
  267. creAnims[stackAliveByHex[b][v]]->nextFrame(to, creAnims[stackAliveByHex[b][v]]->pos.x, creAnims[stackAliveByHex[b][v]]->pos.y, creDir[stackAliveByHex[b][v]], (animCount%2==0 || creAnims[stackAliveByHex[b][v]]->getType()!=2) && stacks[stackAliveByHex[b][v]].alive, stackAliveByHex[b][v]==activeStack); //increment always when moving, never if stack died
  268. //printing amount
  269. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  270. {
  271. if(stacks[stackAliveByHex[b][v]].attackerOwned)
  272. {
  273. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackAliveByHex[b][v]]->pos.x + 220, creAnims[stackAliveByHex[b][v]]->pos.y + 260));
  274. std::stringstream ss;
  275. ss<<stacks[stackAliveByHex[b][v]].amount;
  276. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackAliveByHex[b][v]]->pos.x + 220 + 14, creAnims[stackAliveByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  277. }
  278. else
  279. {
  280. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackAliveByHex[b][v]]->pos.x + 202, creAnims[stackAliveByHex[b][v]]->pos.y + 260));
  281. std::stringstream ss;
  282. ss<<stacks[stackAliveByHex[b][v]].amount;
  283. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackAliveByHex[b][v]]->pos.x + 202 + 14, creAnims[stackAliveByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  284. }
  285. }
  286. }
  287. }
  288. //units shown
  289. }
  290. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  291. {
  292. if(creAnims[number]==NULL)
  293. return false; //there is no such creature
  294. creAnims[number]->setType(8);
  295. for(int g=0; g<creAnims[number]->framesInGroup(8); ++g)
  296. {
  297. show();
  298. CSDL_Ext::update();
  299. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  300. }
  301. creDir[number] = !creDir[number];
  302. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  303. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  304. creAnims[number]->pos.x = coords.first;
  305. //creAnims[number]->pos.y = coords.second;
  306. if(wideTrick && curs.creature->isDoubleWide())
  307. {
  308. creAnims[number]->pos.x -= 44;
  309. }
  310. creAnims[number]->setType(7);
  311. for(int g=0; g<creAnims[number]->framesInGroup(7); ++g)
  312. {
  313. show();
  314. CSDL_Ext::update();
  315. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  316. }
  317. creAnims[number]->setType(2);
  318. return true;
  319. }
  320. void CBattleInterface::bOptionsf()
  321. {
  322. }
  323. void CBattleInterface::bSurrenderf()
  324. {
  325. }
  326. void CBattleInterface::bFleef()
  327. {
  328. BattleAction * ba = new BattleAction;
  329. ba->actionType = 4;
  330. givenCommand = ba;
  331. }
  332. void CBattleInterface::bAutofightf()
  333. {
  334. }
  335. void CBattleInterface::bSpellf()
  336. {
  337. }
  338. void CBattleInterface::bWaitf()
  339. {
  340. }
  341. void CBattleInterface::bDefencef()
  342. {
  343. BattleAction * ba = new BattleAction;
  344. ba->actionType = 3;
  345. ba->stackNumber = activeStack;
  346. givenCommand = ba;
  347. }
  348. void CBattleInterface::bConsoleUpf()
  349. {
  350. console->scrollUp();
  351. }
  352. void CBattleInterface::bConsoleDownf()
  353. {
  354. console->scrollDown();
  355. }
  356. void CBattleInterface::newStack(CStack stack)
  357. {
  358. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  359. creAnims[stack.ID]->setType(2);
  360. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  361. }
  362. void CBattleInterface::stackRemoved(CStack stack)
  363. {
  364. delete creAnims[stack.ID];
  365. creAnims.erase(stack.ID);
  366. }
  367. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby)
  368. {
  369. creAnims[ID]->setType(5); //death
  370. for(int i=0; i<creAnims[ID]->framesInGroup(5)-1; ++i)
  371. {
  372. show();
  373. CSDL_Ext::update();
  374. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  375. }
  376. printConsoleAttacked(ID, dmg, killed, IDby);
  377. }
  378. void CBattleInterface::stackActivated(int number)
  379. {
  380. givenCommand = NULL;
  381. activeStack = number;
  382. }
  383. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  384. {
  385. //a few useful variables
  386. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  387. int steps = creAnims[number]->framesInGroup(0);
  388. int hexWbase = 44, hexHbase = 42;
  389. if(startMoving) //animation of starting move
  390. {
  391. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  392. {
  393. show();
  394. CSDL_Ext::update();
  395. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  396. }
  397. }
  398. int mutPos = CBattleHex::mutualPosition(curStackPos, destHex);
  399. if(LOCPLINT->cb->battleGetCreature(number).isDoubleWide() &&
  400. ((creDir[number] && mutPos == 5) || (creDir[number] && mutPos == 0) || (creDir[number] && mutPos == 4))) //for special cases
  401. {
  402. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  403. {
  404. case 0:
  405. if(creDir[number] == true)
  406. reverseCreature(number, curStackPos, true);
  407. break;
  408. case 1:
  409. if(creDir[number] == false)
  410. reverseCreature(number, curStackPos, true);
  411. break;
  412. case 2:
  413. if(creDir[number] == false)
  414. reverseCreature(number, curStackPos, true);
  415. break;
  416. case 3:
  417. if(creDir[number] == false)
  418. reverseCreature(number, curStackPos, true);
  419. break;
  420. case 4:
  421. if(creDir[number] == true)
  422. reverseCreature(number, curStackPos, true);
  423. break;
  424. case 5:
  425. if(creDir[number] == true)
  426. reverseCreature(number, curStackPos, true);
  427. break;
  428. }
  429. //moving instructions
  430. creAnims[number]->setType(0);
  431. for(int i=0; i<steps; ++i)
  432. {
  433. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  434. {
  435. case 0:
  436. creAnims[number]->pos.x -= hexWbase/(2*steps);
  437. creAnims[number]->pos.y -= hexHbase/steps;
  438. break;
  439. case 1:
  440. creAnims[number]->pos.x += hexWbase/(2*steps);
  441. creAnims[number]->pos.y -= hexHbase/steps;
  442. break;
  443. case 2:
  444. creAnims[number]->pos.x += hexWbase/steps;
  445. break;
  446. case 3:
  447. creAnims[number]->pos.x += hexWbase/(2*steps);
  448. creAnims[number]->pos.y += hexHbase/steps;
  449. break;
  450. case 4:
  451. creAnims[number]->pos.x -= hexWbase/(2*steps);
  452. creAnims[number]->pos.y += hexHbase/steps;
  453. break;
  454. case 5:
  455. creAnims[number]->pos.x -= hexWbase/steps;
  456. break;
  457. }
  458. show();
  459. CSDL_Ext::update();
  460. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  461. }
  462. if( (LOCPLINT->cb->battleGetStackByID(number)->owner == attackingHeroInstance->tempOwner ) != creDir[number])
  463. {
  464. reverseCreature(number, curStackPos, true);
  465. }
  466. }
  467. else //normal move instructions
  468. {
  469. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  470. {
  471. case 0:
  472. if(creDir[number] == true)
  473. reverseCreature(number, curStackPos);
  474. break;
  475. case 1:
  476. if(creDir[number] == false)
  477. reverseCreature(number, curStackPos);
  478. break;
  479. case 2:
  480. if(creDir[number] == false)
  481. reverseCreature(number, curStackPos);
  482. break;
  483. case 3:
  484. if(creDir[number] == false)
  485. reverseCreature(number, curStackPos);
  486. break;
  487. case 4:
  488. if(creDir[number] == true)
  489. reverseCreature(number, curStackPos);
  490. break;
  491. case 5:
  492. if(creDir[number] == true)
  493. reverseCreature(number, curStackPos);
  494. break;
  495. }
  496. //moving instructions
  497. creAnims[number]->setType(0);
  498. for(int i=0; i<steps; ++i)
  499. {
  500. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  501. {
  502. case 0:
  503. creAnims[number]->pos.x -= hexWbase/(2*steps);
  504. creAnims[number]->pos.y -= hexHbase/steps;
  505. break;
  506. case 1:
  507. creAnims[number]->pos.x += hexWbase/(2*steps);
  508. creAnims[number]->pos.y -= hexHbase/steps;
  509. break;
  510. case 2:
  511. creAnims[number]->pos.x += hexWbase/steps;
  512. break;
  513. case 3:
  514. creAnims[number]->pos.x += hexWbase/(2*steps);
  515. creAnims[number]->pos.y += hexHbase/steps;
  516. break;
  517. case 4:
  518. creAnims[number]->pos.x -= hexWbase/(2*steps);
  519. creAnims[number]->pos.y += hexHbase/steps;
  520. break;
  521. case 5:
  522. creAnims[number]->pos.x -= hexWbase/steps;
  523. break;
  524. }
  525. show();
  526. CSDL_Ext::update();
  527. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  528. }
  529. }
  530. if(endMoving) //animation of ending move
  531. {
  532. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  533. {
  534. show();
  535. CSDL_Ext::update();
  536. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  537. }
  538. }
  539. creAnims[number]->setType(2); //resetting to default
  540. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  541. if(endMoving) //resetting to default
  542. {
  543. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  544. reverseCreature(number, destHex);
  545. //creDir[number] = (curs.owner == attackingHeroInstance->tempOwner);
  546. }
  547. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  548. creAnims[number]->pos.x = coords.first;
  549. creAnims[number]->pos.y = coords.second;
  550. }
  551. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby)
  552. {
  553. creAnims[ID]->setType(3); //getting hit
  554. for(int i=0; i<creAnims[ID]->framesInGroup(3); ++i)
  555. {
  556. show();
  557. CSDL_Ext::update();
  558. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  559. }
  560. creAnims[ID]->setType(2);
  561. printConsoleAttacked(ID, dmg, killed, IDby);
  562. }
  563. void CBattleInterface::stackAttacking(int ID, int dest)
  564. {
  565. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  566. if(aStack.creature->isDoubleWide())
  567. {
  568. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  569. {
  570. case 0:
  571. //reverseCreature(ID, aStack.position, true);
  572. break;
  573. case 1:
  574. break;
  575. case 2:
  576. break;
  577. case 3:
  578. break;
  579. case 4:
  580. //reverseCreature(ID, aStack.position, true);
  581. break;
  582. case 5:
  583. reverseCreature(ID, aStack.position, true);
  584. break;
  585. }
  586. }
  587. else //else for if(aStack.creature->isDoubleWide())
  588. {
  589. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  590. {
  591. case 0:
  592. reverseCreature(ID, aStack.position, true);
  593. break;
  594. case 1:
  595. break;
  596. case 2:
  597. break;
  598. case 3:
  599. break;
  600. case 4:
  601. reverseCreature(ID, aStack.position, true);
  602. break;
  603. case 5:
  604. reverseCreature(ID, aStack.position, true);
  605. break;
  606. }
  607. }
  608. attackingInfo = new CAttHelper;
  609. attackingInfo->dest = dest;
  610. attackingInfo->frame = 0;
  611. attackingInfo->ID = ID;
  612. attackingInfo->reversing = false;
  613. if(aStack.creature->isDoubleWide())
  614. {
  615. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  616. {
  617. case 0:
  618. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  619. break;
  620. case 1:
  621. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  622. break;
  623. case 2:
  624. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  625. break;
  626. case 3:
  627. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  628. break;
  629. case 4:
  630. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  631. break;
  632. case 5:
  633. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  634. break;
  635. }
  636. }
  637. else //else for if(aStack.creature->isDoubleWide())
  638. {
  639. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  640. {
  641. case 0:
  642. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  643. break;
  644. case 1:
  645. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  646. break;
  647. case 2:
  648. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  649. break;
  650. case 3:
  651. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  652. break;
  653. case 4:
  654. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  655. break;
  656. case 5:
  657. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  658. break;
  659. }
  660. }
  661. }
  662. void CBattleInterface::newRound(int number)
  663. {
  664. console->addText(CGI->generaltexth->allTexts[412]);
  665. }
  666. void CBattleInterface::hexLclicked(int whichOne)
  667. {
  668. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  669. {
  670. int atCre = LOCPLINT->cb->battleGetStack(whichOne); //creature at destination tile; -1 if there is no one
  671. //LOCPLINT->cb->battleGetCreature();
  672. if(atCre==-1) //normal move action
  673. {
  674. BattleAction * ba = new BattleAction(); //to be deleted by engine
  675. ba->actionType = 2;
  676. ba->destinationTile = whichOne;
  677. ba->stackNumber = activeStack;
  678. givenCommand = ba;
  679. }
  680. else if(LOCPLINT->cb->battleGetStackByID(atCre)->owner != attackingHeroInstance->tempOwner) //attacking
  681. {
  682. BattleAction * ba = new BattleAction(); //to be deleted by engine
  683. ba->actionType = 6;
  684. ba->destinationTile = whichOne;
  685. ba->stackNumber = activeStack;
  686. givenCommand = ba;
  687. }
  688. }
  689. }
  690. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  691. {
  692. std::vector<int> shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(ID);
  693. for(int i=0; i<shadedHexes.size(); ++i)
  694. {
  695. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  696. }
  697. }
  698. void CBattleInterface::attackingShowHelper()
  699. {
  700. if(attackingInfo && !attackingInfo->reversing)
  701. {
  702. if(attackingInfo->frame == 0)
  703. {
  704. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  705. if(aStack.creature->isDoubleWide())
  706. {
  707. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  708. {
  709. case 0:
  710. creAnims[attackingInfo->ID]->setType(10);
  711. break;
  712. case 1:
  713. creAnims[attackingInfo->ID]->setType(10);
  714. break;
  715. case 2:
  716. creAnims[attackingInfo->ID]->setType(11);
  717. break;
  718. case 3:
  719. creAnims[attackingInfo->ID]->setType(12);
  720. break;
  721. case 4:
  722. creAnims[attackingInfo->ID]->setType(12);
  723. break;
  724. case 5:
  725. creAnims[attackingInfo->ID]->setType(11);
  726. break;
  727. }
  728. }
  729. else //else for if(aStack.creature->isDoubleWide())
  730. {
  731. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  732. {
  733. case 0:
  734. creAnims[attackingInfo->ID]->setType(10);
  735. break;
  736. case 1:
  737. creAnims[attackingInfo->ID]->setType(10);
  738. break;
  739. case 2:
  740. creAnims[attackingInfo->ID]->setType(11);
  741. break;
  742. case 3:
  743. creAnims[attackingInfo->ID]->setType(12);
  744. break;
  745. case 4:
  746. creAnims[attackingInfo->ID]->setType(12);
  747. break;
  748. case 5:
  749. creAnims[attackingInfo->ID]->setType(11);
  750. break;
  751. }
  752. }
  753. }
  754. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  755. {
  756. attackingInfo->reversing = true;
  757. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  758. if(aStack.creature->isDoubleWide())
  759. {
  760. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  761. {
  762. case 0:
  763. //reverseCreature(ID, aStack.position, true);
  764. break;
  765. case 1:
  766. break;
  767. case 2:
  768. break;
  769. case 3:
  770. break;
  771. case 4:
  772. //reverseCreature(ID, aStack.position, true);
  773. break;
  774. case 5:
  775. reverseCreature(attackingInfo->ID, aStack.position, true);
  776. break;
  777. }
  778. }
  779. else //else for if(aStack.creature->isDoubleWide())
  780. {
  781. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  782. {
  783. case 0:
  784. reverseCreature(attackingInfo->ID, aStack.position, true);
  785. break;
  786. case 1:
  787. break;
  788. case 2:
  789. break;
  790. case 3:
  791. break;
  792. case 4:
  793. reverseCreature(attackingInfo->ID, aStack.position, true);
  794. break;
  795. case 5:
  796. reverseCreature(attackingInfo->ID, aStack.position, true);
  797. break;
  798. }
  799. }
  800. attackingInfo->reversing = false;
  801. creAnims[attackingInfo->ID]->setType(2);
  802. delete attackingInfo;
  803. attackingInfo = NULL;
  804. }
  805. if(attackingInfo)
  806. {
  807. attackingInfo->frame++;
  808. }
  809. }
  810. }
  811. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  812. {
  813. char tabh[200];
  814. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  815. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  816. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  817. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  818. dmg);
  819. if(killed > 0)
  820. {
  821. if(killed > 1)
  822. {
  823. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  824. }
  825. else //killed == 1
  826. {
  827. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  828. }
  829. }
  830. console->addText(std::string(tabh));
  831. }
  832. void CBattleHero::show(SDL_Surface *to)
  833. {
  834. //animation of flag
  835. if(flip)
  836. {
  837. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  838. }
  839. else
  840. {
  841. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  842. }
  843. {
  844. ++flagAnim;
  845. flagAnim %= flag->ourImages.size();
  846. }
  847. //animation of hero
  848. int tick=-1;
  849. for(int i=0; i<dh->ourImages.size(); ++i)
  850. {
  851. if(dh->ourImages[i].groupNumber==phase)
  852. ++tick;
  853. if(tick==image)
  854. {
  855. SDL_Rect posb = pos;
  856. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  857. ++image;
  858. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  859. {
  860. image = 0;
  861. }
  862. break;
  863. }
  864. }
  865. }
  866. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  867. {
  868. dh = CDefHandler::giveDef( defName );
  869. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  870. {
  871. if(flip)
  872. {
  873. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  874. SDL_FreeSurface(dh->ourImages[i].bitmap);
  875. dh->ourImages[i].bitmap = hlp;
  876. }
  877. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  878. }
  879. dh->alphaTransformed = true;
  880. if(flip)
  881. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  882. else
  883. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  884. //coloring flag and adding transparency
  885. for(int i=0; i<flag->ourImages.size(); ++i)
  886. {
  887. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  888. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  889. }
  890. }
  891. CBattleHero::~CBattleHero()
  892. {
  893. delete dh;
  894. delete flag;
  895. }
  896. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  897. {
  898. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  899. ret.second = -139 + 42 * (hexNum/17); //counting y
  900. //counting x
  901. if(attacker)
  902. {
  903. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  904. }
  905. else
  906. {
  907. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  908. }
  909. //shifting position for double - hex creatures
  910. if(creature->isDoubleWide())
  911. {
  912. if(attacker)
  913. {
  914. ret.first -= 42;
  915. }
  916. else
  917. {
  918. ret.first += 42;
  919. }
  920. }
  921. //returning
  922. return ret;
  923. }
  924. signed char CBattleHex::mutualPosition(int hex1, int hex2)
  925. {
  926. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  927. return 0;
  928. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  929. return 1;
  930. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  931. return 5;
  932. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  933. return 2;
  934. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  935. return 4;
  936. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  937. return 3;
  938. return -1;
  939. }
  940. void CBattleHex::activate()
  941. {
  942. Hoverable::activate();
  943. MotionInterested::activate();
  944. ClickableL::activate();
  945. ClickableR::activate();
  946. }
  947. void CBattleHex::deactivate()
  948. {
  949. Hoverable::deactivate();
  950. MotionInterested::deactivate();
  951. ClickableL::deactivate();
  952. ClickableR::deactivate();
  953. }
  954. void CBattleHex::hover(bool on)
  955. {
  956. hovered = on;
  957. Hoverable::hover(on);
  958. if(!on && setAlterText)
  959. {
  960. myInterface->console->alterTxt = std::string();
  961. setAlterText = false;
  962. }
  963. }
  964. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  965. {
  966. }
  967. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  968. {
  969. if(CBattleInterface::cellShade)
  970. {
  971. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  972. {
  973. strictHovered = false;
  974. }
  975. else //hovered pixel is inside hex
  976. {
  977. strictHovered = true;
  978. }
  979. }
  980. if(hovered && strictHovered) //print attacked creature to console
  981. {
  982. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  983. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  984. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive)
  985. {
  986. char tabh[160];
  987. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  988. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  989. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  990. myInterface->console->alterTxt = std::string(tabh);
  991. setAlterText = true;
  992. }
  993. }
  994. else if(setAlterText)
  995. {
  996. myInterface->console->alterTxt = std::string();
  997. setAlterText = false;
  998. }
  999. }
  1000. void CBattleHex::clickLeft(boost::logic::tribool down)
  1001. {
  1002. if(!down && hovered && strictHovered) //we've been really clicked!
  1003. {
  1004. myInterface->hexLclicked(myNumber);
  1005. }
  1006. }
  1007. void CBattleHex::clickRight(boost::logic::tribool down)
  1008. {
  1009. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1010. if(hovered && strictHovered && stID!=-1)
  1011. {
  1012. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1013. StackState *pom = NULL;
  1014. if(down)
  1015. {
  1016. pom = new StackState();
  1017. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1018. if(h)
  1019. {
  1020. pom->attackBonus = h->primSkills[0];
  1021. pom->defenseBonus = h->primSkills[1];
  1022. pom->luck = h->getCurrentLuck();
  1023. pom->morale = h->getCurrentMorale();
  1024. }
  1025. (new CCreInfoWindow(myst.creature->idNumber,0,pom,boost::function<void()>(),boost::function<void()>()))
  1026. ->activate();
  1027. }
  1028. delete pom;
  1029. }
  1030. }
  1031. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1032. {
  1033. }
  1034. CBattleConsole::~CBattleConsole()
  1035. {
  1036. texts.clear();
  1037. }
  1038. void CBattleConsole::show(SDL_Surface * to)
  1039. {
  1040. if(alterTxt.size())
  1041. {
  1042. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1043. }
  1044. else if(texts.size())
  1045. {
  1046. if(texts.size()==1)
  1047. {
  1048. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1049. }
  1050. else
  1051. {
  1052. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1053. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1054. }
  1055. }
  1056. }
  1057. bool CBattleConsole::addText(std::string text)
  1058. {
  1059. if(text.size()>70)
  1060. return false; //text too long!
  1061. int firstInToken = 0;
  1062. for(int i=0; i<text.size(); ++i) //tokenize
  1063. {
  1064. if(text[i] == 10)
  1065. {
  1066. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1067. firstInToken = i+1;
  1068. }
  1069. }
  1070. texts.push_back( text.substr(firstInToken, text.size()) );
  1071. lastShown = texts.size()-1;
  1072. return true;
  1073. }
  1074. void CBattleConsole::eraseText(unsigned int pos)
  1075. {
  1076. if(pos < texts.size())
  1077. {
  1078. texts.erase(texts.begin() + pos);
  1079. if(lastShown == texts.size())
  1080. --lastShown;
  1081. }
  1082. }
  1083. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1084. {
  1085. if(pos >= texts.size()) //no such pos
  1086. return;
  1087. texts[pos] = text;
  1088. }
  1089. void CBattleConsole::scrollUp(unsigned int by)
  1090. {
  1091. if(lastShown > by)
  1092. lastShown -= by;
  1093. }
  1094. void CBattleConsole::scrollDown(unsigned int by)
  1095. {
  1096. if(lastShown + by < texts.size())
  1097. lastShown += by;
  1098. }