Selaa lähdekoodia

* first part of exchange window
* minor changes

mateuszb 16 vuotta sitten
vanhempi
sitoutus
8231a6e6cc

+ 1 - 1
CCallback.cpp

@@ -168,7 +168,7 @@ const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode =
 	}
 	else //object id
 	{
-		return static_cast<CGHeroInstance*>(gs->map->objects[val]);
+		return static_cast<const CGHeroInstance*>(gs->map->objects[val]);
 	}
 	return NULL;
 }

+ 2 - 1
CGameInterface.h

@@ -90,7 +90,8 @@ public:
 	virtual void yourTurn(){};
 	virtual void availableCreaturesChanged(const CGTownInstance *town){};
 	virtual void heroBonusChanged(const CGHeroInstance *hero, const HeroBonus &bonus, bool gain){};//if gain hero received bonus, else he lost it
-	virtual void requestRealized(PackageApplied *pa){}
+	virtual void requestRealized(PackageApplied *pa){};
+	virtual void heroExchangeStarted(si32 hero1, si32 hero2){};
 	virtual void serialize(COSer<CSaveFile> &h, const int version){}; //saving
 	virtual void serialize(CISer<CLoadFile> &h, const int version){}; //loading
 

+ 5 - 0
client/CPlayerInterface.cpp

@@ -1723,6 +1723,11 @@ void CPlayerInterface::requestRealized( PackageApplied *pa )
 		stillMoveHero.setn(CONTINUE_MOVE);
 }
 
+void CPlayerInterface::heroExchangeStarted(si32 hero1, si32 hero2)
+{
+	pushInt(new CExchangeWindow(hero1, hero2));
+}
+
 void CPlayerInterface::recreateWanderingHeroes()
 {
 	wanderingHeroes.clear();

+ 1 - 0
client/CPlayerInterface.h

@@ -144,6 +144,7 @@ public:
 	void availableCreaturesChanged(const CGTownInstance *town);
 	void heroBonusChanged(const CGHeroInstance *hero, const HeroBonus &bonus, bool gain);//if gain hero received bonus, else he lost it
 	void requestRealized(PackageApplied *pa);
+	void heroExchangeStarted(si32 hero1, si32 hero2);
 	void serialize(COSer<CSaveFile> &h, const int version); //saving
 	void serialize(CISer<CLoadFile> &h, const int version); //loading
 

+ 1 - 0
client/Client.h

@@ -106,6 +106,7 @@ public:
 	void giveHero(int id, int player){};
 	void changeObjPos(int objid, int3 newPos, ui8 flags){};
 	void sendAndApply(CPackForClient * info){};
+	void heroExchange(si32 hero1, si32 hero2){};
 
 	//////////////////////////////////////////////////////////////////////////
 	friend class CCallback; //handling players actions

+ 42 - 0
client/GUIClasses.cpp

@@ -3208,3 +3208,45 @@ CRClickPopupInt::~CRClickPopupInt()
 	if(delInner)
 		delete inner;
 }
+
+void CExchangeWindow::close()
+{
+	LOCPLINT->popIntTotally(this);
+}
+
+void CExchangeWindow::activate()
+{
+	quit->activate();
+}
+
+void CExchangeWindow::deactivate()
+{
+	quit->deactivate();
+}
+
+void CExchangeWindow::show(SDL_Surface * to)
+{
+	blitAt(bg, pos, to);
+
+	quit->show(to);
+
+	//printing border around window
+	if(screen->w != 800 || screen->h !=600)
+		CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
+}
+
+CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) //c-tor
+{
+	hero1inst = LOCPLINT->cb->getHeroInfo(hero1, 2);
+	hero2inst = LOCPLINT->cb->getHeroInfo(hero2, 2);
+
+	bg = BitmapHandler::loadBitmap("TRADE2.BMP");
+	graphics->blueToPlayersAdv(bg, hero1inst->tempOwner);
+	quit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
+}
+
+CExchangeWindow::~CExchangeWindow() //d-tor
+{
+	SDL_FreeSurface(bg);
+	delete quit;
+}

+ 16 - 0
client/GUIClasses.h

@@ -566,6 +566,22 @@ public:
 	~CGarrisonWindow(); //d-tor
 };
 
+class CExchangeWindow : public CIntObject, public IShowActivable
+{
+public:
+	SDL_Surface *bg; //background
+	AdventureMapButton *quit;
+
+	const CGHeroInstance *hero1inst, *hero2inst;
+
+	void close();
+	void activate();
+	void deactivate();
+	void show(SDL_Surface * to);
+
+	CExchangeWindow(si32 hero1, si32 hero2); //c-tor
+	~CExchangeWindow(); //d-tor
+};
 
 
 #endif //__GUICLASSES_H__

