diff --git a/CCallback.cpp b/CCallback.cpp index 7053fa569..0bcf961b7 100644 --- a/CCallback.cpp +++ b/CCallback.cpp @@ -168,7 +168,7 @@ const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode = } else //object id { - return static_cast(gs->map->objects[val]); + return static_cast(gs->map->objects[val]); } return NULL; } diff --git a/CGameInterface.h b/CGameInterface.h index 39850627c..cb27ca442 100644 --- a/CGameInterface.h +++ b/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 &h, const int version){}; //saving virtual void serialize(CISer &h, const int version){}; //loading diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index d56ca2e45..6864d8911 100644 --- a/client/CPlayerInterface.cpp +++ b/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(); diff --git a/client/CPlayerInterface.h b/client/CPlayerInterface.h index 3ac9b7745..c8479e022 100644 --- a/client/CPlayerInterface.h +++ b/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 &h, const int version); //saving void serialize(CISer &h, const int version); //loading diff --git a/client/Client.h b/client/Client.h index 07c76825a..a984d3baa 100644 --- a/client/Client.h +++ b/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 diff --git a/client/GUIClasses.cpp b/client/GUIClasses.cpp index 555e0f36b..7730ba505 100644 --- a/client/GUIClasses.cpp +++ b/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; +} \ No newline at end of file diff --git a/client/GUIClasses.h b/client/GUIClasses.h index d6d459616..3ce6a4641 100644 --- a/client/GUIClasses.h +++ b/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__ diff --git a/client/NetPacksClient.cpp b/client/NetPacksClient.cpp index 41a47257f..333819171 100644 --- a/client/NetPacksClient.cpp +++ b/client/NetPacksClient.cpp @@ -460,3 +460,12 @@ void ShowInInfobox::applyCl(CClient *cl) static_cast(cl->playerint[player])->showComp(sc); } } + +void HeroExchange::applyFirstCl(CClient *cl) +{ +} + +void HeroExchange::applyCl(CClient *cl) +{ + cl->playerint[player]->heroExchangeStarted(hero1, hero2); +} diff --git a/hch/CObjectHandler.cpp b/hch/CObjectHandler.cpp index f76f695de..8ad524464 100644 --- a/hch/CObjectHandler.cpp +++ b/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, diff --git a/lib/IGameCallback.h b/lib/IGameCallback.h index 34ae542ef..a4072a948 100644 --- a/lib/IGameCallback.h +++ b/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; diff --git a/lib/NetPacks.h b/lib/NetPacks.h index f6693c8ba..4d515f16a 100644 --- a/lib/NetPacks.h +++ b/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 void serialize(Handler &h, const int version) + { + h & hero1 & hero2 & player; + } +}; + struct NewTurn : public CPackForClient //101 { DLL_EXPORT void applyGs(CGameState *gs); diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 2f588f14f..bda2616e9 100644 --- a/lib/NetPacksLib.cpp +++ b/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; diff --git a/lib/RegisterTypes.cpp b/lib/RegisterTypes.cpp index 467dac037..6958ce9b3 100644 --- a/lib/RegisterTypes.cpp +++ b/lib/RegisterTypes.cpp @@ -93,6 +93,7 @@ void registerTypes2(Serializer &s) s.template registerType(); s.template registerType(); s.template registerType(); + s.template registerType(); s.template registerType(); s.template registerType(); diff --git a/mapHandler.cpp b/mapHandler.cpp index aa592ce68..a7ad5def3 100644 --- a/mapHandler.cpp +++ b/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 > > & visibilityMap, int lvl) diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index ef3f01597..330f1c4db 100644 --- a/server/CGameHandler.cpp +++ b/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 &callback ) { boost::unique_lock lock(gsm); diff --git a/server/CGameHandler.h b/server/CGameHandler.h index c93254113..5917146ee 100644 --- a/server/CGameHandler.h +++ b/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);