CTradeWindow.cpp 37 KB

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