CTradeWindow.cpp 40 KB

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