From 937ef0227ba7cf5acd8e31f9ec458a056935c44b Mon Sep 17 00:00:00 2001 From: ArseniyShestakov Date: Wed, 24 Dec 2014 18:49:12 +0300 Subject: [PATCH 1/4] Use ArtifactID instead of int when possible --- client/widgets/CArtifactHolder.cpp | 8 ++++---- client/windows/CCastleInterface.cpp | 2 +- client/windows/CTradeWindow.cpp | 2 +- lib/CArtHandler.cpp | 2 +- lib/GameConstants.h | 1 + lib/NetPacksLib.cpp | 2 +- lib/mapping/MapFormatH3M.cpp | 2 +- server/CGameHandler.cpp | 2 +- 8 files changed, 11 insertions(+), 10 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index d5ef8a262..8a20d862b 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -122,7 +122,7 @@ void CArtPlace::clickLeft(tribool down, bool previousState) // If clicked on spellbook, open it only if no artifact is held at the moment. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH) { - if(ourArt->artType->id == 0) + if(ourArt->artType->id == ArtifactID::SPELLBOOK) { auto spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt); GH.pushInt(spellWindow); @@ -131,7 +131,7 @@ void CArtPlace::clickLeft(tribool down, bool previousState) if (!down && previousState) { - if(ourArt && ourArt->artType->id == 0) //spellbook + if(ourArt && ourArt->artType->id == ArtifactID::SPELLBOOK) return; //this is handled separately if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked @@ -139,7 +139,7 @@ void CArtPlace::clickLeft(tribool down, bool previousState) if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported) && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player { - if(ourArt->artType->id == 3) //catapult cannot be highlighted + if(ourArt->artType->id == ArtifactID::CATAPULT) //catapult cannot be highlighted { std::vector catapult(1, new CComponent(CComponent::artifact, 3, 0)); LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped. @@ -380,7 +380,7 @@ void CArtPlace::setArtifact(const CArtifactInstance *art) else text = '{' + ourArt->artType->Name() + "}\n\n" + artDesc; //workaround for new artifacts with single name, turns it to H3-style - if(art->artType->id == 1) //spell scroll + if(art->artType->id == ArtifactID::SPELL_SCROLL) { // we expect scroll description to be like this: This scroll contains the [spell name] spell which is added into your spell book for as long as you carry the scroll. // so we want to replace text in [...] with a spell name diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 7a0c7b9e5..e9dfe5c7a 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -815,7 +815,7 @@ void CCastleBuildings::enterMagesGuild() CFunctionList onYes = [this]{ openMagesGuild(); }; CFunctionList onNo = onYes; onYes += [hero]{ LOCPLINT->cb->buyArtifact(hero, ArtifactID::SPELLBOOK); }; - std::vector components(1, new CComponent(CComponent::artifact,0,0)); + std::vector components(1, new CComponent(CComponent::artifact,ArtifactID::SPELLBOOK,0)); LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214], onYes, onNo, true, components); } diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index 52d7cf571..8f03d8e67 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -1460,7 +1460,7 @@ void CAltarWindow::showAll(SDL_Surface * to) bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art) { int artID = art->artType->id; - if(artID != 1 && artID < 7) //special art + if(artID != ArtifactID::SPELL_SCROLL && artID < 7) //special art { logGlobal->warnStream() << "Cannot put special artifact on altar!"; return false; diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 388d10ac6..cc7fb191c 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -78,7 +78,7 @@ CArtifact::~CArtifact() int CArtifact::getArtClassSerial() const { - if(id == 1) + if(id == ArtifactID::SPELL_SCROLL) return 4; switch(aClass) { diff --git a/lib/GameConstants.h b/lib/GameConstants.h index 4447c1d6f..e194aed32 100644 --- a/lib/GameConstants.h +++ b/lib/GameConstants.h @@ -779,6 +779,7 @@ public: FIRST_AID_TENT = 6, //CENTAUR_AXE = 7, //BLACKSHARD_OF_THE_DEAD_KNIGHT = 8, + TITANS_THUNDER = 135, //CORNUCOPIA = 140, ART_SELECTION = 144, ART_LOCK = 145, diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 96d25aa13..d237eb16c 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -909,7 +909,7 @@ DLL_LINKAGE void MoveArtifact::applyGs( CGameState *gs ) a->move(src, dst); //TODO what'll happen if Titan's thunder is equipped by pickin git up or the start of game? - if (a->artType->id == 135 && dst.slot == ArtifactPosition::RIGHT_HAND) //Titan's Thunder creates new spellbook on equip + if (a->artType->id == ArtifactID::TITANS_THUNDER && dst.slot == ArtifactPosition::RIGHT_HAND) //Titan's Thunder creates new spellbook on equip { auto hPtr = boost::get >(&dst.artHolder); if(hPtr) diff --git a/lib/mapping/MapFormatH3M.cpp b/lib/mapping/MapFormatH3M.cpp index 3325645e2..92ed58724 100644 --- a/lib/mapping/MapFormatH3M.cpp +++ b/lib/mapping/MapFormatH3M.cpp @@ -1229,7 +1229,7 @@ void CMapLoaderH3M::readObjects() if(objTempl.id == Obj::SPELL_SCROLL) { spellID = reader.readUInt32(); - artID = 1; + artID = ArtifactID::SPELL_SCROLL; } else if(objTempl.id == Obj::ARTIFACT) { diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 184f7bbca..cc94f8e1e 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -522,7 +522,7 @@ void CGameHandler::endBattle(int3 tile, const CGHeroInstance *hero1, const CGHer MoveArtifact ma; ma.src = ArtifactLocation (finishingBattle->loserHero, artSlot.first); const CArtifactInstance * art = ma.src.getArt(); - if (art && !art->artType->isBig() && art->artType->id != 0) // don't move war machines or locked arts (spellbook) + if (art && !art->artType->isBig() && art->artType->id != ArtifactID::SPELLBOOK) // don't move war machines or locked arts (spellbook) { arts.push_back (art->artType->id); ma.dst = ArtifactLocation (finishingBattle->winnerHero, art->firstAvailableSlot(finishingBattle->winnerHero)); From de6752c048723f49f166cb34d28116ed7fe9dc5e Mon Sep 17 00:00:00 2001 From: ArseniyShestakov Date: Wed, 24 Dec 2014 20:43:35 +0300 Subject: [PATCH 2/4] Implement CArtHandler::isBigArtifact and CArtifact::isTradable --- lib/CArtHandler.cpp | 13 +++++++++++++ lib/CArtHandler.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index cc7fb191c..378d1e738 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -64,6 +64,11 @@ bool CArtifact::isBig () const return VLC->arth->isBigArtifact(id); } +bool CArtifact::isTradable () const +{ + return VLC->arth->isTradableArtifact(id); +} + CArtifact::CArtifact() { setNodeType(ARTIFACT); @@ -578,6 +583,14 @@ bool CArtHandler::legalArtifact(ArtifactID id) !(art->constituents); //no combo artifacts spawning } +bool CArtHandler::isTradableArtifact(ArtifactID id) const +{ + if (id < 7 && id != ArtifactID::SPELL_SCROLL) + return false; + + return true; +} + void CArtHandler::initAllowedArtifactsList(const std::vector &allowed) { allowedArtifacts.clear(); diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 805cc6aeb..b241863b7 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -59,6 +59,7 @@ public: const std::string &EventText() const; bool isBig () const; + bool isTradable () const; int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other std::string nodeName() const override; @@ -215,6 +216,7 @@ public: //void getAllowedArts(std::vector > &out, std::vector *arts, int flag); //void getAllowed(std::vector > &out, int flags); bool isBigArtifact (ArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();} + bool isTradableArtifact (ArtifactID id) const; void initAllowedArtifactsList(const std::vector &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed static ArtifactID creatureToMachineID(CreatureID id); static CreatureID machineIDToCreature(ArtifactID id); From 4cb98e7e8d1d8112c9e54a370a7314ea45bf95e9 Mon Sep 17 00:00:00 2001 From: ArseniyShestakov Date: Wed, 24 Dec 2014 20:48:37 +0300 Subject: [PATCH 3/4] Use isTradable and fix issue 1392 This change make it possible to sell spell scrolls in Artifacts Merchant. --- client/widgets/CArtifactHolder.cpp | 2 +- client/windows/CTradeWindow.cpp | 5 ++--- server/CGameHandler.cpp | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/client/widgets/CArtifactHolder.cpp b/client/widgets/CArtifactHolder.cpp index 8a20d862b..2737d6e8d 100644 --- a/client/widgets/CArtifactHolder.cpp +++ b/client/widgets/CArtifactHolder.cpp @@ -105,7 +105,7 @@ void CArtPlace::clickLeft(tribool down, bool previousState) { if(down) { - if(ourArt->artType->id < 7) //War Machine or Spellbook + if(!ourArt->artType->isTradable()) //War Machine or Spellbook { LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded. } diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index 8f03d8e67..d76a279b1 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -1459,8 +1459,7 @@ void CAltarWindow::showAll(SDL_Surface * to) bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art) { - int artID = art->artType->id; - if(artID != ArtifactID::SPELL_SCROLL && artID < 7) //special art + if(!art->artType->isTradable()) //special art { logGlobal->warnStream() << "Cannot put special artifact on altar!"; return false; @@ -1478,7 +1477,7 @@ bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance } int dmp, val; - market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP); + market->getOffer(art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP); arts->artifactsOnAltar.insert(art); altarSlot->setArtInstance(art); diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index cc94f8e1e..943a87dbc 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -2971,7 +2971,7 @@ bool CGameHandler::sellArtifact( const IMarket *m, const CGHeroInstance *h, Arti const CArtifactInstance *art = h->getArtByInstanceId(aid); if(!art) COMPLAIN_RET("There is no artifact to sell!"); - if(art->artType->id < 7) + if(!art->artType->isTradable()) COMPLAIN_RET("Cannot sell a war machine or spellbook!"); int resVal = 0, dump = 1; From 04d15174e70f79d245000326df113be969f3cd89 Mon Sep 17 00:00:00 2001 From: ArseniyShestakov Date: Thu, 25 Dec 2014 17:18:37 +0300 Subject: [PATCH 4/4] Use switch for CArtHandler::isTradableArtifact --- lib/CArtHandler.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 378d1e738..84f821160 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -585,10 +585,18 @@ bool CArtHandler::legalArtifact(ArtifactID id) bool CArtHandler::isTradableArtifact(ArtifactID id) const { - if (id < 7 && id != ArtifactID::SPELL_SCROLL) + switch (id) + { + case ArtifactID::SPELLBOOK: + case ArtifactID::GRAIL: + case ArtifactID::CATAPULT: + case ArtifactID::BALLISTA: + case ArtifactID::AMMO_CART: + case ArtifactID::FIRST_AID_TENT: return false; - - return true; + default: + return true; + } } void CArtHandler::initAllowedArtifactsList(const std::vector &allowed)