CTradeWindow.cpp 37 KB

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