+ 9 - 0
client/NetPacksClient.cpp

@@ -460,3 +460,12 @@ void ShowInInfobox::applyCl(CClient *cl)
 		static_cast<CPlayerInterface*>(cl->playerint[player])->showComp(sc);
 	}
 }
+
+void HeroExchange::applyFirstCl(CClient *cl)
+{
+}
+
+void HeroExchange::applyCl(CClient *cl)
+{
+	cl->playerint[player]->heroExchangeStarted(hero1, hero2);
+}

+ 2 - 0
hch/CObjectHandler.cpp

@@ -604,9 +604,11 @@ void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
 		if(tempOwner == h->tempOwner) //our hero
 		{
 			//exchange
+			cb->heroExchange(id, h->id);
 		}
 		else
 		{
+			//battle
 			cb->startBattleI(
 				&h->army,
 				&army,

+ 1 - 0
lib/IGameCallback.h

@@ -83,6 +83,7 @@ public:
 	virtual void giveHero(int id, int player)=0;
 	virtual void changeObjPos(int objid, int3 newPos, ui8 flags)=0;
 	virtual void sendAndApply(CPackForClient * info)=0;
+	virtual void heroExchange(si32 hero1, si32 hero2)=0; //when two heroes meet on adventure map
 
 
 	friend struct CPackForClient;

+ 16 - 0
lib/NetPacks.h

@@ -475,6 +475,22 @@ struct GiveHero : public CPackForClient //516
 	}
 };  
 
+struct HeroExchange : public CPackForClient //517
+{
+	HeroExchange(){type = 517;};
+	void applyFirstCl(CClient *cl);
+	void applyCl(CClient *cl);
+	DLL_EXPORT void applyGs(CGameState *gs);
+
+	si32 hero1, hero2; //heroes for exchange
+	ui8 player;
+
+	template <typename Handler> void serialize(Handler &h, const int version)
+	{
+		h & hero1 & hero2 & player;
+	}
+};
+
 struct NewTurn : public CPackForClient //101
 {
 	DLL_EXPORT void applyGs(CGameState *gs);

+ 4 - 0
lib/NetPacksLib.cpp

@@ -418,6 +418,10 @@ DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
 	h->inTownGarrison = false;
 }
 
+DLL_EXPORT void HeroExchange::applyGs(CGameState *gs)
+{
+}
+
 DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
 {
 	gs->day = day;

+ 1 - 0
lib/RegisterTypes.cpp

@@ -93,6 +93,7 @@ void registerTypes2(Serializer &s)
 	s.template registerType<SetStackEffect>();
 	s.template registerType<StacksInjured>();
 	s.template registerType<ShowInInfobox>();
+	s.template registerType<HeroExchange>();
 
 	s.template registerType<SaveGame>();
 	s.template registerType<SetSelection>();

+ 1 - 1
mapHandler.cpp

@@ -975,7 +975,7 @@ void CMapHandler::terrainRect(int3 top_tile, unsigned char anim, std::vector< st
 
 	SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
 
-	delete rSurf;
+	SDL_FreeSurface(rSurf);
 }
 
 SDL_Surface * CMapHandler::getVisBitmap(int x, int y, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)

+ 15 - 0
server/CGameHandler.cpp

@@ -1422,6 +1422,21 @@ void CGameHandler::changeObjPos( int objid, int3 newPos, ui8 flags )
 	sendAndApply(&cop);
 }
 
+void CGameHandler::heroExchange(si32 hero1, si32 hero2)
+{
+	ui8 player1 = getHero(hero1)->tempOwner;
+	ui8 player2 = getHero(hero2)->tempOwner;
+
+	if(player1 == player2)
+	{
+		HeroExchange hex;
+		hex.hero1 = hero1;
+		hex.hero2 = hero2;
+		hex.player = player1;
+		sendAndApply(&hex);
+	}
+}
+
 void CGameHandler::applyAndAsk( Query * sel, ui8 player, boost::function<void(ui32)> &callback )
 {
 	boost::unique_lock<boost::recursive_mutex> lock(gsm);

+ 1 - 0
server/CGameHandler.h

@@ -130,6 +130,7 @@ public:
 	void setManaPoints(int hid, int val);
 	void giveHero(int id, int player);
 	void changeObjPos(int objid, int3 newPos, ui8 flags);
+	void heroExchange(si32 hero1, si32 hero2);
 	//////////////////////////////////////////////////////////////////////////
 
 	void init(StartInfo *si, int Seed);