CTradeWindow.cpp 40 KB

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