2007-08-27 14:15:03 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "CCallback.h"
|
2010-12-20 21:22:53 +00:00
|
|
|
#include "lib/CCreatureHandler.h"
|
2009-05-20 10:08:56 +00:00
|
|
|
#include "client/CGameInfo.h"
|
|
|
|
#include "lib/CGameState.h"
|
2010-12-25 19:23:30 +00:00
|
|
|
#include "lib/BattleState.h"
|
2009-05-20 10:08:56 +00:00
|
|
|
#include "client/CPlayerInterface.h"
|
2008-08-29 21:41:32 +00:00
|
|
|
#include "client/Client.h"
|
2009-05-20 10:08:56 +00:00
|
|
|
#include "lib/map.h"
|
2010-12-20 21:22:53 +00:00
|
|
|
#include "lib/CBuildingHandler.h"
|
|
|
|
#include "lib/CDefObjInfoHandler.h"
|
|
|
|
#include "lib/CGeneralTextHandler.h"
|
|
|
|
#include "lib/CHeroHandler.h"
|
|
|
|
#include "lib/CObjectHandler.h"
|
2008-07-27 17:07:37 +00:00
|
|
|
#include "lib/Connection.h"
|
2008-07-28 12:44:08 +00:00
|
|
|
#include "lib/NetPacks.h"
|
2010-04-06 08:59:24 +00:00
|
|
|
#include "client/mapHandler.h"
|
2008-08-29 21:41:32 +00:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
2008-08-04 23:04:15 +00:00
|
|
|
#include <boost/thread/shared_mutex.hpp>
|
2010-12-20 21:22:53 +00:00
|
|
|
#include "lib/CSpellHandler.h"
|
|
|
|
#include "lib/CArtHandler.h"
|
2008-08-04 15:56:36 +00:00
|
|
|
#ifdef min
|
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
2008-03-14 18:24:37 +00:00
|
|
|
|
2009-04-15 14:03:31 +00:00
|
|
|
/*
|
|
|
|
* CCallback.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-03-06 22:11:17 +00:00
|
|
|
template <ui16 N> bool isType(CPack *pack)
|
|
|
|
{
|
|
|
|
return pack->getType() == N;
|
|
|
|
}
|
|
|
|
|
2010-06-30 19:27:35 +00:00
|
|
|
bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
|
|
|
|
{
|
|
|
|
CastleTeleportHero pack(who->id, where->id, 1);
|
|
|
|
sendRequest(&pack);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-03 23:53:18 +00:00
|
|
|
bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
|
2009-03-12 18:50:36 +00:00
|
|
|
{
|
2009-03-20 18:51:48 +00:00
|
|
|
MoveHero pack(dst,h->id);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2007-08-30 10:11:53 +00:00
|
|
|
return true;
|
2007-08-29 12:18:31 +00:00
|
|
|
}
|
2007-12-25 16:25:53 +00:00
|
|
|
void CCallback::selectionMade(int selection, int asker)
|
|
|
|
{
|
2009-03-20 18:51:48 +00:00
|
|
|
QueryReply pack(asker,selection);
|
|
|
|
*cl->serv << &pack;
|
2007-12-25 16:25:53 +00:00
|
|
|
}
|
2010-07-10 16:50:23 +00:00
|
|
|
void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
|
2008-04-11 17:41:02 +00:00
|
|
|
{
|
2010-05-15 08:33:32 +00:00
|
|
|
if(player!=obj->tempOwner && obj->ID != 106)
|
|
|
|
return;
|
2009-03-20 18:51:48 +00:00
|
|
|
|
2010-07-10 16:50:23 +00:00
|
|
|
RecruitCreatures pack(obj->id,ID,amount,level);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-04-11 17:41:02 +00:00
|
|
|
}
|
2007-09-14 13:11:10 +00:00
|
|
|
|
2008-05-23 19:50:11 +00:00
|
|
|
bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
|
|
|
|
{
|
2010-05-02 18:20:26 +00:00
|
|
|
if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
|
2008-05-31 20:37:54 +00:00
|
|
|
return false;
|
2009-03-20 18:51:48 +00:00
|
|
|
|
|
|
|
DisbandCreature pack(stackPos,obj->id);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-05-31 20:37:54 +00:00
|
|
|
return true;
|
2008-05-23 19:50:11 +00:00
|
|
|
}
|
2011-05-03 03:14:18 +00:00
|
|
|
|
2008-05-31 20:37:54 +00:00
|
|
|
bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
|
2008-05-23 19:50:11 +00:00
|
|
|
{
|
2009-03-20 18:51:48 +00:00
|
|
|
UpgradeCreature pack(stackPos,obj->id,newID);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-05-23 19:50:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-03 03:14:18 +00:00
|
|
|
|
2008-07-27 17:07:37 +00:00
|
|
|
void CCallback::endTurn()
|
|
|
|
{
|
2009-10-31 16:33:11 +00:00
|
|
|
tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
|
2009-03-20 18:51:48 +00:00
|
|
|
EndTurn pack;
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack); //report that we ended turn
|
2008-07-27 17:07:37 +00:00
|
|
|
}
|
2009-04-12 00:58:41 +00:00
|
|
|
int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
|
2008-01-26 19:36:31 +00:00
|
|
|
{
|
2009-03-20 18:51:48 +00:00
|
|
|
ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-04-13 11:05:39 +00:00
|
|
|
return 0;
|
2008-01-26 19:36:31 +00:00
|
|
|
}
|
2008-01-27 16:07:27 +00:00
|
|
|
|
2009-04-12 00:58:41 +00:00
|
|
|
int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
|
|
|
|
{
|
2009-03-20 18:51:48 +00:00
|
|
|
ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-04-13 11:05:39 +00:00
|
|
|
return 0;
|
2008-01-29 22:47:43 +00:00
|
|
|
}
|
2009-04-12 00:58:41 +00:00
|
|
|
int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
|
2008-01-29 22:47:43 +00:00
|
|
|
{
|
2009-03-20 18:51:48 +00:00
|
|
|
ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-04-19 16:15:04 +00:00
|
|
|
return 0;
|
2008-01-29 22:47:43 +00:00
|
|
|
}
|
|
|
|
|
2008-01-27 16:07:27 +00:00
|
|
|
bool CCallback::dismissHero(const CGHeroInstance *hero)
|
|
|
|
{
|
2008-08-01 21:41:38 +00:00
|
|
|
if(player!=hero->tempOwner) return false;
|
2009-03-20 18:51:48 +00:00
|
|
|
|
|
|
|
DismissHero pack(hero->id);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-08-01 21:41:38 +00:00
|
|
|
return true;
|
2008-01-27 16:07:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 11:36:52 +00:00
|
|
|
// int CCallback::getMySerial() const
|
|
|
|
// {
|
|
|
|
// boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
// return gs->players[player].serial;
|
|
|
|
// }
|
2007-11-18 22:58:28 +00:00
|
|
|
|
2009-03-29 12:02:37 +00:00
|
|
|
bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
|
2008-01-30 10:55:43 +00:00
|
|
|
{
|
2010-08-12 15:54:25 +00:00
|
|
|
if(player!=hero1->tempOwner && player!=hero2->tempOwner)
|
2008-01-30 10:55:43 +00:00
|
|
|
return false;
|
2009-03-29 12:02:37 +00:00
|
|
|
|
2009-04-03 22:34:31 +00:00
|
|
|
ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&ea);
|
2008-01-30 10:55:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-18 22:58:28 +00:00
|
|
|
|
2010-02-16 14:39:56 +00:00
|
|
|
/**
|
|
|
|
* Assembles or disassembles a combination artifact.
|
|
|
|
* @param hero Hero holding the artifact(s).
|
|
|
|
* @param artifactSlot The worn slot ID of the combination- or constituent artifact.
|
|
|
|
* @param assemble True for assembly operation, false for disassembly.
|
|
|
|
* @param assembleTo If assemble is true, this represents the artifact ID of the combination
|
|
|
|
* artifact to assemble to. Otherwise it's not used.
|
|
|
|
*/
|
|
|
|
bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
|
|
|
|
{
|
|
|
|
if (player != hero->tempOwner)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
|
|
|
|
sendRequest(&aa);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:21:15 +00:00
|
|
|
bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
|
2008-03-21 00:03:31 +00:00
|
|
|
{
|
|
|
|
CGTownInstance * t = const_cast<CGTownInstance *>(town);
|
2008-05-03 15:30:11 +00:00
|
|
|
|
2008-08-01 11:21:15 +00:00
|
|
|
if(town->tempOwner!=player)
|
2008-03-23 01:01:17 +00:00
|
|
|
return false;
|
2010-12-20 21:22:53 +00:00
|
|
|
const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
|
2009-08-05 09:46:55 +00:00
|
|
|
for(int i=0;i<b->resources.size();i++)
|
2008-08-01 11:21:15 +00:00
|
|
|
if(b->resources[i] > gs->players[player].resources[i])
|
|
|
|
return false; //lack of resources
|
|
|
|
|
2009-03-20 18:51:48 +00:00
|
|
|
BuildStructure pack(town->id,buildingID);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-03-21 00:03:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-22 20:14:40 +00:00
|
|
|
int CBattleCallback::battleMakeAction(BattleAction* action)
|
2008-09-28 21:01:49 +00:00
|
|
|
{
|
2010-12-08 20:17:05 +00:00
|
|
|
assert(action->actionType == BattleAction::HERO_SPELL);
|
2009-03-20 18:51:48 +00:00
|
|
|
MakeCustomAction mca(*action);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&mca);
|
2008-09-28 21:01:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-22 20:14:40 +00:00
|
|
|
template <typename T>
|
|
|
|
void CBattleCallback::sendRequest(const T* request)
|
|
|
|
{
|
|
|
|
//TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
|
|
|
|
if(waitTillRealize)
|
|
|
|
cl->waitingRequest.set(true);
|
|
|
|
|
|
|
|
*cl->serv << request;
|
|
|
|
|
|
|
|
if(waitTillRealize)
|
|
|
|
cl->waitingRequest.waitWhileTrue();
|
|
|
|
}
|
|
|
|
|
2008-08-16 08:47:41 +00:00
|
|
|
void CCallback::swapGarrisonHero( const CGTownInstance *town )
|
|
|
|
{
|
|
|
|
if(town->tempOwner != player) return;
|
2009-03-20 18:51:48 +00:00
|
|
|
|
|
|
|
GarrisonHeroSwap pack(town->id);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-08-25 10:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
|
|
|
|
{
|
|
|
|
if(hero->tempOwner != player) return;
|
2009-03-20 18:51:48 +00:00
|
|
|
|
|
|
|
BuyArtifact pack(hero->id,aid);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-08-28 17:36:34 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 07:01:54 +00:00
|
|
|
void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
|
2008-09-07 03:38:37 +00:00
|
|
|
{
|
2010-05-18 07:01:54 +00:00
|
|
|
TradeOnMarketplace pack;
|
|
|
|
pack.market = market;
|
|
|
|
pack.hero = hero;
|
|
|
|
pack.mode = mode;
|
|
|
|
pack.r1 = id1;
|
|
|
|
pack.r2 = id2;
|
|
|
|
pack.val = val1;
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-09-18 13:54:54 +00:00
|
|
|
}
|
|
|
|
|
2008-09-19 08:16:19 +00:00
|
|
|
void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
|
2008-09-18 13:54:54 +00:00
|
|
|
{
|
2010-05-02 18:20:26 +00:00
|
|
|
const_cast<CGHeroInstance*>(hero)-> formation = tight;
|
2009-03-20 18:51:48 +00:00
|
|
|
SetFormation pack(hero->id,tight);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2008-10-18 23:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::setSelection(const CArmedInstance * obj)
|
|
|
|
{
|
2008-12-27 01:01:59 +00:00
|
|
|
SetSelection ss;
|
|
|
|
ss.player = player;
|
|
|
|
ss.id = obj->id;
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&ss);
|
2009-09-12 22:17:23 +00:00
|
|
|
|
|
|
|
if(obj->ID == HEROI_TYPE)
|
|
|
|
{
|
2011-06-25 14:22:19 +00:00
|
|
|
if(cl->pathInfo->hero != obj) //calculate new paths only if we selected a different hero
|
|
|
|
cl->calculatePaths(static_cast<const CGHeroInstance *>(obj));
|
|
|
|
|
2009-09-12 22:17:23 +00:00
|
|
|
//nasty workaround. TODO: nice workaround
|
|
|
|
cl->gs->getPlayer(player)->currentSelection = obj->id;
|
|
|
|
}
|
2008-10-26 20:58:34 +00:00
|
|
|
}
|
|
|
|
|
2010-07-08 23:03:27 +00:00
|
|
|
void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
|
2008-10-26 20:58:34 +00:00
|
|
|
{
|
|
|
|
ui8 i=0;
|
2009-03-09 10:37:49 +00:00
|
|
|
for(; i<gs->players[player].availableHeroes.size(); i++)
|
|
|
|
{
|
2008-10-26 20:58:34 +00:00
|
|
|
if(gs->players[player].availableHeroes[i] == hero)
|
2009-03-09 10:37:49 +00:00
|
|
|
{
|
2010-07-08 23:03:27 +00:00
|
|
|
HireHero pack(i,townOrTavern->id);
|
|
|
|
pack.player = player;
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pack);
|
2009-03-09 10:37:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-10-26 20:58:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-11 17:21:06 +00:00
|
|
|
bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
|
2009-03-19 14:17:19 +00:00
|
|
|
{
|
2009-06-11 17:21:06 +00:00
|
|
|
return gs->getPath(src,dest,hero, ret);
|
2009-03-28 00:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::save( const std::string &fname )
|
|
|
|
{
|
|
|
|
cl->save(fname);
|
2009-04-04 19:26:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 14:37:14 +00:00
|
|
|
|
|
|
|
void CCallback::sendMessage(const std::string &mess)
|
|
|
|
{
|
|
|
|
PlayerMessage pm(player, mess);
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&pm);
|
2009-05-12 03:35:51 +00:00
|
|
|
}
|
2009-07-20 01:47:49 +00:00
|
|
|
|
2009-07-26 03:33:13 +00:00
|
|
|
void CCallback::buildBoat( const IShipyard *obj )
|
|
|
|
{
|
|
|
|
BuildBoat bb;
|
|
|
|
bb.objid = obj->o->id;
|
2009-08-03 23:53:18 +00:00
|
|
|
sendRequest(&bb);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCallback::CCallback( CGameState * GS, int Player, CClient *C )
|
2010-12-22 20:14:40 +00:00
|
|
|
:CBattleCallback(GS, Player, C)
|
2009-08-03 23:53:18 +00:00
|
|
|
{
|
|
|
|
waitTillRealize = false;
|
2009-07-26 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2009-08-30 12:47:40 +00:00
|
|
|
const CGPathNode * CCallback::getPathInfo( int3 tile )
|
|
|
|
{
|
|
|
|
return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCallback::getPath2( int3 dest, CGPath &ret )
|
|
|
|
{
|
2009-10-23 02:11:45 +00:00
|
|
|
if (!gs->map->isInTheMap(dest))
|
|
|
|
return false;
|
|
|
|
|
2009-09-07 02:29:44 +00:00
|
|
|
const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
|
|
|
|
assert(cl->pathInfo->hero == h);
|
|
|
|
if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
|
|
|
|
{
|
|
|
|
recalculatePaths();
|
|
|
|
}
|
2011-06-25 14:22:19 +00:00
|
|
|
boost::unique_lock<boost::mutex> pathLock(cl->pathMx);
|
2009-08-30 12:47:40 +00:00
|
|
|
return cl->pathInfo->getPath(dest, ret);
|
|
|
|
}
|
|
|
|
|
2009-09-07 02:29:44 +00:00
|
|
|
void CCallback::recalculatePaths()
|
|
|
|
{
|
2011-06-25 14:22:19 +00:00
|
|
|
cl->calculatePaths(cl->IGameCallback::getSelectedHero(player));
|
2009-09-07 02:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
|
|
|
|
{
|
|
|
|
gs->calculatePaths(hero, out, src, movement);
|
|
|
|
}
|
|
|
|
|
2010-02-21 15:03:30 +00:00
|
|
|
void CCallback::dig( const CGObjectInstance *hero )
|
|
|
|
{
|
|
|
|
DigWithHero dwh;
|
|
|
|
dwh.id = hero->id;
|
|
|
|
sendRequest(&dwh);
|
|
|
|
}
|
|
|
|
|
2010-03-10 23:16:30 +00:00
|
|
|
void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
|
|
|
|
{
|
|
|
|
CastAdvSpell cas;
|
|
|
|
cas.hid = hero->id;
|
|
|
|
cas.sid = spellID;
|
|
|
|
cas.pos = pos;
|
|
|
|
sendRequest(&cas);
|
|
|
|
}
|
2011-05-29 23:49:25 +00:00
|
|
|
|
|
|
|
boost::shared_mutex& CCallback::getGsMutex()
|
|
|
|
{
|
|
|
|
return *gs->mx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCallback::unregisterMyInterface()
|
|
|
|
{
|
|
|
|
assert(player >= 0); //works only for player callback
|
|
|
|
cl->playerint.erase(player);
|
|
|
|
cl->battleints.erase(player);
|
|
|
|
//TODO? should callback be disabled as well?
|
|
|
|
}
|
|
|
|
|
2010-12-22 20:14:40 +00:00
|
|
|
CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
|
|
|
|
{
|
|
|
|
gs = GS;
|
|
|
|
player = Player;
|
|
|
|
cl = C;
|
2011-01-08 18:33:40 +00:00
|
|
|
}
|
|
|
|
|
2011-02-12 16:12:48 +00:00
|
|
|
bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
|
|
|
|
{
|
|
|
|
MakeAction ma;
|
|
|
|
ma.ba = *action;
|
|
|
|
sendRequest(&ma);
|
|
|
|
return true;
|
|
|
|
}
|