2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2009-03-09 12:37:49 +02:00
|
|
|
#include "../lib/NetPacks.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2009-03-09 12:37:49 +02:00
|
|
|
#include "CGameHandler.h"
|
2010-05-18 10:01:54 +03:00
|
|
|
#include "../lib/IGameCallback.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../lib/mapping/CMap.h"
|
2010-12-25 21:23:30 +02:00
|
|
|
#include "../lib/CGameState.h"
|
|
|
|
#include "../lib/BattleState.h"
|
2011-02-12 18:12:48 +02:00
|
|
|
#include "../lib/BattleAction.h"
|
2009-03-09 12:37:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define PLAYER_OWNS(id) (gh->getPlayerAt(c)==gh->getOwner(id))
|
2011-06-11 07:54:41 +03:00
|
|
|
#define ERROR_AND_RETURN \
|
|
|
|
do { if(c) { \
|
|
|
|
SystemMessage temp_message("You are not allowed to perform this action!"); \
|
2012-09-29 17:44:06 +03:00
|
|
|
boost::unique_lock<boost::mutex> lock(*c->wmx); \
|
|
|
|
*c << &temp_message; \
|
2011-06-11 07:54:41 +03:00
|
|
|
} \
|
2013-04-10 20:18:01 +03:00
|
|
|
logNetwork->errorStream()<<"Player is not allowed to perform this action!"; \
|
2011-06-11 07:54:41 +03:00
|
|
|
return false;} while(0)
|
2011-05-28 04:02:28 +03:00
|
|
|
|
|
|
|
#define WRONG_PLAYER_MSG(expectedplayer) do {std::ostringstream oss;\
|
2013-03-03 20:06:03 +03:00
|
|
|
oss << "You were identified as player " << gh->getPlayerAt(c) << " while expecting " << expectedplayer;\
|
2013-04-10 20:18:01 +03:00
|
|
|
logNetwork->errorStream() << oss.str(); \
|
2012-09-29 17:44:06 +03:00
|
|
|
if(c) { SystemMessage temp_message(oss.str()); boost::unique_lock<boost::mutex> lock(*c->wmx); *c << &temp_message; } } while(0)
|
2011-05-28 04:02:28 +03:00
|
|
|
|
|
|
|
#define ERROR_IF_NOT_OWNS(id) do{if(!PLAYER_OWNS(id)){WRONG_PLAYER_MSG(gh->getOwner(id)); ERROR_AND_RETURN; }}while(0)
|
|
|
|
#define ERROR_IF_NOT(player) do{if(player != gh->getPlayerAt(c)){WRONG_PLAYER_MSG(player); ERROR_AND_RETURN; }}while(0)
|
2010-10-30 22:26:39 +03:00
|
|
|
#define COMPLAIN_AND_RETURN(txt) { gh->complain(txt); ERROR_AND_RETURN; }
|
2009-03-09 12:37:49 +02:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* NetPacksServer.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-27 22:17:28 +02:00
|
|
|
|
2009-03-09 12:37:49 +02:00
|
|
|
CGameState* CPackForServer::GS(CGameHandler *gh)
|
|
|
|
{
|
|
|
|
return gh->gs;
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool SaveGame::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2009-07-11 02:40:10 +03:00
|
|
|
//gh->sendMessageTo(*c,"Saving...");
|
2009-03-09 12:37:49 +02:00
|
|
|
gh->save(fname);
|
2009-07-11 02:40:10 +03:00
|
|
|
gh->sendMessageTo(*c,"Game has been saved as " + fname);
|
2009-04-16 03:28:54 +03:00
|
|
|
return true;
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:46:52 +03:00
|
|
|
bool CommitPackage::applyGh( CGameHandler *gh )
|
|
|
|
{
|
|
|
|
gh->sendAndApply(packToCommit);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool CloseServer::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
gh->close();
|
2009-04-16 03:28:54 +03:00
|
|
|
return true;
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool EndTurn::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor player = GS(gh)->currentPlayer;
|
2011-07-17 21:49:05 +03:00
|
|
|
ERROR_IF_NOT(player);
|
2013-04-20 14:34:01 +03:00
|
|
|
if(gh->queries.topQuery(player))
|
|
|
|
COMPLAIN_AND_RETURN("Cannot end turn before resolving queries!");
|
2011-07-17 21:49:05 +03:00
|
|
|
|
2009-03-09 12:37:49 +02:00
|
|
|
gh->states.setFlag(GS(gh)->currentPlayer,&PlayerStatus::makingTurn,false);
|
2009-04-16 03:28:54 +03:00
|
|
|
return true;
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool DismissHero::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid);
|
2013-02-09 00:17:39 +03:00
|
|
|
return gh->removeObject(gh->getObj(hid));
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool MoveHero::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->moveHero(hid,dest,0,gh->getPlayerAt(c));
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 22:27:35 +03:00
|
|
|
bool CastleTeleportHero::applyGh( CGameHandler *gh )
|
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid);
|
|
|
|
|
|
|
|
return gh->teleportHero(hid,dest,source,gh->getPlayerAt(c));
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool ArrangeStacks::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2009-04-16 03:28:54 +03:00
|
|
|
//checks for owning in the gh func
|
2010-08-12 18:54:25 +03:00
|
|
|
return gh->arrangeStacks(id1,id2,what,p1,p2,val,gh->getPlayerAt(c));
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool DisbandCreature::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(id);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->disbandCreature(id,pos);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool BuildStructure::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(tid);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->buildStructure(tid,bid);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool RecruitCreatures::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2014-09-19 00:18:49 +03:00
|
|
|
return gh->recruitCreatures(tid,dst,crid,amount,level);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool UpgradeCreature::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(id);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->upgradeCreature(id,pos,cid);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool GarrisonHeroSwap::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2010-08-12 18:54:25 +03:00
|
|
|
const CGTownInstance * town = gh->getTown(tid);
|
|
|
|
if (!PLAYER_OWNS(tid) && !( town->garrisonHero && PLAYER_OWNS(town->garrisonHero->id) ) )
|
|
|
|
ERROR_AND_RETURN;//neither town nor garrisoned hero (if present) is ours
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->garrisonSwap(tid);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool ExchangeArtifacts::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2012-04-14 05:20:22 +03:00
|
|
|
ERROR_IF_NOT(src.owningPlayer());//second hero can be ally
|
|
|
|
return gh->moveArtifact(src, dst);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2010-02-16 16:39:56 +02:00
|
|
|
bool AssembleArtifacts::applyGh( CGameHandler *gh )
|
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(heroID);
|
|
|
|
return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool BuyArtifact::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->buyArtifact(hid,aid);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool TradeOnMarketplace::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2010-05-18 10:01:54 +03:00
|
|
|
//market must be owned or visited
|
|
|
|
const IMarket *m = IMarket::castFrom(market);
|
|
|
|
|
|
|
|
if(!m)
|
|
|
|
COMPLAIN_AND_RETURN("market is not-a-market! :/");
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor player = market->tempOwner;
|
2010-05-18 10:01:54 +03:00
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
if(player >= PlayerColor::PLAYER_LIMIT)
|
2010-05-18 10:01:54 +03:00
|
|
|
player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
if(player >= PlayerColor::PLAYER_LIMIT)
|
2010-05-18 10:01:54 +03:00
|
|
|
COMPLAIN_AND_RETURN("No player can use this market!");
|
|
|
|
|
|
|
|
if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos()))
|
|
|
|
COMPLAIN_AND_RETURN("This hero can't use this marketplace!");
|
|
|
|
|
2011-05-28 04:02:28 +03:00
|
|
|
ERROR_IF_NOT(player);
|
2010-05-18 10:01:54 +03:00
|
|
|
|
2010-05-08 01:10:32 +03:00
|
|
|
switch(mode)
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::RESOURCE_RESOURCE:
|
2010-05-18 10:01:54 +03:00
|
|
|
return gh->tradeResources(m, val, player, r1, r2);
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::RESOURCE_PLAYER:
|
2013-03-03 20:06:03 +03:00
|
|
|
return gh->sendResources(val, player, static_cast<Res::ERes>(r1), PlayerColor(r2));
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::CREATURE_RESOURCE:
|
2010-05-26 12:47:53 +03:00
|
|
|
if(!hero)
|
|
|
|
COMPLAIN_AND_RETURN("Only hero can sell creatures!");
|
2013-02-16 17:03:47 +03:00
|
|
|
return gh->sellCreatures(val, m, hero, SlotID(r1), static_cast<Res::ERes>(r2));
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::RESOURCE_ARTIFACT:
|
2010-06-27 19:03:01 +03:00
|
|
|
if(!hero)
|
|
|
|
COMPLAIN_AND_RETURN("Only hero can buy artifacts!");
|
2013-02-11 02:24:57 +03:00
|
|
|
return gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1), ArtifactID(r2));
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::ARTIFACT_RESOURCE:
|
2011-04-23 00:51:10 +03:00
|
|
|
if(!hero)
|
|
|
|
COMPLAIN_AND_RETURN("Only hero can sell artifacts!");
|
2013-02-14 02:55:42 +03:00
|
|
|
return gh->sellArtifact(m, hero, ArtifactInstanceID(r1), static_cast<Res::ERes>(r2));
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::CREATURE_UNDEAD:
|
2013-02-16 17:03:47 +03:00
|
|
|
return gh->transformInUndead(m, hero, SlotID(r1));
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::RESOURCE_SKILL:
|
2013-02-12 22:49:40 +03:00
|
|
|
return gh->buySecSkill(m, hero, SecondarySkill(r2));
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::CREATURE_EXP:
|
2013-02-16 17:03:47 +03:00
|
|
|
return gh->sacrificeCreatures(m, hero, SlotID(r1), val);
|
2011-12-14 00:23:17 +03:00
|
|
|
case EMarketMode::ARTIFACT_EXP:
|
2013-02-12 22:49:40 +03:00
|
|
|
return gh->sacrificeArtifact(m, hero, ArtifactPosition(r1));
|
2010-05-08 01:10:32 +03:00
|
|
|
default:
|
2010-06-27 19:03:01 +03:00
|
|
|
COMPLAIN_AND_RETURN("Unknown exchange mode!");
|
2010-05-08 01:10:32 +03:00
|
|
|
}
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool SetFormation::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->setFormation(hid,formation);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool HireHero::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2010-07-09 02:03:27 +03:00
|
|
|
const CGObjectInstance *obj = gh->getObj(tid);
|
|
|
|
|
2012-09-23 21:01:04 +03:00
|
|
|
if(obj->ID == Obj::TOWN)
|
2010-07-09 02:03:27 +03:00
|
|
|
ERROR_IF_NOT_OWNS(tid);
|
|
|
|
//TODO check for visiting hero
|
|
|
|
|
|
|
|
return gh->hireHero(obj, hid,player);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-07-26 06:33:13 +03:00
|
|
|
bool BuildBoat::applyGh( CGameHandler *gh )
|
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(objid);
|
|
|
|
return gh->buildBoat(objid);
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool QueryReply::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2012-07-15 18:34:00 +03:00
|
|
|
auto playerToConnection = gh->connections.find(player);
|
|
|
|
if(playerToConnection == gh->connections.end())
|
|
|
|
COMPLAIN_AND_RETURN("No such player!");
|
|
|
|
if(playerToConnection->second != c)
|
|
|
|
COMPLAIN_AND_RETURN("Message came from wrong connection!");
|
2013-05-27 13:53:28 +03:00
|
|
|
if(qid == QueryID(-1))
|
2012-07-15 18:34:00 +03:00
|
|
|
COMPLAIN_AND_RETURN("Cannot answer the query with id -1!");
|
|
|
|
|
2011-09-06 09:00:32 +03:00
|
|
|
assert(vstd::contains(gh->states.players, player));
|
|
|
|
return gh->queryReply(qid, answer, player);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool MakeAction::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2011-02-14 00:46:04 +02:00
|
|
|
const BattleInfo *b = GS(gh)->curB;
|
|
|
|
if(!b) ERROR_AND_RETURN;
|
|
|
|
|
|
|
|
if(b->tacticDistance)
|
2011-02-12 18:12:48 +02:00
|
|
|
{
|
2013-02-04 00:05:44 +03:00
|
|
|
if(ba.actionType != Battle::WALK && ba.actionType != Battle::END_TACTIC_PHASE
|
|
|
|
&& ba.actionType != Battle::RETREAT && ba.actionType != Battle::SURRENDER)
|
2011-02-12 18:12:48 +02:00
|
|
|
ERROR_AND_RETURN;
|
2013-07-22 01:01:29 +03:00
|
|
|
if(gh->connections[b->sides[b->tacticsSide].color] != c)
|
2011-02-12 18:12:48 +02:00
|
|
|
ERROR_AND_RETURN;
|
|
|
|
}
|
2012-08-26 12:07:48 +03:00
|
|
|
else if(gh->connections[b->battleGetStackByID(b->activeStack)->owner] != c)
|
2011-02-14 00:46:04 +02:00
|
|
|
ERROR_AND_RETURN;
|
2011-02-12 18:12:48 +02:00
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->makeBattleAction(ba);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool MakeCustomAction::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2012-03-29 00:54:43 +03:00
|
|
|
const BattleInfo *b = GS(gh)->curB;
|
|
|
|
if(!b) ERROR_AND_RETURN;
|
|
|
|
if(b->tacticDistance) ERROR_AND_RETURN;
|
2012-08-26 12:07:48 +03:00
|
|
|
const CStack *active = GS(gh)->curB->battleGetStackByID(GS(gh)->curB->activeStack);
|
2012-03-29 00:54:43 +03:00
|
|
|
if(!active) ERROR_AND_RETURN;
|
|
|
|
if(gh->connections[active->owner] != c) ERROR_AND_RETURN;
|
2013-02-04 00:05:44 +03:00
|
|
|
if(ba.actionType != Battle::HERO_SPELL) ERROR_AND_RETURN;
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->makeCustomAction(ba);
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|
|
|
|
|
2010-02-21 17:03:30 +02:00
|
|
|
bool DigWithHero::applyGh( CGameHandler *gh )
|
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(id);
|
|
|
|
return gh->dig(gh->getHero(id));
|
|
|
|
}
|
|
|
|
|
2010-03-11 01:16:30 +02:00
|
|
|
bool CastAdvSpell::applyGh( CGameHandler *gh )
|
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid);
|
|
|
|
return gh->castSpell(gh->getHero(hid), sid, pos);
|
|
|
|
}
|
|
|
|
|
2009-04-16 03:28:54 +03:00
|
|
|
bool PlayerMessage::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
2011-05-28 04:02:28 +03:00
|
|
|
ERROR_IF_NOT(player);
|
2009-03-09 12:37:49 +02:00
|
|
|
if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
|
2014-09-21 20:35:53 +03:00
|
|
|
gh->playerMessage(player,text, currObj);
|
2009-04-16 03:28:54 +03:00
|
|
|
return true;
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|