1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00
vcmi/CCallback.cpp

400 lines
10 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
#include "CCallback.h"
#include "lib/CCreatureHandler.h"
#include "lib/gameState/CGameState.h"
2009-05-20 13:08:56 +03:00
#include "client/CPlayerInterface.h"
#include "client/Client.h"
2013-04-07 13:48:07 +03:00
#include "lib/mapping/CMap.h"
#include "lib/CBuildingHandler.h"
#include "lib/CGeneralTextHandler.h"
#include "lib/CHeroHandler.h"
#include "lib/NetPacks.h"
#include "lib/CArtHandler.h"
#include "lib/GameConstants.h"
#include "lib/CPlayerState.h"
#include "lib/UnlockGuard.h"
#include "lib/battle/BattleInfo.h"
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, bool transit)
{
MoveHero pack(dst,h->id,transit);
sendRequest(&pack);
return true;
}
int CCallback::selectionMade(int selection, QueryID queryID)
{
JsonNode reply(JsonNode::JsonType::DATA_INTEGER);
reply.Integer() = selection;
return sendQueryReply(reply, queryID);
}
int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID)
2007-12-25 18:25:53 +02:00
{
2013-02-11 17:42:09 +03:00
ASSERT_IF_CALLED_WITH_PLAYER
if(queryID == QueryID(-1))
{
2017-08-10 18:39:27 +02:00
logGlobal->error("Cannot answer the query -1!");
return -1;
}
2017-07-04 00:32:40 +02:00
QueryReply pack(queryID, reply);
2013-02-11 17:42:09 +03:00
pack.player = *player;
return sendRequest(&pack);
2007-12-25 18:25:53 +02:00
}
void CCallback::recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level)
{
// TODO exception for neutral dwellings shouldn't be hardcoded
if(player != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
2010-05-15 11:33:32 +03:00
return;
RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
sendRequest(&pack);
}
2013-02-16 17:03:47 +03:00
bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
{
2015-05-07 02:16:51 +02:00
if((player && 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
2013-02-16 17:03:47 +03:00
bool CCallback::upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID)
{
UpgradeCreature pack(stackPos,obj->id,newID);
sendRequest(&pack);
return false;
}
2011-05-03 06:14:18 +03:00
void CCallback::endTurn()
{
2023-04-16 19:42:56 +02:00
logGlobal->trace("Player %d ended his turn.", player->getNum());
EndTurn pack;
sendRequest(&pack);
}
2013-02-16 17:03:47 +03:00
int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
{
ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
sendRequest(&pack);
return 0;
}
2013-02-16 17:03:47 +03:00
int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
{
ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
sendRequest(&pack);
return 0;
}
2013-02-16 17:03:47 +03:00
int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID 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;
}
int CCallback::bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot)
{
BulkMoveArmy pack(srcArmy, destArmy, srcSlot);
sendRequest(&pack);
return 0;
}
int CCallback::bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany)
{
BulkSplitStack pack(armyId, srcSlot, howMany);
sendRequest(&pack);
return 0;
}
int CCallback::bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot)
{
BulkSmartSplitStack pack(armyId, srcSlot);
sendRequest(&pack);
return 0;
}
int CCallback::bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot)
{
BulkMergeStacks pack(armyId, srcSlot);
sendRequest(&pack);
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;
}
bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
{
ExchangeArtifacts ea;
ea.src = l1;
ea.dst = l2;
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.
*/
2013-02-16 17:03:47 +03:00
bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
2010-02-16 16:39:56 +02:00
{
if (player != hero->tempOwner)
return false;
AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
sendRequest(&aa);
return true;
}
void CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap)
{
2022-11-10 23:24:41 +02:00
BulkExchangeArtifacts bma(srcHero, dstHero, swap);
sendRequest(&bma);
}
2023-04-07 23:41:55 +02:00
void CCallback::eraseArtifactByClient(const ArtifactLocation & al)
{
EraseArtifactByClient ea(al);
sendRequest(&ea);
}
2013-02-11 22:11:34 +03:00
bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
2008-03-21 02:03:31 +02:00
{
if(town->tempOwner!=player)
return false;
if(!canBuildStructure(town, buildingID))
return false;
BuildStructure pack(town->id,buildingID);
sendRequest(&pack);
2008-03-21 02:03:31 +02:00
return true;
}
void CBattleCallback::battleMakeSpellAction(const BattleAction & action)
{
assert(action.actionType == EActionType::HERO_SPELL);
MakeCustomAction mca(action);
sendRequest(&mca);
}
int CBattleCallback::sendRequest(const CPackForServer * request)
{
2013-02-11 17:42:09 +03:00
int requestID = cl->sendRequest(request, *player);
if(waitTillRealize)
2011-07-05 22:05:41 +03:00
{
2017-08-10 18:39:27 +02:00
logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
2017-06-14 06:59:41 +02:00
auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
2017-07-12 16:02:25 +02:00
CClient::waitingRequest.waitWhileContains(requestID);
2011-07-05 22:05:41 +03:00
}
2014-02-02 00:34:14 +03:00
boost::this_thread::interruption_point();
return requestID;
}
void CCallback::swapGarrisonHero( const CGTownInstance *town )
{
if(town->tempOwner == *player
|| (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
{
GarrisonHeroSwap pack(town->id);
sendRequest(&pack);
}
}
void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
{
if(hero->tempOwner != player) return;
BuyArtifact pack(hero->id,aid);
sendRequest(&pack);
}
2023-04-28 03:14:27 +02:00
void CCallback::trade(const IMarket * market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
{
2018-04-06 04:48:28 +02:00
trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
}
2023-04-28 03:14:27 +02:00
void CCallback::trade(const IMarket * market, EMarketMode::EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero)
{
TradeOnMarketplace pack;
2023-04-28 03:14:27 +02:00
pack.marketId = dynamic_cast<const CGObjectInstance *>(market)->id;
2018-04-06 04:48:28 +02:00
pack.heroId = hero ? hero->id : ObjectInstanceID();
pack.mode = mode;
2017-10-18 21:02:23 +02:00
pack.r1 = id1;
pack.r2 = id2;
pack.val = val1;
sendRequest(&pack);
}
void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
{
SetFormation pack(hero->id,tight);
sendRequest(&pack);
}
2010-07-09 02:03:27 +03:00
void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
{
assert(townOrTavern);
assert(hero);
HireHero pack(HeroTypeID(hero->subID), townOrTavern->id);
pack.player = *player;
sendRequest(&pack);
}
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, const CGObjectInstance * currentObject)
2009-04-05 17:37:14 +03:00
{
2013-02-11 17:42:09 +03:00
ASSERT_IF_CALLED_WITH_PLAYER
PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
if(player)
pm.player = *player;
sendRequest(&pm);
2009-05-12 06:35:51 +03:00
}
void CCallback::buildBoat( const IShipyard *obj )
{
BuildBoat bb;
2023-06-07 00:55:21 +02:00
bb.objid = dynamic_cast<const CGObjectInstance*>(obj)->id;
sendRequest(&bb);
}
2023-04-16 19:42:56 +02:00
CCallback::CCallback(CGameState * GS, std::optional<PlayerColor> Player, CClient * C):
CBattleCallback(Player, C)
{
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
gs = GS;
waitTillRealize = false;
unlockGsWhenWaiting = false;
}
CCallback::~CCallback()
{
//trivial, but required. Don`t remove.
}
bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
{
//bidirectional
return gs->map->canMoveBetween(a, b);
}
std::shared_ptr<const CPathsInfo> CCallback::getPathsInfo(const CGHeroInstance * h)
{
return cl->getPathsInfo(h);
}
int3 CCallback::getGuardingCreaturePosition(int3 tile)
{
if (!gs->map->isInTheMap(tile))
return int3(-1,-1,-1);
return gs->map->guardingCreaturePositions[tile.z][tile.x][tile.y];
}
2010-02-21 17:03:30 +02:00
void CCallback::dig( const CGObjectInstance *hero )
{
DigWithHero dwh;
dwh.id = hero->id;
sendRequest(&dwh);
}
void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
2010-03-11 01:16:30 +02:00
{
CastAdvSpell cas;
cas.hid = hero->id;
cas.sid = spellID;
cas.pos = pos;
sendRequest(&cas);
}
2013-02-16 17:03:47 +03:00
int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
{
if(s1->getCreature(p1) == s2->getCreature(p2))
return mergeStacks(s1, s2, p1, p2);
else
return swapCreatures(s1, s2, p1, p2);
}
void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
{
cl->additionalBattleInts[*player].push_back(battleEvents);
}
void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
{
cl->additionalBattleInts[*player] -= battleEvents;
}
#if SCRIPTING_ENABLED
scripting::Pool * CBattleCallback::getContextPool() const
{
return cl->getGlobalContextPool();
}
#endif
2023-04-16 19:42:56 +02:00
CBattleCallback::CBattleCallback(std::optional<PlayerColor> Player, CClient * C)
{
player = Player;
cl = C;
}
void CBattleCallback::battleMakeUnitAction(const BattleAction & action)
{
assert(!cl->gs->curB->tacticDistance);
MakeAction ma;
ma.ba = action;
sendRequest(&ma);
}
void CBattleCallback::battleMakeTacticAction( const BattleAction & action )
{
assert(cl->gs->curB->tacticDistance);
MakeAction ma;
ma.ba = action;
sendRequest(&ma);
}
2022-10-14 10:24:29 +02:00
2023-04-16 19:42:56 +02:00
std::optional<BattleAction> CBattleCallback::makeSurrenderRetreatDecision(const BattleStateInfoForRetreat & battleState)
2022-10-14 10:24:29 +02:00
{
2023-04-16 19:42:56 +02:00
return cl->playerint[getPlayerID().value()]->makeSurrenderRetreatDecision(battleState);
}