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