CTradeWindow.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. if (!market->allowsTrade(M))
  783. return false;
  784. if (M == mode)
  785. return false;
  786. if ( M == EMarketMode::RESOURCE_RESOURCE || M == EMarketMode::RESOURCE_PLAYER)
  787. {
  788. auto * town = dynamic_cast<const CGTownInstance *>(market);
  789. if (town)
  790. return town->getOwner() == LOCPLINT->playerID;
  791. else
  792. return true;
  793. }
  794. else
  795. {
  796. return hero != nullptr;
  797. }
  798. }
  799. void CMarketplaceWindow::garrisonChanged()
  800. {
  801. if(mode != EMarketMode::CREATURE_RESOURCE)
  802. return;
  803. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  804. getEmptySlots(toRemove);
  805. removeItems(toRemove);
  806. initSubs(true);
  807. }
  808. void CMarketplaceWindow::artifactsChanged(bool Left)
  809. {
  810. assert(!Left);
  811. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  812. return;
  813. std::vector<int> available = market->availableItemsIds(mode);
  814. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  815. for(auto item : items[0])
  816. if(!vstd::contains(available, item->id))
  817. toRemove.insert(item);
  818. removeItems(toRemove);
  819. // clear set to erase final instance of shared_ptr - we want to redraw screen only after it has been deleted
  820. toRemove.clear();
  821. redraw();
  822. }
  823. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  824. {
  825. if(Left)
  826. {
  827. switch(itemsType[1])
  828. {
  829. case RESOURCE:
  830. case CREATURE:
  831. {
  832. int val = slider
  833. ? slider->getValue() * r1
  834. : (((deal->isBlocked())) ? 0 : r1);
  835. return std::to_string(val);
  836. }
  837. case ARTIFACT_INSTANCE:
  838. return ((deal->isBlocked()) ? "0" : "1");
  839. }
  840. }
  841. else
  842. {
  843. switch(itemsType[0])
  844. {
  845. case RESOURCE:
  846. if(slider)
  847. return std::to_string( slider->getValue() * r2 );
  848. else
  849. return std::to_string(r2);
  850. case ARTIFACT_TYPE:
  851. return ((deal->isBlocked()) ? "0" : "1");
  852. case PLAYER:
  853. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  854. }
  855. }
  856. return "???";
  857. }
  858. Point CMarketplaceWindow::selectionOffset(bool Left) const
  859. {
  860. if(Left)
  861. {
  862. switch(itemsType[1])
  863. {
  864. case RESOURCE:
  865. return Point(122, 446);
  866. case CREATURE:
  867. return Point(128, 450);
  868. case ARTIFACT_INSTANCE:
  869. return Point(134, 466);
  870. }
  871. }
  872. else
  873. {
  874. switch(itemsType[0])
  875. {
  876. case RESOURCE:
  877. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  878. return Point(410, 469);
  879. else
  880. return Point(410, 446);
  881. case ARTIFACT_TYPE:
  882. return Point(425, 447);
  883. case PLAYER:
  884. return Point(417, 451);
  885. }
  886. }
  887. assert(0);
  888. return Point(0,0);
  889. }
  890. void CMarketplaceWindow::resourceChanged()
  891. {
  892. initSubs(true);
  893. }
  894. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  895. {
  896. switch(type)
  897. {
  898. case RESOURCE:
  899. dx = 82;
  900. dy = 79;
  901. x = 39;
  902. y = 180;
  903. h = 68;
  904. w = 70;
  905. break;
  906. case PLAYER:
  907. dx = 83;
  908. dy = 118;
  909. h = 64;
  910. w = 58;
  911. x = 44;
  912. y = 83;
  913. assert(Right);
  914. break;
  915. case CREATURE://45,123
  916. x = 45;
  917. y = 123;
  918. w = 58;
  919. h = 64;
  920. dx = 83;
  921. dy = 98;
  922. assert(!Right);
  923. break;
  924. case ARTIFACT_TYPE://45,123
  925. x = 340-289;
  926. y = 180;
  927. w = 44;
  928. h = 44;
  929. dx = 83;
  930. dy = 79;
  931. break;
  932. }
  933. leftToRightOffset = 289;
  934. }
  935. void CMarketplaceWindow::updateTraderText()
  936. {
  937. if(readyToTrade)
  938. {
  939. if(mode == EMarketMode::RESOURCE_PLAYER)
  940. {
  941. //I can give %s to the %s player.
  942. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  943. }
  944. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  945. {
  946. //I can offer you the %s for %d %s of %s.
  947. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  948. }
  949. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  950. {
  951. //I can offer you %d %s of %s for %d %s of %s.
  952. 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()));
  953. }
  954. else if(mode == EMarketMode::CREATURE_RESOURCE)
  955. {
  956. //I can offer you %d %s of %s for %d %s.
  957. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  958. }
  959. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  960. {
  961. //I can offer you %d %s of %s for your %s.
  962. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  963. }
  964. return;
  965. }
  966. int gnrtxtnr = -1;
  967. if(madeTransaction)
  968. {
  969. if(mode == EMarketMode::RESOURCE_PLAYER)
  970. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  971. else
  972. 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?
  973. }
  974. else
  975. {
  976. if(mode == EMarketMode::RESOURCE_PLAYER)
  977. 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.
  978. else
  979. 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.
  980. }
  981. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  982. }
  983. CAltarWindow::CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, EMarketMode Mode)
  984. : CTradeWindow(ImagePath::builtin(Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  985. {
  986. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  987. if(Mode == EMarketMode::CREATURE_EXP)
  988. {
  989. //%s's Creatures
  990. labels.push_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
  991. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  992. //Altar of Sacrifice
  993. labels.push_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
  994. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  995. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW);
  996. slider = std::make_shared<CSlider>(Point(231, 481), 137, std::bind(&CAltarWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  997. max = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[578], std::bind(&CSlider::scrollToMax, slider));
  998. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  999. sacrificeAll = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[579], std::bind(&CAltarWindow::SacrificeAll,this));
  1000. initItems(true);
  1001. mimicCres();
  1002. }
  1003. else
  1004. {
  1005. //Sacrifice artifacts for experience
  1006. labels.push_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  1007. //%s's Creatures
  1008. labels.push_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  1009. sacrificeAll = std::make_shared<CButton>(Point(393, 520), AnimationPath::builtin("ALTFILL.DEF"), CGI->generaltexth->zelp[571], std::bind(&CAltarWindow::SacrificeAll,this));
  1010. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  1011. sacrificeBackpack = std::make_shared<CButton>(Point(147, 520), AnimationPath::builtin("ALTEMBK.DEF"), CGI->generaltexth->zelp[570], std::bind(&CAltarWindow::SacrificeBackpack,this));
  1012. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  1013. arts = std::make_shared<CArtifactsOfHeroAltar>(Point(-365, -12));
  1014. arts->setHero(hero);
  1015. addSetAndCallbacks(arts);
  1016. initItems(true);
  1017. initItems(false);
  1018. artIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("ARTIFACT"), 0, 0, 281, 442);
  1019. artIcon->disable();
  1020. }
  1021. //Experience needed to reach next level
  1022. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1023. //Total experience on the Altar
  1024. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1025. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1026. ok = std::make_shared<CButton>(Point(516, 520), AnimationPath::builtin("IOK6432.DEF"), CGI->generaltexth->zelp[568], [&](){ close();}, EShortcut::GLOBAL_RETURN);
  1027. deal = std::make_shared<CButton>(Point(269, 520), AnimationPath::builtin("ALTSACR.DEF"), CGI->generaltexth->zelp[585], std::bind(&CAltarWindow::makeDeal,this));
  1028. if(Mode == EMarketMode::CREATURE_EXP)
  1029. {
  1030. auto changeMode = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTART.DEF"), CGI->generaltexth->zelp[580], std::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP));
  1031. if(Hero->getAlignment() == ::EAlignment::EVIL)
  1032. changeMode->block(true);
  1033. buttons.push_back(changeMode);
  1034. }
  1035. else if(Mode == EMarketMode::ARTIFACT_EXP)
  1036. {
  1037. auto changeMode = std::make_shared<CButton>(Point(516, 421), AnimationPath::builtin("ALTSACC.DEF"), CGI->generaltexth->zelp[572], std::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP));
  1038. if(Hero->getAlignment() == ::EAlignment::GOOD)
  1039. changeMode->block(true);
  1040. buttons.push_back(changeMode);
  1041. }
  1042. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  1043. getExpValues();
  1044. expToLevel = std::make_shared<CLabel>(73, 475, FONT_SMALL, ETextAlignment::CENTER);
  1045. expOnAltar = std::make_shared<CLabel>(73, 543, FONT_SMALL, ETextAlignment::CENTER);
  1046. setExpToLevel();
  1047. calcTotalExp();
  1048. blockTrade();
  1049. }
  1050. CAltarWindow::~CAltarWindow() = default;
  1051. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1052. {
  1053. leftToRightOffset = 289;
  1054. x = 45;
  1055. y = 110;
  1056. w = 58;
  1057. h = 64;
  1058. dx = 83;
  1059. dy = 98;
  1060. }
  1061. void CAltarWindow::sliderMoved(int to)
  1062. {
  1063. if(hLeft)
  1064. sacrificedUnits[hLeft->serial] = to;
  1065. if(hRight)
  1066. updateRight(hRight);
  1067. deal->block(!to);
  1068. calcTotalExp();
  1069. redraw();
  1070. }
  1071. void CAltarWindow::makeDeal()
  1072. {
  1073. if(mode == EMarketMode::CREATURE_EXP)
  1074. {
  1075. blockTrade();
  1076. slider->scrollTo(0);
  1077. std::vector<ui32> ids;
  1078. std::vector<ui32> toSacrifice;
  1079. for(int i = 0; i < sacrificedUnits.size(); i++)
  1080. {
  1081. if(sacrificedUnits[i])
  1082. {
  1083. ids.push_back(i);
  1084. toSacrifice.push_back(sacrificedUnits[i]);
  1085. }
  1086. }
  1087. LOCPLINT->cb->trade(market, mode, ids, {}, toSacrifice, hero);
  1088. for(int& val : sacrificedUnits)
  1089. val = 0;
  1090. for(auto item : items[0])
  1091. {
  1092. item->setType(CREATURE_PLACEHOLDER);
  1093. item->subtitle = "";
  1094. }
  1095. }
  1096. else
  1097. {
  1098. std::vector<ui32> positions;
  1099. for(const CArtifactInstance * art : arts->artifactsOnAltar)
  1100. {
  1101. positions.push_back(hero->getSlotByInstance(art));
  1102. }
  1103. std::sort(positions.begin(), positions.end(), std::greater<>());
  1104. LOCPLINT->cb->trade(market, mode, positions, {}, {}, hero);
  1105. arts->artifactsOnAltar.clear();
  1106. for(auto item : items[0])
  1107. {
  1108. item->setID(-1);
  1109. item->subtitle = "";
  1110. }
  1111. //arts->scrollBackpack(0);
  1112. deal->block(true);
  1113. }
  1114. calcTotalExp();
  1115. }
  1116. void CAltarWindow::SacrificeAll()
  1117. {
  1118. if(mode == EMarketMode::CREATURE_EXP)
  1119. {
  1120. bool movedAnything = false;
  1121. for(auto item : items[1])
  1122. sacrificedUnits[item->serial] = hero->getStackCount(SlotID(item->serial));
  1123. sacrificedUnits[items[1].front()->serial]--;
  1124. for(auto item : items[0])
  1125. {
  1126. updateRight(item);
  1127. if(item->type == CREATURE)
  1128. movedAnything = true;
  1129. }
  1130. deal->block(!movedAnything);
  1131. calcTotalExp();
  1132. }
  1133. else
  1134. {
  1135. std::vector<ConstTransitivePtr<CArtifactInstance>> artsForMove;
  1136. for(const auto& slotInfo : arts->visibleArtSet.artifactsWorn)
  1137. {
  1138. if(!slotInfo.second.locked && slotInfo.second.artifact->artType->isTradable())
  1139. artsForMove.push_back(slotInfo.second.artifact);
  1140. }
  1141. for(auto artInst : artsForMove)
  1142. moveArtToAltar(nullptr, artInst);
  1143. arts->updateWornSlots();
  1144. SacrificeBackpack();
  1145. }
  1146. redraw();
  1147. }
  1148. void CAltarWindow::selectionChanged(bool side)
  1149. {
  1150. if(mode != EMarketMode::CREATURE_EXP)
  1151. return;
  1152. int stackCount = 0;
  1153. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  1154. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  1155. stackCount++;
  1156. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  1157. slider->block(!slider->getAmount());
  1158. slider->scrollTo(sacrificedUnits[hLeft->serial]);
  1159. max->block(!slider->getAmount());
  1160. selectOppositeItem(side);
  1161. readyToTrade = true;
  1162. redraw();
  1163. }
  1164. void CAltarWindow::selectOppositeItem(bool side)
  1165. {
  1166. bool oppositeSide = !side;
  1167. int pos = vstd::find_pos(items[side], side ? hLeft : hRight);
  1168. int oppositePos = vstd::find_pos(items[oppositeSide], oppositeSide ? hLeft : hRight);
  1169. if(pos >= 0 && pos != oppositePos)
  1170. {
  1171. if(oppositeSide)
  1172. hLeft = items[oppositeSide][pos];
  1173. else
  1174. hRight = items[oppositeSide][pos];
  1175. selectionChanged(oppositeSide);
  1176. }
  1177. }
  1178. void CAltarWindow::mimicCres()
  1179. {
  1180. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1181. std::vector<Rect> positions;
  1182. getPositionsFor(positions, false, CREATURE);
  1183. for(auto item : items[1])
  1184. {
  1185. auto hlp = std::make_shared<CTradeableItem>(positions[item->serial].topLeft(), CREATURE_PLACEHOLDER, item->id, false, item->serial);
  1186. hlp->pos = positions[item->serial] + this->pos.topLeft();
  1187. items[0].push_back(hlp);
  1188. }
  1189. }
  1190. Point CAltarWindow::selectionOffset(bool Left) const
  1191. {
  1192. if(Left)
  1193. return Point(150, 421);
  1194. else
  1195. return Point(396, 421);
  1196. }
  1197. std::string CAltarWindow::selectionSubtitle(bool Left) const
  1198. {
  1199. if(Left && slider && hLeft)
  1200. return std::to_string(slider->getValue());
  1201. else if(!Left && hRight)
  1202. return hRight->subtitle;
  1203. else
  1204. return "";
  1205. }
  1206. void CAltarWindow::artifactsChanged(bool left)
  1207. {
  1208. }
  1209. void CAltarWindow::garrisonChanged()
  1210. {
  1211. if(mode != EMarketMode::CREATURE_EXP)
  1212. return;
  1213. std::set<std::shared_ptr<CTradeableItem>> empty;
  1214. getEmptySlots(empty);
  1215. removeItems(empty);
  1216. initSubs(true);
  1217. getExpValues();
  1218. }
  1219. void CAltarWindow::getExpValues()
  1220. {
  1221. int dump;
  1222. for(auto item : items[1])
  1223. {
  1224. if(item->id >= 0)
  1225. market->getOffer(item->id, 0, dump, expPerUnit[item->serial], EMarketMode::CREATURE_EXP);
  1226. }
  1227. }
  1228. void CAltarWindow::calcTotalExp()
  1229. {
  1230. int val = 0;
  1231. if(mode == EMarketMode::CREATURE_EXP)
  1232. {
  1233. for (int i = 0; i < sacrificedUnits.size(); i++)
  1234. {
  1235. val += expPerUnit[i] * sacrificedUnits[i];
  1236. }
  1237. }
  1238. else
  1239. {
  1240. auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1241. for(const CArtifactInstance * art : artifactsOfHero->artifactsOnAltar)
  1242. {
  1243. int dmp, valOfArt;
  1244. market->getOffer(art->artType->getId(), 0, dmp, valOfArt, mode);
  1245. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  1246. }
  1247. }
  1248. val = static_cast<int>(hero->calculateXp(val));
  1249. expOnAltar->setText(std::to_string(val));
  1250. }
  1251. void CAltarWindow::setExpToLevel()
  1252. {
  1253. expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  1254. }
  1255. void CAltarWindow::blockTrade()
  1256. {
  1257. hLeft = hRight = nullptr;
  1258. readyToTrade = false;
  1259. if(slider)
  1260. {
  1261. slider->block(true);
  1262. max->block(true);
  1263. }
  1264. deal->block(true);
  1265. }
  1266. void CAltarWindow::updateRight(std::shared_ptr<CTradeableItem> toUpdate)
  1267. {
  1268. int val = sacrificedUnits[toUpdate->serial];
  1269. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  1270. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(val * expPerUnit[toUpdate->serial]))) : ""; //%s exp
  1271. }
  1272. int CAltarWindow::firstFreeSlot()
  1273. {
  1274. int ret = -1;
  1275. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  1276. return items[0][ret]->id == -1 ? ret : -1;
  1277. }
  1278. void CAltarWindow::SacrificeBackpack()
  1279. {
  1280. while(!arts->visibleArtSet.artifactsInBackpack.empty())
  1281. {
  1282. if(!putOnAltar(nullptr, arts->visibleArtSet.artifactsInBackpack[0].artifact))
  1283. break;
  1284. };
  1285. calcTotalExp();
  1286. }
  1287. void CAltarWindow::artifactPicked()
  1288. {
  1289. redraw();
  1290. }
  1291. void CAltarWindow::showAll(Canvas & to)
  1292. {
  1293. CTradeWindow::showAll(to);
  1294. if(mode == EMarketMode::ARTIFACT_EXP && arts)
  1295. {
  1296. if(auto pickedArt = arts->getPickedArtifact())
  1297. {
  1298. artIcon->setFrame(pickedArt->artType->getIconIndex());
  1299. artIcon->showAll(to);
  1300. int dmp, val;
  1301. market->getOffer(pickedArt->getTypeId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1302. val = static_cast<int>(hero->calculateXp(val));
  1303. to.drawText(Point(304, 498), FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, std::to_string(val));
  1304. }
  1305. }
  1306. }
  1307. bool CAltarWindow::putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1308. {
  1309. if(!art->artType->isTradable()) //special art
  1310. {
  1311. logGlobal->warn("Cannot put special artifact on altar!");
  1312. return false;
  1313. }
  1314. if(!altarSlot || altarSlot->id != -1)
  1315. {
  1316. int slotIndex = firstFreeSlot();
  1317. if(slotIndex < 0)
  1318. {
  1319. logGlobal->warn("No free slots on altar!");
  1320. return false;
  1321. }
  1322. altarSlot = items[0][slotIndex];
  1323. }
  1324. int dmp, val;
  1325. market->getOffer(art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1326. val = static_cast<int>(hero->calculateXp(val));
  1327. arts->artifactsOnAltar.insert(art);
  1328. arts->deleteFromVisible(art);
  1329. altarSlot->setArtInstance(art);
  1330. altarSlot->subtitle = std::to_string(val);
  1331. deal->block(false);
  1332. return true;
  1333. }
  1334. void CAltarWindow::moveArtToAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1335. {
  1336. if(putOnAltar(altarSlot, art))
  1337. {
  1338. CCS->curh->dragAndDropCursor(nullptr);
  1339. arts->unmarkSlots();
  1340. }
  1341. }