CTradeWindow.cpp 40 KB

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