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