| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- /*
- * CArtifactHolder.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "CArtifactHolder.h"
- #include "../gui/CGuiHandler.h"
- #include "../gui/CursorHandler.h"
- #include "Buttons.h"
- #include "CComponent.h"
- #include "../windows/CHeroWindow.h"
- #include "../windows/CSpellWindow.h"
- #include "../windows/GUIClasses.h"
- #include "../renderSDL/SDL_Extensions.h"
- #include "../CPlayerInterface.h"
- #include "../CGameInfo.h"
- #include "../../CCallback.h"
- #include "../../lib/CArtHandler.h"
- #include "../../lib/CGeneralTextHandler.h"
- #include "../../lib/mapObjects/CGHeroInstance.h"
- CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
- : CArtPlace(position, Art),
- locked(false),
- picked(false),
- marked(false),
- ourOwner(nullptr)
- {
- createImage();
- }
- void CHeroArtPlace::createImage()
- {
- OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
- si32 imageIndex = 0;
- if(locked)
- imageIndex = ArtifactID::ART_LOCK;
- else if(ourArt)
- imageIndex = ourArt->artType->getIconIndex();
- image = std::make_shared<CAnimImage>("artifact", imageIndex);
- if(!ourArt)
- image->disable();
- selection = std::make_shared<CAnimImage>("artifact", ArtifactID::ART_SELECTION);
- selection->disable();
- }
- void CHeroArtPlace::lockSlot(bool on)
- {
- if (locked == on)
- return;
- locked = on;
- if (on)
- image->setFrame(ArtifactID::ART_LOCK);
- else if (ourArt)
- image->setFrame(ourArt->artType->getIconIndex());
- else
- image->setFrame(0);
- }
- void CHeroArtPlace::pickSlot(bool on)
- {
- if (picked == on)
- return;
- picked = on;
- if (on)
- image->disable();
- else
- image->enable();
- }
- void CHeroArtPlace::selectSlot(bool on)
- {
- if (marked == on)
- return;
- marked = on;
- if (on)
- selection->enable();
- else
- selection->disable();
- }
- void CHeroArtPlace::clickLeft(tribool down, bool previousState)
- {
- //LRClickableAreaWTextComp::clickLeft(down);
- bool inBackpack = ArtifactUtils::isSlotBackpack(slotID);
- bool srcInBackpack = ArtifactUtils::isSlotBackpack(ourOwner->commonInfo->src.slotID);
- bool srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
- if(ourOwner->highlightModeCallback && ourArt)
- {
- if(down)
- {
- if(!ourArt->artType->isTradable()) //War Machine or Spellbook
- {
- LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
- }
- else
- {
- ourOwner->unmarkSlots(false);
- selectSlot(true);
- ourOwner->highlightModeCallback(this);
- }
- }
- return;
- }
- // If clicked on spellbook, open it only if no artifact is held at the moment.
- if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
- {
- if(ourArt->artType->getId() == ArtifactID::SPELLBOOK)
- GH.pushIntT<CSpellWindow>(ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt.get());
- }
- if (!down && previousState)
- {
- if(ourArt && ourArt->artType->getId() == ArtifactID::SPELLBOOK)
- return; //this is handled separately
- if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
- {
- if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
- && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
- {
- if(ourArt->artType->getId() == ArtifactID::CATAPULT) //catapult cannot be highlighted
- {
- std::vector<std::shared_ptr<CComponent>> catapult(1, std::make_shared<CComponent>(CComponent::artifact, 3, 0));
- LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
- return;
- }
- select();
- }
- }
- // Perform artifact transition
- else if(ourArt != ourOwner->commonInfo->src.art)
- {
- if(inBackpack) // Backpack destination.
- {
- if(!srcInBackpack || slotID != ourOwner->commonInfo->src.slotID + 1)
- {
- const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
- if(cur->getId() == ArtifactID::CATAPULT)
- {
- //should not happen, catapult cannot be selected
- logGlobal->error("Attempt to move Catapult");
- }
- else if(cur->isBig())
- {
- //war machines cannot go to backpack
- LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->getNameTranslated()));
- }
- else
- {
- setMeAsDest();
- vstd::amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
- if(ArtifactUtils::isBackpackFreeSlots(ourOwner->curHero))
- {
- if(!srcInSameHero || ourOwner->commonInfo->dst.slotID != ourOwner->commonInfo->src.slotID)
- ourOwner->realizeCurrentTransaction();
- }
- else
- {
- LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
- }
- }
- }
- }
- //check if swap is possible
- else if (fitsHere(ourOwner->commonInfo->src.art) &&
- (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
- {
- setMeAsDest();
- ourOwner->realizeCurrentTransaction();
- }
- }
- }
- }
- bool CHeroArtPlace::askToAssemble(const CGHeroInstance * hero, ArtifactPosition slot)
- {
- assert(hero);
- const auto art = hero->getArt(slot);
- assert(art);
- auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), ArtifactUtils::isSlotEquipment(slot));
- // If the artifact can be assembled, display dialog.
- for(const auto * combination : assemblyPossibilities)
- {
- LOCPLINT->showArtifactAssemblyDialog(
- art->artType,
- combination,
- std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combination->getId()));
- if(assemblyPossibilities.size() > 2)
- logGlobal->warn("More than one possibility of assembling on %s... taking only first", art->artType->getNameTranslated());
- return true;
- }
- return false;
- }
- bool CHeroArtPlace::askToDisassemble(const CGHeroInstance * hero, ArtifactPosition slot)
- {
- assert(hero);
- const auto art = hero->getArt(slot);
- assert(art);
- if(art->canBeDisassembled())
- {
- if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->constituents->size() - 1))
- return false;
-
- LOCPLINT->showArtifactAssemblyDialog(
- art->artType,
- nullptr,
- std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
- return true;
- }
- return false;
- }
- void CHeroArtPlace::clickRight(tribool down, bool previousState)
- {
- if(ourArt && down && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
- {
- if(ourOwner->allowedAssembling)
- {
- // If the artifact can be assembled, display dialog.
- if(askToAssemble(ourOwner->curHero, slotID))
- {
- return;
- }
- if(askToDisassemble(ourOwner->curHero, slotID))
- {
- return;
- }
- }
- // Lastly just show the artifact description.
- LRClickableAreaWTextComp::clickRight(down, previousState);
- }
- }
- void CArtifactsOfHero::activate()
- {
- if (commonInfo->src.AOH == this && commonInfo->src.art)
- CCS->curh->dragAndDropCursor("artifact", commonInfo->src.art->artType->getIconIndex());
- CIntObject::activate();
- }
- void CArtifactsOfHero::deactivate()
- {
- if (commonInfo->src.AOH == this && commonInfo->src.art)
- CCS->curh->dragAndDropCursor(nullptr);
- CIntObject::deactivate();
- }
- /**
- * Selects artifact slot so that the containing artifact looks like it's picked up.
- */
- void CHeroArtPlace::select()
- {
- if(locked)
- return;
- pickSlot(true);
- if(ourArt->canBeDisassembled() && ArtifactUtils::isSlotEquipment(slotID)) //worn combined artifact -> locks have to disappear
- {
- for(auto slot : ArtifactUtils::constituentWornSlots())
- {
- auto ap = ourOwner->getArtPlace(slot);
- if(ap)//getArtPlace may return null
- ap->pickSlot(ourArt->isPart(ap->ourArt));
- }
- }
- ourOwner->commonInfo->src.setTo(this, false);
- ourOwner->commonInfo->src.slotID = ArtifactPosition::TRANSITION_POS;
- LOCPLINT->cb->swapArtifacts(ArtifactLocation(ourOwner->curHero, slotID),
- ArtifactLocation(ourOwner->curHero, ArtifactPosition::TRANSITION_POS));
- }
- void CHeroArtPlace::showAll(SDL_Surface * to)
- {
- if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
- {
- CIntObject::showAll(to);
- }
- if(marked && active)
- {
- // Draw vertical bars.
- for (int i = 0; i < pos.h; ++i)
- {
- CSDL_Ext::putPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
- CSDL_Ext::putPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
- }
- // Draw horizontal bars.
- for (int i = 0; i < pos.w; ++i)
- {
- CSDL_Ext::putPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
- CSDL_Ext::putPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
- }
- }
- }
- bool CHeroArtPlace::fitsHere(const CArtifactInstance * art) const
- {
- // You can place 'no artifact' anywhere.
- if(!art)
- return true;
- // Anything but War Machines can be placed in backpack.
- if (slotID >= GameConstants::BACKPACK_START)
- return !art->artType->isBig();
- return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
- }
- void CHeroArtPlace::setMeAsDest(bool backpackAsVoid)
- {
- ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
- }
- void CHeroArtPlace::setArtifact(const CArtifactInstance *art)
- {
- baseType = -1; //by default we don't store any component
- ourArt = art;
- if(!art)
- {
- image->disable();
- text.clear();
- hoverText = CGI->generaltexth->allTexts[507];
- return;
- }
- image->enable();
- image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->getIconIndex());
- text = art->getDescription();
- // Display info about set
- if(ourOwner && ourOwner->curHero && !art->canBeDisassembled())
- {
- for(const auto combinedArt : art->artType->constituentOf)
- {
- std::string artList;
- text += "\n\n";
- text += "{" + combinedArt->getNameTranslated() + "}";
- int wornArtifacts = 0;
- for(const auto part : *combinedArt->constituents)
- {
- if(art->artType->constituentOf.size() <= 1)
- artList += "\n" + part->getNameTranslated();
- if(ourOwner->curHero->hasArt(part->getId(), true))
- wornArtifacts++;
- }
- text += " (" + boost::str(boost::format("%d") % wornArtifacts) + " / " +
- boost::str(boost::format("%d") % combinedArt->constituents->size()) + ")" + artList;
- }
- }
- if(art->artType->getId() == ArtifactID::SPELL_SCROLL)
- {
- int spellID = art->getScrollSpellID();
- if(spellID >= 0)
- {
- //add spell component info (used to provide a pic in r-click popup)
- baseType = CComponent::spell;
- type = spellID;
- bonusValue = 0;
- }
- }
- else
- {
- baseType = CComponent::artifact;
- type = art->artType->getId();
- bonusValue = 0;
- }
- if (locked) // Locks should appear as empty.
- hoverText = CGI->generaltexth->allTexts[507];
- else
- hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->getNameTranslated());
- }
- void CArtifactsOfHero::SCommonPart::reset()
- {
- src.clear();
- dst.clear();
- CCS->curh->dragAndDropCursor(nullptr);
- }
- void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
- {
- curHero = hero;
- if (curHero->artifactsInBackpack.size() > 0)
- backpackPos %= curHero->artifactsInBackpack.size();
- else
- backpackPos = 0;
- // Fill the slots for worn artifacts and backpack.
- for(auto p : artWorn)
- {
- setSlotData(p.second, p.first);
- }
- scrollBackpack(0);
- }
- void CArtifactsOfHero::dispose()
- {
- CCS->curh->dragAndDropCursor(nullptr);
- }
- void CArtifactsOfHero::scrollBackpack(int dir)
- {
- int artsInBackpack = static_cast<int>(curHero->artifactsInBackpack.size());
- backpackPos += dir;
- if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
- backpackPos += artsInBackpack;
- if(artsInBackpack)
- backpackPos %= artsInBackpack;
- std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
- if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
- toOmit.insert(commonInfo->src.art);
- int omitedSoFar = 0;
- //set new data
- size_t s = 0;
- for( ; s < artsInBackpack; ++s)
- {
- if (s < artsInBackpack)
- {
- auto slotID = ArtifactPosition(GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack);
- const CArtifactInstance *art = curHero->getArt(slotID);
- assert(art);
- if(!vstd::contains(toOmit, art))
- {
- if(s - omitedSoFar < backpack.size())
- setSlotData(backpack[s-omitedSoFar], slotID);
- }
- else
- {
- toOmit -= art;
- omitedSoFar++;
- continue;
- }
- }
- }
- for( ; s - omitedSoFar < backpack.size(); s++)
- eraseSlotData(backpack[s-omitedSoFar], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
- //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
- if(highlightModeCallback)
- {
- for(auto & elem : backpack)
- {
- if(elem->marked)
- {
- highlightModeCallback(elem.get());
- break;
- }
- }
- }
- //blocking scrolling if there is not enough artifacts to scroll
- bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
- leftArtRoll->block(!scrollingPossible);
- rightArtRoll->block(!scrollingPossible);
- safeRedraw();
- }
- /**
- * Marks possible slots where a given artifact can be placed, except backpack.
- *
- * @param art Artifact checked against.
- */
- void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art, bool withRedraw)
- {
- for(CArtifactsOfHero *aoh : commonInfo->participants)
- for(auto p : aoh->artWorn)
- p.second->selectSlot(art->canBePutAt(ArtifactLocation(aoh->curHero, p.second->slotID), true));
- if(withRedraw)
- safeRedraw();
- }
- /**
- * Unamarks all slots.
- */
- void CArtifactsOfHero::unmarkSlots(bool withRedraw)
- {
- if(commonInfo)
- for(CArtifactsOfHero *aoh : commonInfo->participants)
- aoh->unmarkLocalSlots(false);
- else
- unmarkLocalSlots(false);
- if(withRedraw)
- safeRedraw();
- }
- void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw)
- {
- for(auto & p : artWorn)
- p.second->selectSlot(false);
- for(auto & place : backpack)
- place->selectSlot(false);
- if(withRedraw)
- safeRedraw();
- }
- /**
- * Assigns an artifacts to an artifact place depending on it's new slot ID.
- */
- void CArtifactsOfHero::setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
- {
- if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
- {
- return;
- }
- artPlace->pickSlot(false);
- artPlace->slotID = slotID;
- if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
- {
- artPlace->lockSlot(asi->locked);
- artPlace->setArtifact(asi->artifact);
- }
- else
- artPlace->setArtifact(nullptr);
- }
- /**
- * Makes given artifact slot appear as empty with a certain slot ID.
- */
- void CArtifactsOfHero::eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID)
- {
- artPlace->pickSlot(false);
- artPlace->slotID = slotID;
- artPlace->setArtifact(nullptr);
- }
- CArtifactsOfHero::CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
- std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart)
- : curHero(nullptr),
- artWorn(ArtWorn),
- backpack(Backpack),
- backpackPos(0),
- commonInfo(nullptr),
- leftArtRoll(leftScroll),
- rightArtRoll(rightScroll),
- allowedAssembling(true),
- highlightModeCallback(nullptr)
- {
- if(createCommonPart)
- {
- commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
- commonInfo->participants.insert(this);
- }
- // Init slots for worn artifacts.
- for(auto p : artWorn)
- {
- p.second->ourOwner = this;
- eraseSlotData(p.second, p.first);
- }
- // Init slots for the backpack.
- for(size_t s=0; s<backpack.size(); ++s)
- {
- backpack[s]->ourOwner = this;
- eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + (si32)s));
- }
- leftArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,-1));
- rightArtRoll->addCallback(std::bind(&CArtifactsOfHero::scrollBackpack, this,+1));
- }
- CArtifactsOfHero::CArtifactsOfHero(const Point & position, bool createCommonPart)
- : curHero(nullptr),
- backpackPos(0),
- commonInfo(nullptr),
- allowedAssembling(true),
- highlightModeCallback(nullptr)
- {
- if(createCommonPart)
- {
- commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
- commonInfo->participants.insert(this);
- }
- OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
- pos += position;
- std::vector<Point> slotPos =
- {
- Point(509,30), Point(567,240), Point(509,80), //0-2
- Point(383,68), Point(564,183), Point(509,130), //3-5
- Point(431,68), Point(610,183), Point(515,295), //6-8
- Point(383,143), Point(399,194), Point(415,245), //9-11
- Point(431,296), Point(564,30), Point(610,30), //12-14
- Point(610,76), Point(610,122), Point(610,310), //15-17
- Point(381,296) //18
- };
- // Create slots for worn artifacts.
- for(si32 g = 0; g < GameConstants::BACKPACK_START; g++)
- {
- artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
- artWorn[ArtifactPosition(g)]->ourOwner = this;
- eraseSlotData(artWorn[ArtifactPosition(g)], ArtifactPosition(g));
- }
- // Create slots for the backpack.
- for(int s=0; s<5; ++s)
- {
- auto add = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
- add->ourOwner = this;
- eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
- backpack.push_back(add);
- }
- leftArtRoll = std::make_shared<CButton>(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [&](){ scrollBackpack(-1);}, SDLK_LEFT);
- rightArtRoll = std::make_shared<CButton>(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [&](){ scrollBackpack(+1);}, SDLK_RIGHT);
- }
- CArtifactsOfHero::~CArtifactsOfHero()
- {
- dispose();
- // Artifact located in artifactsTransitionPos should be returned
- if(!curHero->artifactsTransitionPos.empty())
- {
- auto artPlace = getArtPlace(
- ArtifactUtils::getArtAnyPosition(curHero, curHero->artifactsTransitionPos.begin()->artifact->getTypeId()));
- if(artPlace)
- {
- assert(artPlace->ourOwner);
- artPlace->setMeAsDest();
- artPlace->ourOwner->realizeCurrentTransaction();
- }
- else
- {
- LOCPLINT->cb->eraseArtifactByClient(ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
- }
- }
- }
- void CArtifactsOfHero::updateParentWindow()
- {
- if(CHeroWindow * chw = dynamic_cast<CHeroWindow*>(GH.topInt().get()))
- {
- chw->update(curHero, true);
- }
- else if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(GH.topInt().get()))
- {
- cew->updateWidgets();
- }
- }
- void CArtifactsOfHero::safeRedraw()
- {
- if (active)
- {
- if(parent)
- parent->redraw();
- else
- redraw();
- }
- }
- void CArtifactsOfHero::realizeCurrentTransaction()
- {
- assert(commonInfo->src.AOH);
- assert(commonInfo->dst.AOH);
- LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
- ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
- }
- void CArtifactsOfHero::artifactMoved(const ArtifactLocation & src, const ArtifactLocation & dst, bool withUIUpdate)
- {
- bool isCurHeroSrc = src.isHolder(curHero),
- isCurHeroDst = dst.isHolder(curHero);
- if(isCurHeroSrc && ArtifactUtils::isSlotBackpack(src.slot))
- updateSlot(src.slot);
- if(isCurHeroDst && ArtifactUtils::isSlotBackpack(dst.slot))
- updateSlot(dst.slot);
- // We need to update all slots, artifact might be combined and affect more slots
- if(isCurHeroSrc || isCurHeroDst)
- updateWornSlots(false);
- if(!isCurHeroSrc && !isCurHeroDst)
- return;
- // When moving one artifact onto another it leads to two art movements: dst->TRANSITION_POS; src->dst
- // however after first movement we pick the art from TRANSITION_POS and the second movement coming when
- // we have a different artifact may look surprising... but it's valid.
- // Used when doing dragAndDrop and artifact swap multiple times
- if(src.slot == ArtifactPosition::TRANSITION_POS &&
- commonInfo->src.slotID == ArtifactPosition::TRANSITION_POS &&
- commonInfo->dst.slotID == ArtifactPosition::PRE_FIRST &&
- isCurHeroDst)
- {
- auto art = curHero->getArt(ArtifactPosition::TRANSITION_POS);
- assert(art);
- CCS->curh->dragAndDropCursor("artifact", art->artType->getIconIndex());
- if(withUIUpdate)
- markPossibleSlots(art);
- commonInfo->src.art = art;
- commonInfo->src.slotID = src.slot;
- }
- // Artifact was taken from us
- else if(commonInfo->src == src && dst.slot != ArtifactPosition::TRANSITION_POS)
- {
- // Expected movement from slot ot slot
- assert(commonInfo->dst == dst
- // Artifact moved back to backpack (eg. to make place for art we are moving)
- || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START
- || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
- commonInfo->reset();
- unmarkSlots();
- }
- else
- {
- // The dest artifact was moved after the swap -> we are picking it
- if(commonInfo->dst == src)
- {
- assert(dst.slot == ArtifactPosition::TRANSITION_POS);
- commonInfo->reset();
- for(CArtifactsOfHero * aoh : commonInfo->participants)
- {
- if(dst.isHolder(aoh->curHero))
- {
- commonInfo->src.AOH = aoh;
- break;
- }
- }
- commonInfo->src.art = dst.getArt();
- commonInfo->src.slotID = dst.slot;
- assert(commonInfo->src.AOH);
- CCS->curh->dragAndDropCursor("artifact", dst.getArt()->artType->getIconIndex());
- }
- if(!curHero->artifactsTransitionPos.empty() && withUIUpdate)
- {
- auto artInst = curHero->getArt(ArtifactPosition::TRANSITION_POS);
- markPossibleSlots(artInst);
- CCS->curh->dragAndDropCursor("artifact", artInst->artType->getIconIndex());
- }
- }
- if(withUIUpdate)
- {
- updateParentWindow();
- scrollBackpack(0);
- }
- }
- void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
- {
- if(al.isHolder(curHero))
- {
- if(al.slot < GameConstants::BACKPACK_START)
- updateWornSlots(0);
- else
- scrollBackpack(0); //update backpack slots
- }
- }
- CArtifactsOfHero::ArtPlacePtr CArtifactsOfHero::getArtPlace(ArtifactPosition slot)
- {
- if(ArtifactUtils::isSlotEquipment(slot))
- {
- if(artWorn.find(slot) == artWorn.end())
- {
- logGlobal->error("CArtifactsOfHero::getArtPlace: invalid slot %d", slot);
- return nullptr;
- }
- return artWorn[slot];
- }
- if(ArtifactUtils::isSlotBackpack(slot))
- {
- for(ArtPlacePtr ap : backpack)
- if(ap->slotID == slot)
- return ap;
- return nullptr;
- }
- else
- {
- return nullptr;
- }
- }
- void CArtifactsOfHero::artifactUpdateSlots(const ArtifactLocation & al)
- {
- if(al.isHolder(curHero))
- {
- if(ArtifactUtils::isSlotBackpack(al.slot))
- updateBackpackSlots(true);
- else
- updateWornSlots(true);
- }
- }
- void CArtifactsOfHero::updateWornSlots(bool redrawParent)
- {
- for(auto p : artWorn)
- updateSlot(p.first);
- if(redrawParent)
- updateParentWindow();
- }
- void CArtifactsOfHero::updateBackpackSlots(bool redrawParent)
- {
- for(auto artPlace : backpack)
- updateSlot(artPlace->slotID);
- scrollBackpack(0);
- if(redrawParent)
- updateParentWindow();
- }
- const CGHeroInstance * CArtifactsOfHero::getHero() const
- {
- return curHero;
- }
- void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
- {
- setSlotData(getArtPlace(slotID), slotID);
- }
- CArtifactHolder::CArtifactHolder()
- {
- }
- void CWindowWithArtifacts::addSet(std::shared_ptr<CArtifactsOfHero> artSet)
- {
- artSets.emplace_back(artSet);
- }
- std::shared_ptr<CArtifactsOfHero::SCommonPart> CWindowWithArtifacts::getCommonPart()
- {
- for(auto artSetWeak : artSets)
- {
- std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
- if(realPtr)
- return realPtr->commonInfo;
- }
- return std::shared_ptr<CArtifactsOfHero::SCommonPart>();
- }
- void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
- {
- for(auto artSetWeak : artSets)
- {
- std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
- if(realPtr)
- realPtr->artifactRemoved(artLoc);
- }
- }
- void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw)
- {
- CArtifactsOfHero * destaoh = nullptr;
- for(auto artSetWeak : artSets)
- {
- std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
- if(realPtr)
- {
- realPtr->artifactMoved(artLoc, destLoc, withRedraw);
- if(destLoc.isHolder(realPtr->getHero()))
- destaoh = realPtr.get();
- }
- }
- //Make sure the status bar is updated so it does not display old text
- if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
- {
- destaoh->getArtPlace(destLoc.slot)->hover(true);
- }
- }
- void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
- {
- for(auto artSetWeak : artSets)
- {
- std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
- if(realPtr)
- realPtr->artifactUpdateSlots(artLoc);
- }
- }
- void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
- {
- for(auto artSetWeak : artSets)
- {
- std::shared_ptr<CArtifactsOfHero> realPtr = artSetWeak.lock();
- if(realPtr)
- realPtr->artifactUpdateSlots(artLoc);
- }
- }
- void CArtifactsOfHero::SCommonPart::Artpos::clear()
- {
- slotID = ArtifactPosition::PRE_FIRST;
- AOH = nullptr;
- art = nullptr;
- }
- void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CHeroArtPlace *place, bool dontTakeBackpack)
- {
- slotID = place->slotID;
- AOH = place->ourOwner;
- if(ArtifactUtils::isSlotBackpack(slotID) && dontTakeBackpack)
- art = nullptr;
- else
- art = place->ourArt;
- }
- bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
- {
- if(!AOH)
- return false;
- bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
- //assert(al.getArt() == art);
- return ret;
- }
- bool CArtifactsOfHero::SCommonPart::Artpos::valid()
- {
- assert(AOH && art);
- return art == AOH->curHero->getArt(slotID);
- }
- CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art) : ourArt(Art)
- {
- image = nullptr;
- pos += position;
- pos.w = pos.h = 44;
- }
- void CArtPlace::clickLeft(tribool down, bool previousState)
- {
- LRClickableAreaWTextComp::clickLeft(down, previousState);
- }
- void CArtPlace::clickRight(tribool down, bool previousState)
- {
- LRClickableAreaWTextComp::clickRight(down, previousState);
- }
- CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art) : CArtPlace(position, Art), commanderOwner(commanderOwner), commanderSlotID(artSlot.num)
- {
- createImage();
- setArtifact(Art);
- }
- void CCommanderArtPlace::clickLeft(tribool down, bool previousState)
- {
- if (ourArt && text.size() && down)
- LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this](){ returnArtToHeroCallback(); }, [](){});
- }
- void CCommanderArtPlace::clickRight(tribool down, bool previousState)
- {
- if (ourArt && text.size() && down)
- CArtPlace::clickRight(down, previousState);
- }
- void CCommanderArtPlace::createImage()
- {
- OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
- int imageIndex = 0;
- if(ourArt)
- imageIndex = ourArt->artType->getIconIndex();
- image = std::make_shared<CAnimImage>("artifact", imageIndex);
- if(!ourArt)
- image->disable();
- }
- void CCommanderArtPlace::returnArtToHeroCallback()
- {
- ArtifactPosition artifactPos = commanderSlotID;
- ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
- if(freeSlot == ArtifactPosition::PRE_FIRST)
- {
- LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
- }
- else
- {
- ArtifactLocation src(commanderOwner->commander.get(), artifactPos);
- ArtifactLocation dst(commanderOwner, freeSlot);
- if(ourArt->canBePutAt(dst, true))
- {
- LOCPLINT->cb->swapArtifacts(src, dst);
- setArtifact(nullptr);
- parent->redraw();
- }
- }
- }
- void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
- {
- baseType = -1; //by default we don't store any component
- ourArt = art;
- if (!art)
- {
- image->disable();
- text.clear();
- return;
- }
- image->enable();
- image->setFrame(art->artType->getIconIndex());
- text = art->getDescription();
- if (art->artType->getId() == ArtifactID::SPELL_SCROLL)
- {
- int spellID = art->getScrollSpellID();
- if (spellID >= 0)
- {
- //add spell component info (used to provide a pic in r-click popup)
- baseType = CComponent::spell;
- type = spellID;
- bonusValue = 0;
- }
- }
- else
- {
- baseType = CComponent::artifact;
- type = art->artType->getId();
- bonusValue = 0;
- }
- }
|