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