CTradeWindow.cpp 39 KB

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