2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CQuery.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
|
|
|
|
*
|
|
|
|
*/
|
2013-04-20 14:34:01 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CQuery.h"
|
|
|
|
#include "CGameHandler.h"
|
2017-06-24 16:42:05 +02:00
|
|
|
#include "../lib/battle/BattleInfo.h"
|
2015-12-02 21:05:10 +02:00
|
|
|
#include "../lib/mapObjects/MiscObjects.h"
|
2018-08-13 23:48:00 +02:00
|
|
|
#include "../lib/serializer/Cast.h"
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
boost::mutex Queries::mx;
|
|
|
|
|
|
|
|
template <typename Container>
|
2017-07-04 00:32:40 +02:00
|
|
|
std::string formatContainer(const Container & c, std::string delimeter = ", ", std::string opener = "(", std::string closer=")")
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
std::string ret = opener;
|
2013-06-29 16:05:48 +03:00
|
|
|
auto itr = std::begin(c);
|
|
|
|
if(itr != std::end(c))
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
ret += boost::lexical_cast<std::string>(*itr);
|
2013-06-29 16:05:48 +03:00
|
|
|
while(++itr != std::end(c))
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
ret += delimeter;
|
|
|
|
ret += boost::lexical_cast<std::string>(*itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret += closer;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
std::ostream & operator<<(std::ostream & out, const CQuery & query)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
return out << query.toString();
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
std::ostream & operator<<(std::ostream & out, QueryPtr query)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
return out << "[" << query.get() << "] " << query->toString();
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CQuery::CQuery(Queries * Owner):
|
|
|
|
owner(Owner)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> l(Queries::mx);
|
|
|
|
|
2013-05-27 17:20:46 +03:00
|
|
|
static QueryID QID = QueryID(0);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2013-05-27 17:20:46 +03:00
|
|
|
queryID = ++QID;
|
2016-08-30 00:11:54 +02:00
|
|
|
logGlobal->trace("Created a new query with id %d", queryID);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
CQuery::~CQuery()
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2016-08-30 00:11:54 +02:00
|
|
|
logGlobal->trace("Destructed the query with id %d", queryID);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CQuery::addPlayer(PlayerColor color)
|
|
|
|
{
|
|
|
|
if(color.isValidPlayer())
|
|
|
|
players.push_back(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CQuery::toString() const
|
|
|
|
{
|
2021-11-28 14:57:38 +02:00
|
|
|
const auto size = players.size();
|
|
|
|
const std::string plural = size > 1 ? "s" : "";
|
|
|
|
std::string names;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
names += boost::to_upper_copy<std::string>(players[i].getStr());
|
|
|
|
|
|
|
|
if(i < size - 2)
|
|
|
|
names += ", ";
|
|
|
|
else if(size > 1 && i == size - 2)
|
|
|
|
names += " and ";
|
|
|
|
}
|
|
|
|
std::string ret = boost::str(boost::format("A query of type '%s' and qid = %d affecting player%s %s")
|
|
|
|
% typeid(*this).name()
|
|
|
|
% queryID
|
|
|
|
% plural
|
|
|
|
% names
|
|
|
|
);
|
2013-04-20 14:34:01 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CQuery::endsByPlayerAnswer() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CQuery::onRemoval(PlayerColor color)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2017-07-04 00:32:40 +02:00
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
bool CQuery::blocksPack(const CPack * pack) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void CQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2017-07-04 00:32:40 +02:00
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CQuery::onExposure(QueryPtr topQuery)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2017-07-03 20:09:27 +02:00
|
|
|
logGlobal->trace("Exposed query with id %d", queryID);
|
2017-06-06 06:53:51 +02:00
|
|
|
owner->popQuery(*this);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CQuery::onAdding(PlayerColor color)
|
2013-09-28 02:46:58 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CQuery::onAdded(PlayerColor color)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CQuery::setReply(const JsonNode & reply)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CQuery::blockAllButReply(const CPack * pack) const
|
|
|
|
{
|
|
|
|
//We accept only query replies from correct player
|
|
|
|
if(auto reply = dynamic_ptr_cast<QueryReply>(pack))
|
|
|
|
return !vstd::contains(players, reply->player);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGhQuery::CGhQuery(CGameHandler * owner):
|
|
|
|
CQuery(&owner->queries), gh(owner)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CObjectVisitQuery::CObjectVisitQuery(CGameHandler * owner, const CGObjectInstance * Obj, const CGHeroInstance * Hero, int3 Tile):
|
|
|
|
CGhQuery(owner), visitedObject(Obj), visitingHero(Hero), tile(Tile), removeObjectAfterVisit(false)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
addPlayer(Hero->tempOwner);
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:53:44 +02:00
|
|
|
bool CObjectVisitQuery::blocksPack(const CPack *pack) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
//During the visit itself ALL actions are blocked.
|
|
|
|
//(However, the visit may trigger a query above that'll pass some.)
|
2015-10-28 22:53:44 +02:00
|
|
|
return true;
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CObjectVisitQuery::onRemoval(PlayerColor color)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
gh->objectVisitEnded(*this);
|
2013-05-14 16:42:52 +03:00
|
|
|
|
|
|
|
//TODO or should it be destructor?
|
|
|
|
//Can object visit affect 2 players and what would be desired behavior?
|
|
|
|
if(removeObjectAfterVisit)
|
|
|
|
gh->removeObject(visitedObject);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CObjectVisitQuery::onExposure(QueryPtr topQuery)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
//Object may have been removed and deleted.
|
|
|
|
if(gh->isValidObject(visitedObject))
|
|
|
|
topQuery->notifyObjectAboutRemoval(*this);
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
owner->popQuery(*this);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Queries::popQuery(PlayerColor player, QueryPtr query)
|
|
|
|
{
|
2016-09-22 20:35:37 +02:00
|
|
|
//LOG_TRACE_PARAMS(logGlobal, "player='%s', query='%s'", player % query);
|
2013-04-20 14:34:01 +03:00
|
|
|
if(topQuery(player) != query)
|
|
|
|
{
|
2016-08-30 00:11:54 +02:00
|
|
|
logGlobal->trace("Cannot remove, not a top!");
|
2013-04-20 14:34:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queries[player] -= query;
|
|
|
|
auto nextQuery = topQuery(player);
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
query->onRemoval(player);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2014-03-23 15:59:03 +03:00
|
|
|
//Exposure on query below happens only if removal didn't trigger any new query
|
2013-04-20 14:34:01 +03:00
|
|
|
if(nextQuery && nextQuery == topQuery(player))
|
2017-06-06 06:53:51 +02:00
|
|
|
nextQuery->onExposure(query);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Queries::popQuery(const CQuery &query)
|
|
|
|
{
|
2016-09-22 20:35:37 +02:00
|
|
|
//LOG_TRACE_PARAMS(logGlobal, "query='%s'", query);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
assert(query.players.size());
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto player : query.players)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
auto top = topQuery(player);
|
|
|
|
if(top.get() == &query)
|
|
|
|
popQuery(top);
|
|
|
|
else
|
2016-08-30 00:11:54 +02:00
|
|
|
logGlobal->trace("Cannot remove query %s", query.toString());
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Queries::popQuery(QueryPtr query)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto player : query->players)
|
2013-04-20 14:34:01 +03:00
|
|
|
popQuery(player, query);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Queries::addQuery(QueryPtr query)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto player : query->players)
|
2013-04-20 14:34:01 +03:00
|
|
|
addQuery(player, query);
|
2017-06-06 06:53:51 +02:00
|
|
|
|
|
|
|
for(auto player : query->players)
|
|
|
|
query->onAdded(player);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Queries::addQuery(PlayerColor player, QueryPtr query)
|
|
|
|
{
|
2016-09-22 20:35:37 +02:00
|
|
|
//LOG_TRACE_PARAMS(logGlobal, "player='%d', query='%s'", player.getNum() % query);
|
2017-06-06 06:53:51 +02:00
|
|
|
query->onAdding(player);
|
2013-04-20 14:34:01 +03:00
|
|
|
queries[player].push_back(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryPtr Queries::topQuery(PlayerColor player)
|
|
|
|
{
|
|
|
|
return vstd::backOrNull(queries[player]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Queries::popIfTop(QueryPtr query)
|
|
|
|
{
|
2016-09-22 20:35:37 +02:00
|
|
|
//LOG_TRACE_PARAMS(logGlobal, "query='%d'", query);
|
2013-06-17 18:45:55 +03:00
|
|
|
if(!query)
|
2016-08-30 00:11:54 +02:00
|
|
|
logGlobal->error("The query is nullptr! Ignoring.");
|
2013-06-17 18:45:55 +03:00
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
popIfTop(*query);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void Queries::popIfTop(const CQuery & query)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(PlayerColor color : query.players)
|
2013-04-20 14:34:01 +03:00
|
|
|
if(topQuery(color).get() == &query)
|
|
|
|
popQuery(color, topQuery(color));
|
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::vector<std::shared_ptr<const CQuery>> Queries::allQueries() const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
std::vector<std::shared_ptr<const CQuery>> ret;
|
2017-07-04 00:32:40 +02:00
|
|
|
for(auto & playerQueries : queries)
|
|
|
|
for(auto & query : playerQueries.second)
|
2013-04-20 14:34:01 +03:00
|
|
|
ret.push_back(query);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:39:57 +02:00
|
|
|
std::vector<QueryPtr> Queries::allQueries()
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
//TODO code duplication with const function :(
|
2020-10-19 21:39:57 +02:00
|
|
|
std::vector<QueryPtr> ret;
|
2017-07-04 00:32:40 +02:00
|
|
|
for(auto & playerQueries : queries)
|
|
|
|
for(auto & query : playerQueries.second)
|
|
|
|
ret.push_back(query);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:39:57 +02:00
|
|
|
QueryPtr Queries::getQuery(QueryID queryID)
|
|
|
|
{
|
|
|
|
for(auto & playerQueries : queries)
|
|
|
|
for(auto & query : playerQueries.second)
|
|
|
|
if(query->queryID == queryID)
|
|
|
|
return query;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void CBattleQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
assert(result);
|
|
|
|
objectVisit.visitedObject->battleFinished(objectVisit.visitingHero, *result);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
CBattleQuery::CBattleQuery(CGameHandler * owner, const BattleInfo * Bi):
|
2017-06-06 06:53:51 +02:00
|
|
|
CGhQuery(owner)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2013-07-22 01:01:29 +03:00
|
|
|
belligerents[0] = Bi->sides[0].armyObject;
|
|
|
|
belligerents[1] = Bi->sides[1].armyObject;
|
2013-04-20 14:34:01 +03:00
|
|
|
|
|
|
|
bi = Bi;
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
for(auto & side : bi->sides)
|
2013-07-22 01:01:29 +03:00
|
|
|
addPlayer(side.color);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CBattleQuery::CBattleQuery(CGameHandler * owner):
|
|
|
|
CGhQuery(owner), bi(nullptr)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2016-11-27 21:37:41 +02:00
|
|
|
belligerents[0] = belligerents[1] = nullptr;
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
bool CBattleQuery::blocksPack(const CPack * pack) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2015-10-28 22:53:44 +02:00
|
|
|
const char * name = typeid(*pack).name();
|
|
|
|
return strcmp(name, typeid(MakeAction).name()) && strcmp(name, typeid(MakeCustomAction).name());
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CBattleQuery::onRemoval(PlayerColor color)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
gh->battleAfterLevelUp(*result);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void CGarrisonDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
objectVisit.visitedObject->garrisonDialogClosed(objectVisit.visitingHero);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CGarrisonDialogQuery::CGarrisonDialogQuery(CGameHandler * owner, const CArmedInstance * up, const CArmedInstance * down):
|
|
|
|
CDialogQuery(owner)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
exchangingArmies[0] = up;
|
|
|
|
exchangingArmies[1] = down;
|
|
|
|
|
|
|
|
addPlayer(up->tempOwner);
|
|
|
|
addPlayer(down->tempOwner);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
bool CGarrisonDialogQuery::blocksPack(const CPack * pack) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2013-11-07 15:48:41 +03:00
|
|
|
std::set<ObjectInstanceID> ourIds;
|
2013-06-01 01:23:53 +03:00
|
|
|
ourIds.insert(this->exchangingArmies[0]->id);
|
|
|
|
ourIds.insert(this->exchangingArmies[1]->id);
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
if(auto stacks = dynamic_ptr_cast<ArrangeStacks>(pack))
|
2013-04-20 14:34:01 +03:00
|
|
|
return !vstd::contains(ourIds, stacks->id1) || !vstd::contains(ourIds, stacks->id2);
|
2015-10-28 22:53:44 +02:00
|
|
|
|
2021-11-28 14:57:38 +02:00
|
|
|
if(auto stacks = dynamic_ptr_cast<BulkSplitStack>(pack))
|
|
|
|
return !vstd::contains(ourIds, stacks->srcOwner);
|
|
|
|
|
|
|
|
if(auto stacks = dynamic_ptr_cast<BulkMergeStacks>(pack))
|
|
|
|
return !vstd::contains(ourIds, stacks->srcOwner);
|
|
|
|
|
|
|
|
if(auto stacks = dynamic_ptr_cast<BulkSmartSplitStack>(pack))
|
|
|
|
return !vstd::contains(ourIds, stacks->srcOwner);
|
|
|
|
|
|
|
|
if(auto stacks = dynamic_ptr_cast<BulkMoveArmy>(pack))
|
|
|
|
return !vstd::contains(ourIds, stacks->srcArmy) || !vstd::contains(ourIds, stacks->destArmy);
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
if(auto arts = dynamic_ptr_cast<ExchangeArtifacts>(pack))
|
2013-06-01 01:23:53 +03:00
|
|
|
{
|
|
|
|
if(auto id1 = boost::apply_visitor(GetEngagedHeroIds(), arts->src.artHolder))
|
|
|
|
if(!vstd::contains(ourIds, *id1))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(auto id2 = boost::apply_visitor(GetEngagedHeroIds(), arts->dst.artHolder))
|
|
|
|
if(!vstd::contains(ourIds, *id2))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-04 00:32:40 +02:00
|
|
|
if(auto dismiss = dynamic_ptr_cast<DisbandCreature>(pack))
|
2014-02-09 22:47:23 +03:00
|
|
|
return !vstd::contains(ourIds, dismiss->id);
|
2022-11-07 00:19:43 +02:00
|
|
|
|
2022-11-10 18:29:39 +02:00
|
|
|
if(auto arts = dynamic_ptr_cast<BulkExchangeArtifacts>(pack))
|
2022-11-07 00:19:43 +02:00
|
|
|
return !vstd::contains(ourIds, arts->srcHero) || !vstd::contains(ourIds, arts->dstHero);
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
if(auto dismiss = dynamic_ptr_cast<AssembleArtifacts>(pack))
|
2014-06-14 16:52:30 +03:00
|
|
|
return !vstd::contains(ourIds, dismiss->heroID);
|
2015-10-28 22:53:44 +02:00
|
|
|
|
|
|
|
if(auto upgrade = dynamic_ptr_cast<UpgradeCreature>(pack))
|
2015-09-15 09:54:55 +02:00
|
|
|
return !vstd::contains(ourIds, upgrade->id);
|
2017-07-04 00:32:40 +02:00
|
|
|
|
2013-04-20 14:34:01 +03:00
|
|
|
return CDialogQuery::blocksPack(pack);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void CBlockingDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
assert(answer);
|
|
|
|
objectVisit.visitedObject->blockingDialogAnswered(objectVisit.visitingHero, *answer);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CBlockingDialogQuery::CBlockingDialogQuery(CGameHandler * owner, const BlockingDialog & bd):
|
|
|
|
CDialogQuery(owner)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
this->bd = bd;
|
|
|
|
addPlayer(bd.player);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CTeleportDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2015-03-08 15:37:33 +02:00
|
|
|
{
|
2016-01-18 09:44:46 +02:00
|
|
|
// do not change to dynamic_ptr_cast - SIGSEGV!
|
|
|
|
auto obj = dynamic_cast<const CGTeleport*>(objectVisit.visitedObject);
|
2016-11-27 21:50:37 +02:00
|
|
|
if(obj)
|
|
|
|
obj->teleportDialogAnswered(objectVisit.visitingHero, *answer, td.exits);
|
|
|
|
else
|
|
|
|
logGlobal->error("Invalid instance in teleport query");
|
2015-03-08 15:37:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CTeleportDialogQuery::CTeleportDialogQuery(CGameHandler * owner, const TeleportDialog & td):
|
|
|
|
CDialogQuery(owner)
|
2015-03-08 15:37:33 +02:00
|
|
|
{
|
|
|
|
this->td = td;
|
2018-03-10 21:19:55 +02:00
|
|
|
addPlayer(td.player);
|
2015-03-08 15:37:33 +02:00
|
|
|
}
|
|
|
|
|
2018-03-10 21:19:55 +02:00
|
|
|
CHeroLevelUpDialogQuery::CHeroLevelUpDialogQuery(CGameHandler * owner, const HeroLevelUp & Hlu, const CGHeroInstance * Hero):
|
|
|
|
CDialogQuery(owner), hero(Hero)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
hlu = Hlu;
|
2018-03-10 21:19:55 +02:00
|
|
|
addPlayer(hero->tempOwner);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CHeroLevelUpDialogQuery::onRemoval(PlayerColor color)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
assert(answer);
|
2018-03-10 21:19:55 +02:00
|
|
|
logGlobal->trace("Completing hero level-up query. %s gains skill %d", hero->getObjectName(), answer.get());
|
|
|
|
gh->levelUpHero(hero, hlu.skills[*answer]);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void CHeroLevelUpDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2013-05-28 00:46:04 +03:00
|
|
|
{
|
|
|
|
objectVisit.visitedObject->heroLevelUpDone(objectVisit.visitingHero);
|
|
|
|
}
|
|
|
|
|
2018-03-10 21:19:55 +02:00
|
|
|
CCommanderLevelUpDialogQuery::CCommanderLevelUpDialogQuery(CGameHandler * owner, const CommanderLevelUp & Clu, const CGHeroInstance * Hero):
|
|
|
|
CDialogQuery(owner), hero(Hero)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
clu = Clu;
|
2018-03-10 21:19:55 +02:00
|
|
|
addPlayer(hero->tempOwner);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CCommanderLevelUpDialogQuery::onRemoval(PlayerColor color)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
assert(answer);
|
2018-03-10 21:19:55 +02:00
|
|
|
logGlobal->trace("Completing commander level-up query. Commander of hero %s gains skill %s", hero->getObjectName(), answer.get());
|
|
|
|
gh->levelUpCommander(hero->commander, clu.skills[*answer]);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:32:40 +02:00
|
|
|
void CCommanderLevelUpDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
|
2013-05-28 00:46:04 +03:00
|
|
|
{
|
|
|
|
objectVisit.visitedObject->heroLevelUpDone(objectVisit.visitingHero);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CDialogQuery::CDialogQuery(CGameHandler * owner):
|
|
|
|
CGhQuery(owner)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:53:44 +02:00
|
|
|
bool CDialogQuery::endsByPlayerAnswer() const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
bool CDialogQuery::blocksPack(const CPack * pack) const
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
2017-06-06 06:53:51 +02:00
|
|
|
return blockAllButReply(pack);
|
|
|
|
}
|
2013-04-20 14:34:01 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CDialogQuery::setReply(const JsonNode & reply)
|
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
if(reply.getType() == JsonNode::JsonType::DATA_INTEGER)
|
2017-06-06 06:53:51 +02:00
|
|
|
answer = reply.Integer();
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
CHeroMovementQuery::CHeroMovementQuery(CGameHandler * owner, const TryMoveHero & Tmh, const CGHeroInstance * Hero, bool VisitDestAfterVictory):
|
|
|
|
CGhQuery(owner), tmh(Tmh), visitDestAfterVictory(VisitDestAfterVictory), hero(Hero)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
players.push_back(hero->tempOwner);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CHeroMovementQuery::onExposure(QueryPtr topQuery)
|
2013-04-20 14:34:01 +03:00
|
|
|
{
|
|
|
|
assert(players.size() == 1);
|
|
|
|
|
|
|
|
if(visitDestAfterVictory && hero->tempOwner == players[0]) //hero still alive, so he won with the guard
|
|
|
|
//TODO what if there were H4-like escape? we should also check pos
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("Hero %s after victory over guard finishes visit to %s", hero->name, tmh.end.toString());
|
2013-04-20 14:34:01 +03:00
|
|
|
//finish movement
|
|
|
|
visitDestAfterVictory = false;
|
2022-12-09 14:42:47 +02:00
|
|
|
gh->visitObjectOnTile(*gh->getTile(hero->convertToVisitablePos(tmh.end)), hero);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
owner->popIfTop(*this);
|
2013-04-20 14:34:01 +03:00
|
|
|
}
|
2013-09-28 02:46:58 +03:00
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CHeroMovementQuery::onRemoval(PlayerColor color)
|
2013-09-28 02:46:58 +03:00
|
|
|
{
|
|
|
|
PlayerBlocked pb;
|
|
|
|
pb.player = color;
|
|
|
|
pb.reason = PlayerBlocked::ONGOING_MOVEMENT;
|
|
|
|
pb.startOrEnd = PlayerBlocked::BLOCKADE_ENDED;
|
|
|
|
gh->sendAndApply(&pb);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
void CHeroMovementQuery::onAdding(PlayerColor color)
|
2013-09-28 02:46:58 +03:00
|
|
|
{
|
|
|
|
PlayerBlocked pb;
|
|
|
|
pb.player = color;
|
|
|
|
pb.reason = PlayerBlocked::ONGOING_MOVEMENT;
|
|
|
|
pb.startOrEnd = PlayerBlocked::BLOCKADE_STARTED;
|
|
|
|
gh->sendAndApply(&pb);
|
|
|
|
}
|
2017-06-06 06:53:51 +02:00
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
CGenericQuery::CGenericQuery(Queries * Owner, PlayerColor color, std::function<void(const JsonNode &)> Callback):
|
|
|
|
CQuery(Owner), callback(Callback)
|
2017-06-06 06:53:51 +02:00
|
|
|
{
|
2017-07-03 20:09:27 +02:00
|
|
|
addPlayer(color);
|
2017-06-06 06:53:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
bool CGenericQuery::blocksPack(const CPack * pack) const
|
2017-06-06 06:53:51 +02:00
|
|
|
{
|
|
|
|
return blockAllButReply(pack);
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
bool CGenericQuery::endsByPlayerAnswer() const
|
2017-06-06 06:53:51 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
void CGenericQuery::onExposure(QueryPtr topQuery)
|
2017-06-06 06:53:51 +02:00
|
|
|
{
|
2017-07-03 20:09:27 +02:00
|
|
|
//do nothing
|
2017-06-06 06:53:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
void CGenericQuery::setReply(const JsonNode & reply)
|
2017-06-06 06:53:51 +02:00
|
|
|
{
|
2017-07-03 20:09:27 +02:00
|
|
|
callback(reply);
|
2017-06-06 06:53:51 +02:00
|
|
|
}
|