CTradeWindow.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * CTradeWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CTradeWindow.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/CursorHandler.h"
  14. #include "../widgets/Images.h"
  15. #include "../adventureMap/CAdvMapInt.h"
  16. #include "../renderSDL/SDL_Extensions.h"
  17. #include "../gui/TextAlignment.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../CGameInfo.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/VCMI_Lib.h"
  24. #include "../../lib/CArtHandler.h"
  25. #include "../../lib/CCreatureHandler.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/CHeroHandler.h"
  28. #include "../../lib/CGameState.h"
  29. #include "../../lib/mapObjects/CGHeroInstance.h"
  30. #include "../../lib/mapObjects/CGTownInstance.h"
  31. #include "../../lib/mapObjects/CGMarket.h"
  32. CTradeWindow::CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial)
  33. : CIntObject(LCLICK | HOVER | RCLICK, pos),
  34. type(EType(-1)),// set to invalid, will be corrected in setType
  35. id(ID),
  36. serial(Serial),
  37. left(Left)
  38. {
  39. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  40. downSelection = false;
  41. hlp = nullptr;
  42. setType(Type);
  43. }
  44. void CTradeWindow::CTradeableItem::setType(EType newType)
  45. {
  46. if(type != newType)
  47. {
  48. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  49. type = newType;
  50. if(getIndex() < 0)
  51. {
  52. image = std::make_shared<CAnimImage>(getFilename(), 0);
  53. image->disable();
  54. }
  55. else
  56. {
  57. image = std::make_shared<CAnimImage>(getFilename(), getIndex());
  58. }
  59. }
  60. }
  61. void CTradeWindow::CTradeableItem::setID(int newID)
  62. {
  63. if (id != newID)
  64. {
  65. id = newID;
  66. if (image)
  67. {
  68. int index = getIndex();
  69. if (index < 0)
  70. image->disable();
  71. else
  72. {
  73. image->enable();
  74. image->setFrame(index);
  75. }
  76. }
  77. }
  78. }
  79. std::string CTradeWindow::CTradeableItem::getFilename()
  80. {
  81. switch(type)
  82. {
  83. case RESOURCE:
  84. return "RESOURCE";
  85. case PLAYER:
  86. return "CREST58";
  87. case ARTIFACT_TYPE:
  88. case ARTIFACT_PLACEHOLDER:
  89. case ARTIFACT_INSTANCE:
  90. return "artifact";
  91. case CREATURE:
  92. return "TWCRPORT";
  93. default:
  94. return "";
  95. }
  96. }
  97. int CTradeWindow::CTradeableItem::getIndex()
  98. {
  99. if (id < 0)
  100. return -1;
  101. switch(type)
  102. {
  103. case RESOURCE:
  104. case PLAYER:
  105. return id;
  106. case ARTIFACT_TYPE:
  107. case ARTIFACT_INSTANCE:
  108. case ARTIFACT_PLACEHOLDER:
  109. return CGI->artifacts()->getByIndex(id)->getIconIndex();
  110. case CREATURE:
  111. return CGI->creatures()->getByIndex(id)->getIconIndex();
  112. default:
  113. return -1;
  114. }
  115. }
  116. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  117. {
  118. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  119. assert(mw);
  120. Point posToBitmap;
  121. Point posToSubCenter;
  122. switch(type)
  123. {
  124. case RESOURCE:
  125. posToBitmap = Point(19,9);
  126. posToSubCenter = Point(36, 59);
  127. break;
  128. case CREATURE_PLACEHOLDER:
  129. case CREATURE:
  130. posToSubCenter = Point(29, 76);
  131. // Positing of unit count is different in Altar of Sacrifice and Freelancer's Guild
  132. if(mw->mode == EMarketMode::CREATURE_EXP && downSelection)
  133. posToSubCenter.y += 5;
  134. break;
  135. case PLAYER:
  136. posToSubCenter = Point(31, 76);
  137. break;
  138. case ARTIFACT_PLACEHOLDER:
  139. case ARTIFACT_INSTANCE:
  140. posToSubCenter = Point(19, 55);
  141. if(downSelection)
  142. posToSubCenter.y += 8;
  143. break;
  144. case ARTIFACT_TYPE:
  145. posToSubCenter = Point(19, 58);
  146. break;
  147. }
  148. if (image)
  149. {
  150. image->moveTo(pos.topLeft() + posToBitmap);
  151. CIntObject::showAll(to);
  152. }
  153. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::WHITE, to);
  154. }
  155. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  156. {
  157. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  158. assert(mw);
  159. if(down)
  160. {
  161. if(type == ARTIFACT_PLACEHOLDER)
  162. {
  163. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  164. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  165. {
  166. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this->shared_from_this(), movedArt);
  167. }
  168. else if(const CArtifactInstance *art = getArtInstance())
  169. {
  170. aw->arts->commonInfo->src.AOH = aw->arts.get();
  171. aw->arts->commonInfo->src.art = art;
  172. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  173. aw->arts->markPossibleSlots(art);
  174. //aw->arts->commonInfo->dst.AOH = aw->arts;
  175. CCS->curh->dragAndDropCursor("artifact", art->artType->iconIndex);
  176. aw->arts->artifactsOnAltar.erase(art);
  177. setID(-1);
  178. subtitle.clear();
  179. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  180. }
  181. aw->calcTotalExp();
  182. return;
  183. }
  184. if(left)
  185. {
  186. if(mw->hLeft != this->shared_from_this())
  187. mw->hLeft = this->shared_from_this();
  188. else
  189. return;
  190. }
  191. else
  192. {
  193. if(mw->hRight != this->shared_from_this())
  194. mw->hRight = this->shared_from_this();
  195. else
  196. return;
  197. }
  198. mw->selectionChanged(left);
  199. }
  200. }
  201. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  202. {
  203. Rect oldPos = pos;
  204. std::string oldSub = subtitle;
  205. downSelection = true;
  206. moveTo(dstPos);
  207. subtitle = customSub;
  208. showAll(to);
  209. downSelection = false;
  210. moveTo(oldPos.topLeft());
  211. subtitle = oldSub;
  212. }
  213. void CTradeWindow::CTradeableItem::hover(bool on)
  214. {
  215. if(!on)
  216. {
  217. GH.statusbar->clear();
  218. return;
  219. }
  220. switch(type)
  221. {
  222. case CREATURE:
  223. case CREATURE_PLACEHOLDER:
  224. GH.statusbar->write(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->getNamePluralTranslated()));
  225. break;
  226. case ARTIFACT_PLACEHOLDER:
  227. if(id < 0)
  228. GH.statusbar->write(CGI->generaltexth->zelp[582].first);
  229. else
  230. GH.statusbar->write(CGI->artifacts()->getByIndex(id)->getNameTranslated());
  231. break;
  232. }
  233. }
  234. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  235. {
  236. if(down)
  237. {
  238. switch(type)
  239. {
  240. case CREATURE:
  241. case CREATURE_PLACEHOLDER:
  242. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->namePl));
  243. break;
  244. case ARTIFACT_TYPE:
  245. case ARTIFACT_PLACEHOLDER:
  246. //TODO: it's would be better for market to contain actual CArtifactInstance and not just ids of certain artifact type so we can use getEffectiveDescription.
  247. if(id >= 0)
  248. adventureInt->handleRightClick(CGI->artifacts()->getByIndex(id)->getDescriptionTranslated(), down);
  249. break;
  250. }
  251. }
  252. }
  253. std::string CTradeWindow::CTradeableItem::getName(int number) const
  254. {
  255. switch(type)
  256. {
  257. case PLAYER:
  258. return CGI->generaltexth->capColors[id];
  259. case RESOURCE:
  260. return CGI->generaltexth->restypes[id];
  261. case CREATURE:
  262. if(number == 1)
  263. return CGI->creh->objects[id]->getNameSingularTranslated();
  264. else
  265. return CGI->creh->objects[id]->getNamePluralTranslated();
  266. case ARTIFACT_TYPE:
  267. case ARTIFACT_INSTANCE:
  268. return CGI->artifacts()->getByIndex(id)->getNameTranslated();
  269. }
  270. logGlobal->error("Invalid trade item type: %d", (int)type);
  271. return "";
  272. }
  273. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  274. {
  275. switch(type)
  276. {
  277. case ARTIFACT_PLACEHOLDER:
  278. case ARTIFACT_INSTANCE:
  279. return hlp;
  280. default:
  281. return nullptr;
  282. }
  283. }
  284. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  285. {
  286. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  287. hlp = art;
  288. if(art)
  289. setID(art->artType->getId());
  290. else
  291. setID(-1);
  292. }
  293. CTradeWindow::CTradeWindow(std::string bgName, const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode):
  294. CWindowObject(PLAYER_COLORED, bgName),
  295. market(Market),
  296. hero(Hero),
  297. readyToTrade(false)
  298. {
  299. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  300. type |= BLOCK_ADV_HOTKEYS;
  301. mode = Mode;
  302. initTypes();
  303. }
  304. void CTradeWindow::initTypes()
  305. {
  306. switch(mode)
  307. {
  308. case EMarketMode::RESOURCE_RESOURCE:
  309. itemsType[1] = RESOURCE;
  310. itemsType[0] = RESOURCE;
  311. break;
  312. case EMarketMode::RESOURCE_PLAYER:
  313. itemsType[1] = RESOURCE;
  314. itemsType[0] = PLAYER;
  315. break;
  316. case EMarketMode::CREATURE_RESOURCE:
  317. itemsType[1] = CREATURE;
  318. itemsType[0] = RESOURCE;
  319. break;
  320. case EMarketMode::RESOURCE_ARTIFACT:
  321. itemsType[1] = RESOURCE;
  322. itemsType[0] = ARTIFACT_TYPE;
  323. break;
  324. case EMarketMode::ARTIFACT_RESOURCE:
  325. itemsType[1] = ARTIFACT_INSTANCE;
  326. itemsType[0] = RESOURCE;
  327. break;
  328. case EMarketMode::CREATURE_EXP:
  329. itemsType[1] = CREATURE;
  330. itemsType[0] = CREATURE_PLACEHOLDER;
  331. break;
  332. case EMarketMode::ARTIFACT_EXP:
  333. itemsType[1] = ARTIFACT_TYPE;
  334. itemsType[0] = ARTIFACT_PLACEHOLDER;
  335. break;
  336. }
  337. }
  338. void CTradeWindow::initItems(bool Left)
  339. {
  340. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  341. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  342. {
  343. int xOffset = 0, yOffset = 0;
  344. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  345. {
  346. xOffset = -361;
  347. yOffset = +46;
  348. auto item = std::make_shared<CTradeableItem>(Point(137, 469), itemsType[Left], -1, 1, 0);
  349. item->recActions &= ~(UPDATE | SHOWALL);
  350. items[Left].push_back(item);
  351. }
  352. else //ARTIFACT_EXP
  353. {
  354. xOffset = -363;
  355. yOffset = -12;
  356. }
  357. arts = std::make_shared<CArtifactsOfHero>(Point(xOffset, yOffset), true);
  358. arts->recActions = 255-DISPOSE;
  359. arts->setHero(hero);
  360. arts->allowedAssembling = false;
  361. addSet(arts);
  362. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  363. arts->highlightModeCallback = std::bind(&CTradeWindow::artifactSelected, this, _1);
  364. }
  365. else
  366. {
  367. std::vector<int> *ids = getItemsIds(Left);
  368. std::vector<Rect> pos;
  369. int amount = -1;
  370. getPositionsFor(pos, Left, itemsType[Left]);
  371. if(Left || !ids)
  372. amount = 7;
  373. else
  374. amount = static_cast<int>(ids->size());
  375. if(ids)
  376. vstd::amin(amount, ids->size());
  377. for(int j=0; j<amount; j++)
  378. {
  379. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  380. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  381. continue;
  382. auto item = std::make_shared<CTradeableItem>(pos[j].topLeft(), itemsType[Left], id, Left, j);
  383. item->pos = pos[j] + this->pos.topLeft();
  384. items[Left].push_back(item);
  385. }
  386. vstd::clear_pointer(ids);
  387. initSubs(Left);
  388. }
  389. }
  390. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  391. {
  392. std::vector<int> *ids = nullptr;
  393. if(mode == EMarketMode::ARTIFACT_EXP)
  394. return new std::vector<int>(22, -1);
  395. if(Left)
  396. {
  397. switch(itemsType[1])
  398. {
  399. case CREATURE:
  400. ids = new std::vector<int>;
  401. for(int i = 0; i < 7; i++)
  402. {
  403. if(const CCreature *c = hero->getCreature(SlotID(i)))
  404. ids->push_back(c->idNumber);
  405. else
  406. ids->push_back(-1);
  407. }
  408. break;
  409. }
  410. }
  411. else
  412. {
  413. switch(itemsType[0])
  414. {
  415. case PLAYER:
  416. ids = new std::vector<int>;
  417. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  418. if(PlayerColor(i) != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  419. ids->push_back(i);
  420. break;
  421. case ARTIFACT_TYPE:
  422. ids = new std::vector<int>(market->availableItemsIds(mode));
  423. break;
  424. }
  425. }
  426. return ids;
  427. }
  428. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  429. {
  430. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  431. {
  432. //22 boxes, 5 in row, last row: two boxes centered
  433. int h, w, x, y, dx, dy;
  434. h = w = 44;
  435. x = 317;
  436. y = 53;
  437. dx = 54;
  438. dy = 70;
  439. for (int i = 0; i < 4 ; i++)
  440. for (int j = 0; j < 5 ; j++)
  441. poss.push_back(Rect(x + dx*j, y + dy*i, w, h));
  442. poss.push_back(Rect((int)(x + dx * 1.5), (y + dy * 4), w, h));
  443. poss.push_back(Rect((int)(x + dx * 2.5), (y + dy * 4), w, h));
  444. }
  445. else
  446. {
  447. //seven boxes:
  448. // X X X
  449. // X X X
  450. // X
  451. int h, w, x, y, dx, dy;
  452. int leftToRightOffset;
  453. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  454. const std::vector<Rect> tmp =
  455. {
  456. Rect(Point(x + 0 * dx, y + 0 * dx), Point(h, w) ),
  457. Rect(Point(x + 1 * dx, y + 0 * dx), Point(h, w) ),
  458. Rect(Point(x + 2 * dx, y + 0 * dx), Point(h, w) ),
  459. Rect(Point(x + 0 * dx, y + 1 * dy), Point(h, w) ),
  460. Rect(Point(x + 1 * dx, y + 1 * dy), Point(h, w) ),
  461. Rect(Point(x + 2 * dx, y + 1 * dy), Point(h, w) ),
  462. Rect(Point(x + 1 * dx, y + 2 * dy), Point(h, w) )
  463. };
  464. vstd::concatenate(poss, tmp);
  465. if(!Left)
  466. {
  467. for(Rect &r : poss)
  468. r.x += leftToRightOffset;
  469. }
  470. }
  471. }
  472. void CTradeWindow::initSubs(bool Left)
  473. {
  474. for(auto item : items[Left])
  475. {
  476. if(Left)
  477. {
  478. switch(itemsType[1])
  479. {
  480. case CREATURE:
  481. item->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(SlotID(item->serial)));
  482. break;
  483. case RESOURCE:
  484. item->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(item->serial)));
  485. break;
  486. }
  487. }
  488. else //right side
  489. {
  490. if(itemsType[0] == PLAYER)
  491. {
  492. item->subtitle = CGI->generaltexth->capColors[item->id];
  493. }
  494. else if(hLeft)//artifact, creature
  495. {
  496. int h1, h2; //hlp variables for getting offer
  497. market->getOffer(hLeft->id, item->id, h1, h2, mode);
  498. if(item->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  499. {
  500. std::ostringstream oss;
  501. oss << h2;
  502. if(h1!=1)
  503. oss << "/" << h1;
  504. item->subtitle = oss.str();
  505. }
  506. else
  507. item->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  508. }
  509. else
  510. item->subtitle = "";
  511. }
  512. }
  513. }
  514. void CTradeWindow::showAll(SDL_Surface * to)
  515. {
  516. CWindowObject::showAll(to);
  517. if(hRight)
  518. CSDL_Ext::drawBorder(to, hRight->pos.x-1, hRight->pos.y-1, hRight->pos.w+2, hRight->pos.h+2, Colors::BRIGHT_YELLOW);
  519. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  520. CSDL_Ext::drawBorder(to, hLeft->pos.x-1, hLeft->pos.y-1, hLeft->pos.w+2, hLeft->pos.h+2, Colors::BRIGHT_YELLOW);
  521. if(readyToTrade)
  522. {
  523. if(hLeft)
  524. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  525. if(hRight)
  526. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  527. }
  528. }
  529. void CTradeWindow::removeItems(const std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  530. {
  531. for(auto item : toRemove)
  532. removeItem(item);
  533. }
  534. void CTradeWindow::removeItem(std::shared_ptr<CTradeableItem> item)
  535. {
  536. items[item->left] -= item;
  537. if(hRight == item)
  538. {
  539. hRight.reset();
  540. selectionChanged(false);
  541. }
  542. }
  543. void CTradeWindow::getEmptySlots(std::set<std::shared_ptr<CTradeableItem>> & toRemove)
  544. {
  545. for(auto item : items[1])
  546. if(!hero->getStackCount(SlotID(item->serial)))
  547. toRemove.insert(item);
  548. }
  549. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  550. {
  551. const IMarket *m = market;
  552. const CGHeroInstance *h = hero;
  553. close();
  554. switch(Mode)
  555. {
  556. case EMarketMode::CREATURE_EXP:
  557. case EMarketMode::ARTIFACT_EXP:
  558. GH.pushIntT<CAltarWindow>(m, h, Mode);
  559. break;
  560. default:
  561. GH.pushIntT<CMarketplaceWindow>(m, h, Mode);
  562. break;
  563. }
  564. }
  565. void CTradeWindow::artifactSelected(CHeroArtPlace *slot)
  566. {
  567. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  568. items[1][0]->setArtInstance(slot->ourArt);
  569. if(slot->ourArt && !slot->locked)
  570. hLeft = items[1][0];
  571. else
  572. hLeft = nullptr;
  573. selectionChanged(true);
  574. }
  575. std::string CMarketplaceWindow::getBackgroundForMode(EMarketMode::EMarketMode mode)
  576. {
  577. switch(mode)
  578. {
  579. case EMarketMode::RESOURCE_RESOURCE:
  580. return "TPMRKRES.bmp";
  581. case EMarketMode::RESOURCE_PLAYER:
  582. return "TPMRKPTS.bmp";
  583. case EMarketMode::CREATURE_RESOURCE:
  584. return "TPMRKCRS.bmp";
  585. case EMarketMode::RESOURCE_ARTIFACT:
  586. return "TPMRKABS.bmp";
  587. case EMarketMode::ARTIFACT_RESOURCE:
  588. return "TPMRKASS.bmp";
  589. }
  590. assert(0);
  591. return "";
  592. }
  593. CMarketplaceWindow::CMarketplaceWindow(const IMarket * Market, const CGHeroInstance * Hero, EMarketMode::EMarketMode Mode)
  594. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, Mode)
  595. {
  596. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  597. madeTransaction = false;
  598. bool sliderNeeded = true;
  599. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  600. std::string title;
  601. if(market->o->ID == Obj::TOWN)
  602. {
  603. switch (mode)
  604. {
  605. case EMarketMode::CREATURE_RESOURCE:
  606. title = (*CGI->townh)[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->getNameTranslated();
  607. break;
  608. case EMarketMode::RESOURCE_ARTIFACT:
  609. title = (*CGI->townh)[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  610. sliderNeeded = false;
  611. break;
  612. case EMarketMode::ARTIFACT_RESOURCE:
  613. title = (*CGI->townh)[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  614. // create image that copies part of background containing slot MISC_1 into position of slot MISC_5
  615. // this is workaround for bug in H3 files where this slot for ragdoll on this screen is missing
  616. images.push_back(std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 18, 339 ));
  617. sliderNeeded = false;
  618. break;
  619. default:
  620. title = CGI->generaltexth->allTexts[158];
  621. break;
  622. }
  623. }
  624. else
  625. {
  626. switch(market->o->ID)
  627. {
  628. case Obj::BLACK_MARKET:
  629. title = CGI->generaltexth->allTexts[349];
  630. sliderNeeded = false;
  631. break;
  632. case Obj::TRADING_POST:
  633. title = CGI->generaltexth->allTexts[159];
  634. break;
  635. case Obj::TRADING_POST_SNOW:
  636. title = CGI->generaltexth->allTexts[159];
  637. break;
  638. default:
  639. title = market->o->getObjectName();
  640. break;
  641. }
  642. }
  643. titleLabel = std::make_shared<CLabel>(300, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title);
  644. initItems(false);
  645. initItems(true);
  646. ok = std::make_shared<CButton>(Point(516, 520), "IOK6432.DEF", CGI->generaltexth->zelp[600], [&](){ close(); }, SDLK_RETURN);
  647. ok->assignedKeys.insert(SDLK_ESCAPE);
  648. deal = std::make_shared<CButton>(Point(307, 520), "TPMRKB.DEF", CGI->generaltexth->zelp[595], [&](){ makeDeal(); } );
  649. deal->block(true);
  650. if(sliderNeeded)
  651. {
  652. slider = std::make_shared<CSlider>(Point(231, 490),137, std::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  653. max = std::make_shared<CButton>(Point(229, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[596], [&](){ setMax(); });
  654. max->block(true);
  655. }
  656. else
  657. {
  658. deal->moveBy(Point(-30, 0));
  659. }
  660. //left side
  661. switch(Mode)
  662. {
  663. case EMarketMode::RESOURCE_RESOURCE:
  664. case EMarketMode::RESOURCE_PLAYER:
  665. case EMarketMode::RESOURCE_ARTIFACT:
  666. labels.push_back(std::make_shared<CLabel>(154, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
  667. break;
  668. case EMarketMode::CREATURE_RESOURCE:
  669. //%s's Creatures
  670. labels.push_back(std::make_shared<CLabel>(152, 102, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  671. break;
  672. case EMarketMode::ARTIFACT_RESOURCE:
  673. //%s's Artifacts
  674. labels.push_back(std::make_shared<CLabel>(152, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->getNameTranslated())));
  675. break;
  676. }
  677. Rect traderTextRect;
  678. //right side
  679. switch(Mode)
  680. {
  681. case EMarketMode::RESOURCE_RESOURCE:
  682. case EMarketMode::CREATURE_RESOURCE:
  683. case EMarketMode::RESOURCE_ARTIFACT:
  684. case EMarketMode::ARTIFACT_RESOURCE:
  685. labels.push_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  686. traderTextRect = Rect(316, 48, 260, 75);
  687. break;
  688. case EMarketMode::RESOURCE_PLAYER:
  689. labels.push_back(std::make_shared<CLabel>(445, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
  690. traderTextRect = Rect(28, 48, 260, 75);
  691. break;
  692. }
  693. traderText = std::make_shared<CTextBox>("", traderTextRect, 0, FONT_SMALL, ETextAlignment::CENTER);
  694. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  695. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  696. buttons.push_back(std::make_shared<CButton>(Point(18, 520),"TPMRKBU1.DEF", CGI->generaltexth->zelp[612], [&](){ setMode(EMarketMode::RESOURCE_PLAYER);}));
  697. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  698. buttons.push_back(std::make_shared<CButton>(Point(516, 450 + specialOffset),"TPMRKBU5.DEF", CGI->generaltexth->zelp[605], [&](){ setMode(EMarketMode::RESOURCE_RESOURCE);}));
  699. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  700. buttons.push_back(std::make_shared<CButton>(Point(516, 485),"TPMRKBU4.DEF", CGI->generaltexth->zelp[599], [&](){ setMode(EMarketMode::CREATURE_RESOURCE);}));
  701. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  702. buttons.push_back(std::make_shared<CButton>(Point(18, 450 + specialOffset),"TPMRKBU2.DEF", CGI->generaltexth->zelp[598], [&](){ setMode(EMarketMode::RESOURCE_ARTIFACT);}));
  703. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  704. buttons.push_back(std::make_shared<CButton>(Point(18, 485),"TPMRKBU3.DEF", CGI->generaltexth->zelp[613], [&](){ setMode(EMarketMode::ARTIFACT_RESOURCE);}));
  705. updateTraderText();
  706. }
  707. CMarketplaceWindow::~CMarketplaceWindow() = default;
  708. void CMarketplaceWindow::setMax()
  709. {
  710. slider->moveToMax();
  711. }
  712. void CMarketplaceWindow::makeDeal()
  713. {
  714. int sliderValue = 0;
  715. if(slider)
  716. sliderValue = slider->getValue();
  717. else
  718. sliderValue = !deal->isBlocked(); //should always be 1
  719. if(!sliderValue)
  720. return;
  721. int leftIdToSend = -1;
  722. switch (mode)
  723. {
  724. case EMarketMode::CREATURE_RESOURCE:
  725. leftIdToSend = hLeft->serial;
  726. break;
  727. case EMarketMode::ARTIFACT_RESOURCE:
  728. leftIdToSend = hLeft->getArtInstance()->id.getNum();
  729. break;
  730. default:
  731. leftIdToSend = hLeft->id;
  732. break;
  733. }
  734. if(slider)
  735. {
  736. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->getValue()*r1, hero);
  737. slider->moveTo(0);
  738. }
  739. else
  740. {
  741. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  742. }
  743. madeTransaction = true;
  744. hLeft = nullptr;
  745. hRight = nullptr;
  746. selectionChanged(true);
  747. }
  748. void CMarketplaceWindow::sliderMoved( int to )
  749. {
  750. redraw();
  751. }
  752. void CMarketplaceWindow::selectionChanged(bool side)
  753. {
  754. readyToTrade = hLeft && hRight;
  755. if(mode == EMarketMode::RESOURCE_RESOURCE)
  756. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  757. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  758. arts->unmarkSlots(false);
  759. if(readyToTrade)
  760. {
  761. int soldItemId = hLeft->id;
  762. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  763. if(slider)
  764. {
  765. int newAmount = -1;
  766. if(itemsType[1] == RESOURCE)
  767. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId));
  768. else if(itemsType[1] == CREATURE)
  769. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack());
  770. else
  771. assert(0);
  772. slider->setAmount(newAmount / r1);
  773. slider->moveTo(0);
  774. max->block(false);
  775. deal->block(false);
  776. }
  777. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  778. {
  779. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId)) < r1);
  780. }
  781. else
  782. deal->block(false);
  783. }
  784. else
  785. {
  786. if(slider)
  787. {
  788. max->block(true);
  789. slider->setAmount(0);
  790. slider->moveTo(0);
  791. }
  792. deal->block(true);
  793. }
  794. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  795. initSubs(false);
  796. updateTraderText();
  797. redraw();
  798. }
  799. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  800. {
  801. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  802. }
  803. void CMarketplaceWindow::garrisonChanged()
  804. {
  805. if(mode != EMarketMode::CREATURE_RESOURCE)
  806. return;
  807. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  808. getEmptySlots(toRemove);
  809. removeItems(toRemove);
  810. initSubs(true);
  811. }
  812. void CMarketplaceWindow::artifactsChanged(bool Left)
  813. {
  814. assert(!Left);
  815. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  816. return;
  817. std::vector<int> available = market->availableItemsIds(mode);
  818. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  819. for(auto item : items[0])
  820. if(!vstd::contains(available, item->id))
  821. toRemove.insert(item);
  822. removeItems(toRemove);
  823. redraw();
  824. }
  825. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  826. {
  827. if(Left)
  828. {
  829. switch(itemsType[1])
  830. {
  831. case RESOURCE:
  832. case CREATURE:
  833. {
  834. int val = slider
  835. ? slider->getValue() * r1
  836. : (((deal->isBlocked())) ? 0 : r1);
  837. return boost::lexical_cast<std::string>(val);
  838. }
  839. case ARTIFACT_INSTANCE:
  840. return ((deal->isBlocked()) ? "0" : "1");
  841. }
  842. }
  843. else
  844. {
  845. switch(itemsType[0])
  846. {
  847. case RESOURCE:
  848. if(slider)
  849. return boost::lexical_cast<std::string>( slider->getValue() * r2 );
  850. else
  851. return boost::lexical_cast<std::string>(r2);
  852. case ARTIFACT_TYPE:
  853. return ((deal->isBlocked()) ? "0" : "1");
  854. case PLAYER:
  855. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  856. }
  857. }
  858. return "???";
  859. }
  860. Point CMarketplaceWindow::selectionOffset(bool Left) const
  861. {
  862. if(Left)
  863. {
  864. switch(itemsType[1])
  865. {
  866. case RESOURCE:
  867. return Point(122, 446);
  868. case CREATURE:
  869. return Point(128, 450);
  870. case ARTIFACT_INSTANCE:
  871. return Point(134, 466);
  872. }
  873. }
  874. else
  875. {
  876. switch(itemsType[0])
  877. {
  878. case RESOURCE:
  879. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  880. return Point(410, 469);
  881. else
  882. return Point(410, 446);
  883. case ARTIFACT_TYPE:
  884. return Point(425, 447);
  885. case PLAYER:
  886. return Point(417, 451);
  887. }
  888. }
  889. assert(0);
  890. return Point(0,0);
  891. }
  892. void CMarketplaceWindow::resourceChanged()
  893. {
  894. initSubs(true);
  895. }
  896. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  897. {
  898. switch(type)
  899. {
  900. case RESOURCE:
  901. dx = 82;
  902. dy = 79;
  903. x = 39;
  904. y = 180;
  905. h = 68;
  906. w = 70;
  907. break;
  908. case PLAYER:
  909. dx = 83;
  910. dy = 118;
  911. h = 64;
  912. w = 58;
  913. x = 44;
  914. y = 83;
  915. assert(Right);
  916. break;
  917. case CREATURE://45,123
  918. x = 45;
  919. y = 123;
  920. w = 58;
  921. h = 64;
  922. dx = 83;
  923. dy = 98;
  924. assert(!Right);
  925. break;
  926. case ARTIFACT_TYPE://45,123
  927. x = 340-289;
  928. y = 180;
  929. w = 44;
  930. h = 44;
  931. dx = 83;
  932. dy = 79;
  933. break;
  934. }
  935. leftToRightOffset = 289;
  936. }
  937. void CMarketplaceWindow::updateTraderText()
  938. {
  939. if(readyToTrade)
  940. {
  941. if(mode == EMarketMode::RESOURCE_PLAYER)
  942. {
  943. //I can give %s to the %s player.
  944. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  945. }
  946. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  947. {
  948. //I can offer you the %s for %d %s of %s.
  949. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  950. }
  951. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  952. {
  953. //I can offer you %d %s of %s for %d %s of %s.
  954. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  955. }
  956. else if(mode == EMarketMode::CREATURE_RESOURCE)
  957. {
  958. //I can offer you %d %s of %s for %d %s.
  959. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  960. }
  961. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  962. {
  963. //I can offer you %d %s of %s for your %s.
  964. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  965. }
  966. return;
  967. }
  968. int gnrtxtnr = -1;
  969. if(madeTransaction)
  970. {
  971. if(mode == EMarketMode::RESOURCE_PLAYER)
  972. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  973. else
  974. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  975. }
  976. else
  977. {
  978. if(mode == EMarketMode::RESOURCE_PLAYER)
  979. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  980. else
  981. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  982. }
  983. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  984. }
  985. CAltarWindow::CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, EMarketMode::EMarketMode Mode)
  986. : CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  987. {
  988. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  989. if(Mode == EMarketMode::CREATURE_EXP)
  990. {
  991. //%s's Creatures
  992. labels.push_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
  993. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  994. //Altar of Sacrifice
  995. labels.push_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
  996. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  997. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW);
  998. slider = std::make_shared<CSlider>(Point(231,481),137,std::bind(&CAltarWindow::sliderMoved,this,_1),0,0);
  999. max = std::make_shared<CButton>(Point(147, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[578], std::bind(&CSlider::moveToMax, slider));
  1000. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  1001. sacrificeAll = std::make_shared<CButton>(Point(393, 520), "ALTARMY.DEF", CGI->generaltexth->zelp[579], std::bind(&CAltarWindow::SacrificeAll,this));
  1002. initItems(true);
  1003. mimicCres();
  1004. }
  1005. else
  1006. {
  1007. //Sacrifice artifacts for experience
  1008. labels.push_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  1009. //%s's Creatures
  1010. labels.push_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  1011. sacrificeAll = std::make_shared<CButton>(Point(393, 520), "ALTFILL.DEF", CGI->generaltexth->zelp[571], std::bind(&CAltarWindow::SacrificeAll,this));
  1012. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  1013. sacrificeBackpack = std::make_shared<CButton>(Point(147, 520), "ALTEMBK.DEF", CGI->generaltexth->zelp[570], std::bind(&CAltarWindow::SacrificeBackpack,this));
  1014. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  1015. initItems(true);
  1016. initItems(false);
  1017. artIcon = std::make_shared<CAnimImage>("ARTIFACT", 0, 0, 281, 442);
  1018. artIcon->disable();
  1019. }
  1020. //Experience needed to reach next level
  1021. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1022. //Total experience on the Altar
  1023. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1024. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1025. ok = std::make_shared<CButton>(Point(516, 520), "IOK6432.DEF", CGI->generaltexth->zelp[568], [&](){ close();}, SDLK_RETURN);
  1026. ok->assignedKeys.insert(SDLK_ESCAPE);
  1027. deal = std::make_shared<CButton>(Point(269, 520), "ALTSACR.DEF", CGI->generaltexth->zelp[585], std::bind(&CAltarWindow::makeDeal,this));
  1028. if(Mode == EMarketMode::CREATURE_EXP)
  1029. {
  1030. auto changeMode = std::make_shared<CButton>(Point(516, 421), "ALTART.DEF", CGI->generaltexth->zelp[580], std::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP));
  1031. if(Hero->getAlignment() == ::EAlignment::EVIL)
  1032. changeMode->block(true);
  1033. buttons.push_back(changeMode);
  1034. }
  1035. else if(Mode == EMarketMode::ARTIFACT_EXP)
  1036. {
  1037. auto changeMode = std::make_shared<CButton>(Point(516, 421), "ALTSACC.DEF", CGI->generaltexth->zelp[572], std::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP));
  1038. if(Hero->getAlignment() == ::EAlignment::GOOD)
  1039. changeMode->block(true);
  1040. buttons.push_back(changeMode);
  1041. }
  1042. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  1043. getExpValues();
  1044. expToLevel = std::make_shared<CLabel>(73, 475, FONT_SMALL, ETextAlignment::CENTER);
  1045. expOnAltar = std::make_shared<CLabel>(73, 543, FONT_SMALL, ETextAlignment::CENTER);
  1046. setExpToLevel();
  1047. calcTotalExp();
  1048. blockTrade();
  1049. }
  1050. CAltarWindow::~CAltarWindow() = default;
  1051. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1052. {
  1053. leftToRightOffset = 289;
  1054. x = 45;
  1055. y = 110;
  1056. w = 58;
  1057. h = 64;
  1058. dx = 83;
  1059. dy = 98;
  1060. }
  1061. void CAltarWindow::sliderMoved(int to)
  1062. {
  1063. if(hLeft)
  1064. sacrificedUnits[hLeft->serial] = to;
  1065. if(hRight)
  1066. updateRight(hRight);
  1067. deal->block(!to);
  1068. calcTotalExp();
  1069. redraw();
  1070. }
  1071. void CAltarWindow::makeDeal()
  1072. {
  1073. if(mode == EMarketMode::CREATURE_EXP)
  1074. {
  1075. blockTrade();
  1076. slider->moveTo(0);
  1077. std::vector<ui32> ids;
  1078. std::vector<ui32> toSacrifice;
  1079. for(int i = 0; i < sacrificedUnits.size(); i++)
  1080. {
  1081. if(sacrificedUnits[i])
  1082. {
  1083. ids.push_back(i);
  1084. toSacrifice.push_back(sacrificedUnits[i]);
  1085. }
  1086. }
  1087. LOCPLINT->cb->trade(market->o, mode, ids, {}, toSacrifice, hero);
  1088. for(int& val : sacrificedUnits)
  1089. val = 0;
  1090. for(auto item : items[0])
  1091. {
  1092. item->setType(CREATURE_PLACEHOLDER);
  1093. item->subtitle = "";
  1094. }
  1095. }
  1096. else
  1097. {
  1098. std::vector<ui32> positions;
  1099. for(const CArtifactInstance *art : arts->artifactsOnAltar) //sacrifice each artifact on the list
  1100. {
  1101. positions.push_back(hero->getArtPos(art));
  1102. }
  1103. LOCPLINT->cb->trade(market->o, mode, positions, {}, {}, hero);
  1104. arts->artifactsOnAltar.clear();
  1105. for(auto item : items[0])
  1106. {
  1107. item->setID(-1);
  1108. item->subtitle = "";
  1109. }
  1110. arts->commonInfo->reset();
  1111. //arts->scrollBackpack(0);
  1112. deal->block(true);
  1113. }
  1114. calcTotalExp();
  1115. }
  1116. void CAltarWindow::SacrificeAll()
  1117. {
  1118. if(mode == EMarketMode::CREATURE_EXP)
  1119. {
  1120. bool movedAnything = false;
  1121. for(auto item : items[1])
  1122. sacrificedUnits[item->serial] = hero->getStackCount(SlotID(item->serial));
  1123. sacrificedUnits[items[1].front()->serial]--;
  1124. for(auto item : items[0])
  1125. {
  1126. updateRight(item);
  1127. if(item->type == CREATURE)
  1128. movedAnything = true;
  1129. }
  1130. deal->block(!movedAnything);
  1131. calcTotalExp();
  1132. }
  1133. else
  1134. {
  1135. for(auto i = hero->artifactsWorn.cbegin(); i != hero->artifactsWorn.cend(); i++)
  1136. {
  1137. if(!i->second.locked) //ignore locks from assembled artifacts
  1138. moveFromSlotToAltar(i->first, nullptr, i->second.artifact);
  1139. }
  1140. SacrificeBackpack();
  1141. }
  1142. redraw();
  1143. }
  1144. void CAltarWindow::selectionChanged(bool side)
  1145. {
  1146. if(mode != EMarketMode::CREATURE_EXP)
  1147. return;
  1148. int stackCount = 0;
  1149. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  1150. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  1151. stackCount++;
  1152. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  1153. slider->block(!slider->getAmount());
  1154. slider->moveTo(sacrificedUnits[hLeft->serial]);
  1155. max->block(!slider->getAmount());
  1156. selectOppositeItem(side);
  1157. readyToTrade = true;
  1158. redraw();
  1159. }
  1160. void CAltarWindow::selectOppositeItem(bool side)
  1161. {
  1162. bool oppositeSide = !side;
  1163. int pos = vstd::find_pos(items[side], side ? hLeft : hRight);
  1164. int oppositePos = vstd::find_pos(items[oppositeSide], oppositeSide ? hLeft : hRight);
  1165. if(pos >= 0 && pos != oppositePos)
  1166. {
  1167. if(oppositeSide)
  1168. hLeft = items[oppositeSide][pos];
  1169. else
  1170. hRight = items[oppositeSide][pos];
  1171. selectionChanged(oppositeSide);
  1172. }
  1173. }
  1174. void CAltarWindow::mimicCres()
  1175. {
  1176. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1177. std::vector<Rect> positions;
  1178. getPositionsFor(positions, false, CREATURE);
  1179. for(auto item : items[1])
  1180. {
  1181. auto hlp = std::make_shared<CTradeableItem>(positions[item->serial].topLeft(), CREATURE_PLACEHOLDER, item->id, false, item->serial);
  1182. hlp->pos = positions[item->serial] + this->pos.topLeft();
  1183. items[0].push_back(hlp);
  1184. }
  1185. }
  1186. Point CAltarWindow::selectionOffset(bool Left) const
  1187. {
  1188. if(Left)
  1189. return Point(150, 421);
  1190. else
  1191. return Point(396, 421);
  1192. }
  1193. std::string CAltarWindow::selectionSubtitle(bool Left) const
  1194. {
  1195. if(Left && slider && hLeft)
  1196. return boost::lexical_cast<std::string>(slider->getValue());
  1197. else if(!Left && hRight)
  1198. return hRight->subtitle;
  1199. else
  1200. return "";
  1201. }
  1202. void CAltarWindow::artifactsChanged(bool left)
  1203. {
  1204. }
  1205. void CAltarWindow::garrisonChanged()
  1206. {
  1207. if(mode != EMarketMode::CREATURE_EXP)
  1208. return;
  1209. std::set<std::shared_ptr<CTradeableItem>> empty;
  1210. getEmptySlots(empty);
  1211. removeItems(empty);
  1212. initSubs(true);
  1213. getExpValues();
  1214. }
  1215. void CAltarWindow::getExpValues()
  1216. {
  1217. int dump;
  1218. for(auto item : items[1])
  1219. {
  1220. if(item->id >= 0)
  1221. market->getOffer(item->id, 0, dump, expPerUnit[item->serial], EMarketMode::CREATURE_EXP);
  1222. }
  1223. }
  1224. void CAltarWindow::calcTotalExp()
  1225. {
  1226. int val = 0;
  1227. if(mode == EMarketMode::CREATURE_EXP)
  1228. {
  1229. for (int i = 0; i < sacrificedUnits.size(); i++)
  1230. {
  1231. val += expPerUnit[i] * sacrificedUnits[i];
  1232. }
  1233. }
  1234. else
  1235. {
  1236. for(const CArtifactInstance *art : arts->artifactsOnAltar)
  1237. {
  1238. int dmp, valOfArt;
  1239. market->getOffer(art->artType->getId(), 0, dmp, valOfArt, mode);
  1240. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  1241. }
  1242. }
  1243. val = static_cast<int>(hero->calculateXp(val));
  1244. expOnAltar->setText(boost::lexical_cast<std::string>(val));
  1245. }
  1246. void CAltarWindow::setExpToLevel()
  1247. {
  1248. expToLevel->setText(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  1249. }
  1250. void CAltarWindow::blockTrade()
  1251. {
  1252. hLeft = hRight = nullptr;
  1253. readyToTrade = false;
  1254. if(slider)
  1255. {
  1256. slider->block(true);
  1257. max->block(true);
  1258. }
  1259. deal->block(true);
  1260. }
  1261. void CAltarWindow::updateRight(std::shared_ptr<CTradeableItem> toUpdate)
  1262. {
  1263. int val = sacrificedUnits[toUpdate->serial];
  1264. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  1265. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(hero->calculateXp(val * expPerUnit[toUpdate->serial]))) : ""; //%s exp
  1266. }
  1267. int CAltarWindow::firstFreeSlot()
  1268. {
  1269. int ret = -1;
  1270. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  1271. return items[0][ret]->id == -1 ? ret : -1;
  1272. }
  1273. void CAltarWindow::SacrificeBackpack()
  1274. {
  1275. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  1276. for (auto & elem : hero->artifactsInBackpack)
  1277. {
  1278. if(vstd::contains(toOmmit, elem.artifact))
  1279. {
  1280. toOmmit -= elem.artifact;
  1281. continue;
  1282. }
  1283. putOnAltar(nullptr, elem.artifact);
  1284. }
  1285. arts->scrollBackpack(0);
  1286. calcTotalExp();
  1287. }
  1288. void CAltarWindow::artifactPicked()
  1289. {
  1290. redraw();
  1291. }
  1292. void CAltarWindow::showAll(SDL_Surface * to)
  1293. {
  1294. CTradeWindow::showAll(to);
  1295. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  1296. {
  1297. artIcon->setFrame(arts->commonInfo->src.art->artType->iconIndex);
  1298. artIcon->showAll(to);
  1299. int dmp, val;
  1300. market->getOffer(arts->commonInfo->src.art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1301. val = static_cast<int>(hero->calculateXp(val));
  1302. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
  1303. }
  1304. }
  1305. bool CAltarWindow::putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1306. {
  1307. if(!art->artType->isTradable()) //special art
  1308. {
  1309. logGlobal->warn("Cannot put special artifact on altar!");
  1310. return false;
  1311. }
  1312. if(!altarSlot || altarSlot->id != -1)
  1313. {
  1314. int slotIndex = firstFreeSlot();
  1315. if(slotIndex < 0)
  1316. {
  1317. logGlobal->warn("No free slots on altar!");
  1318. return false;
  1319. }
  1320. altarSlot = items[0][slotIndex];
  1321. }
  1322. int dmp, val;
  1323. market->getOffer(art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1324. val = static_cast<int>(hero->calculateXp(val));
  1325. arts->artifactsOnAltar.insert(art);
  1326. altarSlot->setArtInstance(art);
  1327. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  1328. deal->block(false);
  1329. return true;
  1330. }
  1331. void CAltarWindow::moveFromSlotToAltar(ArtifactPosition slotID, std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1332. {
  1333. auto freeBackpackSlot = ArtifactPosition((si32)hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  1334. if(arts->commonInfo->src.art)
  1335. {
  1336. arts->commonInfo->dst.slotID = freeBackpackSlot;
  1337. arts->commonInfo->dst.AOH = arts.get();
  1338. }
  1339. if(putOnAltar(altarSlot, art))
  1340. {
  1341. if(slotID < GameConstants::BACKPACK_START)
  1342. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
  1343. else
  1344. {
  1345. arts->commonInfo->src.clear();
  1346. arts->commonInfo->dst.clear();
  1347. CCS->curh->dragAndDropCursor(nullptr);
  1348. arts->unmarkSlots(false);
  1349. }
  1350. }
  1351. }