CTradeWindow.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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. using namespace boost::assign;
  424. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  425. {
  426. //22 boxes, 5 in row, last row: two boxes centered
  427. int h, w, x, y, dx, dy;
  428. h = w = 44;
  429. x = 317;
  430. y = 53;
  431. dx = 54;
  432. dy = 70;
  433. for (int i = 0; i < 4 ; i++)
  434. for (int j = 0; j < 5 ; j++)
  435. poss += Rect(x + dx*j, y + dy*i, w, h);
  436. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  437. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  438. }
  439. else
  440. {
  441. //seven boxes:
  442. // X X X
  443. // X X X
  444. // X
  445. int h, w, x, y, dx, dy;
  446. int leftToRightOffset;
  447. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  448. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  449. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  450. genRect(h, w, x + dx, y + 2*dy);
  451. if(!Left)
  452. {
  453. for(Rect &r : poss)
  454. r.x += leftToRightOffset;
  455. }
  456. }
  457. }
  458. void CTradeWindow::initSubs(bool Left)
  459. {
  460. for(CTradeableItem *t : items[Left])
  461. {
  462. if(Left)
  463. {
  464. switch(itemsType[1])
  465. {
  466. case CREATURE:
  467. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(SlotID(t->serial)));
  468. break;
  469. case RESOURCE:
  470. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(t->serial)));
  471. break;
  472. }
  473. }
  474. else //right side
  475. {
  476. if(itemsType[0] == PLAYER)
  477. {
  478. t->subtitle = CGI->generaltexth->capColors[t->id];
  479. }
  480. else if(hLeft)//artifact, creature
  481. {
  482. int h1, h2; //hlp variables for getting offer
  483. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  484. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  485. {
  486. std::ostringstream oss;
  487. oss << h2;
  488. if(h1!=1)
  489. oss << "/" << h1;
  490. t->subtitle = oss.str();
  491. }
  492. else
  493. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  494. }
  495. else
  496. t->subtitle = "";
  497. }
  498. }
  499. }
  500. void CTradeWindow::showAll(SDL_Surface * to)
  501. {
  502. CWindowObject::showAll(to);
  503. if(hRight)
  504. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  505. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  506. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  507. if(readyToTrade)
  508. {
  509. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  510. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  511. }
  512. }
  513. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  514. {
  515. for(CTradeableItem *t : toRemove)
  516. removeItem(t);
  517. }
  518. void CTradeWindow::removeItem(CTradeableItem * t)
  519. {
  520. items[t->left] -= t;
  521. delete t;
  522. if(hRight == t)
  523. {
  524. hRight = nullptr;
  525. selectionChanged(false);
  526. }
  527. }
  528. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  529. {
  530. for(CTradeableItem *t : items[1])
  531. if(!hero->getStackCount(SlotID(t->serial)))
  532. toRemove.insert(t);
  533. }
  534. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  535. {
  536. const IMarket *m = market;
  537. const CGHeroInstance *h = hero;
  538. CTradeWindow *nwindow = nullptr;
  539. GH.popIntTotally(this);
  540. switch(Mode)
  541. {
  542. case EMarketMode::CREATURE_EXP:
  543. case EMarketMode::ARTIFACT_EXP:
  544. nwindow = new CAltarWindow(m, h, Mode);
  545. break;
  546. default:
  547. nwindow = new CMarketplaceWindow(m, h, Mode);
  548. break;
  549. }
  550. GH.pushInt(nwindow);
  551. }
  552. void CTradeWindow::artifactSelected(CArtPlace *slot)
  553. {
  554. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  555. items[1][0]->setArtInstance(slot->ourArt);
  556. if(slot->ourArt)
  557. hLeft = items[1][0];
  558. else
  559. hLeft = nullptr;
  560. selectionChanged(true);
  561. }
  562. std::string CMarketplaceWindow::getBackgroundForMode(EMarketMode::EMarketMode mode)
  563. {
  564. switch(mode)
  565. {
  566. case EMarketMode::RESOURCE_RESOURCE:
  567. return "TPMRKRES.bmp";
  568. case EMarketMode::RESOURCE_PLAYER:
  569. return "TPMRKPTS.bmp";
  570. case EMarketMode::CREATURE_RESOURCE:
  571. return "TPMRKCRS.bmp";
  572. case EMarketMode::RESOURCE_ARTIFACT:
  573. return "TPMRKABS.bmp";
  574. case EMarketMode::ARTIFACT_RESOURCE:
  575. return "TPMRKASS.bmp";
  576. }
  577. assert(0);
  578. return "";
  579. }
  580. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  581. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, Mode)
  582. {
  583. OBJ_CONSTRUCTION_CAPTURING_ALL;
  584. madeTransaction = false;
  585. bool sliderNeeded = true;
  586. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  587. std::string title;
  588. if (market->o->ID == Obj::TOWN)
  589. {
  590. switch (mode)
  591. {
  592. break; case EMarketMode::CREATURE_RESOURCE:
  593. title = CGI->townh->factions[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->Name();
  594. break; case EMarketMode::RESOURCE_ARTIFACT:
  595. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  596. sliderNeeded = false;
  597. break; case EMarketMode::ARTIFACT_RESOURCE:
  598. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  599. sliderNeeded = false;
  600. break; default:
  601. title = CGI->generaltexth->allTexts[158];
  602. }
  603. }
  604. else
  605. {
  606. switch (market->o->ID)
  607. {
  608. break; case Obj::BLACK_MARKET: title = CGI->generaltexth->allTexts[349];
  609. break; case Obj::TRADING_POST: title = CGI->generaltexth->allTexts[159];
  610. break; case Obj::TRADING_POST_SNOW: title = CGI->generaltexth->allTexts[159];
  611. break; default: title = market->o->getObjectName();
  612. }
  613. }
  614. new CLabel(300, 27, FONT_BIG, CENTER, Colors::YELLOW, title);
  615. initItems(false);
  616. initItems(true);
  617. ok = new CButton(Point(516, 520), "IOK6432.DEF", CGI->generaltexth->zelp[600], [&]{ close(); }, SDLK_RETURN);
  618. ok->assignedKeys.insert(SDLK_ESCAPE);
  619. deal = new CButton(Point(307, 520), "TPMRKB.DEF", CGI->generaltexth->zelp[595], [&] { makeDeal(); } );
  620. deal->block(true);
  621. if(sliderNeeded)
  622. {
  623. slider = new CSlider(Point(231, 490),137, std::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  624. max = new CButton(Point(229, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[596], [&] { setMax(); });
  625. max->block(true);
  626. }
  627. else
  628. {
  629. slider = nullptr;
  630. max = nullptr;
  631. deal->moveBy(Point(-30, 0));
  632. }
  633. Rect traderTextRect;
  634. //left side
  635. switch(Mode)
  636. {
  637. case EMarketMode::RESOURCE_RESOURCE:
  638. case EMarketMode::RESOURCE_PLAYER:
  639. case EMarketMode::RESOURCE_ARTIFACT:
  640. new CLabel(154, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]);
  641. break;
  642. case EMarketMode::CREATURE_RESOURCE:
  643. //%s's Creatures
  644. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  645. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  646. break;
  647. case EMarketMode::ARTIFACT_RESOURCE:
  648. //%s's Artifacts
  649. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  650. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  651. break;
  652. }
  653. //right side
  654. switch(Mode)
  655. {
  656. case EMarketMode::RESOURCE_RESOURCE:
  657. case EMarketMode::CREATURE_RESOURCE:
  658. case EMarketMode::RESOURCE_ARTIFACT:
  659. case EMarketMode::ARTIFACT_RESOURCE:
  660. new CLabel(445, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]);
  661. traderTextRect = Rect(316, 48, 260, 75);
  662. break;
  663. case EMarketMode::RESOURCE_PLAYER:
  664. new CLabel(445, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]);
  665. traderTextRect = Rect(28, 48, 260, 75);
  666. break;
  667. }
  668. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  669. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  670. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  671. new CButton(Point(18, 520),"TPMRKBU1.DEF", CGI->generaltexth->zelp[612], [&] { setMode(EMarketMode::RESOURCE_PLAYER);});
  672. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  673. new CButton(Point(516, 450 + specialOffset),"TPMRKBU5.DEF", CGI->generaltexth->zelp[605], [&] { setMode(EMarketMode::RESOURCE_RESOURCE);});
  674. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  675. new CButton(Point(516, 485),"TPMRKBU4.DEF", CGI->generaltexth->zelp[599], [&] { setMode(EMarketMode::CREATURE_RESOURCE);});
  676. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  677. new CButton(Point(18, 450 + specialOffset),"TPMRKBU2.DEF", CGI->generaltexth->zelp[598], [&] { setMode(EMarketMode::RESOURCE_ARTIFACT);});
  678. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  679. new CButton(Point(18, 485),"TPMRKBU3.DEF", CGI->generaltexth->zelp[613], [&] { setMode(EMarketMode::ARTIFACT_RESOURCE);});
  680. updateTraderText();
  681. }
  682. CMarketplaceWindow::~CMarketplaceWindow()
  683. {
  684. hLeft = hRight = nullptr;
  685. for(auto & elem : items[1])
  686. delete elem;
  687. for(auto & elem : items[0])
  688. delete elem;
  689. items[1].clear();
  690. items[0].clear();
  691. }
  692. void CMarketplaceWindow::setMax()
  693. {
  694. slider->moveToMax();
  695. }
  696. void CMarketplaceWindow::makeDeal()
  697. {
  698. int sliderValue = 0;
  699. if(slider)
  700. sliderValue = slider->getValue();
  701. else
  702. sliderValue = !deal->isBlocked(); //should always be 1
  703. if(!sliderValue)
  704. return;
  705. int leftIdToSend = -1;
  706. switch (mode)
  707. {
  708. case EMarketMode::CREATURE_RESOURCE:
  709. leftIdToSend = hLeft->serial;
  710. break;
  711. case EMarketMode::ARTIFACT_RESOURCE:
  712. leftIdToSend = hLeft->getArtInstance()->id.getNum();
  713. break;
  714. default:
  715. leftIdToSend = hLeft->id;
  716. break;
  717. }
  718. if(slider)
  719. {
  720. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->getValue()*r1, hero);
  721. slider->moveTo(0);
  722. }
  723. else
  724. {
  725. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  726. }
  727. madeTransaction = true;
  728. hLeft = nullptr;
  729. hRight = nullptr;
  730. selectionChanged(true);
  731. }
  732. void CMarketplaceWindow::sliderMoved( int to )
  733. {
  734. redraw();
  735. }
  736. void CMarketplaceWindow::selectionChanged(bool side)
  737. {
  738. readyToTrade = hLeft && hRight;
  739. if(mode == EMarketMode::RESOURCE_RESOURCE)
  740. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  741. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  742. arts->unmarkSlots(false);
  743. if(readyToTrade)
  744. {
  745. int soldItemId = hLeft->id;
  746. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  747. if(slider)
  748. {
  749. int newAmount = -1;
  750. if(itemsType[1] == RESOURCE)
  751. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId));
  752. else if(itemsType[1] == CREATURE)
  753. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->Slots().size() == 1 && hero->needsLastStack());
  754. else
  755. assert(0);
  756. slider->setAmount(newAmount / r1);
  757. slider->moveTo(0);
  758. max->block(false);
  759. deal->block(false);
  760. }
  761. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  762. {
  763. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId)) < r1);
  764. }
  765. else
  766. deal->block(false);
  767. }
  768. else
  769. {
  770. if(slider)
  771. {
  772. max->block(true);
  773. slider->setAmount(0);
  774. slider->moveTo(0);
  775. }
  776. deal->block(true);
  777. }
  778. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  779. initSubs(false);
  780. updateTraderText();
  781. redraw();
  782. }
  783. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  784. {
  785. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  786. }
  787. void CMarketplaceWindow::garrisonChanged()
  788. {
  789. if(mode != EMarketMode::CREATURE_RESOURCE)
  790. return;
  791. std::set<CTradeableItem *> toRemove;
  792. getEmptySlots(toRemove);
  793. removeItems(toRemove);
  794. initSubs(true);
  795. }
  796. void CMarketplaceWindow::artifactsChanged(bool Left)
  797. {
  798. assert(!Left);
  799. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  800. return;
  801. std::vector<int> available = market->availableItemsIds(mode);
  802. std::set<CTradeableItem *> toRemove;
  803. for(CTradeableItem *t : items[0])
  804. if(!vstd::contains(available, t->id))
  805. toRemove.insert(t);
  806. removeItems(toRemove);
  807. redraw();
  808. }
  809. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  810. {
  811. if(Left)
  812. {
  813. switch(itemsType[1])
  814. {
  815. case RESOURCE:
  816. case CREATURE:
  817. {
  818. int val = slider
  819. ? slider->getValue() * r1
  820. : (((deal->isBlocked())) ? 0 : r1);
  821. return boost::lexical_cast<std::string>(val);
  822. }
  823. case ARTIFACT_INSTANCE:
  824. return ((deal->isBlocked()) ? "0" : "1");
  825. }
  826. }
  827. else
  828. {
  829. switch(itemsType[0])
  830. {
  831. case RESOURCE:
  832. if(slider)
  833. return boost::lexical_cast<std::string>( slider->getValue() * r2 );
  834. else
  835. return boost::lexical_cast<std::string>(r2);
  836. case ARTIFACT_TYPE:
  837. return ((deal->isBlocked()) ? "0" : "1");
  838. case PLAYER:
  839. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  840. }
  841. }
  842. return "???";
  843. }
  844. Point CMarketplaceWindow::selectionOffset(bool Left) const
  845. {
  846. if(Left)
  847. {
  848. switch(itemsType[1])
  849. {
  850. case RESOURCE:
  851. return Point(122, 446);
  852. case CREATURE:
  853. return Point(128, 450);
  854. case ARTIFACT_INSTANCE:
  855. return Point(134, 466);
  856. }
  857. }
  858. else
  859. {
  860. switch(itemsType[0])
  861. {
  862. case RESOURCE:
  863. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  864. return Point(410, 469);
  865. else
  866. return Point(410, 446);
  867. case ARTIFACT_TYPE:
  868. return Point(425, 447);
  869. case PLAYER:
  870. return Point(417, 451);
  871. }
  872. }
  873. assert(0);
  874. return Point(0,0);
  875. }
  876. void CMarketplaceWindow::resourceChanged(int type, int val)
  877. {
  878. initSubs(true);
  879. }
  880. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  881. {
  882. switch(type)
  883. {
  884. case RESOURCE:
  885. dx = 82;
  886. dy = 79;
  887. x = 39;
  888. y = 180;
  889. h = 66;
  890. w = 74;
  891. break;
  892. case PLAYER:
  893. dx = 83;
  894. dy = 118;
  895. h = 64;
  896. w = 58;
  897. x = 44;
  898. y = 83;
  899. assert(Right);
  900. break;
  901. case CREATURE://45,123
  902. x = 45;
  903. y = 123;
  904. w = 58;
  905. h = 64;
  906. dx = 83;
  907. dy = 98;
  908. assert(!Right);
  909. break;
  910. case ARTIFACT_TYPE://45,123
  911. x = 340-289;
  912. y = 180;
  913. w = 44;
  914. h = 44;
  915. dx = 83;
  916. dy = 79;
  917. break;
  918. }
  919. leftToRightOffset = 289;
  920. }
  921. void CMarketplaceWindow::updateTraderText()
  922. {
  923. if(readyToTrade)
  924. {
  925. if(mode == EMarketMode::RESOURCE_PLAYER)
  926. {
  927. //I can give %s to the %s player.
  928. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  929. }
  930. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  931. {
  932. //I can offer you the %s for %d %s of %s.
  933. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  934. }
  935. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  936. {
  937. //I can offer you %d %s of %s for %d %s of %s.
  938. 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()));
  939. }
  940. else if(mode == EMarketMode::CREATURE_RESOURCE)
  941. {
  942. //I can offer you %d %s of %s for %d %s.
  943. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  944. }
  945. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  946. {
  947. //I can offer you %d %s of %s for your %s.
  948. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  949. }
  950. return;
  951. }
  952. int gnrtxtnr = -1;
  953. if(madeTransaction)
  954. {
  955. if(mode == EMarketMode::RESOURCE_PLAYER)
  956. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  957. else
  958. 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?
  959. }
  960. else
  961. {
  962. if(mode == EMarketMode::RESOURCE_PLAYER)
  963. 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.
  964. else
  965. 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.
  966. }
  967. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  968. }
  969. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= nullptr*/, EMarketMode::EMarketMode Mode)
  970. :CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  971. {
  972. OBJ_CONSTRUCTION_CAPTURING_ALL;
  973. if(Mode == EMarketMode::CREATURE_EXP)
  974. {
  975. //%s's Creatures
  976. new CLabel(155, 30, FONT_SMALL, CENTER, Colors::YELLOW,
  977. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  978. //Altar of Sacrifice
  979. new CLabel(450, 30, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]);
  980. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  981. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  982. slider = new CSlider(Point(231,481),137,std::bind(&CAltarWindow::sliderMoved,this,_1),0,0);
  983. max = new CButton(Point(147, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[578], std::bind(&CSlider::moveToMax, slider));
  984. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  985. sacrificeAll = new CButton(Point(393, 520), "ALTARMY.DEF", CGI->generaltexth->zelp[579], std::bind(&CAltarWindow::SacrificeAll,this));
  986. sacrificeBackpack = nullptr;
  987. initItems(true);
  988. mimicCres();
  989. artIcon = nullptr;
  990. }
  991. else
  992. {
  993. //Sacrifice artifacts for experience
  994. new CLabel(450, 34, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]);
  995. //%s's Creatures
  996. new CLabel(302, 423, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]);
  997. sacrificeAll = new CButton(Point(393, 520), "ALTFILL.DEF", CGI->generaltexth->zelp[571], std::bind(&CAltarWindow::SacrificeAll,this));
  998. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  999. sacrificeBackpack = new CButton(Point(147, 520), "ALTEMBK.DEF", CGI->generaltexth->zelp[570], std::bind(&CAltarWindow::SacrificeBackpack,this));
  1000. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  1001. slider = nullptr;
  1002. max = nullptr;
  1003. initItems(true);
  1004. initItems(false);
  1005. artIcon = new CAnimImage("ARTIFACT", 0, 0, 281, 442);
  1006. artIcon->disable();
  1007. }
  1008. //Experience needed to reach next level
  1009. new CTextBox(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  1010. //Total experience on the Altar
  1011. new CTextBox(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  1012. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1013. ok = new CButton(Point(516, 520), "IOK6432.DEF", CGI->generaltexth->zelp[568], [&]{ close();}, SDLK_RETURN);
  1014. ok->assignedKeys.insert(SDLK_ESCAPE);
  1015. deal = new CButton(Point(269, 520), "ALTSACR.DEF", CGI->generaltexth->zelp[585], std::bind(&CAltarWindow::makeDeal,this));
  1016. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  1017. new CButton(Point(516, 421), "ALTART.DEF", CGI->generaltexth->zelp[580], std::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP));
  1018. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  1019. new CButton(Point(516, 421), "ALTSACC.DEF", CGI->generaltexth->zelp[572], std::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP));
  1020. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  1021. getExpValues();
  1022. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  1023. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  1024. setExpToLevel();
  1025. calcTotalExp();
  1026. blockTrade();
  1027. }
  1028. CAltarWindow::~CAltarWindow()
  1029. {
  1030. }
  1031. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1032. {
  1033. leftToRightOffset = 289;
  1034. x = 45;
  1035. y = 110;
  1036. w = 58;
  1037. h = 64;
  1038. dx = 83;
  1039. dy = 98;
  1040. }
  1041. void CAltarWindow::sliderMoved(int to)
  1042. {
  1043. sacrificedUnits[hLeft->serial] = to;
  1044. updateRight(hRight);
  1045. deal->block(!to);
  1046. calcTotalExp();
  1047. redraw();
  1048. }
  1049. void CAltarWindow::makeDeal()
  1050. {
  1051. if(mode == EMarketMode::CREATURE_EXP)
  1052. {
  1053. blockTrade();
  1054. slider->moveTo(0);
  1055. std::vector<int> toSacrifice = sacrificedUnits;
  1056. for (int i = 0; i < toSacrifice.size(); i++)
  1057. {
  1058. if(toSacrifice[i])
  1059. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  1060. }
  1061. for(int& val : sacrificedUnits)
  1062. val = 0;
  1063. for(CTradeableItem *t : items[0])
  1064. {
  1065. t->setType(CREATURE_PLACEHOLDER);
  1066. t->subtitle = "";
  1067. }
  1068. }
  1069. else
  1070. {
  1071. for(const CArtifactInstance *art : arts->artifactsOnAltar) //sacrifice each artifact on the list
  1072. {
  1073. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  1074. }
  1075. arts->artifactsOnAltar.clear();
  1076. for(CTradeableItem *t : items[0])
  1077. {
  1078. t->setID(-1);
  1079. t->subtitle = "";
  1080. }
  1081. arts->commonInfo->reset();
  1082. //arts->scrollBackpack(0);
  1083. deal->block(true);
  1084. }
  1085. calcTotalExp();
  1086. }
  1087. void CAltarWindow::SacrificeAll()
  1088. {
  1089. if(mode == EMarketMode::CREATURE_EXP)
  1090. {
  1091. bool movedAnything = false;
  1092. for(CTradeableItem *t : items[1])
  1093. sacrificedUnits[t->serial] = hero->getStackCount(SlotID(t->serial));
  1094. sacrificedUnits[items[1].front()->serial]--;
  1095. for(CTradeableItem *t : items[0])
  1096. {
  1097. updateRight(t);
  1098. if(t->type == CREATURE)
  1099. movedAnything = true;
  1100. }
  1101. deal->block(!movedAnything);
  1102. calcTotalExp();
  1103. }
  1104. else
  1105. {
  1106. for(auto i = hero->artifactsWorn.cbegin(); i != hero->artifactsWorn.cend(); i++)
  1107. {
  1108. if(i->second.artifact->artType->id != ArtifactID::ART_LOCK) //ignore locks from assembled artifacts
  1109. moveFromSlotToAltar(i->first, nullptr, i->second.artifact);
  1110. }
  1111. SacrificeBackpack();
  1112. }
  1113. redraw();
  1114. }
  1115. void CAltarWindow::selectionChanged(bool side)
  1116. {
  1117. if(mode != EMarketMode::CREATURE_EXP)
  1118. return;
  1119. CTradeableItem *&selected = side ? hLeft : hRight;
  1120. CTradeableItem *&theOther = side ? hRight : hLeft;
  1121. theOther = *std::find_if(items[!side].begin(), items[!side].end(), [&](const CTradeableItem * item)
  1122. {
  1123. return item->serial == selected->serial;
  1124. });
  1125. int stackCount = 0;
  1126. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  1127. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  1128. stackCount++;
  1129. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  1130. slider->block(!slider->getAmount());
  1131. slider->moveTo(sacrificedUnits[hLeft->serial]);
  1132. max->block(!slider->getAmount());
  1133. readyToTrade = true;
  1134. redraw();
  1135. }
  1136. void CAltarWindow::mimicCres()
  1137. {
  1138. std::vector<Rect> positions;
  1139. getPositionsFor(positions, false, CREATURE);
  1140. for(CTradeableItem *t : items[1])
  1141. {
  1142. auto hlp = new CTradeableItem(positions[t->serial].topLeft(), CREATURE_PLACEHOLDER, t->id, false, t->serial);
  1143. hlp->pos = positions[t->serial] + this->pos.topLeft();
  1144. items[0].push_back(hlp);
  1145. }
  1146. }
  1147. Point CAltarWindow::selectionOffset(bool Left) const
  1148. {
  1149. if(Left)
  1150. return Point(150, 421);
  1151. else
  1152. return Point(396, 421);
  1153. }
  1154. std::string CAltarWindow::selectionSubtitle(bool Left) const
  1155. {
  1156. if(Left && slider && hLeft)
  1157. return boost::lexical_cast<std::string>(slider->getValue());
  1158. else if(!Left && hRight)
  1159. return hRight->subtitle;
  1160. else
  1161. return "";
  1162. }
  1163. void CAltarWindow::artifactsChanged(bool left)
  1164. {
  1165. }
  1166. void CAltarWindow::garrisonChanged()
  1167. {
  1168. if(mode != EMarketMode::CREATURE_EXP)
  1169. return;
  1170. std::set<CTradeableItem *> empty;
  1171. getEmptySlots(empty);
  1172. for(CTradeableItem *t : empty)
  1173. {
  1174. removeItem(*std::find_if(items[0].begin(), items[0].end(), [&](const CTradeableItem * item)
  1175. {
  1176. return item->serial == t->serial;
  1177. }));
  1178. }
  1179. initSubs(true);
  1180. getExpValues();
  1181. }
  1182. void CAltarWindow::getExpValues()
  1183. {
  1184. int dump;
  1185. for(CTradeableItem *t : items[1])
  1186. if(t->id >= 0)
  1187. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  1188. }
  1189. void CAltarWindow::calcTotalExp()
  1190. {
  1191. int val = 0;
  1192. if(mode == EMarketMode::CREATURE_EXP)
  1193. {
  1194. for (int i = 0; i < sacrificedUnits.size(); i++)
  1195. {
  1196. val += expPerUnit[i] * sacrificedUnits[i];
  1197. }
  1198. }
  1199. else
  1200. {
  1201. for(const CArtifactInstance *art : arts->artifactsOnAltar)
  1202. {
  1203. int dmp, valOfArt;
  1204. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  1205. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  1206. }
  1207. }
  1208. val = hero->calculateXp(val);
  1209. expOnAltar->setText(boost::lexical_cast<std::string>(val));
  1210. }
  1211. void CAltarWindow::setExpToLevel()
  1212. {
  1213. expToLevel->setText(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  1214. }
  1215. void CAltarWindow::blockTrade()
  1216. {
  1217. hLeft = hRight = nullptr;
  1218. readyToTrade = false;
  1219. if(slider)
  1220. {
  1221. slider->block(true);
  1222. max->block(true);
  1223. }
  1224. deal->block(true);
  1225. }
  1226. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  1227. {
  1228. int val = sacrificedUnits[toUpdate->serial];
  1229. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  1230. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  1231. }
  1232. int CAltarWindow::firstFreeSlot()
  1233. {
  1234. int ret = -1;
  1235. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  1236. return ret < items[0].size() ? ret : -1;
  1237. }
  1238. void CAltarWindow::SacrificeBackpack()
  1239. {
  1240. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  1241. for (auto & elem : hero->artifactsInBackpack)
  1242. {
  1243. if(vstd::contains(toOmmit, elem.artifact))
  1244. {
  1245. toOmmit -= elem.artifact;
  1246. continue;
  1247. }
  1248. putOnAltar(nullptr, elem.artifact);
  1249. }
  1250. arts->scrollBackpack(0);
  1251. calcTotalExp();
  1252. }
  1253. void CAltarWindow::artifactPicked()
  1254. {
  1255. redraw();
  1256. }
  1257. void CAltarWindow::showAll(SDL_Surface * to)
  1258. {
  1259. CTradeWindow::showAll(to);
  1260. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  1261. {
  1262. artIcon->setFrame(arts->commonInfo->src.art->artType->iconIndex);
  1263. artIcon->showAll(to);
  1264. int dmp, val;
  1265. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1266. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
  1267. }
  1268. }
  1269. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  1270. {
  1271. int artID = art->artType->id;
  1272. if(artID != 1 && artID < 7) //special art
  1273. {
  1274. logGlobal->warnStream() << "Cannot put special artifact on altar!";
  1275. return false;
  1276. }
  1277. if(!altarSlot)
  1278. {
  1279. int slotIndex = firstFreeSlot();
  1280. if(slotIndex < 0)
  1281. {
  1282. logGlobal->warnStream() << "No free slots on altar!";
  1283. return false;
  1284. }
  1285. altarSlot = items[0][slotIndex];
  1286. }
  1287. int dmp, val;
  1288. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  1289. arts->artifactsOnAltar.insert(art);
  1290. altarSlot->setArtInstance(art);
  1291. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  1292. deal->block(false);
  1293. return true;
  1294. }
  1295. void CAltarWindow::moveFromSlotToAltar(ArtifactPosition slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  1296. {
  1297. auto freeBackpackSlot = ArtifactPosition(hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  1298. if(arts->commonInfo->src.art)
  1299. {
  1300. arts->commonInfo->dst.slotID = freeBackpackSlot;
  1301. arts->commonInfo->dst.AOH = arts;
  1302. }
  1303. if(putOnAltar(altarSlot, art))
  1304. {
  1305. if(slotID < GameConstants::BACKPACK_START)
  1306. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
  1307. else
  1308. {
  1309. arts->commonInfo->src.clear();
  1310. arts->commonInfo->dst.clear();
  1311. CCS->curh->dragAndDropCursor(nullptr);
  1312. arts->unmarkSlots(false);
  1313. }
  1314. }
  1315. }