1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Fixed artifact screens so only local changes are made. Some ugly const casting, for now.

This commit is contained in:
OnionKnight 2010-02-05 22:28:31 +00:00
parent 5fe80d89ef
commit 137ab65a13
8 changed files with 95 additions and 127 deletions

View File

@ -433,16 +433,6 @@ bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGH
return true;
}
bool CCallback::setArtifact(const CGHeroInstance * hero, ui16 pos, int artID)
{
if(player != hero->tempOwner)
return false;
SetArtifact sa(hero->id, pos, artID);
sendRequest(&sa);
return true;
}
bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
{
CGTownInstance * t = const_cast<CGTownInstance *>(town);

View File

@ -3613,7 +3613,7 @@ void CArtPlace::clickLeft(tribool down, bool previousState)
}
else //perform artifact substitution
{
if (slotID >= 19) // Backpack slot - Remove active artifact and insert it into the designated position in backpack.
if (slotID >= 19) // Backpack destination.
{
const CArtifact * cur = ourOwner->commonInfo->srcArtifact;
@ -3635,10 +3635,17 @@ void CArtPlace::clickLeft(tribool down, bool previousState)
ourOwner->commonInfo->destSlotID = slotID;
ourOwner->commonInfo->destArtifact = NULL;
LOCPLINT->cb->setArtifact(
// Correction for backpack position when src lies before dest.
ourOwner->commonInfo->destSlotID +=
(ourOwner->commonInfo->srcAOH == ourOwner
&& ourOwner->commonInfo->srcSlotID >= 19
&& ourOwner->commonInfo->srcSlotID <= slotID);
LOCPLINT->cb->swapArtifacts(
ourOwner->commonInfo->srcAOH->curHero,
ourOwner->commonInfo->srcSlotID,
ourOwner->curHero,
slotID,
ourOwner->commonInfo->srcArtifact->id);
ourOwner->commonInfo->destSlotID);
break;
}
@ -3650,10 +3657,11 @@ void CArtPlace::clickLeft(tribool down, bool previousState)
ourOwner->commonInfo->destSlotID = slotID;
ourOwner->commonInfo->destArtifact = ourArt;
LOCPLINT->cb->setArtifact(
ourOwner->curHero,
slotID,
ourOwner->commonInfo->srcArtifact->id);
LOCPLINT->cb->swapArtifacts(
ourOwner->commonInfo->srcAOH->curHero,
ourOwner->commonInfo->srcSlotID,
ourOwner->curHero,
slotID);
}
}
}
@ -3684,13 +3692,41 @@ void CArtPlace::select ()
ourOwner->commonInfo->srcSlotID = slotID;
ourOwner->commonInfo->srcAOH = ourOwner;
// Temporarily remove artifact from hero.
CGHeroInstance* hero = const_cast<CGHeroInstance*>(ourOwner->curHero);
if (slotID < 19)
hero->artifWorn.erase(slotID);
else
hero->artifacts.erase(hero->artifacts.begin() + (slotID - 19));
hero->recreateArtBonuses();
// Update the hero bonuses.
CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt());
if (chw != NULL) {
chw->deactivate();
chw->setHero(hero);
chw->activate();
} else {
CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
assert(cew); // Either an exchange- or hero window should be active if an artifact slot is selected.
cew->deactivate();
for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
{
if(cew->heroInst[g] == hero)
{
cew->artifs[g]->setHero(hero);
}
}
cew->prepareBackground();
cew->activate();
}
if (slotID >= 19) {
// Correcting position in backpack.
ourOwner->scrollBackpack(-(slotID - 19 < ourOwner->backpackPos));
} else {
ourOwner->eraseSlotData(this, slotID);
}
LOCPLINT->cb->setArtifact(ourOwner->curHero, slotID, -1);
}
/**
@ -3952,32 +3988,42 @@ void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
backpackPos++;
}
// A swap was made, make the replaced artifact into current selected.
if (commonInfo->destSlotID < 19 && commonInfo->destArtifact) {
// Source <- Dest
commonInfo->srcAOH = commonInfo->destAOH;
commonInfo->srcArtifact = commonInfo->destArtifact;
commonInfo->srcSlotID = -1; // The artifact's original place is taken now.
if (commonInfo->srcAOH == this) {
// A swap was made, make the replaced artifact the current selected.
if (commonInfo->destSlotID < 19 && commonInfo->destArtifact) {
// Temporarily remove artifact from hero.
CGHeroInstance * hero = const_cast<CGHeroInstance *>(curHero);
if (commonInfo->srcSlotID < 19)
hero->artifWorn.erase(commonInfo->srcSlotID);
else
hero->artifacts.erase(hero->artifacts.begin() + (commonInfo->srcSlotID - 19));
hero->recreateArtBonuses();
// Reset destination parameters.
commonInfo->destAOH = NULL;
commonInfo->destArtifact = NULL;
commonInfo->destSlotID = -1;
// Source <- Dest
//commonInfo->srcAOH = commonInfo->destAOH;
commonInfo->srcArtifact = commonInfo->destArtifact;
//commonInfo->srcSlotID = commonInfo->destSlotID;
CGI->curh->dragAndDropCursor(
graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
markPossibleSlots(commonInfo->srcArtifact);
} else if (commonInfo->destAOH != NULL) {
// Reset all parameters.
commonInfo->srcAOH = NULL;
commonInfo->srcArtifact = NULL;
commonInfo->srcSlotID = -1;
commonInfo->destAOH = NULL;
commonInfo->destArtifact = NULL;
commonInfo->destSlotID = -1;
// Reset destination parameters.
commonInfo->destAOH = NULL;
commonInfo->destArtifact = NULL;
commonInfo->destSlotID = -1;
CGI->curh->dragAndDropCursor(NULL);
unmarkSlots();
CGI->curh->dragAndDropCursor(
graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
markPossibleSlots(commonInfo->srcArtifact);
} else if (commonInfo->destAOH != NULL) {
// Reset all parameters.
commonInfo->srcAOH = NULL;
commonInfo->srcArtifact = NULL;
commonInfo->srcSlotID = -1;
commonInfo->destAOH = NULL;
commonInfo->destArtifact = NULL;
commonInfo->destSlotID = -1;
CGI->curh->dragAndDropCursor(NULL);
unmarkSlots();
}
}
} else {
rollback();
@ -4007,42 +4053,37 @@ void CArtifactsOfHero::rollback()
{
if (curHero != NULL) {
// Restore any held artifact to it's original position.
if (commonInfo->destArtifact && commonInfo->destAOH == this) {
// For an unlikely race condition scenario, put swapped artifact into backpack.
LOCPLINT->cb->setArtifact(
curHero,
19 + curHero->artifacts.size(),
commonInfo->destArtifact->id);
}
else if (commonInfo->srcArtifact && commonInfo->srcAOH == this) {
if (commonInfo->srcSlotID != -1) { // Held artifact, just put it back to it's spot.
LOCPLINT->cb->setArtifact(
curHero,
commonInfo->srcSlotID,
commonInfo->srcArtifact->id);
} else { // Swapped artifact.
if (commonInfo->srcArtifact && commonInfo->srcAOH == this) {
CGHeroInstance * hero = const_cast<CGHeroInstance *>(curHero);
if (commonInfo->srcSlotID != -1) {
// Put a held artifact back to it's spot.
if (commonInfo->srcSlotID < 19)
hero->artifWorn[commonInfo->srcSlotID] = commonInfo->srcArtifact->id;
else
hero->artifacts.insert(hero->artifacts.begin() + (commonInfo->srcSlotID - 19), commonInfo->srcArtifact->id);
} else { // Held swapped artifact.
// Wear the artifact in a suitable spot.
ui16 i = 0;
for (; i < 19; i++) {
if (artWorn[i]->fitsHere(commonInfo->srcArtifact)
&& curHero->artifWorn.find(i) == curHero->artifWorn.end())
{
LOCPLINT->cb->setArtifact(
curHero, i, commonInfo->srcArtifact->id);
hero->artifWorn[i] = commonInfo->srcArtifact->id;
break;
}
}
// If it can't be worn, put it in the backpack.
if (i == 19)
LOCPLINT->cb->setArtifact(
curHero,
19 + curHero->artifacts.size(),
commonInfo->srcArtifact->id);
hero->artifacts.push_back(commonInfo->srcArtifact->id);
}
hero->recreateArtBonuses();
}
}
unmarkSlots();
unmarkSlots();
backpackPos = 0;
commonInfo->srcAOH = NULL;

View File

@ -1225,7 +1225,7 @@ void CGHeroInstance::giveArtifact (ui32 aid)
void CGHeroInstance::recreateArtBonuses()
{
//clear all bonuses from artifacts (if present) and give them again
std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
bonuses.remove_if(boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
{
CArtifact &art = VLC->arth->artifacts[ari->second];

View File

@ -1249,22 +1249,6 @@ struct ExchangeArtifacts : public CPackForServer
}
};
struct SetArtifact : public CPackForServer
{
SetArtifact () {};
SetArtifact (si32 hid, ui16 slot, int artID)
:hid(hid), slot(slot), artID(artID) {};
si32 hid;
ui16 slot;
int artID;
bool applyGh(CGameHandler *gh);
template <typename Handler> void serialize(Handler &h, const int version)
{
h & hid & slot & artID;
}
};
struct BuyArtifact : public CPackForServer
{
BuyArtifact(){};

View File

@ -139,7 +139,6 @@ void registerTypes3(Serializer &s)
s.template registerType<UpgradeCreature>();
s.template registerType<GarrisonHeroSwap>();
s.template registerType<ExchangeArtifacts>();
s.template registerType<SetArtifact>();
s.template registerType<BuyArtifact>();
s.template registerType<TradeOnMarketplace>();
s.template registerType<SetFormation>();

View File

@ -2522,45 +2522,6 @@ bool CGameHandler::swapArtifacts(si32 srcHeroID, si32 destHeroID, ui16 srcSlot,
return true;
}
/**
* Sets a hero artifact slot to contain a specific artifact.
*
* @param artID ID of an artifact or -1 for no artifact.
*/
bool CGameHandler::setArtifact(si32 heroID, ui16 slot, int artID)
{
const CGHeroInstance *hero = gs->getHero(heroID);
if (artID != -1) {
const CArtifact &artifact = VLC->arth->artifacts[artID];
if (slot < 19 && !vstd::contains(artifact.possibleSlots, slot)) {
complain("Artifact does not fit!");
return false;
}
if (slot >= 19 && artifact.isBig()) {
complain("Cannot put big artifacts in backpack!");
return false;
}
}
if (slot == 16) {
complain("Cannot alter catapult slot!");
return false;
}
// Perform the exchange.
SetHeroArtifacts sha;
sha.hid = heroID;
sha.artifacts = hero->artifacts;
sha.artifWorn = hero->artifWorn;
sha.setArtAtPos(slot, artID);
sendAndApply(&sha);
return true;
}
bool CGameHandler::buyArtifact( ui32 hid, si32 aid )
{
CGHeroInstance *hero = gs->getHero(hid);

View File

@ -163,7 +163,6 @@ public:
bool tradeResources( ui32 val, ui8 player, ui32 id1, ui32 id2 );
bool buyArtifact( ui32 hid, si32 aid );
bool swapArtifacts(si32 srcHeroID, si32 destHeroID, ui16 srcSlot, ui16 destSlot);
bool setArtifact(si32 heroID, ui16 slot, int artID);
bool garrisonSwap(si32 tid);
bool upgradeCreature( ui32 objid, ui8 pos, ui32 upgID );
bool recruitCreatures(si32 objid, ui32 crid, ui32 cram);

View File

@ -98,12 +98,6 @@ bool ExchangeArtifacts::applyGh( CGameHandler *gh )
return gh->swapArtifacts(hid1,hid2,slot1,slot2);
}
bool SetArtifact::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->setArtifact(hid, slot, artID);
}
bool BuyArtifact::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);