CTradeWindow.cpp 37 KB

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