CTradeWindow.cpp 40 KB

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