1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00

Merge pull request #2055 from SoundSSGood/cartifactholder-rework

Artifact related code refactoring part2 (client CArtifactHolder)
This commit is contained in:
Ivan Savenko 2023-05-01 02:33:08 +03:00 committed by GitHub
commit 4aa232a60a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 1470 additions and 1205 deletions

View File

@ -85,6 +85,12 @@ set(client_SRCS
widgets/MiscWidgets.cpp
widgets/ObjectLists.cpp
widgets/TextControls.cpp
widgets/CArtifactsOfHeroBase.cpp
widgets/CArtifactsOfHeroMain.cpp
widgets/CArtifactsOfHeroKingdom.cpp
widgets/CArtifactsOfHeroAltar.cpp
widgets/CArtifactsOfHeroMarket.cpp
widgets/CWindowWithArtifacts.cpp
windows/CCastleInterface.cpp
windows/CCreatureWindow.cpp
@ -214,6 +220,12 @@ set(client_HEADERS
widgets/MiscWidgets.h
widgets/ObjectLists.h
widgets/TextControls.h
widgets/CArtifactsOfHeroBase.h
widgets/CArtifactsOfHeroMain.h
widgets/CArtifactsOfHeroKingdom.h
widgets/CArtifactsOfHeroAltar.h
widgets/CArtifactsOfHeroMarket.h
widgets/CWindowWithArtifacts.h
windows/CCastleInterface.h
windows/CCreatureWindow.h

View File

@ -1792,7 +1792,7 @@ void CPlayerInterface::askToAssembleArtifact(const ArtifactLocation &al)
al.slot.num);
return;
}
CHeroArtPlace::askToAssemble(hero, al.slot);
ArtifactUtilsClient::askToAssemble(hero, al.slot);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -14,37 +14,38 @@
VCMI_LIB_NAMESPACE_BEGIN
struct ArtifactLocation;
class CArtifactSet;
VCMI_LIB_NAMESPACE_END
class CArtifactsOfHero;
class CAnimImage;
class CButton;
class CArtifactHolder
{
public:
CArtifactHolder();
virtual void artifactRemoved(const ArtifactLocation &artLoc)=0;
virtual void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw)=0;
virtual void artifactDisassembled(const ArtifactLocation &artLoc)=0;
virtual void artifactAssembled(const ArtifactLocation &artLoc)=0;
virtual void artifactRemoved(const ArtifactLocation & artLoc)=0;
virtual void artifactMoved(const ArtifactLocation & artLoc, const ArtifactLocation & destLoc, bool withRedraw)=0;
virtual void artifactDisassembled(const ArtifactLocation & artLoc)=0;
virtual void artifactAssembled(const ArtifactLocation & artLoc)=0;
};
class CArtPlace : public LRClickableAreaWTextComp
{
protected:
std::shared_ptr<CAnimImage> image;
virtual void createImage()=0;
public:
const CArtifactInstance * ourArt; // should be changed only with setArtifact()
const CArtifactInstance * ourArt;
void setInternals(const CArtifactInstance * artInst);
virtual void createImage()=0;
public:
CArtPlace(Point position, const CArtifactInstance * Art = nullptr);
void clickLeft(tribool down, bool previousState) override;
void clickRight(tribool down, bool previousState) override;
const CArtifactInstance * getArt();
virtual void setArtifact(const CArtifactInstance *art)=0;
virtual void setArtifact(const CArtifactInstance * art)=0;
};
class CCommanderArtPlace : public CArtPlace
@ -55,138 +56,44 @@ protected:
void createImage() override;
void returnArtToHeroCallback();
public:
CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art = nullptr);
void clickLeft(tribool down, bool previousState) override;
void clickRight(tribool down, bool previousState) override;
virtual void setArtifact(const CArtifactInstance * art) override;
void setArtifact(const CArtifactInstance * art) override;
};
/// Artifacts can be placed there. Gets shown at the hero window
class CHeroArtPlace: public CArtPlace
{
std::shared_ptr<CAnimImage> selection;
void createImage() override;
public:
// consider these members as const - change them only with appropriate methods e.g. lockSlot()
bool locked;
bool picked;
bool marked;
using ClickHandler = std::function<void(CHeroArtPlace&)>;
ArtifactPosition slotID; //Arts::EPOS enum + backpack starting from Arts::BACKPACK_START
CArtifactsOfHero * ourOwner;
ArtifactPosition slot;
ClickHandler leftClickCallback;
ClickHandler rightClickCallback;
CHeroArtPlace(Point position, const CArtifactInstance * Art = nullptr);
void lockSlot(bool on);
void pickSlot(bool on);
bool isLocked();
void selectSlot(bool on);
bool isMarked() const;
void clickLeft(tribool down, bool previousState) override;
void clickRight(tribool down, bool previousState) override;
void select();
void showAll(SDL_Surface * to) override;
bool fitsHere (const CArtifactInstance * art) const; //returns true if given artifact can be placed here
void setArtifact(const CArtifactInstance * art) override;
void addCombinedArtInfo(std::map<const CArtifact*, int> & arts);
void setMeAsDest(bool backpackAsVoid = true);
void setArtifact(const CArtifactInstance *art) override;
static bool askToAssemble(const CGHeroInstance * hero, ArtifactPosition slot);
static bool askToDisassemble(const CGHeroInstance * hero, ArtifactPosition slot);
protected:
std::shared_ptr<CAnimImage> selection;
bool locked;
bool marked;
void createImage() override;
};
/// Contains artifacts of hero. Distincts which artifacts are worn or backpacked
class CArtifactsOfHero : public CIntObject
namespace ArtifactUtilsClient
{
public:
using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>;
using ArtPlaceMap = std::map<ArtifactPosition, ArtPlacePtr>;
struct SCommonPart
{
struct Artpos
{
ArtifactPosition slotID;
const CArtifactsOfHero *AOH;
const CArtifactInstance *art;
void clear();
void setTo(const CHeroArtPlace *place, bool dontTakeBackpack);
bool valid();
bool operator==(const ArtifactLocation &al) const;
} src, dst;
std::set<CArtifactsOfHero *> participants; // Needed to mark slots.
void reset();
};
std::shared_ptr<SCommonPart> commonInfo; //when we have more than one CArtifactsOfHero in one window with exchange possibility, we use this (eg. in exchange window); to be provided externally
std::shared_ptr<CButton> leftArtRoll;
std::shared_ptr<CButton> rightArtRoll;
bool allowedAssembling;
std::multiset<const CArtifactInstance*> artifactsOnAltar; //artifacts id that are technically present in backpack but in GUI are moved to the altar - they'll be omitted in backpack slots
std::function<void(CHeroArtPlace*)> highlightModeCallback; //if set, clicking on art place doesn't pick artifact but highlights the slot and calls this function
void realizeCurrentTransaction(); //calls callback with parameters stored in commonInfo
void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst, bool withUIUpdate);
void artifactRemoved(const ArtifactLocation &al);
void artifactUpdateSlots(const ArtifactLocation &al);
ArtPlacePtr getArtPlace(ArtifactPosition slot);//may return null
void setHero(const CGHeroInstance * hero);
const CGHeroInstance *getHero() const;
void dispose(); //free resources not needed after closing windows and reset state
void scrollBackpack(int dir); //dir==-1 => to left; dir==1 => to right
void activate() override;
void deactivate() override;
void safeRedraw();
void markPossibleSlots(const CArtifactInstance * art, bool withRedraw = false);
void unmarkSlots(bool withRedraw = false); //unmarks slots in all visible AOHs
void unmarkLocalSlots(bool withRedraw = false); //unmarks slots in that particular AOH
void updateWornSlots(bool redrawParent = false);
void updateBackpackSlots(bool redrawParent = false);
void updateSlot(ArtifactPosition i);
CArtifactsOfHero(const Point& position, bool createCommonPart = false);
//Alternative constructor, used if custom artifacts positioning required (Kingdom interface)
CArtifactsOfHero(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll, bool createCommonPart = false);
~CArtifactsOfHero();
void updateParentWindow();
friend class CHeroArtPlace;
private:
const CGHeroInstance * curHero;
ArtPlaceMap artWorn;
std::vector<ArtPlacePtr> backpack; //hero's visible backpack (only 5 elements!)
int backpackPos; //number of first art visible in backpack (in hero's vector)
void eraseSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
void setSlotData(ArtPlacePtr artPlace, ArtifactPosition slotID);
};
class CWindowWithArtifacts : public CArtifactHolder
{
std::vector<std::weak_ptr<CArtifactsOfHero>> artSets;
public:
void addSet(std::shared_ptr<CArtifactsOfHero> artSet);
std::shared_ptr<CArtifactsOfHero::SCommonPart> getCommonPart();
void artifactRemoved(const ArtifactLocation &artLoc) override;
void artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc, bool withRedraw) override;
void artifactDisassembled(const ArtifactLocation &artLoc) override;
void artifactAssembled(const ArtifactLocation &artLoc) override;
};
bool askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot);
bool askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot);
}

