2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CArtifactHolder.h"
|
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-04-27 19:21:06 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "CComponent.h"
|
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../windows/GUIClasses.h"
|
2023-06-02 15:42:18 +02:00
|
|
|
#include "../render/Canvas.h"
|
|
|
|
#include "../render/Colors.h"
|
2023-09-25 22:58:59 +02:00
|
|
|
#include "../render/IRenderHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
2023-05-17 15:52:16 +02:00
|
|
|
#include "../../lib/ArtifactUtils.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2023-10-23 15:38:05 +02:00
|
|
|
#include "../../lib/networkPacks/ArtifactLocation.h"
|
2023-09-25 22:58:59 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CArtPlace::setInternals(const CArtifactInstance * artInst)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
ourArt = artInst;
|
|
|
|
if(!artInst)
|
|
|
|
{
|
|
|
|
image->disable();
|
|
|
|
text.clear();
|
|
|
|
hoverText = CGI->generaltexth->allTexts[507];
|
|
|
|
return;
|
|
|
|
}
|
2023-09-25 22:58:59 +02:00
|
|
|
|
2023-10-19 20:22:26 +02:00
|
|
|
imageIndex = artInst->artType->getIconIndex();
|
2023-04-23 13:09:49 +02:00
|
|
|
if(artInst->getTypeId() == ArtifactID::SPELL_SCROLL)
|
|
|
|
{
|
|
|
|
auto spellID = artInst->getScrollSpellID();
|
2023-10-19 20:22:26 +02:00
|
|
|
assert(spellID.num >= 0);
|
2023-09-25 22:58:59 +02:00
|
|
|
|
2023-10-19 20:22:26 +02:00
|
|
|
if(settings["general"]["enableUiEnhancements"].Bool())
|
|
|
|
{
|
|
|
|
imageIndex = spellID.num;
|
|
|
|
if(baseType != CComponent::spell)
|
2023-09-25 22:58:59 +02:00
|
|
|
{
|
2023-10-19 20:22:26 +02:00
|
|
|
image->setScale(Point(pos.w, 34));
|
|
|
|
image->setAnimationPath(AnimationPath::builtin("spellscr"), imageIndex);
|
|
|
|
image->moveTo(Point(pos.x, pos.y + 4));
|
2023-09-25 22:58:59 +02:00
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
2023-10-19 20:22:26 +02:00
|
|
|
// Add spell component info (used to provide a pic in r-click popup)
|
|
|
|
baseType = CComponent::spell;
|
|
|
|
type = spellID;
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-19 20:22:26 +02:00
|
|
|
if(settings["general"]["enableUiEnhancements"].Bool() && baseType != CComponent::artifact)
|
|
|
|
{
|
|
|
|
image->setScale(Point());
|
|
|
|
image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
|
|
|
|
image->moveTo(Point(pos.x, pos.y));
|
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
baseType = CComponent::artifact;
|
|
|
|
type = artInst->getTypeId();
|
|
|
|
}
|
2023-10-19 20:22:26 +02:00
|
|
|
bonusValue = 0;
|
|
|
|
image->enable();
|
2023-04-23 13:09:49 +02:00
|
|
|
text = artInst->getDescription();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art)
|
|
|
|
: ourArt(Art)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
image = nullptr;
|
|
|
|
pos += position;
|
|
|
|
pos.w = pos.h = 44;
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
const CArtifactInstance * CArtPlace::getArt()
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
return ourArt;
|
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot, const CArtifactInstance * Art)
|
|
|
|
: CArtPlace(position, Art),
|
|
|
|
commanderOwner(commanderOwner),
|
|
|
|
commanderSlotID(artSlot.num)
|
|
|
|
{
|
|
|
|
createImage();
|
|
|
|
setArtifact(Art);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CCommanderArtPlace::createImage()
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-10-19 20:22:26 +02:00
|
|
|
imageIndex = 0;
|
2023-04-23 13:09:49 +02:00
|
|
|
if(ourArt)
|
|
|
|
imageIndex = ourArt->artType->getIconIndex();
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
|
2023-04-23 13:09:49 +02:00
|
|
|
if(!ourArt)
|
|
|
|
image->disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCommanderArtPlace::returnArtToHeroCallback()
|
|
|
|
{
|
|
|
|
ArtifactPosition artifactPos = commanderSlotID;
|
|
|
|
ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, ourArt->getTypeId());
|
|
|
|
if(freeSlot == ArtifactPosition::PRE_FIRST)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
else
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-10-14 21:00:39 +02:00
|
|
|
ArtifactLocation src(commanderOwner->id, artifactPos);
|
|
|
|
ArtifactLocation dst(commanderOwner->id, freeSlot);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-10-14 21:00:39 +02:00
|
|
|
if(ourArt->canBePutAt(commanderOwner, freeSlot, true))
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
LOCPLINT->cb->swapArtifacts(src, dst);
|
|
|
|
setArtifact(nullptr);
|
|
|
|
parent->redraw();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
|
2016-01-23 14:20:51 +02:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
if(ourArt && text.size())
|
2023-04-23 13:09:49 +02:00
|
|
|
LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
|
|
|
|
}
|
2016-01-23 14:20:51 +02:00
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
2023-06-11 17:20:10 +02:00
|
|
|
if(ourArt && text.size())
|
2023-07-08 13:33:04 +02:00
|
|
|
CArtPlace::showPopupWindow(cursorPosition);
|
2016-01-23 14:20:51 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CCommanderArtPlace::setArtifact(const CArtifactInstance * art)
|
2023-03-18 12:44:01 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
setInternals(art);
|
|
|
|
}
|
2023-03-18 12:44:01 +02:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
CHeroArtPlace::CHeroArtPlace(Point position, const CArtifactInstance * Art)
|
|
|
|
: CArtPlace(position, Art),
|
|
|
|
locked(false),
|
|
|
|
marked(false)
|
|
|
|
{
|
|
|
|
createImage();
|
2023-03-18 12:44:01 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CHeroArtPlace::lockSlot(bool on)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
if(locked == on)
|
|
|
|
return;
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
locked = on;
|
|
|
|
|
|
|
|
if(on)
|
|
|
|
image->setFrame(ArtifactID::ART_LOCK);
|
|
|
|
else if(ourArt)
|
2023-10-19 20:22:26 +02:00
|
|
|
image->setFrame(imageIndex);
|
2023-04-23 13:09:49 +02:00
|
|
|
else
|
|
|
|
image->setFrame(0);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
bool CHeroArtPlace::isLocked()
|
2022-12-18 14:34:38 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
return locked;
|
2022-12-18 14:34:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CHeroArtPlace::selectSlot(bool on)
|
2022-12-18 14:34:38 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
if(marked == on)
|
|
|
|
return;
|
2022-12-18 14:34:38 +02:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
marked = on;
|
|
|
|
if(on)
|
|
|
|
selection->enable();
|
|
|
|
else
|
|
|
|
selection->disable();
|
2022-12-18 14:34:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
bool CHeroArtPlace::isMarked() const
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
return marked;
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CHeroArtPlace::clickPressed(const Point & cursorPosition)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
|
|
|
if(leftClickCallback)
|
|
|
|
leftClickCallback(*this);
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CHeroArtPlace::showPopupWindow(const Point & cursorPosition)
|
2023-04-23 13:09:49 +02:00
|
|
|
{
|
2023-09-17 17:40:14 +02:00
|
|
|
if(showPopupCallback)
|
|
|
|
showPopupCallback(*this);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CHeroArtPlace::showAll(Canvas & to)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
if(ourArt)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
CIntObject::showAll(to);
|
|
|
|
}
|
|
|
|
|
2023-05-17 22:22:45 +02:00
|
|
|
if(marked && isActive())
|
2023-06-02 15:42:18 +02:00
|
|
|
to.drawBorder(pos, Colors::BRIGHT_YELLOW);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CHeroArtPlace::setArtifact(const CArtifactInstance * art)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
setInternals(art);
|
|
|
|
if(art)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-10-19 20:22:26 +02:00
|
|
|
image->setFrame(locked ? static_cast<int>(ArtifactID::ART_LOCK) : imageIndex);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
else
|
2023-04-13 19:40:36 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
lockSlot(false);
|
2023-04-13 19:40:36 +02:00
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CHeroArtPlace::addCombinedArtInfo(std::map<const CArtifact*, int> & arts)
|
|
|
|
{
|
2023-05-01 13:45:32 +02:00
|
|
|
for(const auto & combinedArt : arts)
|
2016-01-21 19:23:45 +02:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
std::string artList;
|
|
|
|
text += "\n\n";
|
|
|
|
text += "{" + combinedArt.first->getNameTranslated() + "}";
|
|
|
|
if(arts.size() == 1)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-07-03 18:15:40 +02:00
|
|
|
for(const auto part : combinedArt.first->getConstituents())
|
2023-04-23 13:09:49 +02:00
|
|
|
artList += "\n" + part->getNameTranslated();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
2023-04-23 13:09:49 +02:00
|
|
|
text += " (" + boost::str(boost::format("%d") % combinedArt.second) + " / " +
|
2023-07-03 18:15:40 +02:00
|
|
|
boost::str(boost::format("%d") % combinedArt.first->getConstituents().size()) + ")" + artList;
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
void CHeroArtPlace::createImage()
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
2023-04-23 13:09:49 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
si32 imageIndex = 0;
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2023-04-23 13:09:49 +02:00
|
|
|
if(locked)
|
|
|
|
imageIndex = ArtifactID::ART_LOCK;
|
|
|
|
else if(ourArt)
|
|
|
|
imageIndex = ourArt->artType->getIconIndex();
|
2016-01-21 09:49:46 +02:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), imageIndex);
|
2023-10-19 20:22:26 +02:00
|
|
|
image->disable();
|
2016-01-21 09:49:46 +02:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
selection = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), ArtifactID::ART_SELECTION);
|
2023-04-23 13:09:49 +02:00
|
|
|
selection->disable();
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 14:10:35 +02:00
|
|
|
bool ArtifactUtilsClient::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
|
2023-04-23 13:17:27 +02:00
|
|
|
{
|
|
|
|
assert(hero);
|
|
|
|
const auto art = hero->getArt(slot);
|
|
|
|
assert(art);
|
|
|
|
|
2023-09-03 20:43:41 +02:00
|
|
|
if(hero->tempOwner != LOCPLINT->playerID)
|
|
|
|
return false;
|
2023-04-23 13:17:27 +02:00
|
|
|
|
2023-09-12 17:30:48 +02:00
|
|
|
auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId());
|
2023-09-03 20:43:41 +02:00
|
|
|
if(!assemblyPossibilities.empty())
|
|
|
|
{
|
|
|
|
auto askThread = new boost::thread([hero, art, slot, assemblyPossibilities]() -> void
|
|
|
|
{
|
2023-09-27 17:44:08 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-09-03 20:43:41 +02:00
|
|
|
for(const auto combinedArt : assemblyPossibilities)
|
|
|
|
{
|
|
|
|
bool assembleConfirmed = false;
|
|
|
|
CFunctionList<void()> onYesHandlers([&assembleConfirmed]() -> void {assembleConfirmed = true; });
|
|
|
|
onYesHandlers += std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, true, combinedArt->getId());
|
|
|
|
|
|
|
|
LOCPLINT->showArtifactAssemblyDialog(art->artType, combinedArt, onYesHandlers);
|
2023-09-27 17:44:08 +02:00
|
|
|
LOCPLINT->waitWhileDialog();
|
2023-09-03 20:43:41 +02:00
|
|
|
if(assembleConfirmed)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
askThread->detach();
|
2023-04-23 13:17:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 14:10:35 +02:00
|
|
|
bool ArtifactUtilsClient::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
|
2023-04-23 13:17:27 +02:00
|
|
|
{
|
|
|
|
assert(hero);
|
|
|
|
const auto art = hero->getArt(slot);
|
|
|
|
assert(art);
|
|
|
|
|
2023-09-03 20:43:41 +02:00
|
|
|
if(hero->tempOwner != LOCPLINT->playerID)
|
|
|
|
return false;
|
|
|
|
|
2023-06-29 17:34:07 +02:00
|
|
|
if(art->isCombined())
|
2023-04-23 13:17:27 +02:00
|
|
|
{
|
2023-07-03 18:15:40 +02:00
|
|
|
if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->artType->getConstituents().size() - 1))
|
2023-04-23 13:17:27 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
LOCPLINT->showArtifactAssemblyDialog(
|
|
|
|
art->artType,
|
|
|
|
nullptr,
|
|
|
|
std::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), hero, slot, false, ArtifactID()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|