CTradeWindow.cpp 40 KB

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