CTradeWindow.cpp 37 KB

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