瀏覽代碼

Merged changes from trunk ( r1885 )

Michał W. Urbańczyk 15 年之前
父節點
當前提交
f7f917845c

+ 3 - 1
client/CCastleInterface.cpp

@@ -18,7 +18,9 @@
 #include "../hch/CObjectHandler.h"
 #include "../hch/CSpellHandler.h"
 #include "../hch/CTownHandler.h"
+#include "../hch/CCreatureHandler.h"
 #include "../lib/map.h"
+#include "../hch/CMusicHandler.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string/replace.hpp>
 #include <boost/assign/std/vector.hpp>
@@ -749,7 +751,7 @@ void CCastleInterface::enterBlacksmith(int ArtifactID)
 	}
 	int price = CGI->arth->artifacts[ArtifactID]->price;
 	bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
-	if(vstd::contains(hero->artifWorn,ui16(ArtifactID+9))) //hero already has machine
+	if( hero->hasArt(ui16(ArtifactID+9)) ) //hero already has machine
 		possible = false;
 	GH.pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(ArtifactID,false),ArtifactID,hero->id));
 }

+ 1 - 1
client/Client.h

@@ -126,7 +126,7 @@ public:
 	void stopHeroVisitCastle(int obj, int heroID){};
 	void giveHeroArtifact(int artid, int hid, int position){}; //pos==-1 - first free slot in backpack=0; pos==-2 - default if available or backpack
 	void giveNewArtifact(int hid, int position){};
-	bool removeArtifact(CArtifact* art, int hid){return false;};
+	bool removeArtifact(const CArtifact* art, int hid){return false;};
 	void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, boost::function<void(BattleResult*)> cb = 0, const CGTownInstance *town = NULL){}; //use hero=NULL for no hero
 	void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false){}; //if any of armies is hero, hero will be used
 	void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false){}; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle

+ 1 - 1
client/GUIClasses.cpp

