1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/CCallback.cpp

371 lines
9.3 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "CCallback.h"
#include "lib/CCreatureHandler.h"
2009-05-20 13:08:56 +03:00
#include "client/CGameInfo.h"
#include "lib/CGameState.h"
#include "lib/BattleState.h"
2009-05-20 13:08:56 +03:00
#include "client/CPlayerInterface.h"
#include "client/Client.h"
2009-05-20 13:08:56 +03:00
#include "lib/map.h"
#include "lib/CBuildingHandler.h"
#include "lib/CDefObjInfoHandler.h"
#include "lib/CGeneralTextHandler.h"
#include "lib/CHeroHandler.h"
#include "lib/CObjectHandler.h"
#include "lib/Connection.h"
#include "lib/NetPacks.h"
2010-04-06 11:59:24 +03:00
#include "client/mapHandler.h"
#include <boost/foreach.hpp>
#include <boost/thread.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "lib/CSpellHandler.h"
#include "lib/CArtHandler.h"
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
/*
* 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-07 00:11:17 +02:00
template <ui16 N> bool isType(CPack *pack)
{
return pack->getType() == N;
}
bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
{
CastleTeleportHero pack(who->id, where->id, 1);
sendRequest(&pack);
return true;
}
bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
{
MoveHero pack(dst,h->id);
sendRequest(&pack);
return true;
}
2007-12-25 18:25:53 +02:00
void CCallback::selectionMade(int selection, int asker)
{
QueryReply pack(asker,selection);
boost::unique_lock<boost::mutex> lock(*cl->serv->wmx);
*cl->serv << &pack;
2007-12-25 18:25:53 +02:00
}
void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
{
2010-05-15 11:33:32 +03:00
if(player!=obj->tempOwner && obj->ID != 106)
return;
RecruitCreatures pack(obj->id,ID,amount,level);
sendRequest(&pack);
}
bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
{
if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
return false;
DisbandCreature pack(stackPos,obj->id);
sendRequest(&pack);
return true;
}
2011-05-03 06:14:18 +03:00
bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
{
UpgradeCreature pack(stackPos,obj->id,newID);
sendRequest(&pack);
return false;
}
2011-05-03 06:14:18 +03:00
void CCallback::endTurn()
{
2009-10-31 18:33:11 +02:00
tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
EndTurn pack;
sendRequest(&pack); //report that we ended turn
}
int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
{
ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
sendRequest(&pack);
return 0;
}
int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
{
ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
sendRequest(&pack);
return 0;
}
int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
{
ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
sendRequest(&pack);
2008-04-19 19:15:04 +03:00
return 0;
}
bool CCallback::dismissHero(const CGHeroInstance *hero)
{
2008-08-02 00:41:38 +03:00
if(player!=hero->tempOwner) return false;
DismissHero pack(hero->id);
sendRequest(&pack);
2008-08-02 00:41:38 +03:00
return true;
}
// int CCallback::getMySerial() const
// {
// boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
// return gs->players[player].serial;
// }
bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
{
if(player!=hero1->tempOwner && player!=hero2->tempOwner)
return false;
ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
sendRequest(&ea);
return true;
}
2010-02-16 16:39:56 +02: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;
}
bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
2008-03-21 02:03:31 +02:00
{
//CGTownInstance * t = const_cast<CGTownInstance *>(town);
if(town->tempOwner!=player)
return false;
if(!canBuildStructure(town, buildingID))
return false;
// const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
// for(int i=0;i<b->resources.size();i++)
// if(b->resources[i] > gs->players[player].resources[i])
// return false; //lack of resources
BuildStructure pack(town->id,buildingID);
sendRequest(&pack);
2008-03-21 02:03:31 +02:00
return true;
}
int CBattleCallback::battleMakeAction(BattleAction* action)
{
2010-12-08 22:17:05 +02:00
assert(action->actionType == BattleAction::HERO_SPELL);
MakeCustomAction mca(*action);
sendRequest(&mca);
return 0;
}
void CBattleCallback::sendRequest(const CPack* request)
{
2011-07-05 22:05:41 +03:00
//TODO should be part of CClient (client owns connection, not CB)
//but it would have to be very tricky cause template/serialization issues
if(waitTillRealize)
cl->waitingRequest.set(typeList.getTypeID(request));
cl->serv->sendPack(*request);
if(waitTillRealize)
2011-07-05 22:05:41 +03:00
{
if(unlockGsWhenWaiting)
getGsMutex().unlock_shared();
cl->waitingRequest.waitWhileTrue();
2011-07-05 22:05:41 +03:00
if(unlockGsWhenWaiting)
getGsMutex().lock_shared();
2011-07-05 22:05:41 +03:00
}
}
void CCallback::swapGarrisonHero( const CGTownInstance *town )
{
if(town->tempOwner != player) return;
GarrisonHeroSwap pack(town->id);
sendRequest(&pack);
}
void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
{
if(hero->tempOwner != player) return;
BuyArtifact pack(hero->id,aid);
sendRequest(&pack);
}
2010-05-18 10:01:54 +03:00
void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
{
2010-05-18 10:01:54 +03:00
TradeOnMarketplace pack;
pack.market = market;
pack.hero = hero;
pack.mode = mode;
pack.r1 = id1;
pack.r2 = id2;
pack.val = val1;
sendRequest(&pack);
}
void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
{
const_cast<CGHeroInstance*>(hero)-> formation = tight;
SetFormation pack(hero->id,tight);
sendRequest(&pack);
}
void CCallback::setSelection(const CArmedInstance * obj)
{
SetSelection ss;
ss.player = player;
ss.id = obj->id;
sendRequest(&(CPackForClient&)ss);
if(obj->ID == HEROI_TYPE)
{
2011-06-25 17:22:19 +03:00
if(cl->pathInfo->hero != obj) //calculate new paths only if we selected a different hero
cl->calculatePaths(static_cast<const CGHeroInstance *>(obj));
//nasty workaround. TODO: nice workaround
cl->gs->getPlayer(player)->currentSelection = obj->id;
}
}
2010-07-09 02:03:27 +03:00
void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
{
ui8 i=0;
for(; i<gs->players[player].availableHeroes.size(); i++)
{
if(gs->players[player].availableHeroes[i] == hero)
{
2010-07-09 02:03:27 +03:00
HireHero pack(i,townOrTavern->id);
pack.player = player;
sendRequest(&pack);
return;
}
}
}
bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
{
return gs->getPath(src,dest,hero, ret);
}
void CCallback::save( const std::string &fname )
{
cl->save(fname);
}
2009-04-05 17:37:14 +03:00
void CCallback::sendMessage(const std::string &mess)
{
PlayerMessage pm(player, mess);
sendRequest(&(CPackForClient&)pm);
2009-05-12 06:35:51 +03:00
}
void CCallback::buildBoat( const IShipyard *obj )
{
BuildBoat bb;
bb.objid = obj->o->id;
sendRequest(&bb);
}
CCallback::CCallback( CGameState * GS, int Player, CClient *C )
:CBattleCallback(GS, Player, C)
{
waitTillRealize = false;
}
const CGPathNode * CCallback::getPathInfo( int3 tile )
{
return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
}
bool CCallback::getPath2( int3 dest, CGPath &ret )
{
if (!gs->map->isInTheMap(dest))
return false;
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 17:22:19 +03:00
boost::unique_lock<boost::mutex> pathLock(cl->pathMx);
return cl->pathInfo->getPath(dest, ret);
}
void CCallback::recalculatePaths()
{
2011-06-25 17:22:19 +03:00
cl->calculatePaths(cl->IGameCallback::getSelectedHero(player));
}
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 17:03:30 +02:00
void CCallback::dig( const CGObjectInstance *hero )
{
DigWithHero dwh;
dwh.id = hero->id;
sendRequest(&dwh);
}
2010-03-11 01:16:30 +02: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);
}
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?
}
CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
{
gs = GS;
player = Player;
cl = C;
}
bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
{
MakeAction ma;
ma.ba = *action;
sendRequest(&ma);
return true;
}