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