View File

@ -0,0 +1,109 @@
/*
* CArtifactsOfHeroAltar.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 "CArtifactsOfHeroAltar.h"
#include "../CPlayerInterface.h"
#include "../../CCallback.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
CArtifactsOfHeroAltar::CArtifactsOfHeroAltar(const Point & position)
: visibleArtSet(ArtBearer::ArtBearer::HERO)
{
init(
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1),
position,
std::bind(&CArtifactsOfHeroAltar::scrollBackpack, this, _1));
pickedArtFromSlot = ArtifactPosition::PRE_FIRST;
};
void CArtifactsOfHeroAltar::setHero(const CGHeroInstance * hero)
{
if(hero)
{
visibleArtSet.artifactsWorn = hero->artifactsWorn;
visibleArtSet.artifactsInBackpack = hero->artifactsInBackpack;
CArtifactsOfHeroBase::setHero(hero);
}
}
void CArtifactsOfHeroAltar::updateWornSlots()
{
for(auto place : artWorn)
setSlotData(getArtPlace(place.first), place.first, visibleArtSet);
}
void CArtifactsOfHeroAltar::updateBackpackSlots()
{
for(auto artPlace : backpack)
setSlotData(getArtPlace(artPlace->slot), artPlace->slot, visibleArtSet);
}
void CArtifactsOfHeroAltar::scrollBackpack(int offset)
{
CArtifactsOfHeroBase::scrollBackpackForArtSet(offset, visibleArtSet);
safeRedraw();
}
void CArtifactsOfHeroAltar::pickUpArtifact(CHeroArtPlace & artPlace)
{
if(const auto art = artPlace.getArt())
{
pickedArtFromSlot = artPlace.slot;
artPlace.setArtifact(nullptr);
deleteFromVisible(art);
if(ArtifactUtils::isSlotBackpack(pickedArtFromSlot))
pickedArtFromSlot = curHero->getSlotByInstance(art);
assert(pickedArtFromSlot != ArtifactPosition::PRE_FIRST);
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero, pickedArtFromSlot), ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
}
}
void CArtifactsOfHeroAltar::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
const auto pickedArtInst = curHero->getArt(ArtifactPosition::TRANSITION_POS);
assert(pickedArtInst);
visibleArtSet.putArtifact(dstLoc.slot, const_cast<CArtifactInstance*>(pickedArtInst));
}
void CArtifactsOfHeroAltar::pickedArtMoveToAltar(const ArtifactPosition & slot)
{
if(ArtifactUtils::isSlotBackpack(slot) || ArtifactUtils::isSlotEquipment(slot) || slot == ArtifactPosition::TRANSITION_POS)
{
assert(!curHero->getSlot(pickedArtFromSlot)->getArt());
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero, slot), ArtifactLocation(curHero, pickedArtFromSlot));
pickedArtFromSlot = ArtifactPosition::PRE_FIRST;
}
}
void CArtifactsOfHeroAltar::deleteFromVisible(const CArtifactInstance * artInst)
{
const auto slot = visibleArtSet.getSlotByInstance(artInst);
visibleArtSet.removeArtifact(slot);
if(ArtifactUtils::isSlotBackpack(slot))
{
scrollBackpackForArtSet(0, visibleArtSet);
}
else
{
if(artInst->canBeDisassembled())
{
for(const auto part : dynamic_cast<const CCombinedArtifactInstance*>(artInst)->constituentsInfo)
{
if(part.slot != ArtifactPosition::PRE_FIRST)
getArtPlace(part.slot)->setArtifact(nullptr);
}
}
}
}

View File

@ -0,0 +1,32 @@
/*
* CArtifactsOfHeroAltar.h, 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
*
*/
#pragma once
#include "CArtifactsOfHeroBase.h"
#include "../../lib/CArtHandler.h"
class CArtifactsOfHeroAltar : public CArtifactsOfHeroBase
{
public:
std::set<const CArtifactInstance*> artifactsOnAltar;
ArtifactPosition pickedArtFromSlot;
CArtifactFittingSet visibleArtSet;
CArtifactsOfHeroAltar(const Point & position);
void setHero(const CGHeroInstance * hero) override;
void updateWornSlots() override;
void updateBackpackSlots() override;
void scrollBackpack(int offset) override;
void pickUpArtifact(CHeroArtPlace & artPlace);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickedArtMoveToAltar(const ArtifactPosition & slot);
void deleteFromVisible(const CArtifactInstance * artInst);
};

View File

