CTradeWindow.cpp 39 KB

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