1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Some code cleanup.

This commit is contained in:
OnionKnight 2010-03-02 23:40:13 +00:00
parent eec43d3c58
commit 38004fc45c
5 changed files with 27 additions and 33 deletions

View File

@ -3661,8 +3661,7 @@ void CArtPlace::clickLeft(tribool down, bool previousState)
ourOwner->commonInfo->destArtifact = ourArt;
// Special case when the dest artifact can't be fit into the src slot.
CGHeroInstance *destHero = const_cast<CGHeroInstance *>(ourOwner->curHero);
CGI->arth->unequipArtifact(destHero->artifWorn, slotID);
CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->srcAOH;
ui16 srcSlotID = ourOwner->commonInfo->srcSlotID;
if (ourArt && srcSlotID < 19 && !ourArt->fitsAt(srcAOH->curHero->artifWorn, srcSlotID)) {
@ -3753,19 +3752,18 @@ void CArtPlace::select ()
ourOwner->commonInfo->srcAOH = ourOwner;
// Temporarily remove artifact from hero.
CGHeroInstance* hero = const_cast<CGHeroInstance*>(ourOwner->curHero);
if (slotID < 19)
CGI->arth->unequipArtifact(hero->artifWorn, slotID);
CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
else
hero->artifacts.erase(hero->artifacts.begin() + (slotID - 19));
ourOwner->curHero->artifacts.erase(ourOwner->curHero->artifacts.begin() + (slotID - 19));
ourOwner->markPossibleSlots(ourArt);
hero->recreateArtBonuses();
ourOwner->curHero->recreateArtBonuses();
// Update the hero bonuses.
CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt());
if (chw != NULL) {
chw->deactivate();
chw->setHero(hero);
chw->setHero(ourOwner->curHero);
chw->activate();
} else {
CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
@ -3773,9 +3771,9 @@ void CArtPlace::select ()
cew->deactivate();
for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
{
if(cew->heroInst[g] == hero)
if(cew->heroInst[g] == ourOwner->curHero)
{
cew->artifs[g]->setHero(hero);
cew->artifs[g]->setHero(ourOwner->curHero);
}
}
cew->prepareBackground();
@ -4043,6 +4041,8 @@ void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
{
// An update is made, rather than initialization.
if (curHero == hero) {
curHero = const_cast<CGHeroInstance *>(hero);
// Compensate backpack pos if an artifact was insertad before it.
if (commonInfo->destSlotID >= 19 && commonInfo->destAOH == this
&& commonInfo->destSlotID - 19 < backpackPos)
@ -4051,16 +4051,14 @@ void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
}
if (updateState && commonInfo->srcAOH == this) {
curHero = hero;
// A swap was made, make the replaced artifact the current selected.
if (commonInfo->destSlotID < 19 && commonInfo->destArtifact) {
// Temporarily remove artifact from hero.
CGHeroInstance * nonconstCurHero = const_cast<CGHeroInstance *>(curHero);
if (commonInfo->srcSlotID < 19)
CGI->arth->unequipArtifact(nonconstCurHero->artifWorn, commonInfo->srcSlotID);
CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
else
nonconstCurHero->artifacts.erase(nonconstCurHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
nonconstCurHero->recreateArtBonuses();
curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
curHero->recreateArtBonuses();
// Source <- Dest
commonInfo->srcArtifact = commonInfo->destArtifact;
@ -4090,7 +4088,7 @@ void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
rollback();
}
curHero = hero;
curHero = const_cast<CGHeroInstance *>(hero);
if (curHero->artifacts.size() > 0)
backpackPos %= curHero->artifacts.size();
@ -4115,32 +4113,28 @@ void CArtifactsOfHero::rollback()
if (curHero != NULL) {
// Restore any held artifact to it's original position.
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)
CGI->arth->equipArtifact(hero->artifWorn, commonInfo->srcSlotID, commonInfo->srcArtifact->id);
CGI->arth->equipArtifact(curHero->artifWorn, commonInfo->srcSlotID, commonInfo->srcArtifact->id);
else
hero->artifacts.insert(hero->artifacts.begin() + (commonInfo->srcSlotID - 19), commonInfo->srcArtifact->id);
curHero->artifacts.insert(curHero->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())
{
CGI->arth->equipArtifact(hero->artifWorn, i, commonInfo->srcArtifact->id);
if (artWorn[i]->fitsHere(commonInfo->srcArtifact) && !vstd::contains(curHero->artifWorn, i)) {
CGI->arth->equipArtifact(curHero->artifWorn, i, commonInfo->srcArtifact->id);
break;
}
}
// If it can't be worn, put it in the backpack.
if (i == 19)
hero->artifacts.push_back(commonInfo->srcArtifact->id);
curHero->artifacts.push_back(commonInfo->srcArtifact->id);
}
hero->recreateArtBonuses();
curHero->recreateArtBonuses();
}
}

View File

@ -773,7 +773,7 @@ inline bool CArtPlace::locked () const
class CArtifactsOfHero : public CIntObject
{
const CGHeroInstance * curHero;
CGHeroInstance * curHero;
std::vector<CArtPlace *> artWorn; // 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5
std::vector<CArtPlace *> backpack; //hero's visible backpack (only 5 elements!)

View File

@ -615,7 +615,7 @@ void CArtHandler::equipArtifact (std::map<ui16, ui32> &artifWorn, ui16 slotID, u
destConsumed = true;
} else {
BOOST_FOREACH(ui16 slot, constituent.possibleSlots) {
if (artifWorn.find(slot) == artifWorn.end()) {
if (!vstd::contains(artifWorn, slot)) {
artifWorn[slot] = 145;
break;
}
@ -632,7 +632,7 @@ void CArtHandler::equipArtifact (std::map<ui16, ui32> &artifWorn, ui16 slotID, u
*/
void CArtHandler::unequipArtifact (std::map<ui16, ui32> &artifWorn, ui16 slotID)
{
if (artifWorn.find(slotID) == artifWorn.end())
if (!vstd::contains(artifWorn, slotID))
return;
const CArtifact &artifact = artifacts[artifWorn[slotID]];
@ -651,7 +651,7 @@ void CArtHandler::unequipArtifact (std::map<ui16, ui32> &artifWorn, ui16 slotID)
destConsumed = true;
} else {
BOOST_REVERSE_FOREACH(ui16 slot, constituent.possibleSlots) {
if (artifWorn.find(slot) != artifWorn.end() && artifWorn[slot] == 145) {
if (vstd::contains(artifWorn, slot) && artifWorn[slot] == 145) {
artifWorn.erase(slot);
break;
}

View File

@ -1178,7 +1178,7 @@ void CGHeroInstance::giveArtifact (ui32 aid)
if (artifact.isBig()) {
for (std::vector<ui16>::const_iterator it = artifact.possibleSlots.begin(); it != artifact.possibleSlots.end(); ++it) {
if (artifWorn.find(*it) == artifWorn.end()) {
if (!vstd::contains(artifWorn, *it)) {
VLC->arth->equipArtifact(artifWorn, *it, aid);
break;
}

View File

@ -2650,7 +2650,7 @@ bool CGameHandler::swapArtifacts(si32 srcHeroID, si32 destHeroID, ui16 srcSlot,
// Combinational artifacts needs to be removed first so they don't get denied movement because of their own locks.
if (srcHeroID == destHeroID && srcSlot < 19 && destSlot < 19) {
sha.setArtAtPos(srcSlot, -1);
if (sha.artifWorn.find(destSlot) == sha.artifWorn.end())
if (!vstd::contains(sha.artifWorn, destSlot))
destArtifact = NULL;
}
@ -2681,7 +2681,7 @@ bool CGameHandler::swapArtifacts(si32 srcHeroID, si32 destHeroID, ui16 srcSlot,
// If dest does not fit in src, put it in dest's backpack instead.
if (srcHeroID == destHeroID) // To avoid stumbling on own locks, remove artifact first.
sha.setArtAtPos(destSlot, -1);
bool destFits = !destArtifact || srcSlot >= 19 || destSlot >= 19 || destArtifact->fitsAt(sha.artifWorn, srcSlot);
const bool destFits = !destArtifact || srcSlot >= 19 || destSlot >= 19 || destArtifact->fitsAt(sha.artifWorn, srcSlot);
if (srcHeroID == destHeroID && destArtifact)
sha.setArtAtPos(destSlot, destArtifact->id);
@ -2802,7 +2802,7 @@ bool CGameHandler::assembleArtifacts (si32 heroID, ui16 artifactSlot, bool assem
const CArtifact &constituent = VLC->arth->artifacts[constituentID];
BOOST_REVERSE_FOREACH(ui16 slotID, constituent.possibleSlots) {
if (sha.artifWorn.find(slotID) != sha.artifWorn.end()) {
if (vstd::contains(sha.artifWorn, slotID)) {
if (sha.artifWorn[slotID] == 145 || (!destConsumed && slotID == artifactSlot)) {
if (slotID == artifactSlot)
destConsumed = true;