@ -0,0 +1,277 @@
/*
* CArtifactsOfHeroBase.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 "CArtifactsOfHeroBase.h"
#include "../gui/CGuiHandler.h"
#include "../gui/CursorHandler.h"
#include "Buttons.h"
#include "../renderSDL/SDL_Extensions.h"
#include "../CPlayerInterface.h"
#include "../CGameInfo.h"
#include "../../CCallback.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
CArtifactsOfHeroBase::CArtifactsOfHeroBase()
: backpackPos(0),
curHero(nullptr)
{
}
CArtifactsOfHeroBase::~CArtifactsOfHeroBase()
{
// TODO: cursor handling is CWindowWithArtifacts level. Should be moved when trading, kingdom and hero window are reworked
// This will interfere with the implementation of a separate backpack window
CCS->curh->dragAndDropCursor(nullptr);
// Artifact located in artifactsTransitionPos should be returned
if(getPickedArtifact())
{
auto slot = ArtifactUtils::getArtAnyPosition(curHero, curHero->artifactsTransitionPos.begin()->artifact->getTypeId());
if(slot == ArtifactPosition::PRE_FIRST)
{
LOCPLINT->cb->eraseArtifactByClient(ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
}
else
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS), ArtifactLocation(curHero, slot));
}
}
}
void CArtifactsOfHeroBase::init(
CHeroArtPlace::ClickHandler lClickCallback,
CHeroArtPlace::ClickHandler rClickCallback,
const Point & position,
BpackScrollHandler scrollHandler)
{
// CArtifactsOfHeroBase::init may be transform to CArtifactsOfHeroBase::CArtifactsOfHeroBase if OBJECT_CONSTRUCTION_CAPTURING is removed
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
pos += position;
for(int g = 0; g < GameConstants::BACKPACK_START; g++)
{
artWorn[ArtifactPosition(g)] = std::make_shared<CHeroArtPlace>(slotPos[g]);
}
backpack.clear();
for(int s = 0; s < 5; s++)
{
auto artPlace = std::make_shared<CHeroArtPlace>(Point(403 + 46 * s, 365));
backpack.push_back(artPlace);
}
for(auto artPlace : artWorn)
{
artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr);
artPlace.second->leftClickCallback = lClickCallback;
artPlace.second->rightClickCallback = rClickCallback;
}
for(auto artPlace : backpack)
{
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = lClickCallback;
artPlace->rightClickCallback = rClickCallback;
}
leftBackpackRoll = std::make_shared<CButton>(Point(379, 364), "hsbtns3.def", CButton::tooltip(), [scrollHandler]() { scrollHandler(-1); }, SDLK_LEFT);
rightBackpackRoll = std::make_shared<CButton>(Point(632, 364), "hsbtns5.def", CButton::tooltip(), [scrollHandler]() { scrollHandler(+1); }, SDLK_RIGHT);
leftBackpackRoll->block(true);
rightBackpackRoll->block(true);
}
void CArtifactsOfHeroBase::leftClickArtPlace(CHeroArtPlace & artPlace)
{
if(leftClickCallback)
leftClickCallback(*this, artPlace);
}
void CArtifactsOfHeroBase::rightClickArtPlace(CHeroArtPlace & artPlace)
{
if(rightClickCallback)
rightClickCallback(*this, artPlace);
}
void CArtifactsOfHeroBase::setHero(const CGHeroInstance * hero)
{
curHero = hero;
if(curHero->artifactsInBackpack.size() > 0)
backpackPos %= curHero->artifactsInBackpack.size();
else
backpackPos = 0;
for(auto slot : artWorn)
{
setSlotData(slot.second, slot.first, *curHero);
}
scrollBackpackForArtSet(0, *curHero);
}
const CGHeroInstance * CArtifactsOfHeroBase::getHero() const
{
return curHero;
}
void CArtifactsOfHeroBase::scrollBackpack(int offset)
{
scrollBackpackForArtSet(offset, *curHero);
safeRedraw();
}
void CArtifactsOfHeroBase::scrollBackpackForArtSet(int offset, const CArtifactSet & artSet)
{
// offset==-1 => to left; offset==1 => to right
using slotInc = std::function<ArtifactPosition(ArtifactPosition&)>;
auto artsInBackpack = static_cast<int>(artSet.artifactsInBackpack.size());
auto scrollingPossible = artsInBackpack > backpack.size();
slotInc inc_straight = [](ArtifactPosition & slot) -> ArtifactPosition
{
return slot + 1;
};
slotInc inc_ring = [artsInBackpack](ArtifactPosition & slot) -> ArtifactPosition
{
return ArtifactPosition(GameConstants::BACKPACK_START + (slot - GameConstants::BACKPACK_START + 1) % artsInBackpack);
};
slotInc inc;
if(scrollingPossible)
inc = inc_ring;
else
inc = inc_straight;
backpackPos += offset;
if(backpackPos < 0)
backpackPos += artsInBackpack;
if(artsInBackpack)
backpackPos %= artsInBackpack;
auto slot = ArtifactPosition(GameConstants::BACKPACK_START + backpackPos);
for(auto artPlace : backpack)
{
setSlotData(artPlace, slot, artSet);
slot = inc(slot);
}
// Blocking scrolling if there is not enough artifacts to scroll
leftBackpackRoll->block(!scrollingPossible);
rightBackpackRoll->block(!scrollingPossible);
}
void CArtifactsOfHeroBase::safeRedraw()
{
if(active)
{
if(parent)
parent->redraw();
else
redraw();
}
}
void CArtifactsOfHeroBase::markPossibleSlots(const CArtifactInstance * art, bool assumeDestRemoved)
{
for(auto artPlace : artWorn)
artPlace.second->selectSlot(art->artType->canBePutAt(curHero, artPlace.second->slot, assumeDestRemoved));
}
void CArtifactsOfHeroBase::unmarkSlots()
{
for(auto & artPlace : artWorn)
artPlace.second->selectSlot(false);
for(auto & artPlace : backpack)
artPlace->selectSlot(false);
}
CArtifactsOfHeroBase::ArtPlacePtr CArtifactsOfHeroBase::getArtPlace(const 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 artPlace : backpack)
if(artPlace->slot == slot)
return artPlace;
return nullptr;
}
else
{
return nullptr;
}
}
void CArtifactsOfHeroBase::updateWornSlots()
{
for(auto place : artWorn)
updateSlot(place.first);
}
void CArtifactsOfHeroBase::updateBackpackSlots()
{
for(auto artPlace : backpack)
updateSlot(artPlace->slot);
scrollBackpackForArtSet(0, *curHero);
}
void CArtifactsOfHeroBase::updateSlot(const ArtifactPosition & slot)
{
setSlotData(getArtPlace(slot), slot, *curHero);
}
const CArtifactInstance * CArtifactsOfHeroBase::getPickedArtifact()
{
// Returns only the picked up artifact. Not just highlighted like in the trading window.
if(!curHero || curHero->artifactsTransitionPos.empty())
return nullptr;
else
return curHero->getArt(ArtifactPosition::TRANSITION_POS);
}
void CArtifactsOfHeroBase::setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot, const CArtifactSet & artSet)
{
// Spurious call from artifactMoved in attempt to update hidden backpack slot
if(!artPlace && ArtifactUtils::isSlotBackpack(slot))
{
return;
}
artPlace->slot = slot;
if(auto slotInfo = artSet.getSlot(slot))
{
artPlace->lockSlot(slotInfo->locked);
artPlace->setArtifact(slotInfo->artifact);
if(!slotInfo->artifact->canBeDisassembled())
{
// If the artifact is part of at least one combined artifact, add additional information
std::map<const CArtifact*, int> arts;
for(const auto combinedArt : slotInfo->artifact->artType->constituentOf)
{
arts.insert(std::pair(combinedArt, 0));
for(const auto part : *combinedArt->constituents)
if(artSet.hasArt(part->getId(), true))
arts.at(combinedArt)++;
}
artPlace->addCombinedArtInfo(arts);
}
}
else
{
artPlace->setArtifact(nullptr);
}
}

View File

@ -0,0 +1,67 @@
/*
* CArtifactsOfHeroBase.h, 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
*
*/
#pragma once
#include "CArtifactHolder.h"
class CArtifactsOfHeroBase : public CIntObject
{
protected:
using ArtPlacePtr = std::shared_ptr<CHeroArtPlace>;
using BpackScrollHandler = std::function<void(int)>;
public:
using ArtPlaceMap = std::map<ArtifactPosition, ArtPlacePtr>;
using ClickHandler = std::function<void(CArtifactsOfHeroBase&, CHeroArtPlace&)>;
const CGHeroInstance * curHero;
ClickHandler leftClickCallback;
ClickHandler rightClickCallback;
CArtifactsOfHeroBase();
virtual ~CArtifactsOfHeroBase();
virtual void leftClickArtPlace(CHeroArtPlace & artPlace);
virtual void rightClickArtPlace(CHeroArtPlace & artPlace);
virtual void setHero(const CGHeroInstance * hero);
virtual const CGHeroInstance * getHero() const;
virtual void scrollBackpack(int offset);
virtual void safeRedraw();
virtual void markPossibleSlots(const CArtifactInstance * art, bool assumeDestRemoved = true);
virtual void unmarkSlots();
virtual ArtPlacePtr getArtPlace(const ArtifactPosition & slot);
virtual void updateWornSlots();
virtual void updateBackpackSlots();
virtual void updateSlot(const ArtifactPosition & slot);
virtual const CArtifactInstance * getPickedArtifact();
protected:
ArtPlaceMap artWorn;
std::vector<ArtPlacePtr> backpack;
std::shared_ptr<CButton> leftBackpackRoll;
std::shared_ptr<CButton> rightBackpackRoll;
int backpackPos; // Position to display artifacts in heroes backpack
const 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
};
virtual void init(CHeroArtPlace::ClickHandler lClickCallback, CHeroArtPlace::ClickHandler rClickCallback,
const Point & position, BpackScrollHandler scrollHandler);
// Assigns an artifacts to an artifact place depending on it's new slot ID
virtual void setSlotData(ArtPlacePtr artPlace, const ArtifactPosition & slot, const CArtifactSet & artSet);
virtual void scrollBackpackForArtSet(int offset, const CArtifactSet & artSet);
};

