CTradeWindow.cpp 37 KB

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