CTradeWindow.cpp 37 KB

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