CTradeWindow.cpp 40 KB

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