2009-03-09 12:37:49 +02:00
|
|
|
#include "../lib/NetPacks.h"
|
|
|
|
#include "CGameHandler.h"
|
2010-05-18 10:01:54 +03:00
|
|
|
#include "../hch/CObjectHandler.h"
|
|
|
|
#include "../lib/IGameCallback.h"
|
|
|
|
#include "../lib/map.h"
|
2009-03-09 12:37:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define PLAYER_OWNS(id) (gh->getPlayerAt(c)==gh->getOwner(id))
|
2009-03-09 21:40:43 +02:00
|
|
|
#define ERROR_AND_RETURN {if(c) *c << &SystemMessage("You are not allowed to perform this action!"); \
|
2009-03-09 12:37:49 +02:00
|
|
|
tlog1<<"Player is not allowed to perform this action!\n"; \
|
2009-04-16 03:28:54 +03:00
|
|
|
return false;}
|
2009-03-09 12:37:49 +02:00
|
|
|
#define ERROR_IF_NOT_OWNS(id) if(!PLAYER_OWNS(id)) ERROR_AND_RETURN
|
2010-05-18 10:01:54 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->removeObject(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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return gh->arrangeStacks(id1,id2,what,p1,p2,val);
|
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
|
|
|
{
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->recruitCreatures(tid,crid,amount);
|
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
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(tid);
|
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
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(hid1);
|
|
|
|
ERROR_IF_NOT_OWNS(hid2);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->swapArtifacts(hid1,hid2,slot1,slot2);
|
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! :/");
|
|
|
|
|
|
|
|
ui8 player = market->tempOwner;
|
|
|
|
|
|
|
|
if(player >= PLAYER_LIMIT)
|
|
|
|
player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
|
|
|
|
|
|
|
|
if(player >= PLAYER_LIMIT)
|
|
|
|
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!");
|
|
|
|
|
|
|
|
if(gh->getPlayerAt(c) != player)
|
|
|
|
ERROR_AND_RETURN;
|
|
|
|
|
2010-05-08 01:10:32 +03:00
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case RESOURCE_RESOURCE:
|
2010-05-18 10:01:54 +03:00
|
|
|
return gh->tradeResources(m, val, player, r1, r2);
|
2010-05-08 01:10:32 +03:00
|
|
|
case RESOURCE_PLAYER:
|
|
|
|
return gh->sendResources(val, player, r1, r2);
|
2010-05-26 12:47:53 +03:00
|
|
|
case CREATURE_RESOURCE:
|
|
|
|
if(!hero)
|
|
|
|
COMPLAIN_AND_RETURN("Only hero can sell creatures!");
|
|
|
|
return gh->sellCreatures(val, m, hero, r1, r2);
|
2010-05-08 01:10:32 +03:00
|
|
|
default:
|
|
|
|
gh->complain("Unknown exchange mode!");
|
|
|
|
ERROR_AND_RETURN;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
ERROR_IF_NOT_OWNS(tid);
|
2009-04-16 03:28:54 +03:00
|
|
|
return gh->hireHero(tid,hid);
|
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
|
|
|
{
|
2009-04-16 03:28:54 +03:00
|
|
|
//TODO - check if player matches the query
|
|
|
|
return gh->queryReply(qid,answer);
|
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
|
|
|
{
|
2009-04-16 03:28:54 +03:00
|
|
|
if(!GS(gh)->curB) ERROR_AND_RETURN;
|
2009-03-09 21:40:43 +02:00
|
|
|
if(gh->connections[GS(gh)->curB->getStack(GS(gh)->curB->activeStack)->owner] != c) ERROR_AND_RETURN;
|
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
|
|
|
{
|
2009-04-16 03:28:54 +03:00
|
|
|
if(!GS(gh)->curB) ERROR_AND_RETURN;
|
2009-03-09 21:40:43 +02:00
|
|
|
if(gh->connections[GS(gh)->curB->getStack(GS(gh)->curB->activeStack)->owner] != c) 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
|
|
|
{
|
|
|
|
if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
|
|
|
|
gh->playerMessage(player,text);
|
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 SetSelection::applyGh( CGameHandler *gh )
|
2009-03-09 12:37:49 +02:00
|
|
|
{
|
|
|
|
if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
|
2009-04-16 03:28:54 +03:00
|
|
|
if(!gh->getObj(id)) ERROR_AND_RETURN;
|
2009-03-14 13:25:25 +02:00
|
|
|
gh->sendAndApply(this);
|
2009-04-16 03:28:54 +03:00
|
|
|
return true;
|
2009-03-09 12:37:49 +02:00
|
|
|
}
|