View File

@ -0,0 +1,54 @@
/*
* CArtifactsOfHeroKingdom.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 "CArtifactsOfHeroKingdom.h"
#include "Buttons.h"
#include "../CPlayerInterface.h"
#include "../../CCallback.h"
CArtifactsOfHeroKingdom::CArtifactsOfHeroKingdom(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll)
{
artWorn = ArtWorn;
backpack = Backpack;
leftBackpackRoll = leftScroll;
rightBackpackRoll = rightScroll;
for(auto artPlace : artWorn)
{
artPlace.second->slot = artPlace.first;
artPlace.second->setArtifact(nullptr);
artPlace.second->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace.second->rightClickCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
}
for(auto artPlace : backpack)
{
artPlace->setArtifact(nullptr);
artPlace->leftClickCallback = std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1);
artPlace->rightClickCallback = std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1);
}
leftBackpackRoll->addCallback(std::bind(&CArtifactsOfHeroBase::scrollBackpack, this, -1));
rightBackpackRoll->addCallback(std::bind(&CArtifactsOfHeroBase::scrollBackpack, this, +1));
}
void CArtifactsOfHeroKingdom::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroKingdom::pickUpArtifact(CHeroArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero, artPlace.slot),
ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
}

View File

@ -0,0 +1,27 @@
/*
* CArtifactsOfHeroKingdom.h, 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
*
*/
#pragma once
#include "CArtifactsOfHeroBase.h"
VCMI_LIB_NAMESPACE_BEGIN
struct ArtifactLocation;
VCMI_LIB_NAMESPACE_END
class CArtifactsOfHeroKingdom : public CArtifactsOfHeroBase
{
public:
CArtifactsOfHeroKingdom(ArtPlaceMap ArtWorn, std::vector<ArtPlacePtr> Backpack,
std::shared_ptr<CButton> leftScroll, std::shared_ptr<CButton> rightScroll);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CHeroArtPlace & artPlace);
};

View File

@ -0,0 +1,35 @@
/*
* CArtifactsOfHeroMain.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 "CArtifactsOfHeroMain.h"
#include "../CPlayerInterface.h"
#include "../../CCallback.h"
CArtifactsOfHeroMain::CArtifactsOfHeroMain(const Point & position)
{
init(
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1),
position,
std::bind(&CArtifactsOfHeroBase::scrollBackpack, this, _1));
}
void CArtifactsOfHeroMain::swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc)
{
LOCPLINT->cb->swapArtifacts(srcLoc, dstLoc);
}
void CArtifactsOfHeroMain::pickUpArtifact(CHeroArtPlace & artPlace)
{
LOCPLINT->cb->swapArtifacts(ArtifactLocation(curHero, artPlace.slot),
ArtifactLocation(curHero, ArtifactPosition::TRANSITION_POS));
}

View File

@ -0,0 +1,26 @@
/*
* CArtifactsOfHeroMain.h, 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
*
*/
#pragma once
#include "CArtifactsOfHeroBase.h"
VCMI_LIB_NAMESPACE_BEGIN
struct ArtifactLocation;
VCMI_LIB_NAMESPACE_END
class CArtifactsOfHeroMain : public CArtifactsOfHeroBase
{
public:
CArtifactsOfHeroMain(const Point & position);
void swapArtifacts(const ArtifactLocation & srcLoc, const ArtifactLocation & dstLoc);
void pickUpArtifact(CHeroArtPlace & artPlace);
};

View File

@ -0,0 +1,41 @@
/*
* CArtifactsOfHeroMarket.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 "CArtifactsOfHeroMarket.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
CArtifactsOfHeroMarket::CArtifactsOfHeroMarket(const Point & position)
{
init(
std::bind(&CArtifactsOfHeroBase::leftClickArtPlace, this, _1),
std::bind(&CArtifactsOfHeroBase::rightClickArtPlace, this, _1),
position,
std::bind(&CArtifactsOfHeroMarket::scrollBackpack, this, _1));
};
void CArtifactsOfHeroMarket::scrollBackpack(int offset)
{
CArtifactsOfHeroBase::scrollBackpackForArtSet(offset, *curHero);
// We may have highlight on one of backpack artifacts
if(selectArtCallback)
{
for(auto & artPlace : backpack)
{
if(artPlace->isMarked())
{
selectArtCallback(artPlace.get());
break;
}
}
}
safeRedraw();
}

View File

@ -0,0 +1,21 @@
/*
* CArtifactsOfHeroMarket.h, 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
*
*/
#pragma once
#include "CArtifactsOfHeroBase.h"
class CArtifactsOfHeroMarket : public CArtifactsOfHeroBase
{
public:
std::function<void(CHeroArtPlace*)> selectArtCallback;
CArtifactsOfHeroMarket(const Point & position);
void scrollBackpack(int offset) override;
};

View File

