CTradeWindow.cpp 40 KB

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