1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-02 09:02:03 +02:00
vcmi/server/HeroPoolProcessor.cpp

295 lines
8.2 KiB
C++
Raw Normal View History

/*
* HeroPoolProcessor.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 "HeroPoolProcessor.h"
#include "CGameHandler.h"
#include "../lib/CHeroHandler.h"
#include "../lib/CPlayerState.h"
#include "../lib/GameSettings.h"
#include "../lib/NetPacks.h"
#include "../lib/StartInfo.h"
#include "../lib/mapObjects/CGTownInstance.h"
#include "../lib/gameState/CGameState.h"
#include "../lib/gameState/TavernHeroesPool.h"
HeroPoolProcessor::HeroPoolProcessor()
: gameHandler(nullptr)
{
}
HeroPoolProcessor::HeroPoolProcessor(CGameHandler * gameHandler)
: gameHandler(gameHandler)
{
}
void HeroPoolProcessor::onHeroSurrendered(const PlayerColor & color, const CGHeroInstance * hero)
{
SetAvailableHero sah;
sah.slotID = 0;
sah.player = color;
sah.hid = hero->subID;
sah.army.clear();
sah.army.setCreature(SlotID(0), hero->type->initialArmy.at(0).creature, 1);
gameHandler->sendAndApply(&sah);
}
void HeroPoolProcessor::onHeroEscaped(const PlayerColor & color, const CGHeroInstance * hero)
{
SetAvailableHero sah;
sah.slotID = 0;
sah.player = color;
sah.hid = hero->subID;
gameHandler->sendAndApply(&sah);
}
void HeroPoolProcessor::clearHeroFromSlot(const PlayerColor & color, TavernHeroSlot slot)
{
SetAvailableHero sah;
sah.player = color;
sah.slotID = static_cast<int>(slot);
sah.hid = HeroTypeID::NONE;
gameHandler->sendAndApply(&sah);
}
2023-07-11 16:51:14 +02:00
void HeroPoolProcessor::selectNewHeroForSlot(const PlayerColor & color, TavernHeroSlot slot, bool needNativeHero, bool giveArmy)
{
SetAvailableHero sah;
sah.player = color;
sah.slotID = static_cast<int>(slot);
//first hero - native if possible, second hero -> any other class
CGHeroInstance *h = pickHeroFor(needNativeHero, color);
if (h)
{
sah.hid = h->subID;
2023-07-11 16:51:14 +02:00
if (giveArmy)
{
h->initArmy(getRandomGenerator(color), &sah.army);
2023-07-11 16:51:14 +02:00
}
else
{
sah.army.clear();
sah.army.setCreature(SlotID(0), h->type->initialArmy[0].creature, 1);
}
}
else
{
sah.hid = -1;
}
gameHandler->sendAndApply(&sah);
}
void HeroPoolProcessor::onNewWeek(const PlayerColor & color)
{
clearHeroFromSlot(color, TavernHeroSlot::NATIVE);
clearHeroFromSlot(color, TavernHeroSlot::RANDOM);
2023-07-11 16:51:14 +02:00
selectNewHeroForSlot(color, TavernHeroSlot::NATIVE, true, true);
selectNewHeroForSlot(color, TavernHeroSlot::RANDOM, false, true);
}
bool HeroPoolProcessor::hireHero(const CGObjectInstance *obj, const HeroTypeID & heroToRecruit, const PlayerColor & player)
{
const PlayerState * playerState = gameHandler->getPlayerState(player);
const CGTownInstance * town = gameHandler->getTown(obj->id);
if (playerState->resources[EGameResID::GOLD] < GameConstants::HERO_GOLD_COST && gameHandler->complain("Not enough gold for buying hero!"))
return false;
if (gameHandler->getHeroCount(player, false) >= VLC->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_ON_MAP_CAP) && gameHandler->complain("Cannot hire hero, too many wandering heroes already!"))
return false;
if (gameHandler->getHeroCount(player, true) >= VLC->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_TOTAL_CAP) && gameHandler->complain("Cannot hire hero, too many heroes garrizoned and wandering already!"))
return false;
2023-07-11 16:51:14 +02:00
if(town) //tavern in town
{
2023-07-11 16:51:14 +02:00
if(!town->hasBuilt(BuildingID::TAVERN) && gameHandler->complain("No tavern!"))
return false;
2023-07-11 16:51:14 +02:00
if(town->visitingHero && gameHandler->complain("There is visiting hero - no place!"))
return false;
}
2023-07-11 16:51:14 +02:00
if(obj->ID == Obj::TAVERN)
{
2023-07-11 16:51:14 +02:00
if(gameHandler->getTile(obj->visitablePos())->visitableObjects.back() != obj && gameHandler->complain("Tavern entry must be unoccupied!"))
return false;
}
auto recruitableHeroes = gameHandler->gameState()->hpool->getHeroesFor(player);
2023-07-11 16:51:14 +02:00
const CGHeroInstance * recruitedHero = nullptr;
for(const auto & hero : recruitableHeroes)
{
2023-07-11 16:51:14 +02:00
if(hero->subID == heroToRecruit)
recruitedHero = hero;
}
2023-07-11 16:51:14 +02:00
if(!recruitedHero)
{
2023-07-11 16:51:14 +02:00
gameHandler->complain("Hero is not available for hiring!");
return false;
}
HeroRecruited hr;
hr.tid = obj->id;
hr.hid = recruitedHero->subID;
hr.player = player;
hr.tile = recruitedHero->convertFromVisitablePos(obj->visitablePos());
2023-07-11 16:51:14 +02:00
if(gameHandler->getTile(hr.tile)->isWater())
{
//Create a new boat for hero
gameHandler->createObject(obj->visitablePos(), Obj::BOAT, recruitedHero->getBoatType().getNum());
hr.boatId = gameHandler->getTopObj(hr.tile)->id;
}
2023-07-11 16:51:14 +02:00
// apply netpack -> this will remove hired hero from tavern slot
gameHandler->sendAndApply(&hr);
2023-07-11 16:51:14 +02:00
if(recruitableHeroes[0] == recruitedHero)
selectNewHeroForSlot(player, TavernHeroSlot::NATIVE, false, false);
else
2023-07-11 16:51:14 +02:00
selectNewHeroForSlot(player, TavernHeroSlot::RANDOM, false, false);
gameHandler->giveResource(player, EGameResID::GOLD, -GameConstants::HERO_GOLD_COST);
if(town)
{
gameHandler->visitCastleObjects(town, recruitedHero);
gameHandler->giveSpells(town, recruitedHero);
}
return true;
}
2023-07-11 16:51:14 +02:00
std::set<const CHeroClass *> HeroPoolProcessor::findAvailableClassesFor(const PlayerColor & player) const
2023-07-11 16:51:14 +02:00
{
std::set<const CHeroClass *> result;
2023-07-11 16:51:14 +02:00
const auto & hpool = gameHandler->gameState()->hpool;
FactionID factionID = gameHandler->getPlayerSettings(player)->castle;
2023-07-11 16:51:14 +02:00
for(auto & elem : hpool->unusedHeroesFromPool())
2023-07-11 16:51:14 +02:00
{
bool heroAvailable = hpool->isHeroAvailableFor(elem.first, player);
bool heroClassBanned = elem.second->type->heroClass->selectionProbability[factionID] == 0;
2023-07-11 16:51:14 +02:00
if(heroAvailable && !heroClassBanned)
result.insert(elem.second->type->heroClass);
}
2023-07-11 16:51:14 +02:00
return result;
}
2023-07-11 16:51:14 +02:00
std::set<CGHeroInstance *> HeroPoolProcessor::findAvailableHeroesFor(const PlayerColor & player, const CHeroClass * heroClass) const
{
std::set<CGHeroInstance *> result;
2023-07-11 16:51:14 +02:00
const auto & hpool = gameHandler->gameState()->hpool;
2023-07-11 16:51:14 +02:00
for(auto & elem : hpool->unusedHeroesFromPool())
{
bool heroAvailable = hpool->isHeroAvailableFor(elem.first, player);
bool heroClassMatches = elem.second->type->heroClass == heroClass;
2023-07-11 16:51:14 +02:00
if(heroAvailable && heroClassMatches)
result.insert(elem.second);
2023-07-11 16:51:14 +02:00
}
return result;
}
const CHeroClass * HeroPoolProcessor::pickClassFor(bool isNative, const PlayerColor & player)
{
if(player >= PlayerColor::PLAYER_LIMIT)
{
logGlobal->error("Cannot pick hero for player %d. Wrong owner!", player.getStr());
return nullptr;
}
FactionID factionID = gameHandler->getPlayerSettings(player)->castle;
const auto & hpool = gameHandler->gameState()->hpool;
const auto & currentTavern = hpool->getHeroesFor(player);
std::set<const CHeroClass *> potentialClasses = findAvailableClassesFor(player);
std::set<const CHeroClass *> possibleClasses;
if(potentialClasses.empty())
2023-07-11 16:51:14 +02:00
{
logGlobal->error("There are no heroes available for player %s!", player.getStr());
return nullptr;
}
for(const auto & heroClass : potentialClasses)
{
if (isNative && heroClass->faction != factionID)
continue;
bool hasSameClass = vstd::contains_if(currentTavern, [&](const CGHeroInstance * hero){
return hero->type->heroClass == heroClass;
});
if (hasSameClass)
continue;
possibleClasses.insert(heroClass);
}
if (possibleClasses.empty())
{
logGlobal->error("Cannot pick native hero for %s. Picking any...", player.getStr());
possibleClasses = potentialClasses;
}
int totalWeight = 0;
for(const auto & heroClass : possibleClasses)
totalWeight += heroClass->selectionProbability.at(factionID);
int roll = getRandomGenerator(player).nextInt(totalWeight - 1);
for(const auto & heroClass : possibleClasses)
2023-07-11 16:51:14 +02:00
{
roll -= heroClass->selectionProbability.at(factionID);
2023-07-11 16:51:14 +02:00
if(roll < 0)
return heroClass;
2023-07-11 16:51:14 +02:00
}
return *possibleClasses.rbegin();
}
CGHeroInstance * HeroPoolProcessor::pickHeroFor(bool isNative, const PlayerColor & player)
{
const CHeroClass * heroClass = pickClassFor(isNative, player);
if(!heroClass)
return nullptr;
std::set<CGHeroInstance *> possibleHeroes = findAvailableHeroesFor(player, heroClass);
assert(!possibleHeroes.empty());
if(possibleHeroes.empty())
return nullptr;
return *RandomGeneratorUtil::nextItem(possibleHeroes, getRandomGenerator(player));
}
CRandomGenerator & HeroPoolProcessor::getRandomGenerator(const PlayerColor & player)
{
if (playerSeed.count(player) == 0)
playerSeed.emplace(player, CRandomGenerator(gameHandler->getRandomGenerator().nextInt()));
return playerSeed.at(player);
2023-07-11 16:51:14 +02:00
}