1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/server/NetPacksServer.cpp

377 lines
9.9 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
#include "../lib/NetPacks.h"
#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"
#include "../lib/CGameState.h"
#include "../lib/battle/BattleInfo.h"
#include "../lib/battle/BattleAction.h"
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
#include "../lib/battle/Unit.h"
#include "../lib/serializer/Connection.h"
#include "../lib/spells/CSpellHandler.h"
#include "../lib/spells/ISpellMechanics.h"
bool CPackForServer::isPlayerOwns(CGameHandler * gh, ObjectInstanceID id)
{
return gh->getPlayerAt(c) == gh->getOwner(id);
}
void CPackForServer::throwNotAllowedAction()
{
if(c)
{
SystemMessage temp_message("You are not allowed to perform this action!");
*c << &temp_message;
}
logNetwork->error("Player is not allowed to perform this action!");
throw ExceptionNotAllowedAction();
}
void CPackForServer::wrongPlayerMessage(CGameHandler * gh, PlayerColor expectedplayer)
{
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());
*c << &temp_message;
}
}
void CPackForServer::throwOnWrongOwner(CGameHandler * gh, ObjectInstanceID id)
{
if(!isPlayerOwns(gh, id))
{
wrongPlayerMessage(gh, gh->getOwner(id));
throwNotAllowedAction();
}
}
void CPackForServer::throwOnWrongPlayer(CGameHandler * gh, PlayerColor player)
{
if(player != gh->getPlayerAt(c))
{
wrongPlayerMessage(gh, player);
throwNotAllowedAction();
}
}
void CPackForServer::throwAndCompain(CGameHandler * gh, std::string txt)
{
gh->complain(txt);
throwNotAllowedAction();
}
CGameState * CPackForServer::GS(CGameHandler * gh)
{
return gh->gs;
}
bool SaveGame::applyGh(CGameHandler * gh)
{
gh->save(fname);
2016-08-30 00:11:54 +02:00
logGlobal->info("Game has been saved as %s", fname);
return true;
}
bool CommitPackage::applyGh(CGameHandler * gh)
2011-05-22 21:46:52 +03:00
{
gh->sendAndApply(packToCommit);
return true;
}
bool CloseServer::applyGh(CGameHandler * gh)
{
gh->close();
return true;
}
bool LeaveGame::applyGh(CGameHandler * gh)
{
gh->playerLeftGame(c->connectionID);
return true;
}
bool EndTurn::applyGh(CGameHandler * gh)
{
2013-03-03 20:06:03 +03:00
PlayerColor player = GS(gh)->currentPlayer;
throwOnWrongPlayer(gh, player);
if(gh->queries.topQuery(player))
throwAndCompain(gh, "Cannot end turn before resolving queries!");
gh->states.setFlag(GS(gh)->currentPlayer, &PlayerStatus::makingTurn, false);
return true;
}
bool DismissHero::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, hid);
return gh->removeObject(gh->getObj(hid));
}
bool MoveHero::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, hid);
return gh->moveHero(hid, dest, 0, transit, gh->getPlayerAt(c));
}
bool CastleTeleportHero::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, 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)
{
throwOnWrongOwner(gh, id);
return gh->disbandCreature(id, pos);
}
bool BuildStructure::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, tid);
return gh->buildStructure(tid, bid);
}
bool RecruitCreatures::applyGh(CGameHandler * gh)
{
return gh->recruitCreatures(tid, dst, crid, amount, level);
}
bool UpgradeCreature::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, id);
return gh->upgradeCreature(id, pos, cid);
}
bool GarrisonHeroSwap::applyGh(CGameHandler * gh)
{
const CGTownInstance * town = gh->getTown(tid);
if(!isPlayerOwns(gh, tid) && !(town->garrisonHero && isPlayerOwns(gh, town->garrisonHero->id)))
throwNotAllowedAction(); //neither town nor garrisoned hero (if present) is ours
return gh->garrisonSwap(tid);
}
bool ExchangeArtifacts::applyGh(CGameHandler * gh)
{
throwOnWrongPlayer(gh, src.owningPlayer()); //second hero can be ally
return gh->moveArtifact(src, dst);
}
bool AssembleArtifacts::applyGh(CGameHandler * gh)
2010-02-16 16:39:56 +02:00
{
throwOnWrongOwner(gh, heroID);
2010-02-16 16:39:56 +02:00
return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
}
bool BuyArtifact::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, hid);
return gh->buyArtifact(hid, aid);
}
bool TradeOnMarketplace::applyGh(CGameHandler * gh)
{
2010-05-18 10:01:54 +03:00
//market must be owned or visited
const IMarket * m = IMarket::castFrom(market);
2010-05-18 10:01:54 +03:00
if(!m)
throwAndCompain(gh, "market is not-a-market! :/");
2010-05-18 10:01:54 +03:00
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)
throwAndCompain(gh, "No player can use this market!");
2010-05-18 10:01:54 +03:00
if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos()))
throwAndCompain(gh, "This hero can't use this marketplace!");
2010-05-18 10:01:54 +03:00
throwOnWrongPlayer(gh, player);
2010-05-18 10:01:54 +03:00
bool result = true;
switch(mode)
{
case EMarketMode::RESOURCE_RESOURCE:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r1.size(); ++i)
result &= gh->tradeResources(m, val[i], player, r1[i], r2[i]);
break;
case EMarketMode::RESOURCE_PLAYER:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r1.size(); ++i)
result &= gh->sendResources(val[i], player, static_cast<Res::ERes>(r1[i]), PlayerColor(r2[i]));
break;
case EMarketMode::CREATURE_RESOURCE:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r1.size(); ++i)
result &= gh->sellCreatures(val[i], m, hero, SlotID(r1[i]), static_cast<Res::ERes>(r2[i]));
break;
case EMarketMode::RESOURCE_ARTIFACT:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r1.size(); ++i)
result &= gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1[i]), ArtifactID(r2[i]));
break;
case EMarketMode::ARTIFACT_RESOURCE:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r1.size(); ++i)
result &= gh->sellArtifact(m, hero, ArtifactInstanceID(r1[i]), static_cast<Res::ERes>(r2[i]));
break;
case EMarketMode::CREATURE_UNDEAD:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r1.size(); ++i)
result &= gh->transformInUndead(m, hero, SlotID(r1[i]));
break;
case EMarketMode::RESOURCE_SKILL:
2017-10-28 11:04:55 +02:00
for(int i = 0; i < r2.size(); ++i)
result &= gh->buySecSkill(m, hero, SecondarySkill(r2[i]));
break;
case EMarketMode::CREATURE_EXP:
{
std::vector<SlotID> slotIDs(r1.begin(), r1.end());
std::vector<ui32> count(val.begin(), val.end());
return gh->sacrificeCreatures(m, hero, slotIDs, count);
}
case EMarketMode::ARTIFACT_EXP:
{
std::vector<ArtifactPosition> positions(r1.begin(), r1.end());
return gh->sacrificeArtifact(m, hero, positions);
}
default:
throwAndCompain(gh, "Unknown exchange mode!");
}
return result;
}
bool SetFormation::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, 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)))
throwAndCompain(gh, "Can't buy hero in enemy town!");
2010-07-09 02:03:27 +03:00
return gh->hireHero(obj, hid, player);
}
bool BuildBoat::applyGh(CGameHandler * gh)
{
throwOnWrongOwner(gh, objid);
return gh->buildBoat(objid);
}
bool QueryReply::applyGh(CGameHandler * gh)
{
auto playerToConnection = gh->connections.find(player);
if(playerToConnection == gh->connections.end())
throwAndCompain(gh, "No such player!");
if(playerToConnection->second != c)
throwAndCompain(gh, "Message came from wrong connection!");
if(qid == QueryID(-1))
throwAndCompain(gh, "Cannot answer the query with id -1!");
assert(vstd::contains(gh->states.players, player));
return gh->queryReply(qid, reply, player);
}
bool MakeAction::applyGh(CGameHandler * gh)
{
const BattleInfo * b = GS(gh)->curB;
if(!b)
throwNotAllowedAction();
if(b->tacticDistance)
{
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
if(ba.actionType != EActionType::WALK && ba.actionType != EActionType::END_TACTIC_PHASE
&& ba.actionType != EActionType::RETREAT && ba.actionType != EActionType::SURRENDER)
throwNotAllowedAction();
if(gh->connections[b->sides[b->tacticsSide].color] != c)
throwNotAllowedAction();
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
else
{
auto active = b->battleActiveUnit();
if(!active) throwNotAllowedAction();
auto unitOwner = b->battleGetOwner(active);
if(gh->connections[unitOwner] != c) throwNotAllowedAction();
}
return gh->makeBattleAction(ba);
}
bool MakeCustomAction::applyGh(CGameHandler * gh)
{
const BattleInfo * b = GS(gh)->curB;
if(!b)
throwNotAllowedAction();
if(b->tacticDistance)
throwNotAllowedAction();
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto active = b->battleActiveUnit();
if(!active)
throwNotAllowedAction();
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
auto unitOwner = b->battleGetOwner(active);
if(gh->connections[unitOwner] != c)
throwNotAllowedAction();
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
if(ba.actionType != EActionType::HERO_SPELL)
throwNotAllowedAction();
return gh->makeCustomAction(ba);
}
bool DigWithHero::applyGh(CGameHandler * gh)
2010-02-21 17:03:30 +02:00
{
throwOnWrongOwner(gh, id);
2010-02-21 17:03:30 +02:00
return gh->dig(gh->getHero(id));
}
bool CastAdvSpell::applyGh(CGameHandler * gh)
2010-03-11 01:16:30 +02:00
{
throwOnWrongOwner(gh, hid);
const CSpell * s = sid.toSpell();
if(!s)
throwNotAllowedAction();
const CGHeroInstance * h = gh->getHero(hid);
if(!h)
throwNotAllowedAction();
AdventureSpellCastParameters p;
p.caster = h;
p.pos = pos;
return s->adventureCast(gh->spellEnv, p);
2010-03-11 01:16:30 +02:00
}
bool PlayerMessage::applyGh(CGameHandler * gh)
{
if(!player.isSpectator()) // TODO: clearly not a great way to verify permissions
{
throwOnWrongPlayer(gh, player);
if(gh->getPlayerAt(c) != player)
throwNotAllowedAction();
}
gh->playerMessage(player, text, currObj);
return true;
}