@@ -3621,7 +3621,7 @@ void CAltarWindow::SacrificeAll()
 	}
 	else
 	{
-		for(std::map<ui16,CArtifact*>::const_iterator i = hero->artifWorn.begin(); i != hero->artifWorn.end(); i++)
+		for(std::map<ui16, const CArtifact*>::const_iterator i = hero->artifWorn.begin(); i != hero->artifWorn.end(); i++)
 		{
 			if(i->second->id != 145) //ignore locks from assembled artifacts
 				moveFromSlotToAltar(i->first, NULL, i->second->id);

+ 10 - 10
hch/CArtHandler.cpp

@@ -57,13 +57,13 @@ bool CArtifact::isModable () const
  * Checks whether the artifact fits at a given slot.
  * @param artifWorn A hero's set of worn artifacts.
  */
-bool CArtifact::fitsAt (const std::map<ui16, CArtifact*> &artifWorn, ui16 slotID) const
+bool CArtifact::fitsAt (const std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const
 {
 	if (!vstd::contains(possibleSlots, slotID))
 		return false;
 
 	// Can't put an artifact in a locked slot.
-	std::map<ui16, CArtifact*>::const_iterator it = artifWorn.find(slotID);
+	std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.find(slotID);
 	if (it != artifWorn.end() && it->second->id == 145)
 		return false;
 
@@ -72,7 +72,7 @@ bool CArtifact::fitsAt (const std::map<ui16, CArtifact*> &artifWorn, ui16 slotID
 	//       Assumes that misc & rings fits only in their slots, and others in only one slot and no duplicates.
 	if (constituents != NULL)
 	{
-		std::map<ui16, CArtifact*> tempArtifWorn = artifWorn;
+		std::map<ui16, const CArtifact*> tempArtifWorn = artifWorn;
 		const ui16 ringSlots[] = {6, 7};
 		const ui16 miscSlots[] = {9, 10, 11, 12, 18};
 		int rings = 0;
@@ -115,7 +115,7 @@ bool CArtifact::fitsAt (const std::map<ui16, CArtifact*> &artifWorn, ui16 slotID
 	return true;
 }
 
-bool CArtifact::canBeAssembledTo (const std::map<ui16, CArtifact*> &artifWorn, ui32 artifactID) const
+bool CArtifact::canBeAssembledTo (const std::map<ui16, const CArtifact*> &artifWorn, ui32 artifactID) const
 {
 	if (constituentOf == NULL || !vstd::contains(*constituentOf, artifactID))
 		return false;
@@ -126,7 +126,7 @@ bool CArtifact::canBeAssembledTo (const std::map<ui16, CArtifact*> &artifWorn, u
 	BOOST_FOREACH(ui32 constituentID, *artifact.constituents) 
 	{
 		bool found = false;
-		for (std::map<ui16, CArtifact*>::const_iterator it = artifWorn.begin(); it != artifWorn.end(); ++it) 
+		for (std::map<ui16, const CArtifact*>::const_iterator it = artifWorn.begin(); it != artifWorn.end(); ++it) 
 		{
 			if (it->second->id == constituentID) 
 			{
@@ -752,16 +752,16 @@ void CArtHandler::clear()
  * @param artifWorn A hero's set of worn artifacts.
  * @param bonuses Optional list of bonuses to update.
  */
-void CArtHandler::equipArtifact(std::map<ui16, CArtifact*> &artifWorn, ui16 slotID, const CArtifact* newArtifact)
+void CArtHandler::equipArtifact( std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art )
 {
 	unequipArtifact(artifWorn, slotID);
 
-	if (newArtifact) //false when artifact is NULL -> slot set to empty
+	if (art) //false when artifact is NULL -> slot set to empty
 	{
-		const CArtifact &artifact = *newArtifact;
+		const CArtifact &artifact = *art;
 
 		// Add artifact.
-		artifWorn[slotID] = const_cast<CArtifact*>(newArtifact);
+		artifWorn[slotID] = art;
 
 		// Add locks, in reverse order of being removed.
 		if (artifact.constituents != NULL) 
@@ -798,7 +798,7 @@ void CArtHandler::equipArtifact(std::map<ui16, CArtifact*> &artifWorn, ui16 slot
  * @param artifWorn A hero's set of worn artifacts.
  * @param bonuses Optional list of bonuses to update.
  */
-void CArtHandler::unequipArtifact(std::map<ui16, CArtifact*> &artifWorn, ui16 slotID)
+void CArtHandler::unequipArtifact(std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID)
 {
 	if (!vstd::contains(artifWorn, slotID))
 		return;

+ 4 - 4
hch/CArtHandler.h

@@ -29,8 +29,8 @@ public:
 	const std::string &Description() const; //getter
 	bool isBig () const;
 	bool isModable () const;
-	bool fitsAt (const std::map<ui16, CArtifact*> &artifWorn, ui16 slot) const;
-	bool canBeAssembledTo (const std::map<ui16, CArtifact*> &artifWorn, ui32 artifactID) const;
+	bool fitsAt (const std::map<ui16, const CArtifact*> &artifWorn, ui16 slot) const;
+	bool canBeAssembledTo (const std::map<ui16, const CArtifact*> &artifWorn, ui32 artifactID) const;
 	void addBonusesTo (BonusList *otherBonuses) const;
 	void removeBonusesFrom (BonusList *otherBonuses) const;
 	virtual void SetProperty (int mod){};
@@ -134,8 +134,8 @@ public:
 	void getAllowed(std::vector<CArtifact*> &out, int flags);
 	void erasePickedArt (si32 id);
 	bool isBigArtifact (ui32 artID) {return bigArtifacts.find(artID) != bigArtifacts.end();}
-	void equipArtifact (std::map<ui16, CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art);
-	void unequipArtifact (std::map<ui16, CArtifact*> &artifWorn, ui16 slotID);
+	void equipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art);
+	void unequipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID);
 	void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
 	static int convertMachineID(int id, bool creToArt);
 	CArtHandler();

+ 6 - 5
hch/CObjectHandler.cpp

@@ -729,7 +729,7 @@ int CGHeroInstance::maxMovePoints(bool onLand) const
 	return int(base + base*modifier) + bonus;
 }
 
-CArtifact* CGHeroInstance::getArtAtPos(ui16 pos) const
+const CArtifact* CGHeroInstance::getArtAtPos(ui16 pos) const
 {
 	if(pos<19)
 		if(vstd::contains(artifWorn,pos))
@@ -1419,7 +1419,7 @@ si32 CGHeroInstance::manaRegain() const
 
 si32 CGHeroInstance::getArtPos(int aid) const
 {
-	for(std::map<ui16,CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
+	for(std::map<ui16, const CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
 		if(i->second->id == aid)
 			return i->first;
 	return -1;
@@ -1452,10 +1452,10 @@ void CGHeroInstance::giveArtifact (ui32 aid) //use only for fixed artifacts
 
 bool CGHeroInstance::hasArt( ui32 aid ) const
 {
-	for(std::vector<CArtifact*>::const_iterator i = artifacts.begin(); i != artifacts.end(); i++)
+	for(std::vector<const CArtifact*>::const_iterator i = artifacts.begin(); i != artifacts.end(); i++)
 		if((*i)->id == aid)
 			return true;
-	for(std::map<ui16,CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
+	for(std::map<ui16, const CArtifact*>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
 		if(i->second->id == aid)
 			return true;
 
@@ -6529,7 +6529,8 @@ void CGLighthouse::giveBonusTo( ui8 player ) const
 CCreatureSet& CArmedInstance::getArmy() const
 { //do not return itself by value, or it will xplode
 //	CCreatureSet set = *this; return set;
-	return *((CCreatureSet*)(this));
+	//WARNING! A DIRTY CONST_CAST! TO BE INVESTIGATED AND PROBABLY REMOVED!
+	return *(const_cast<CArmedInstance*>(this));
 }
 
 void CArmedInstance::randomizeArmy(int type)

+ 3 - 3
hch/CObjectHandler.h

@@ -266,8 +266,8 @@ public:
 	ui8 inTownGarrison; // if hero is in town garrison 
 	const CGTownInstance * visitedTown; //set if hero is visiting town or in the town garrison
 	const CGBoat *boat; //set to CGBoat when sailing
-	std::vector<CArtifact*> artifacts; //hero's artifacts from bag
-	std::map<ui16, CArtifact*> artifWorn; //map<position,artifact_id>; positions: 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<const CArtifact*> artifacts; //hero's artifacts from bag
+	std::map<ui16, const CArtifact*> artifWorn; //map<position,artifact_id>; positions: 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::set<ui32> spells; //known spells (spell IDs)
 
 	struct DLL_EXPORT Patrol
@@ -337,7 +337,7 @@ public:
 
 	int maxMovePoints(bool onLand) const;
 
-	CArtifact* getArtAtPos(ui16 pos) const; //NULL - no artifact
+	const CArtifact* getArtAtPos(ui16 pos) const; //NULL - no artifact
 	const CArtifact * getArt(int pos) const;
 	si32 getArtPos(int aid) const; //looks for equipped artifact with given ID and returns its slot ID or -1 if none(if more than one such artifact lower ID is returned)
 	bool hasArt(ui32 aid) const; //checks if hero possess artifact of given id (either in backack or worn)

+ 1 - 1
lib/IGameCallback.h

@@ -103,7 +103,7 @@ public:
 	virtual void stopHeroVisitCastle(int obj, int heroID)=0;
 	virtual void giveHeroArtifact(int artid, int hid, int position)=0; //pos==-1 - first free slot in backpack=0; pos==-2 - default if available or backpack
 	virtual void giveNewArtifact(int hid, int position)=0;
-	virtual bool removeArtifact(CArtifact* art, int hid) = 0;
+	virtual bool removeArtifact(const CArtifact* art, int hid) = 0;
 	virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, boost::function<void(BattleResult*)> cb = 0, const CGTownInstance *town = NULL)=0; //use hero=NULL for no hero
 	virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false)=0; //if any of armies is hero, hero will be used
 	virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false)=0; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle

+ 3 - 3
lib/NetPacks.h

@@ -608,15 +608,15 @@ struct SetHeroArtifacts : public CPackForClient //509
 	DLL_EXPORT void setArtAtPos(ui16 pos, const CArtifact* art);
 
 	si32 hid;
-	std::vector<CArtifact*> artifacts; //hero's artifacts from bag
-	std::map<ui16,CArtifact*> artifWorn; //map<position,artifact_id>; positions: 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<const CArtifact*> artifacts; //hero's artifacts from bag
+	std::map<ui16, const CArtifact*> artifWorn; //map<position,artifact_id>; positions: 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
 
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
 		h & hid & artifacts & artifWorn;
 	}
 
-	std::vector<CArtifact*> equiped, unequiped; //used locally
+	std::vector<const CArtifact*> equiped, unequiped; //used locally
 	BonusList gained, lost; //used locally as hlp when applying
 };   
 

+ 4 - 4
lib/NetPacksLib.cpp

@@ -444,11 +444,11 @@ DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
 DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
 {
 	CGHeroInstance *h = gs->getHero(hid);
-	for(std::map<ui16,CArtifact*>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
+	for(std::map<ui16, const CArtifact*>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
 		if(!vstd::contains(artifWorn,i->first)  ||  artifWorn[i->first] != i->second)
 			unequiped.push_back(i->second);
 
-	for(std::map<ui16,CArtifact*>::iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
+	for(std::map<ui16, const CArtifact*>::iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
 		if(!vstd::contains(h->artifWorn,i->first)  ||  h->artifWorn[i->first] != i->second)
 		{
 			equiped.push_back(i->second);
@@ -477,9 +477,9 @@ DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, const CArtifact* art)
 		else // Goes into the backpack.
 		{ 
 			if(pos - 19 < artifacts.size())
-				artifacts.insert(artifacts.begin() + (pos - 19), const_cast<CArtifact*>(art));
+				artifacts.insert(artifacts.begin() + (pos - 19), art);
 			else
-				artifacts.push_back(const_cast<CArtifact*>(art));
+				artifacts.push_back(art);
 		}
 	}
 }

+ 6 - 6
server/CGameHandler.cpp

@@ -2242,7 +2242,7 @@ void CGameHandler::giveNewArtifact(int hid, int position)
 
 	sendAndApply(&sha);
 }
-bool CGameHandler::removeArtifact(CArtifact* art, int hid)
+bool CGameHandler::removeArtifact(const CArtifact* art, int hid)
 {
 	const CGHeroInstance* h = getHero(hid);
 
@@ -2251,12 +2251,12 @@ bool CGameHandler::removeArtifact(CArtifact* art, int hid)
 	sha.artifacts = h->artifacts;
 	sha.artifWorn = h->artifWorn;
 	
-	std::vector<CArtifact*>::iterator it;
+	std::vector<const CArtifact*>::iterator it;
 	if 	((it = std::find(sha.artifacts.begin(), sha.artifacts.end(), art)) != sha.artifacts.end()) //it is in backpack
 		sha.artifacts.erase(it);
 	else //worn
 	{
-		std::map<ui16,CArtifact*>::iterator itr;
+		std::map<ui16, const CArtifact*>::iterator itr;
 		for (itr = sha.artifWorn.begin(); itr != sha.artifWorn.end(); ++itr)
 		{
 			if (itr->second == art)
@@ -3225,7 +3225,7 @@ bool CGameHandler::assembleArtifacts (si32 heroID, ui16 artifactSlot, bool assem
 			}
 
 			bool found = false;
-			for (std::map<ui16, CArtifact*>::iterator it = sha.artifWorn.begin(); it != sha.artifWorn.end(); ++it)
+			for (std::map<ui16, const CArtifact*>::iterator it = sha.artifWorn.begin(); it != sha.artifWorn.end(); ++it)
 			{
 				if (it->second->id == constituentID)
 				{ // Found possible constituent to substitute.
@@ -3284,7 +3284,7 @@ bool CGameHandler::assembleArtifacts (si32 heroID, ui16 artifactSlot, bool assem
 				{
 					if (vstd::contains(sha.artifWorn, slotID) && sha.artifWorn[slotID]->id == 145)
 					{
-						sha.artifWorn[slotID]->id = constituentID;
+						const_cast<CArtifact*>(sha.artifWorn[slotID])->id = constituentID;
 						break;
 					}
 				}
@@ -5177,7 +5177,7 @@ bool CGameHandler::sacrificeCreatures(const IMarket *market, const CGHeroInstanc
 	return true;
 }
 
-bool CGameHandler::sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, CArtifact* art)
+bool CGameHandler::sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, const CArtifact* art)
 {
 	if(!removeArtifact(art, hero->id))
 		COMPLAIN_RET("Cannot find artifact to sacrifice!");

+ 2 - 2
server/CGameHandler.h

@@ -164,7 +164,7 @@ public:
 	void stopHeroVisitCastle(int obj, int heroID);
 	void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
 	void giveNewArtifact(int hid, int position);
-	bool removeArtifact(CArtifact* art, int hid);
+	bool removeArtifact(const CArtifact* art, int hid);
 	void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, boost::function<void(BattleResult*)> cb = 0, const CGTownInstance *town = NULL); //use hero=NULL for no hero
 	void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false); //if any of armies is hero, hero will be used
 	void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false); //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle//void startBattleI(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb); //for hero<=>neutral army
@@ -244,7 +244,7 @@ public:
 	void run(bool resume);
 	void newTurn();
 	void handleAfterAttackCasting( const BattleAttack & bat );
-	bool sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, CArtifact* art);
+	bool sacrificeArtifact(const IMarket * m, const CGHeroInstance * hero, const CArtifact* art);
 	friend class CVCMIServer;
 	friend class CScriptCallback;
 };