@ -185,20 +185,17 @@ bool CGarrisonSlot::highlightOrDropArtifact()
bool artSelected = false;
if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt().get())) //dirty solution
{
std::shared_ptr<CArtifactsOfHero::SCommonPart> commonInfo = chw->getCommonPart();
const CArtifactInstance * art = nullptr;
if(commonInfo)
art = commonInfo->src.art;
const auto pickedArtInst = chw->getPickedArtifact();
if(art)
if(pickedArtInst)
{
const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
const auto * srcHero = chw->getHeroPickedArtifact();
artSelected = true;
if (myStack) // try dropping the artifact only if the slot isn't empty
{
ArtifactLocation src(srcHero, commonInfo->src.slotID);
ArtifactLocation src(srcHero, ArtifactPosition::TRANSITION_POS);
ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
if (art->canBePutAt(dst, true))
if(pickedArtInst->canBePutAt(dst, true))
{ //equip clicked stack
if(dst.getArt())
{

View File

@ -0,0 +1,363 @@
/*
* CWindowWithArtifacts.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 "CWindowWithArtifacts.h"
#include "../gui/CGuiHandler.h"
#include "../gui/CursorHandler.h"
#include "CComponent.h"
#include "../windows/CHeroWindow.h"
#include "../windows/CSpellWindow.h"
#include "../windows/GUIClasses.h"
#include "../CPlayerInterface.h"
#include "../CGameInfo.h"
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
void CWindowWithArtifacts::addSet(CArtifactsOfHeroPtr artSet)
{
artSets.emplace_back(artSet);
std::visit([this](auto artSetWeak)
{
auto artSet = artSetWeak.lock();
artSet->leftClickCallback = std::bind(&CWindowWithArtifacts::leftClickArtPlaceHero, this, _1, _2);
artSet->rightClickCallback = std::bind(&CWindowWithArtifacts::rightClickArtPlaceHero, this, _1, _2);
}, artSet);
}
const CGHeroInstance * CWindowWithArtifacts::getHeroPickedArtifact()
{
auto res = getState();
if(res.has_value())
return std::get<const CGHeroInstance*>(res.value());
else
return nullptr;
}
const CArtifactInstance * CWindowWithArtifacts::getPickedArtifact()
{
auto res = getState();
if(res.has_value())
return std::get<const CArtifactInstance*>(res.value());
else
return nullptr;
}
void CWindowWithArtifacts::leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
{
const auto artSetWeak = findAOHbyRef(artsInst);
assert(artSetWeak.has_value());
if(artPlace.isLocked())
return;
const auto checkSpecialArts = [](const CGHeroInstance * hero, CHeroArtPlace & artPlace) -> bool
{
if(artPlace.getArt()->getTypeId() == ArtifactID::SPELLBOOK)
{
GH.pushIntT<CSpellWindow>(hero, LOCPLINT, LOCPLINT->battleInt.get());
return false;
}
if(artPlace.getArt()->getTypeId() == ArtifactID::CATAPULT)
{
// The Catapult must be equipped
std::vector<std::shared_ptr<CComponent>> catapult(1, std::make_shared<CComponent>(CComponent::artifact, 3, 0));
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult);
return false;
}
return true;
};
std::visit(
[checkSpecialArts, this, &artPlace](auto artSetWeak) -> void
{
const auto artSetPtr = artSetWeak.lock();
// Hero(Main, Exchange) window, Kingdom window, Altar window left click handler
if constexpr(
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMain>> ||
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>> ||
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>>)
{
const auto pickedArtInst = getPickedArtifact();
const auto heroPickedArt = getHeroPickedArtifact();
const auto hero = artSetPtr->getHero();
if(pickedArtInst)
{
auto srcLoc = ArtifactLocation(heroPickedArt, ArtifactPosition::TRANSITION_POS);
auto dstLoc = ArtifactLocation(hero, artPlace.slot);
auto isTransferAllowed = false;
if(ArtifactUtils::isSlotBackpack(artPlace.slot))
{
if(pickedArtInst->artType->isBig())
{
// War machines cannot go to backpack
LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % pickedArtInst->artType->getNameTranslated()));
}
else
{
if(ArtifactUtils::isBackpackFreeSlots(heroPickedArt))
isTransferAllowed = true;
else
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
}
}
// Check if artifact transfer is possible
else if(pickedArtInst->canBePutAt(dstLoc, true) && (!artPlace.getArt() || hero->tempOwner == LOCPLINT->playerID))
{
isTransferAllowed = true;
}
if constexpr(std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
{
if(hero != heroPickedArt)
isTransferAllowed = false;
}
if(isTransferAllowed)
artSetPtr->swapArtifacts(srcLoc, dstLoc);
}
else
{
if(artPlace.getArt())
{
if(artSetPtr->getHero()->tempOwner == LOCPLINT->playerID)
{
if(checkSpecialArts(hero, artPlace))
artSetPtr->pickUpArtifact(artPlace);
}
else
{
for(const auto artSlot : ArtifactUtils::unmovableSlots())
if(artPlace.slot == artSlot)
{
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
break;
}
}
}
}
}
// Market window left click handler
else if constexpr(std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>>)
{
if(artSetPtr->selectArtCallback && artPlace.getArt())
{
if(artPlace.getArt()->artType->isTradable())
{
artSetPtr->unmarkSlots();
artPlace.selectSlot(true);
artSetPtr->selectArtCallback(&artPlace);
}
else
{
// This item can't be traded
LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]);
}
}
}
}, artSetWeak.value());
}
void CWindowWithArtifacts::rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace)
{
const auto artSetWeak = findAOHbyRef(artsInst);
assert(artSetWeak.has_value());
if(artPlace.isLocked())
return;
std::visit(
[&artPlace](auto artSetWeak) -> void
{
const auto artSetPtr = artSetWeak.lock();
// Hero(Main, Exchange) window, Kingdom window right click handler
if constexpr(
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMain>> ||
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
{
if(artPlace.getArt())
{
if(ArtifactUtilsClient::askToDisassemble(artSetPtr->getHero(), artPlace.slot))
{
return;
}
if(ArtifactUtilsClient::askToAssemble(artSetPtr->getHero(), artPlace.slot))
{
return;
}
if(artPlace.text.size())
artPlace.LRClickableAreaWTextComp::clickRight(boost::logic::tribool::true_value, false);
}
}
// Altar window, Market window right click handler
else if constexpr(
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroAltar>> ||
std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroMarket>>)
{
if(artPlace.getArt() && artPlace.text.size())
artPlace.LRClickableAreaWTextComp::clickRight(boost::logic::tribool::true_value, false);
}
}, artSetWeak.value());
}
void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation & artLoc)
{
updateSlots(artLoc.slot);
}
void CWindowWithArtifacts::artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw)
{
auto curState = getState();
if(!curState.has_value())
// Transition state. Nothing to do here. Just skip. Need to wait for final state.
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.
auto pickedArtInst = std::get<const CArtifactInstance*>(curState.value());
assert(srcLoc.isHolder(std::get<const CGHeroInstance*>(curState.value())));
assert(srcLoc.getArt() == pickedArtInst);
auto artifactMovedBody = [this, withRedraw, &srcLoc, &destLoc, &pickedArtInst](auto artSetWeak) -> void
{
auto artSetPtr = artSetWeak.lock();
if(artSetPtr)
{
const auto hero = artSetPtr->getHero();
if(artSetPtr->active)
{
if(pickedArtInst)
{
CCS->curh->dragAndDropCursor("artifact", pickedArtInst->artType->getIconIndex());
if(srcLoc.isHolder(hero) || !std::is_same_v<decltype(artSetWeak), std::weak_ptr<CArtifactsOfHeroKingdom>>)
artSetPtr->markPossibleSlots(pickedArtInst, hero->tempOwner == LOCPLINT->playerID);
}
else
{
artSetPtr->unmarkSlots();
CCS->curh->dragAndDropCursor(nullptr);
}
}
if(withRedraw)
{
artSetPtr->updateWornSlots();
artSetPtr->updateBackpackSlots();
// Update arts bonuses on window.
// TODO rework this part when CHeroWindow and CExchangeWindow are reworked
if(auto * chw = dynamic_cast<CHeroWindow*>(this))
{
chw->update(hero, true);
}
else if(auto * cew = dynamic_cast<CExchangeWindow*>(this))
{
cew->updateWidgets();
}
artSetPtr->safeRedraw();
}
// Make sure the status bar is updated so it does not display old text
if(destLoc.isHolder(hero))
{
if(auto artPlace = artSetPtr->getArtPlace(destLoc.slot))
artPlace->hover(true);
}
}
};
for(auto artSetWeak : artSets)
std::visit(artifactMovedBody, artSetWeak);
}
void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation & artLoc)
{
updateSlots(artLoc.slot);
}
void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation & artLoc)
{
updateSlots(artLoc.slot);
}
void CWindowWithArtifacts::updateSlots(const ArtifactPosition & slot)
{
auto updateSlotBody = [slot](auto artSetWeak) -> void
{
if(const auto artSetPtr = artSetWeak.lock())
{
if(ArtifactUtils::isSlotEquipment(slot))
artSetPtr->updateWornSlots();
else if(ArtifactUtils::isSlotBackpack(slot))
artSetPtr->updateBackpackSlots();
artSetPtr->safeRedraw();
}
};
for(auto artSetWeak : artSets)
std::visit(updateSlotBody, artSetWeak);
}
std::optional<std::tuple<const CGHeroInstance*, const CArtifactInstance*>> CWindowWithArtifacts::getState()
{
const CArtifactInstance * artInst = nullptr;
const CGHeroInstance * hero = nullptr;
size_t pickedCnt = 0;
auto getHeroArtBody = [&hero, &artInst, &pickedCnt](auto artSetWeak) -> void
{
auto artSetPtr = artSetWeak.lock();
if(artSetPtr)
{
if(const auto art = artSetPtr->getPickedArtifact())
{
artInst = art;
hero = artSetPtr->getHero();
pickedCnt += hero->artifactsTransitionPos.size();
}
}
};
for(auto artSetWeak : artSets)
std::visit(getHeroArtBody, artSetWeak);
// The state is possible when the hero has placed an artifact in the ArtifactPosition::TRANSITION_POS,
// and the previous artifact has not yet removed from the ArtifactPosition::TRANSITION_POS.
// This is a transitional state. Then return nullopt.
if(pickedCnt > 1)
return std::nullopt;
else
return std::make_tuple(hero, artInst);
}
std::optional<CWindowWithArtifacts::CArtifactsOfHeroPtr> CWindowWithArtifacts::findAOHbyRef(CArtifactsOfHeroBase & artsInst)
{
std::optional<CArtifactsOfHeroPtr> res;
auto findAOHBody = [&res, &artsInst](auto & artSetWeak) -> void
{
if(&artsInst == artSetWeak.lock().get())
res = artSetWeak;
};
for(auto artSetWeak : artSets)
{
std::visit(findAOHBody, artSetWeak);
if(res.has_value())
return res;
}
return res;
}

View File

@ -0,0 +1,44 @@
/*
* CWindowWithArtifacts.h, 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
*
*/
#pragma once
#include "CArtifactHolder.h"
#include "CArtifactsOfHeroMain.h"
#include "CArtifactsOfHeroKingdom.h"
#include "CArtifactsOfHeroAltar.h"
#include "CArtifactsOfHeroMarket.h"
class CWindowWithArtifacts : public CArtifactHolder
{
public:
using CArtifactsOfHeroPtr = std::variant<
std::weak_ptr<CArtifactsOfHeroMarket>,
std::weak_ptr<CArtifactsOfHeroAltar>,
std::weak_ptr<CArtifactsOfHeroKingdom>,
std::weak_ptr<CArtifactsOfHeroMain>>;
void addSet(CArtifactsOfHeroPtr artSet);
const CGHeroInstance * getHeroPickedArtifact();
const CArtifactInstance * getPickedArtifact();
void leftClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace);
void rightClickArtPlaceHero(CArtifactsOfHeroBase & artsInst, CHeroArtPlace & artPlace);
void artifactRemoved(const ArtifactLocation & artLoc) override;
void artifactMoved(const ArtifactLocation & srcLoc, const ArtifactLocation & destLoc, bool withRedraw) override;
void artifactDisassembled(const ArtifactLocation & artLoc) override;
void artifactAssembled(const ArtifactLocation & artLoc) override;
private:
std::vector<CArtifactsOfHeroPtr> artSets;
void updateSlots(const ArtifactPosition & slot);
std::optional<std::tuple<const CGHeroInstance*, const CArtifactInstance*>> getState();
std::optional<CArtifactsOfHeroPtr> findAOHbyRef(CArtifactsOfHeroBase & artsInst);
};

View File

@ -42,9 +42,10 @@ TConstBonusListPtr CHeroWithMaybePickedArtifact::getAllBonuses(const CSelector &
TConstBonusListPtr heroBonuses = hero->getAllBonuses(selector, limit, hero, cachingStr);
TConstBonusListPtr bonusesFromPickedUpArtifact;
std::shared_ptr<CArtifactsOfHero::SCommonPart> cp = cww->getCommonPart();
if(cp && cp->src.art && cp->src.valid() && cp->src.AOH && cp->src.AOH->getHero() == hero)
bonusesFromPickedUpArtifact = cp->src.art->getAllBonuses(selector, limit, hero);
const auto pickedArtInst = cww->getPickedArtifact();
if(pickedArtInst)
bonusesFromPickedUpArtifact = pickedArtInst->getAllBonuses(selector, limit, hero);
else
bonusesFromPickedUpArtifact = TBonusListPtr(new BonusList());
@ -244,7 +245,7 @@ void CHeroWindow::update(const CGHeroInstance * hero, bool redrawNeeded)
}
if(!arts)
{
arts = std::make_shared<CArtifactsOfHero>(Point(-65, -8), true);
arts = std::make_shared<CArtifactsOfHeroMain>(Point(-65, -8));
arts->setHero(curHero);
addSet(arts);
}
@ -354,25 +355,23 @@ void CHeroWindow::dismissCurrent()
void CHeroWindow::commanderWindow()
{
//bool artSelected = false;
std::shared_ptr<CArtifactsOfHero::SCommonPart> commonInfo = getCommonPart();
const auto pickedArtInst = getPickedArtifact();
const auto hero = getHeroPickedArtifact();
if(const CArtifactInstance *art = commonInfo->src.art)
if(pickedArtInst)
{
const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
//artSelected = true;
const auto freeSlot = ArtifactUtils::getArtAnyPosition(curHero->commander, art->artType->getId());
const auto freeSlot = ArtifactUtils::getArtAnyPosition(curHero->commander, pickedArtInst->getTypeId());
if(freeSlot < ArtifactPosition::COMMANDER_AFTER_LAST) //we don't want to put it in commander's backpack!
{
ArtifactLocation src(srcHero, commonInfo->src.slotID);
ArtifactLocation src(hero, ArtifactPosition::TRANSITION_POS);
ArtifactLocation dst(curHero->commander.get(), freeSlot);
if(art->canBePutAt(dst, true))
if(pickedArtInst->canBePutAt(dst, true))
{ //equip clicked stack
if(dst.getArt())
{
LOCPLINT->cb->swapArtifacts (dst, ArtifactLocation(srcHero,
ArtifactUtils::getArtBackpackPosition(srcHero, art->getTypeId())));
LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(hero,
ArtifactUtils::getArtBackpackPosition(hero, pickedArtInst->getTypeId())));
}
LOCPLINT->cb->swapArtifacts(src, dst);
}

View File

@ -10,7 +10,7 @@
#pragma once
#include "../../lib/HeroBonus.h"
#include "../widgets/CArtifactHolder.h"
#include "../widgets/CWindowWithArtifacts.h"
#include "../widgets/CGarrisonInt.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -25,7 +25,7 @@ class CHeroWindow;
class LClickableAreaHero;
class LRClickableAreaWText;
class LRClickableAreaWTextComp;
class CArtifactsOfHero;
class CArtifactsOfHeroMain;
class MoraleLuckBox;
class CToggleButton;
class CToggleGroup;
@ -105,7 +105,7 @@ class CHeroWindow : public CStatusbarWindow, public CGarrisonHolder, public CWin
std::shared_ptr<CToggleGroup> formations;
std::shared_ptr<CGarrisonInt> garr;
std::shared_ptr<CArtifactsOfHero> arts;
std::shared_ptr<CArtifactsOfHeroMain> arts;
std::vector<std::shared_ptr<CLabel>> labels;

View File

@ -826,7 +826,7 @@ public:
background = std::make_shared<CAnimImage>("OVSLOT", 4);
pos = background->pos;
for(int i=0; i<9; i++)
arts.push_back(std::make_shared<CHeroArtPlace>(Point(270+i*48, 65)));
arts.push_back(std::make_shared<CHeroArtPlace>(Point(269+i*48, 66)));
}
};
@ -846,7 +846,7 @@ public:
btnLeft = std::make_shared<CButton>(Point(269, 66), "HSBTNS3", CButton::tooltip(), 0);
btnRight = std::make_shared<CButton>(Point(675, 66), "HSBTNS5", CButton::tooltip(), 0);
for(int i=0; i<8; i++)
arts.push_back(std::make_shared<CHeroArtPlace>(Point(295+i*48, 65)));
arts.push_back(std::make_shared<CHeroArtPlace>(Point(294+i*48, 66)));
}
};
@ -872,7 +872,7 @@ CHeroItem::CHeroItem(const CGHeroInstance * Hero)
assert(arts1->arts.size() == 9);
assert(arts2->arts.size() == 9);
CArtifactsOfHero::ArtPlaceMap arts =
CArtifactsOfHeroMain::ArtPlaceMap arts =
{
{ArtifactPosition::HEAD, arts1->arts[0]},
{ArtifactPosition::SHOULDERS,arts1->arts[1]},
@ -896,7 +896,7 @@ CHeroItem::CHeroItem(const CGHeroInstance * Hero)
};
heroArts = std::make_shared<CArtifactsOfHero>(arts, backpack->arts, backpack->btnLeft, backpack->btnRight, true);
heroArts = std::make_shared<CArtifactsOfHeroKingdom>(arts, backpack->arts, backpack->btnLeft, backpack->btnRight);
heroArts->setHero(hero);
artsTabs = std::make_shared<CTabbedInt>(std::bind(&CHeroItem::onTabSelected, this, _1));

View File

@ -9,7 +9,7 @@
*/
#pragma once
#include "../widgets/CArtifactHolder.h"
#include "../widgets/CWindowWithArtifacts.h"
#include "../widgets/CGarrisonInt.h"
class CButton;
@ -309,7 +309,7 @@ class CHeroItem : public CIntObject, public CGarrisonHolder
std::shared_ptr<CIntObject> onTabSelected(size_t index);
public:
std::shared_ptr<CArtifactsOfHero> heroArts;
std::shared_ptr<CArtifactsOfHeroKingdom> heroArts;
void updateGarrisons() override;

View File

@ -179,24 +179,26 @@ void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
if(type == ARTIFACT_PLACEHOLDER)
{
CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
const auto pickedArtInst = aw->getPickedArtifact();
auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(aw->arts);
if(pickedArtInst)
{
aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this->shared_from_this(), movedArt);
artifactsOfHero->pickedArtMoveToAltar(ArtifactPosition::TRANSITION_POS);
aw->moveArtToAltar(this->shared_from_this(), pickedArtInst);
}
else if(const CArtifactInstance *art = getArtInstance())
{
aw->arts->commonInfo->src.AOH = aw->arts.get();
aw->arts->commonInfo->src.art = art;
aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
aw->arts->markPossibleSlots(art);
//aw->arts->commonInfo->dst.AOH = aw->arts;
CCS->curh->dragAndDropCursor("artifact", art->artType->getIconIndex());
aw->arts->artifactsOnAltar.erase(art);
const auto hero = artifactsOfHero->getHero();
const auto slot = hero->getSlotByInstance(art);
assert(slot != ArtifactPosition::PRE_FIRST);
LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slot),
ArtifactLocation(hero, ArtifactPosition::TRANSITION_POS));
artifactsOfHero->pickedArtFromSlot = slot;
artifactsOfHero->artifactsOnAltar.erase(art);
setID(-1);
subtitle.clear();
aw->deal->block(!aw->arts->artifactsOnAltar.size());
aw->deal->block(!artifactsOfHero->artifactsOnAltar.size());
}
aw->calcTotalExp();
@ -387,18 +389,25 @@ void CTradeWindow::initItems(bool Left)
}
else //ARTIFACT_EXP
{
xOffset = -363;
xOffset = -365;
yOffset = -12;
}
arts = std::make_shared<CArtifactsOfHero>(Point(xOffset, yOffset), true);
arts->recActions = 255-DISPOSE;
arts->setHero(hero);
arts->allowedAssembling = false;
addSet(arts);
if(mode == EMarketMode::ARTIFACT_RESOURCE)
arts->highlightModeCallback = std::bind(&CTradeWindow::artifactSelected, this, _1);
{
auto artifactsOfHero = std::make_shared<CArtifactsOfHeroMarket>(Point(xOffset, yOffset));
artifactsOfHero->selectArtCallback = std::bind(&CTradeWindow::artifactSelected, this, _1);
artifactsOfHero->setHero(hero);
addSet(artifactsOfHero);
arts = artifactsOfHero;
}
else
{
auto artifactsOfHero = std::make_shared<CArtifactsOfHeroAltar>(Point(xOffset, yOffset));
artifactsOfHero->setHero(hero);
addSet(artifactsOfHero);
arts = artifactsOfHero;
}
}
else
{
@ -630,8 +639,8 @@ void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
void CTradeWindow::artifactSelected(CHeroArtPlace *slot)
{
assert(mode == EMarketMode::ARTIFACT_RESOURCE);
items[1][0]->setArtInstance(slot->ourArt);
if(slot->ourArt && !slot->locked)
items[1][0]->setArtInstance(slot->getArt());
if(slot->getArt())
hLeft = items[1][0];
else
hLeft = nullptr;
@ -858,7 +867,7 @@ void CMarketplaceWindow::selectionChanged(bool side)
readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
arts->unmarkSlots(false);
arts->unmarkSlots();
if(readyToTrade)
{
@ -1250,13 +1259,15 @@ void CAltarWindow::makeDeal()
else
{
std::vector<ui32> positions;
for(const CArtifactInstance *art : arts->artifactsOnAltar) //sacrifice each artifact on the list
auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
for(const CArtifactInstance * art : artifactsOfHero->artifactsOnAltar)
{
positions.push_back(hero->getArtPos(art));
positions.push_back(hero->getSlotByInstance(art));
}
std::sort(positions.begin(), positions.end(), std::greater<>());
LOCPLINT->cb->trade(market->o, mode, positions, {}, {}, hero);
arts->artifactsOnAltar.clear();
artifactsOfHero->artifactsOnAltar.clear();
for(auto item : items[0])
{
@ -1264,7 +1275,6 @@ void CAltarWindow::makeDeal()
item->subtitle = "";
}
arts->commonInfo->reset();
//arts->scrollBackpack(0);
deal->block(true);
}
@ -1294,12 +1304,13 @@ void CAltarWindow::SacrificeAll()
}
else
{
for(auto i = hero->artifactsWorn.cbegin(); i != hero->artifactsWorn.cend(); i++)
auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
for(const auto & aw : artifactsOfHero->visibleArtSet.artifactsWorn)
{
if(!i->second.locked) //ignore locks from assembled artifacts
moveFromSlotToAltar(i->first, nullptr, i->second.artifact);
if(!aw.second.locked)
moveArtToAltar(nullptr, aw.second.artifact);
}
artifactsOfHero->updateWornSlots();
SacrificeBackpack();
}
redraw();
@ -1414,7 +1425,8 @@ void CAltarWindow::calcTotalExp()
}
else
{
for(const CArtifactInstance *art : arts->artifactsOnAltar)
auto artifactsOfHero = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
for(const CArtifactInstance * art : artifactsOfHero->artifactsOnAltar)
{
int dmp, valOfArt;
market->getOffer(art->artType->getId(), 0, dmp, valOfArt, mode);
@ -1458,21 +1470,12 @@ int CAltarWindow::firstFreeSlot()
void CAltarWindow::SacrificeBackpack()
{
std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
for (auto & elem : hero->artifactsInBackpack)
auto artsAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
while(!artsAltar->visibleArtSet.artifactsInBackpack.empty())
{
if(vstd::contains(toOmmit, elem.artifact))
{
toOmmit -= elem.artifact;
continue;
}
putOnAltar(nullptr, elem.artifact);
}
arts->scrollBackpack(0);
if(!putOnAltar(nullptr, artsAltar->visibleArtSet.artifactsInBackpack[0].artifact))
break;
};
calcTotalExp();
}
@ -1484,15 +1487,18 @@ void CAltarWindow::artifactPicked()
void CAltarWindow::showAll(SDL_Surface * to)
{
CTradeWindow::showAll(to);
if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
if(mode == EMarketMode::ARTIFACT_EXP && arts)
{
artIcon->setFrame(arts->commonInfo->src.art->artType->getIconIndex());
artIcon->showAll(to);
if(auto pickedArt = arts->getPickedArtifact())
{
artIcon->setFrame(pickedArt->artType->getIconIndex());
artIcon->showAll(to);
int dmp, val;
market->getOffer(arts->commonInfo->src.art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
val = static_cast<int>(hero->calculateXp(val));
printAtMiddleLoc(std::to_string(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
int dmp, val;
market->getOffer(pickedArt->getTypeId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
val = static_cast<int>(hero->calculateXp(val));
printAtMiddleLoc(std::to_string(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
}
}
}
@ -1519,7 +1525,9 @@ bool CAltarWindow::putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const C
market->getOffer(art->artType->getId(), 0, dmp, val, EMarketMode::ARTIFACT_EXP);
val = static_cast<int>(hero->calculateXp(val));
arts->artifactsOnAltar.insert(art);
auto artsAltar = std::dynamic_pointer_cast<CArtifactsOfHeroAltar>(arts);
artsAltar->artifactsOnAltar.insert(art);
artsAltar->deleteFromVisible(art);
altarSlot->setArtInstance(art);
altarSlot->subtitle = std::to_string(val);
@ -1527,25 +1535,11 @@ bool CAltarWindow::putOnAltar(std::shared_ptr<CTradeableItem> altarSlot, const C
return true;
}
void CAltarWindow::moveFromSlotToAltar(ArtifactPosition slotID, std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
void CAltarWindow::moveArtToAltar(std::shared_ptr<CTradeableItem> altarSlot, const CArtifactInstance *art)
{
auto freeBackpackSlot = ArtifactPosition((si32)hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
if(arts->commonInfo->src.art)
{
arts->commonInfo->dst.slotID = freeBackpackSlot;
arts->commonInfo->dst.AOH = arts.get();
}
if(putOnAltar(altarSlot, art))
{
if(slotID < GameConstants::BACKPACK_START)
LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
else
{
arts->commonInfo->src.clear();
arts->commonInfo->dst.clear();
CCS->curh->dragAndDropCursor(nullptr);
arts->unmarkSlots(false);
}
CCS->curh->dragAndDropCursor(nullptr);
arts->unmarkSlots();
}
}

View File

@ -9,7 +9,7 @@
*/
#pragma once
#include "../widgets/CArtifactHolder.h"
#include "../widgets/CWindowWithArtifacts.h"
#include "CWindowObject.h"
#include "../../lib/FunctionList.h"
@ -67,7 +67,7 @@ public:
const IMarket * market;
const CGHeroInstance * hero;
std::shared_ptr<CArtifactsOfHero> arts;
std::shared_ptr<CArtifactsOfHeroBase> arts;
//all indexes: 1 = left, 0 = right
std::array<std::vector<std::shared_ptr<CTradeableItem>>, 2> items;
@ -186,5 +186,5 @@ public:
void artifactPicked();
int firstFreeSlot();
void moveFromSlotToAltar(ArtifactPosition slotID, std::shared_ptr<CTradeableItem>, const CArtifactInstance * art);
void moveArtToAltar(std::shared_ptr<CTradeableItem>, const CArtifactInstance * art);
};

View File

@ -909,13 +909,9 @@ CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2,
portraits[0] = std::make_shared<CAnimImage>("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
portraits[1] = std::make_shared<CAnimImage>("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
artifs[0] = std::make_shared<CArtifactsOfHero>(Point(-334, 150));
artifs[0]->commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
artifs[0]->commonInfo->participants.insert(artifs[0].get());
artifs[0] = std::make_shared<CArtifactsOfHeroMain>(Point(-334, 150));
artifs[0]->setHero(heroInst[0]);
artifs[1] = std::make_shared<CArtifactsOfHero>(Point(96, 150));
artifs[1]->commonInfo = artifs[0]->commonInfo;
artifs[1]->commonInfo->participants.insert(artifs[1].get());
artifs[1] = std::make_shared<CArtifactsOfHeroMain>(Point(98, 150));
artifs[1]->setHero(heroInst[1]);
addSet(artifs[0]);

View File

@ -14,7 +14,7 @@
#include "../lib/ResourceSet.h"
#include "../lib/CConfigHandler.h"
#include "../lib/int3.h"
#include "../widgets/CArtifactHolder.h"
#include "../widgets/CWindowWithArtifacts.h"
#include "../widgets/CGarrisonInt.h"
#include "../widgets/Images.h"
@ -327,7 +327,7 @@ class CExchangeWindow : public CStatusbarWindow, public CGarrisonHolder, public
public:
std::array<const CGHeroInstance *, 2> heroInst;
std::array<std::shared_ptr<CArtifactsOfHero>, 2> artifs;
std::array<std::shared_ptr<CArtifactsOfHeroMain>, 2> artifs;
void updateGarrisons() override;

View File

@ -1206,6 +1206,25 @@ const CArtifactInstance * CArtifactSet::getArtByInstanceId(const ArtifactInstanc
return nullptr;
}
const ArtifactPosition CArtifactSet::getSlotByInstance(const CArtifactInstance * artInst) const
{
if(artInst)
{
for(auto & slot : artInst->artType->possibleSlots.at(bearerType()))
if(getArt(slot) == artInst)
return slot;
auto backpackSlot = GameConstants::BACKPACK_START;
for(auto & slotInfo : artifactsInBackpack)
{
if(slotInfo.getArt() == artInst)
return backpackSlot;
backpackSlot = ArtifactPosition(backpackSlot + 1);
}
}
return ArtifactPosition::PRE_FIRST;
}
bool CArtifactSet::hasArt(const ArtifactID & aid, bool onlyWorn, bool searchBackpackAssemblies, bool allowLocked) const
{
return getArtPosCount(aid, onlyWorn, searchBackpackAssemblies, allowLocked) > 0;

View File

@ -328,6 +328,7 @@ public:
std::vector<ArtifactPosition> getAllArtPositions(const ArtifactID & aid, bool onlyWorn, bool allowLocked, bool getAll) const;
std::vector<ArtifactPosition> getBackpackArtPositions(const ArtifactID & aid) const;
const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
const ArtifactPosition getSlotByInstance(const CArtifactInstance * artInst) const;
/// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
const CArtifactInstance * getHiddenArt(const ArtifactID & aid) const;
const CCombinedArtifactInstance * getAssemblyByConstituent(const ArtifactID & aid) const;

View File

@ -3902,7 +3902,7 @@ bool CGameHandler::moveArtifact(const ArtifactLocation &al1, const ArtifactLocat
if(srcArtifact == nullptr)
COMPLAIN_RET("No artifact to move!");
if(destArtifact && srcPlayer != dstPlayer)
if(destArtifact && srcPlayer != dstPlayer && !isDstSlotBackpack)
COMPLAIN_RET("Can't touch artifact on hero of another player!");
// Check if src/dest slots are appropriate for the artifacts exchanged.