diff --git a/CCallback.cpp b/CCallback.cpp index 45d8ff291..b7b9a7bdd 100644 --- a/CCallback.cpp +++ b/CCallback.cpp @@ -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(town); diff --git a/client/GUIClasses.cpp b/client/GUIClasses.cpp index d774b2f80..5fc4b0403 100644 --- a/client/GUIClasses.cpp +++ b/client/GUIClasses.cpp @@ -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(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(GH.topInt()); + if (chw != NULL) { + chw->deactivate(); + chw->setHero(hero); + chw->activate(); + } else { + CExchangeWindow* cew = dynamic_cast(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; gheroInst); ++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(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(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; diff --git a/hch/CObjectHandler.cpp b/hch/CObjectHandler.cpp index 3437ecc72..105b94437 100644 --- a/hch/CObjectHandler.cpp +++ b/hch/CObjectHandler.cpp @@ -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::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++) { CArtifact &art = VLC->arth->artifacts[ari->second]; diff --git a/lib/NetPacks.h b/lib/NetPacks.h index 2e5a73da3..e403f49ef 100644 --- a/lib/NetPacks.h +++ b/lib/NetPacks.h @@ -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 void serialize(Handler &h, const int version) - { - h & hid & slot & artID; - } -}; - struct BuyArtifact : public CPackForServer { BuyArtifact(){}; diff --git a/lib/RegisterTypes.cpp b/lib/RegisterTypes.cpp index 5eabc2499..d2f3b7afd 100644 --- a/lib/RegisterTypes.cpp +++ b/lib/RegisterTypes.cpp @@ -139,7 +139,6 @@ void registerTypes3(Serializer &s) s.template registerType(); s.template registerType(); s.template registerType(); - s.template registerType(); s.template registerType(); s.template registerType(); s.template registerType(); diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 1d6ccf4dc..665dace31 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -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); diff --git a/server/CGameHandler.h b/server/CGameHandler.h index f3b4148d2..bfed22c41 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -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); diff --git a/server/NetPacksServer.cpp b/server/NetPacksServer.cpp index 35f3e3ade..6d39fbc99 100644 --- a/server/NetPacksServer.cpp +++ b/server/NetPacksServer.cpp @@ -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);