1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
#include "StdInc.h"
#include "../lib/NetPacks.h"
#include "CGameHandler.h"
#include "../lib/IGameCallback.h"
#include "../lib/mapping/CMap.h"
#include "../lib/CGameState.h"
#include "../lib/BattleState.h"
#include "../lib/BattleAction.h"
#include "../lib/serializer/Connection.h"
#define PLAYER_OWNS(id) (gh->getPlayerAt(c)==gh->getOwner(id))
#define ERROR_AND_RETURN \
do { if(c) { \
SystemMessage temp_message("You are not allowed to perform this action!"); \
boost::unique_lock<boost::mutex> lock(*c->wmx); \
*c << &temp_message; \
} \
logNetwork->error("Player is not allowed to perform this action!"); \
return false;} while(0)
#define WRONG_PLAYER_MSG(expectedplayer) do {std::ostringstream oss;\
oss << "You were identified as player " << gh->getPlayerAt(c) << " while expecting " << expectedplayer;\
logNetwork->error(oss.str()); \
if(c) { SystemMessage temp_message(oss.str()); boost::unique_lock<boost::mutex> lock(*c->wmx); *c << &temp_message; } } while(0)
#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)
#define COMPLAIN_AND_RETURN(txt) { gh->complain(txt); ERROR_AND_RETURN; }
/*
* 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
*
*/
CGameState* CPackForServer::GS(CGameHandler *gh)
{
return gh->gs;
}
bool SaveGame::applyGh( CGameHandler *gh )
{
gh->save(fname);
logGlobal->info("Game has been saved as %s", fname);
return true;
}
bool CommitPackage::applyGh( CGameHandler *gh )
{
gh->sendAndApply(packToCommit);
return true;
}
bool CloseServer::applyGh( CGameHandler *gh )
{
gh->close();
return true;
}
bool EndTurn::applyGh( CGameHandler *gh )
{
PlayerColor player = GS(gh)->currentPlayer;
ERROR_IF_NOT(player);
if(gh->queries.topQuery(player))
COMPLAIN_AND_RETURN("Cannot end turn before resolving queries!");
gh->states.setFlag(GS(gh)->currentPlayer,&PlayerStatus::makingTurn,false);
return true;
}
bool DismissHero::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->removeObject(gh->getObj(hid));
}
bool MoveHero::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->moveHero(hid,dest,0,transit,gh->getPlayerAt(c));
}
bool CastleTeleportHero::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->teleportHero(hid,dest,source,gh->getPlayerAt(c));
}
bool ArrangeStacks::applyGh( CGameHandler *gh )
{
//checks for owning in the gh func
return gh->arrangeStacks(id1,id2,what,p1,p2,val,gh->getPlayerAt(c));
}
bool DisbandCreature::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(id);
return gh->disbandCreature(id,pos);
}
bool BuildStructure::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(tid);
return gh->buildStructure(tid,bid);
}
bool RecruitCreatures::applyGh( CGameHandler *gh )
{
return gh->recruitCreatures(tid,dst,crid,amount,level);
}
bool UpgradeCreature::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(id);
return gh->upgradeCreature(id,pos,cid);
}
bool GarrisonHeroSwap::applyGh( CGameHandler *gh )
{
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
return gh->garrisonSwap(tid);
}
bool ExchangeArtifacts::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT(src.owningPlayer());//second hero can be ally
return gh->moveArtifact(src, dst);
}
bool AssembleArtifacts::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(heroID);
return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
}
bool BuyArtifact::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->buyArtifact(hid,aid);
}
bool TradeOnMarketplace::applyGh( CGameHandler *gh )
{
//market must be owned or visited
const IMarket *m = IMarket::castFrom(market);
if(!m)
COMPLAIN_AND_RETURN("market is not-a-market! :/");
PlayerColor player = market->tempOwner;
if(player >= PlayerColor::PLAYER_LIMIT)
player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
if(player >= PlayerColor::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!");
ERROR_IF_NOT(player);
switch(mode)
{
case EMarketMode::RESOURCE_RESOURCE:
return gh->tradeResources(m, val, player, r1, r2);
case EMarketMode::RESOURCE_PLAYER:
return gh->sendResources(val, player, static_cast<Res::ERes>(r1), PlayerColor(r2));
case EMarketMode::CREATURE_RESOURCE:
if(!hero)
COMPLAIN_AND_RETURN("Only hero can sell creatures!");
return gh->sellCreatures(val, m, hero, SlotID(r1), static_cast<Res::ERes>(r2));
case EMarketMode::RESOURCE_ARTIFACT:
if(!hero)
COMPLAIN_AND_RETURN("Only hero can buy artifacts!");
return gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1), ArtifactID(r2));
case EMarketMode::ARTIFACT_RESOURCE:
if(!hero)
COMPLAIN_AND_RETURN("Only hero can sell artifacts!");
return gh->sellArtifact(m, hero, ArtifactInstanceID(r1), static_cast<Res::ERes>(r2));
case EMarketMode::CREATURE_UNDEAD:
return gh->transformInUndead(m, hero, SlotID(r1));
case EMarketMode::RESOURCE_SKILL:
return gh->buySecSkill(m, hero, SecondarySkill(r2));
case EMarketMode::CREATURE_EXP:
return gh->sacrificeCreatures(m, hero, SlotID(r1), val);
case EMarketMode::ARTIFACT_EXP:
return gh->sacrificeArtifact(m, hero, ArtifactPosition(r1));
default:
COMPLAIN_AND_RETURN("Unknown exchange mode!");
}
}
bool SetFormation::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->setFormation(hid,formation);
}
bool HireHero::applyGh( CGameHandler *gh )
{
const CGObjectInstance *obj = gh->getObj(tid);
const CGTownInstance *town = dynamic_ptr_cast<CGTownInstance>(obj);
if(town && PlayerRelations::ENEMIES == gh->getPlayerRelations(obj->tempOwner, gh->getPlayerAt(c)))
COMPLAIN_AND_RETURN("Can't buy hero in enemy town!");
return gh->hireHero(obj, hid,player);
}
bool BuildBoat::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(objid);
return gh->buildBoat(objid);
}
bool QueryReply::applyGh( CGameHandler *gh )
{
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!");
if(qid == QueryID(-1))
COMPLAIN_AND_RETURN("Cannot answer the query with id -1!");
assert(vstd::contains(gh->states.players, player));
return gh->queryReply(qid, answer, player);
}
bool MakeAction::applyGh( CGameHandler *gh )
{
const BattleInfo *b = GS(gh)->curB;
if(!b) ERROR_AND_RETURN;
if(b->tacticDistance)
{
if(ba.actionType != Battle::WALK && ba.actionType != Battle::END_TACTIC_PHASE
&& ba.actionType != Battle::RETREAT && ba.actionType != Battle::SURRENDER)
ERROR_AND_RETURN;
if(gh->connections[b->sides[b->tacticsSide].color] != c)
ERROR_AND_RETURN;
}
else if(gh->connections[b->battleGetStackByID(b->activeStack)->owner] != c)
ERROR_AND_RETURN;
return gh->makeBattleAction(ba);
}
bool MakeCustomAction::applyGh( CGameHandler *gh )
{
const BattleInfo *b = GS(gh)->curB;
if(!b) ERROR_AND_RETURN;
if(b->tacticDistance) ERROR_AND_RETURN;
const CStack *active = GS(gh)->curB->battleGetStackByID(GS(gh)->curB->activeStack);
if(!active) ERROR_AND_RETURN;
if(gh->connections[active->owner] != c) ERROR_AND_RETURN;
if(ba.actionType != Battle::HERO_SPELL) ERROR_AND_RETURN;
return gh->makeCustomAction(ba);
}
bool DigWithHero::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(id);
return gh->dig(gh->getHero(id));
}
bool CastAdvSpell::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT_OWNS(hid);
return gh->castSpell(gh->getHero(hid), sid, pos);
}
bool PlayerMessage::applyGh( CGameHandler *gh )
{
ERROR_IF_NOT(player);
if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
gh->playerMessage(player,text, currObj);
return true;
}
|