CTradeWindow.cpp 37 KB

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