CTradeWindow.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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 = true;
  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. sliderNeeded = false;
  622. break;
  623. case EMarketMode::ARTIFACT_RESOURCE:
  624. title = (*CGI->townh)[o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->getNameTranslated();
  625. // create image that copies part of background containing slot MISC_1 into position of slot MISC_5
  626. // this is workaround for bug in H3 files where this slot for ragdoll on this screen is missing
  627. images.push_back(std::make_shared<CPicture>(background->getSurface(), Rect(20, 187, 47, 47), 18, 339 ));
  628. sliderNeeded = false;
  629. break;
  630. default:
  631. title = CGI->generaltexth->allTexts[158];
  632. break;
  633. }
  634. }
  635. else
  636. {
  637. if(auto * o = dynamic_cast<const CGObjectInstance *>(market))
  638. {
  639. switch(o->ID)
  640. {
  641. case Obj::BLACK_MARKET:
  642. title = CGI->generaltexth->allTexts[349];
  643. sliderNeeded = false;
  644. break;
  645. case Obj::TRADING_POST:
  646. title = CGI->generaltexth->allTexts[159];
  647. break;
  648. case Obj::TRADING_POST_SNOW:
  649. title = CGI->generaltexth->allTexts[159];
  650. break;
  651. default:
  652. title = o->getObjectName();
  653. break;
  654. }
  655. }
  656. }
  657. titleLabel = std::make_shared<CLabel>(300, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, title);
  658. initItems(false);
  659. initItems(true);
  660. ok = std::make_shared<CButton>(Point(516, 520), "IOK6432.DEF", CGI->generaltexth->zelp[600], [&](){ close(); }, SDLK_RETURN);
  661. ok->assignedKeys.insert(SDLK_ESCAPE);
  662. deal = std::make_shared<CButton>(Point(307, 520), "TPMRKB.DEF", CGI->generaltexth->zelp[595], [&](){ makeDeal(); } );
  663. deal->block(true);
  664. if(sliderNeeded)
  665. {
  666. slider = std::make_shared<CSlider>(Point(231, 490),137, std::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  667. max = std::make_shared<CButton>(Point(229, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[596], [&](){ setMax(); });
  668. max->block(true);
  669. }
  670. else
  671. {
  672. deal->moveBy(Point(-30, 0));
  673. }
  674. //left side
  675. switch(Mode)
  676. {
  677. case EMarketMode::RESOURCE_RESOURCE:
  678. case EMarketMode::RESOURCE_PLAYER:
  679. case EMarketMode::RESOURCE_ARTIFACT:
  680. labels.push_back(std::make_shared<CLabel>(154, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]));
  681. break;
  682. case EMarketMode::CREATURE_RESOURCE:
  683. //%s's Creatures
  684. 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())));
  685. break;
  686. case EMarketMode::ARTIFACT_RESOURCE:
  687. //%s's Artifacts
  688. 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())));
  689. break;
  690. }
  691. Rect traderTextRect;
  692. //right side
  693. switch(Mode)
  694. {
  695. case EMarketMode::RESOURCE_RESOURCE:
  696. case EMarketMode::CREATURE_RESOURCE:
  697. case EMarketMode::RESOURCE_ARTIFACT:
  698. case EMarketMode::ARTIFACT_RESOURCE:
  699. labels.push_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
  700. traderTextRect = Rect(316, 48, 260, 75);
  701. break;
  702. case EMarketMode::RESOURCE_PLAYER:
  703. labels.push_back(std::make_shared<CLabel>(445, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
  704. traderTextRect = Rect(28, 48, 260, 75);
  705. break;
  706. }
  707. traderText = std::make_shared<CTextBox>("", traderTextRect, 0, FONT_SMALL, ETextAlignment::CENTER);
  708. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  709. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  710. buttons.push_back(std::make_shared<CButton>(Point(18, 520),"TPMRKBU1.DEF", CGI->generaltexth->zelp[612], [&](){ setMode(EMarketMode::RESOURCE_PLAYER);}));
  711. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  712. buttons.push_back(std::make_shared<CButton>(Point(516, 450 + specialOffset),"TPMRKBU5.DEF", CGI->generaltexth->zelp[605], [&](){ setMode(EMarketMode::RESOURCE_RESOURCE);}));
  713. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  714. buttons.push_back(std::make_shared<CButton>(Point(516, 485),"TPMRKBU4.DEF", CGI->generaltexth->zelp[599], [&](){ setMode(EMarketMode::CREATURE_RESOURCE);}));
  715. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  716. buttons.push_back(std::make_shared<CButton>(Point(18, 450 + specialOffset),"TPMRKBU2.DEF", CGI->generaltexth->zelp[598], [&](){ setMode(EMarketMode::RESOURCE_ARTIFACT);}));
  717. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  718. buttons.push_back(std::make_shared<CButton>(Point(18, 485),"TPMRKBU3.DEF", CGI->generaltexth->zelp[613], [&](){ setMode(EMarketMode::ARTIFACT_RESOURCE);}));
  719. updateTraderText();
  720. }
  721. CMarketplaceWindow::~CMarketplaceWindow() = default;
  722. void CMarketplaceWindow::setMax()
  723. {
  724. slider->moveToMax();
  725. }
  726. void CMarketplaceWindow::makeDeal()
  727. {
  728. int sliderValue = 0;
  729. if(slider)
  730. sliderValue = slider->getValue();
  731. else
  732. sliderValue = !deal->isBlocked(); //should always be 1
  733. if(!sliderValue)
  734. return;
  735. bool allowDeal = true;
  736. int leftIdToSend = hLeft->id;
  737. switch (mode)
  738. {
  739. case EMarketMode::CREATURE_RESOURCE:
  740. leftIdToSend = hLeft->serial;
  741. break;
  742. case EMarketMode::ARTIFACT_RESOURCE:
  743. leftIdToSend = hLeft->getArtInstance()->id.getNum();
  744. break;
  745. case EMarketMode::RESOURCE_ARTIFACT:
  746. if(!ArtifactID(hRight->id).toArtifact()->canBePutAt(hero))
  747. {
  748. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.326"));
  749. allowDeal = false;
  750. }
  751. break;
  752. default:
  753. break;
  754. }
  755. if(allowDeal)
  756. {
  757. const auto * o = dynamic_cast<const CGObjectInstance *>(market);
  758. if(slider)
  759. {
  760. LOCPLINT->cb->trade(o, mode, leftIdToSend, hRight->id, slider->getValue() * r1, hero);
  761. slider->moveTo(0);
  762. }
  763. else
  764. {
  765. LOCPLINT->cb->trade(o, mode, leftIdToSend, hRight->id, r2, hero);
  766. }
  767. }
  768. madeTransaction = true;
  769. hLeft = nullptr;
  770. hRight = nullptr;
  771. selectionChanged(true);
  772. }
  773. void CMarketplaceWindow::sliderMoved( int to )
  774. {
  775. redraw();
  776. }
  777. void CMarketplaceWindow::selectionChanged(bool side)
  778. {
  779. readyToTrade = hLeft && hRight;
  780. if(mode == EMarketMode::RESOURCE_RESOURCE)
  781. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  782. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  783. arts->unmarkSlots();
  784. if(readyToTrade)
  785. {
  786. int soldItemId = hLeft->id;
  787. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  788. if(slider)
  789. {
  790. int newAmount = -1;
  791. if(itemsType[1] == RESOURCE)
  792. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(soldItemId));
  793. else if(itemsType[1] == CREATURE)
  794. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->stacksCount() == 1 && hero->needsLastStack());
  795. else
  796. assert(0);
  797. slider->setAmount(newAmount / r1);
  798. slider->moveTo(0);
  799. max->block(false);
  800. deal->block(false);
  801. }
  802. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  803. {
  804. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<EGameResID>(soldItemId)) < r1);
  805. }
  806. else
  807. deal->block(false);
  808. }
  809. else
  810. {
  811. if(slider)
  812. {
  813. max->block(true);
  814. slider->setAmount(0);
  815. slider->moveTo(0);
  816. }
  817. deal->block(true);
  818. }
  819. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  820. initSubs(false);
  821. updateTraderText();
  822. redraw();
  823. }
  824. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  825. {
  826. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  827. }
  828. void CMarketplaceWindow::garrisonChanged()
  829. {
  830. if(mode != EMarketMode::CREATURE_RESOURCE)
  831. return;
  832. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  833. getEmptySlots(toRemove);
  834. removeItems(toRemove);
  835. initSubs(true);
  836. }
  837. void CMarketplaceWindow::artifactsChanged(bool Left)
  838. {
  839. assert(!Left);
  840. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  841. return;
  842. std::vector<int> available = market->availableItemsIds(mode);
  843. std::set<std::shared_ptr<CTradeableItem>> toRemove;
  844. for(auto item : items[0])
  845. if(!vstd::contains(available, item->id))
  846. toRemove.insert(item);
  847. removeItems(toRemove);
  848. // clear set to erase final instance of shared_ptr - we want to redraw screen only after it has been deleted
  849. toRemove.clear();
  850. redraw();
  851. }
  852. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  853. {
  854. if(Left)
  855. {
  856. switch(itemsType[1])
  857. {
  858. case RESOURCE:
  859. case CREATURE:
  860. {
  861. int val = slider
  862. ? slider->getValue() * r1
  863. : (((deal->isBlocked())) ? 0 : r1);
  864. return std::to_string(val);
  865. }
  866. case ARTIFACT_INSTANCE:
  867. return ((deal->isBlocked()) ? "0" : "1");
  868. }
  869. }
  870. else
  871. {
  872. switch(itemsType[0])
  873. {
  874. case RESOURCE:
  875. if(slider)
  876. return std::to_string( slider->getValue() * r2 );
  877. else
  878. return std::to_string(r2);
  879. case ARTIFACT_TYPE:
  880. return ((deal->isBlocked()) ? "0" : "1");
  881. case PLAYER:
  882. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  883. }
  884. }
  885. return "???";
  886. }
  887. Point CMarketplaceWindow::selectionOffset(bool Left) const
  888. {
  889. if(Left)
  890. {
  891. switch(itemsType[1])
  892. {
  893. case RESOURCE:
  894. return Point(122, 446);
  895. case CREATURE:
  896. return Point(128, 450);
  897. case ARTIFACT_INSTANCE:
  898. return Point(134, 466);
  899. }
  900. }
  901. else
  902. {
  903. switch(itemsType[0])
  904. {
  905. case RESOURCE:
  906. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  907. return Point(410, 469);
  908. else
  909. return Point(410, 446);
  910. case ARTIFACT_TYPE:
  911. return Point(425, 447);
  912. case PLAYER:
  913. return Point(417, 451);
  914. }
  915. }
  916. assert(0);
  917. return Point(0,0);
  918. }
  919. void CMarketplaceWindow::resourceChanged()
  920. {
  921. initSubs(true);
  922. }
  923. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  924. {
  925. switch(type)
  926. {
  927. case RESOURCE:
  928. dx = 82;
  929. dy = 79;
  930. x = 39;
  931. y = 180;
  932. h = 68;
  933. w = 70;
  934. break;
  935. case PLAYER:
  936. dx = 83;
  937. dy = 118;
  938. h = 64;
  939. w = 58;
  940. x = 44;
  941. y = 83;
  942. assert(Right);
  943. break;
  944. case CREATURE://45,123
  945. x = 45;
  946. y = 123;
  947. w = 58;
  948. h = 64;
  949. dx = 83;
  950. dy = 98;
  951. assert(!Right);
  952. break;
  953. case ARTIFACT_TYPE://45,123
  954. x = 340-289;
  955. y = 180;
  956. w = 44;
  957. h = 44;
  958. dx = 83;
  959. dy = 79;
  960. break;
  961. }
  962. leftToRightOffset = 289;
  963. }
  964. void CMarketplaceWindow::updateTraderText()
  965. {
  966. if(readyToTrade)
  967. {
  968. if(mode == EMarketMode::RESOURCE_PLAYER)
  969. {
  970. //I can give %s to the %s player.
  971. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  972. }
  973. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  974. {
  975. //I can offer you the %s for %d %s of %s.
  976. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  977. }
  978. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  979. {
  980. //I can offer you %d %s of %s for %d %s of %s.
  981. 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()));
  982. }
  983. else if(mode == EMarketMode::CREATURE_RESOURCE)
  984. {
  985. //I can offer you %d %s of %s for %d %s.
  986. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  987. }
  988. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  989. {
  990. //I can offer you %d %s of %s for your %s.
  991. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  992. }
  993. return;
  994. }
  995. int gnrtxtnr = -1;
  996. if(madeTransaction)
  997. {
  998. if(mode == EMarketMode::RESOURCE_PLAYER)
  999. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  1000. else
  1001. 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?
  1002. }
  1003. else
  1004. {
  1005. if(mode == EMarketMode::RESOURCE_PLAYER)
  1006. 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.
  1007. else
  1008. 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.
  1009. }
  1010. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  1011. }
  1012. CAltarWindow::CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, EMarketMode::EMarketMode Mode)
  1013. : CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  1014. {
  1015. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1016. if(Mode == EMarketMode::CREATURE_EXP)
  1017. {
  1018. //%s's Creatures
  1019. labels.push_back(std::make_shared<CLabel>(155, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW,
  1020. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->getNameTranslated())));
  1021. //Altar of Sacrifice
  1022. labels.push_back(std::make_shared<CLabel>(450, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]));
  1023. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  1024. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW);
  1025. slider = std::make_shared<CSlider>(Point(231,481),137,std::bind(&CAltarWindow::sliderMoved,this,_1),0,0);
  1026. max = std::make_shared<CButton>(Point(147, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[578], std::bind(&CSlider::moveToMax, slider));
  1027. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  1028. sacrificeAll = std::make_shared<CButton>(Point(393, 520), "ALTARMY.DEF", CGI->generaltexth->zelp[579], std::bind(&CAltarWindow::SacrificeAll,this));
  1029. initItems(true);
  1030. mimicCres();
  1031. }
  1032. else
  1033. {
  1034. //Sacrifice artifacts for experience
  1035. labels.push_back(std::make_shared<CLabel>(450, 34, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]));
  1036. //%s's Creatures
  1037. labels.push_back(std::make_shared<CLabel>(302, 423, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]));
  1038. sacrificeAll = std::make_shared<CButton>(Point(393, 520), "ALTFILL.DEF", CGI->generaltexth->zelp[571], std::bind(&CAltarWindow::SacrificeAll,this));
  1039. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  1040. sacrificeBackpack = std::make_shared<CButton>(Point(147, 520), "ALTEMBK.DEF", CGI->generaltexth->zelp[570], std::bind(&CAltarWindow::SacrificeBackpack,this));
  1041. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  1042. initItems(true);
  1043. initItems(false);
  1044. artIcon = std::make_shared<CAnimImage>("ARTIFACT", 0, 0, 281, 442);
  1045. artIcon->disable();
  1046. }
  1047. //Experience needed to reach next level
  1048. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1049. //Total experience on the Altar
  1050. texts.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW));
  1051. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1052. ok = std::make_shared<CButton>(Point(516, 520), "IOK6432.DEF", CGI->generaltexth->zelp[568], [&](){ close();}, SDLK_RETURN);
  1053. ok->assignedKeys.insert(SDLK_ESCAPE);
  1054. deal = std::make_shared<CButton>(Point(269, 520), "ALTSACR.DEF", CGI->generaltexth->zelp[585], std::bind(&CAltarWindow::makeDeal,this));
  1055. if(Mode == EMarketMode::CREATURE_EXP)
  1056. {
  1057. auto changeMode = std::make_shared<CButton>(Point(516, 421), "ALTART.DEF", CGI->generaltexth->zelp[580], std::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP));
  1058. if(Hero->getAlignment() == ::EAlignment::EVIL)
  1059. changeMode->block(true);
  1060. buttons.push_back(changeMode);
  1061. }
  1062. else if(Mode == EMarketMode::ARTIFACT_EXP)
  1063. {
  1064. auto changeMode = std::make_shared<CButton>(Point(516, 421), "ALTSACC.DEF", CGI->generaltexth->zelp[572], std::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP));
  1065. if(Hero->getAlignment() == ::EAlignment::GOOD)
  1066. changeMode->block(true);
  1067. buttons.push_back(changeMode);
  1068. }
  1069. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  1070. getExpValues();
  1071. expToLevel = std::make_shared<CLabel>(73, 475, FONT_SMALL, ETextAlignment::CENTER);
  1072. expOnAltar = std::make_shared<CLabel>(73, 543, FONT_SMALL, ETextAlignment::CENTER);
  1073. setExpToLevel();
  1074. calcTotalExp();
  1075. blockTrade();
  1076. }
  1077. CAltarWindow::~CAltarWindow() = default;
  1078. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1079. {
  1080. leftToRightOffset = 289;
  1081. x = 45;
  1082. y = 110;
  1083. w = 58;
  1084. h = 64;
  1085. dx = 83;
  1086. dy = 98;
  1087. }
  1088. void CAltarWindow::sliderMoved(int to)
  1089. {
  1090. if(hLeft)
  1091. sacrificedUnits[hLeft->serial] = to;
  1092. if(hRight)
  1093. updateRight(hRight);
  1094. deal->block(!to);
  1095. calcTotalExp();
  1096. redraw();
  1097. }
  1098. void CAltarWindow::makeDeal()
  1099. {
  1100. if(mode == EMarketMode::CREATURE_EXP)
  1101. {
  1102. blockTrade();
  1103. slider->moveTo(0);
  1104. std::vector<ui32> ids;
  1105. std::vector<ui32> toSacrifice;
  1106. for(int i = 0; i < sacrificedUnits.size(); i++)
  1107. {
  1108. if(sacrificedUnits[i])
  1109. {
  1110. ids.push_back(i);
  1111. toSacrifice.push_back(sacrificedUnits[i]);
  1112. }
  1113. }
  1114. LOCPLINT->cb->trade(dynamic_cast<const CGObjectInstance *>(market), mode, ids, {}, toSacrifice, hero);
  1115. for(int& val : sacrificedUnits)
  1116. val = 0;
  1117. for(auto item : items[0])
  1118. {
  1119. item->setType(CREATURE_PLACEHOLDER);
  1120. item->subtitle = "";
  1121. }
  1122. }
  1123. else
  1124. {
  1125. std::vector<ui32> positions;
  1126. auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1127. for(const CArtifactInstance * art : artifactsOfHero->artifactsOnAltar)
  1128. {
  1129. positions.push_back(hero->getSlotByInstance(art));
  1130. }
  1131. std::sort(positions.begin(), positions.end(), std::greater<>());
  1132. LOCPLINT->cb->trade(dynamic_cast<const CGObjectInstance *>(market), mode, positions, {}, {}, hero);
  1133. arts->artifactsOnAltar.clear();
  1134. for(auto item : items[0])
  1135. {
  1136. item->setID(-1);
  1137. item->subtitle = "";
  1138. }
  1139. //arts->scrollBackpack(0);
  1140. deal->block(true);
  1141. }
  1142. calcTotalExp();
  1143. }
  1144. void CAltarWindow::SacrificeAll()
  1145. {
  1146. if(mode == EMarketMode::CREATURE_EXP)
  1147. {
  1148. bool movedAnything = false;
  1149. for(auto item : items[1])
  1150. sacrificedUnits[item->serial] = hero->getStackCount(SlotID(item->serial));
  1151. sacrificedUnits[items[1].front()->serial]--;
  1152. for(auto item : items[0])
  1153. {
  1154. updateRight(item);
  1155. if(item->type == CREATURE)
  1156. movedAnything = true;
  1157. }
  1158. deal->block(!movedAnything);
  1159. calcTotalExp();
  1160. }
  1161. else
  1162. {
  1163. auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1164. for(const auto & aw : artifactsOfHero->visibleArtSet.artifactsWorn)
  1165. {
  1166. if(!aw.second.locked)
  1167. moveArtToAltar(nullptr, aw.second.artifact);
  1168. }
  1169. artifactsOfHero->updateWornSlots();
  1170. SacrificeBackpack();
  1171. }
  1172. redraw();
  1173. }
  1174. void CAltarWindow::selectionChanged(bool side)
  1175. {
  1176. if(mode != EMarketMode::CREATURE_EXP)
  1177. return;
  1178. int stackCount = 0;
  1179. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  1180. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  1181. stackCount++;
  1182. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  1183. slider->block(!slider->getAmount());
  1184. slider->moveTo(sacrificedUnits[hLeft->serial]);
  1185. max->block(!slider->getAmount());
  1186. selectOppositeItem(side);
  1187. readyToTrade = true;
  1188. redraw();
  1189. }
  1190. void CAltarWindow::selectOppositeItem(bool side)
  1191. {
  1192. bool oppositeSide = !side;
  1193. int pos = vstd::find_pos(items[side], side ? hLeft : hRight);
  1194. int oppositePos = vstd::find_pos(items[oppositeSide], oppositeSide ? hLeft : hRight);
  1195. if(pos >= 0 && pos != oppositePos)
  1196. {
  1197. if(oppositeSide)
  1198. hLeft = items[oppositeSide][pos];
  1199. else
  1200. hRight = items[oppositeSide][pos];
  1201. selectionChanged(oppositeSide);
  1202. }
  1203. }
  1204. void CAltarWindow::mimicCres()
  1205. {
  1206. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1207. std::vector<Rect> positions;
  1208. getPositionsFor(positions, false, CREATURE);
  1209. for(auto item : items[1])
  1210. {
  1211. auto hlp = std::make_shared<CTradeableItem>(positions[item->serial].topLeft(), CREATURE_PLACEHOLDER, item->id, false, item->serial);
  1212. hlp->pos = positions[item->serial] + this->pos.topLeft();
  1213. items[0].push_back(hlp);
  1214. }
  1215. }
  1216. Point CAltarWindow::selectionOffset(bool Left) const
  1217. {
  1218. if(Left)
  1219. return Point(150, 421);
  1220. else
  1221. return Point(396, 421);
  1222. }
  1223. std::string CAltarWindow::selectionSubtitle(bool Left) const
  1224. {
  1225. if(Left && slider && hLeft)
  1226. return std::to_string(slider->getValue());
  1227. else if(!Left && hRight)
  1228. return hRight->subtitle;
  1229. else
  1230. return "";
  1231. }
  1232. void CAltarWindow::artifactsChanged(bool left)
  1233. {
  1234. }
  1235. void CAltarWindow::garrisonChanged()
  1236. {
  1237. if(mode != EMarketMode::CREATURE_EXP)
  1238. return;
  1239. std::set<std::shared_ptr<CTradeableItem>> empty;
  1240. getEmptySlots(empty);
  1241. removeItems(empty);
  1242. initSubs(true);
  1243. getExpValues();
  1244. }
  1245. void CAltarWindow::getExpValues()
  1246. {
  1247. int dump;
  1248. for(auto item : items[1])
  1249. {
  1250. if(item->id >= 0)
  1251. market->getOffer(item->id, 0, dump, expPerUnit[item->serial], EMarketMode::CREATURE_EXP);
  1252. }
  1253. }
  1254. void CAltarWindow::calcTotalExp()
  1255. {
  1256. int val = 0;
  1257. if(mode == EMarketMode::CREATURE_EXP)
  1258. {
  1259. for (int i = 0; i < sacrificedUnits.size(); i++)
  1260. {
  1261. val += expPerUnit[i] * sacrificedUnits[i];
  1262. }
  1263. }
  1264. else
  1265. {
  1266. auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1267. for(const CArtifactInstance * art : artifactsOfHero->artifactsOnAltar)
  1268. {
  1269. int dmp, valOfArt;
  1270. market->getOffer(art->artType->getId(), 0, dmp, valOfArt, mode);
  1271. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  1272. }
  1273. }
  1274. val = static_cast<int>(hero->calculateXp(val));
  1275. expOnAltar->setText(std::to_string(val));
  1276. }
  1277. void CAltarWindow::setExpToLevel()
  1278. {
  1279. expToLevel->setText(std::to_string(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  1280. }
  1281. void CAltarWindow::blockTrade()
  1282. {
  1283. hLeft = hRight = nullptr;
  1284. readyToTrade = false;
  1285. if(slider)
  1286. {
  1287. slider->block(true);
  1288. max->block(true);
  1289. }
  1290. deal->block(true);
  1291. }
  1292. void CAltarWindow::updateRight(std::shared_ptr<CTradeableItem> toUpdate)
  1293. {
  1294. int val = sacrificedUnits[toUpdate->serial];
  1295. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  1296. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % std::to_string(hero->calculateXp(val * expPerUnit[toUpdate->serial]))) : ""; //%s exp
  1297. }
  1298. int CAltarWindow::firstFreeSlot()
  1299. {
  1300. int ret = -1;
  1301. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  1302. return items[0][ret]->id == -1 ? ret : -1;
  1303. }
  1304. void CAltarWindow::SacrificeBackpack()
  1305. {
  1306. auto artsAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1307. while(!artsAltar->visibleArtSet.artifactsInBackpack.empty())
  1308. {
  1309. if(!putOnAltar(nullptr, artsAltar->visibleArtSet.artifactsInBackpack[0].artifact))
  1310. break;
  1311. };
  1312. calcTotalExp();
  1313. }
  1314. void CAltarWindow::artifactPicked()
  1315. {
  1316. redraw();
  1317. }
  1318. void CAltarWindow::showAll(SDL_Surface * to)
  1319. {
  1320. CTradeWindow::showAll(to);
  1321. if(mode == EMarketMode::ARTIFACT_EXP && arts)
  1322. {
  1323. if(auto pickedArt = arts->getPickedArtifact())
  1324. {
  1325. artIcon->setFrame(pickedArt->artType->getIconIndex());
  1326. artIcon->showAll(to);
  1327. int dmp, val;
  1328. market->getOffer(pickedArt->getTypeId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1329. val = static_cast<int>(hero->calculateXp(val));
  1330. printAtMiddleLoc(std::to_string(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
  1331. }
  1332. }
  1333. }
  1334. bool CAltarWindow::putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1335. {
  1336. if(!art->artType->isTradable()) //special art
  1337. {
  1338. logGlobal->warn("Cannot put special artifact on altar!");
  1339. return false;
  1340. }
  1341. if(!altarSlot || altarSlot->id != -1)
  1342. {
  1343. int slotIndex = firstFreeSlot();
  1344. if(slotIndex < 0)
  1345. {
  1346. logGlobal->warn("No free slots on altar!");
  1347. return false;
  1348. }
  1349. altarSlot = items[0][slotIndex];
  1350. }
  1351. int dmp, val;
  1352. market->getOffer(art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1353. val = static_cast<int>(hero->calculateXp(val));
  1354. auto artsAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
  1355. artsAltar->artifactsOnAltar.insert(art);
  1356. artsAltar->deleteFromVisible(art);
  1357. altarSlot->setArtInstance(art);
  1358. altarSlot->subtitle = std::to_string(val);
  1359. deal->block(false);
  1360. return true;
  1361. }
  1362. void CAltarWindow::moveArtToAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
  1363. {
  1364. if(putOnAltar(altarSlot, art))
  1365. {
  1366. CCS->curh->dragAndDropCursor(nullptr);
  1367. arts->unmarkSlots();
  1368. }
  1369. }