1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Merged vcmi/beta with vcmi/develop

This commit is contained in:
Ivan Savenko
2023-01-15 17:46:42 +02:00
344 changed files with 17776 additions and 9562 deletions

View File

@@ -12,8 +12,11 @@
#include <vcmi/Artifact.h>
#include "windows/CAdvmapInterface.h"
#include "battle/CBattleInterface.h"
#include "battle/CBattleInterfaceClasses.h"
#include "battle/BattleInterface.h"
#include "battle/BattleEffectsController.h"
#include "battle/BattleFieldController.h"
#include "battle/BattleInterfaceClasses.h"
#include "battle/BattleWindow.h"
#include "../CCallback.h"
#include "windows/CCastleInterface.h"
#include "gui/CCursorHandler.h"
@@ -29,7 +32,6 @@
#include "windows/CTradeWindow.h"
#include "windows/CSpellWindow.h"
#include "../lib/CConfigHandler.h"
#include "battle/CCreatureAnimation.h"
#include "Graphics.h"
#include "windows/GUIClasses.h"
#include "../lib/CArtHandler.h"
@@ -55,6 +57,7 @@
#include "../lib/CPlayerState.h"
#include "../lib/GameConstants.h"
#include "gui/CGuiHandler.h"
#include "gui/CAnimation.h"
#include "windows/InfoWindows.h"
#include "../lib/UnlockGuard.h"
#include "../lib/CPathfinder.h"
@@ -90,7 +93,7 @@ boost::recursive_mutex * CPlayerInterface::pim = new boost::recursive_mutex;
CPlayerInterface * LOCPLINT;
CBattleInterface * CPlayerInterface::battleInt;
std::shared_ptr<BattleInterface> CPlayerInterface::battleInt;
enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
CondSh<EMoveState> stillMoveHero(STOP_MOVE); //used during hero movement
@@ -139,14 +142,16 @@ CPlayerInterface::CPlayerInterface(PlayerColor Player)
CPlayerInterface::~CPlayerInterface()
{
if(CCS->soundh) CCS->soundh->ambientStopAllChannels();
if(CCS && CCS->soundh)
CCS->soundh->ambientStopAllChannels();
logGlobal->trace("\tHuman player interface for player %s being destructed", playerID.getStr());
delete showingDialog;
delete cingconsole;
if (LOCPLINT == this)
LOCPLINT = nullptr;
}
void CPlayerInterface::init(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB)
void CPlayerInterface::initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB)
{
cb = CB;
env = ENV;
@@ -264,8 +269,8 @@ void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose)
assert(adventureInt->terrain.currentPath->nodes.size() >= 2);
std::vector<CGPathNode>::const_iterator nodesIt = adventureInt->terrain.currentPath->nodes.end() - 1;
if((nodesIt)->coord == CGHeroInstance::convertPosition(details.start, false)
&& (nodesIt - 1)->coord == CGHeroInstance::convertPosition(details.end, false))
if((nodesIt)->coord == hero->convertToVisitablePos(details.start)
&& (nodesIt - 1)->coord == hero->convertToVisitablePos(details.end))
{
//path was between entrance and exit of teleport -> OK, erase node as usual
removeLastNodeFromPath(hero);
@@ -689,7 +694,7 @@ void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet
if (settings["adventure"]["quickCombat"].Bool())
{
autofightingAI = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
autofightingAI->init(env, cb);
autofightingAI->initBattleInterface(env, cb);
autofightingAI->battleStart(army1, army2, int3(0,0,0), hero1, hero2, side);
isAutoFightOn = true;
cb->registerBattleInterface(autofightingAI);
@@ -704,7 +709,7 @@ void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet
BATTLE_EVENT_POSSIBLE_RETURN;
}
void CPlayerInterface::battleUnitsChanged(const std::vector<UnitChanges> & units, const std::vector<CustomEffectInfo> & customEffects)
void CPlayerInterface::battleUnitsChanged(const std::vector<UnitChanges> & units)
{
EVENT_HANDLER_CALLED_BY_CLIENT;
BATTLE_EVENT_POSSIBLE_RETURN;
@@ -715,34 +720,14 @@ void CPlayerInterface::battleUnitsChanged(const std::vector<UnitChanges> & units
{
case UnitChanges::EOperation::RESET_STATE:
{
const battle::Unit * unit = cb->battleGetUnitByID(info.id);
const CStack * stack = cb->battleGetStackByID(info.id );
if(!unit)
if(!stack)
{
logGlobal->error("Invalid unit ID %d", info.id);
continue;
}
auto iter = battleInt->creAnims.find(info.id);
if(iter == battleInt->creAnims.end())
{
logGlobal->error("Unit %d have no animation", info.id);
continue;
}
auto animation = iter->second;
if(unit->alive() && animation->isDead())
animation->setType(CCreatureAnim::HOLDING);
if (unit->isClone())
{
std::unique_ptr<ColorShifterDeepBlue> shifter(new ColorShifterDeepBlue());
animation->shiftColor(shifter.get());
}
//TODO: handle more cases
battleInt->stackReset(stack);
}
break;
case UnitChanges::EOperation::REMOVE:
@@ -756,7 +741,7 @@ void CPlayerInterface::battleUnitsChanged(const std::vector<UnitChanges> & units
logGlobal->error("Invalid unit ID %d", info.id);
continue;
}
battleInt->unitAdded(unit);
battleInt->stackAdded(unit);
}
break;
default:
@@ -764,8 +749,6 @@ void CPlayerInterface::battleUnitsChanged(const std::vector<UnitChanges> & units
break;
}
}
battleInt->displayCustomEffects(customEffects);
}
void CPlayerInterface::battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles)
@@ -773,7 +756,7 @@ void CPlayerInterface::battleObstaclesChanged(const std::vector<ObstacleChanges>
EVENT_HANDLER_CALLED_BY_CLIENT;
BATTLE_EVENT_POSSIBLE_RETURN;
bool needUpdate = false;
std::vector<std::shared_ptr<const CObstacleInstance>> newObstacles;
for(auto & change : obstacles)
{
@@ -781,19 +764,16 @@ void CPlayerInterface::battleObstaclesChanged(const std::vector<ObstacleChanges>
{
auto instance = cb->battleGetObstacleByID(change.id);
if(instance)
battleInt->obstaclePlaced(*instance);
newObstacles.push_back(instance);
else
logNetwork->error("Invalid obstacle instance %d", change.id);
}
else
{
needUpdate = true;
}
}
if(needUpdate)
//update accessible hexes
battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
if (!newObstacles.empty())
battleInt->obstaclePlaced(newObstacles);
battleInt->fieldController->redrawBackgroundWithHexes();
}
void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
@@ -858,36 +838,36 @@ BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when i
autofightingAI.reset();
}
CBattleInterface *b = battleInt;
assert(battleInt);
if(!b)
if(!battleInt)
{
return BattleAction::makeDefend(stack); // probably battle is finished already
}
if(CBattleInterface::givenCommand.get())
if(BattleInterface::givenCommand.get())
{
logGlobal->error("Command buffer must be clean! (we don't want to use old command)");
vstd::clear_pointer(CBattleInterface::givenCommand.data);
vstd::clear_pointer(BattleInterface::givenCommand.data);
}
{
boost::unique_lock<boost::recursive_mutex> un(*pim);
b->stackActivated(stack);
battleInt->stackActivated(stack);
//Regeneration & mana drain go there
}
//wait till BattleInterface sets its command
boost::unique_lock<boost::mutex> lock(CBattleInterface::givenCommand.mx);
while(!CBattleInterface::givenCommand.data)
boost::unique_lock<boost::mutex> lock(BattleInterface::givenCommand.mx);
while(!BattleInterface::givenCommand.data)
{
CBattleInterface::givenCommand.cond.wait(lock);
BattleInterface::givenCommand.cond.wait(lock);
if (!battleInt) //battle ended while we were waiting for movement (eg. because of spell)
throw boost::thread_interrupted(); //will shut the thread peacefully
}
//tidy up
BattleAction ret = *(CBattleInterface::givenCommand.data);
vstd::clear_pointer(CBattleInterface::givenCommand.data);
BattleAction ret = *(BattleInterface::givenCommand.data);
vstd::clear_pointer(BattleInterface::givenCommand.data);
if(ret.actionType == EActionType::CANCEL)
{
@@ -912,7 +892,7 @@ void CPlayerInterface::battleEnd(const BattleResult *br)
if(!battleInt)
{
GH.pushIntT<CBattleResultWindow>(*br, *this);
GH.pushIntT<BattleResultWindow>(*br, *this);
// #1490 - during AI turn when quick combat is on, we need to display the message and wait for user to close it.
// Otherwise NewTurn causes freeze.
waitWhileDialog();
@@ -935,12 +915,12 @@ void CPlayerInterface::battleLogMessage(const std::vector<MetaString> & lines)
battleInt->displayBattleLog(lines);
}
void CPlayerInterface::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
void CPlayerInterface::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
{
EVENT_HANDLER_CALLED_BY_CLIENT;
BATTLE_EVENT_POSSIBLE_RETURN;
battleInt->stackMoved(stack, dest, distance);
battleInt->stackMoved(stack, dest, distance, teleport);
}
void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
{
@@ -962,9 +942,9 @@ void CPlayerInterface::battleTriggerEffect (const BattleTriggerEffect & bte)
//TODO why is this different (no return on LOPLINT != this) ?
RETURN_IF_QUICK_COMBAT;
battleInt->battleTriggerEffect(bte);
battleInt->effectsController->battleTriggerEffect(bte);
}
void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged)
{
EVENT_HANDLER_CALLED_BY_CLIENT;
BATTLE_EVENT_POSSIBLE_RETURN;
@@ -974,24 +954,25 @@ void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacke
{
const CStack * defender = cb->battleGetStackByID(elem.stackAttacked, false);
const CStack * attacker = cb->battleGetStackByID(elem.attackerID, false);
if(elem.isEffect())
{
if(defender && !elem.isSecondary())
battleInt->displayEffect(elem.effect, defender->getPosition());
}
if(elem.isSpell())
{
if(defender)
battleInt->displaySpellEffect(elem.spellID, defender->getPosition());
}
//FIXME: why action is deleted during enchanter cast?
bool remoteAttack = false;
if(LOCPLINT->curAction)
remoteAttack |= LOCPLINT->curAction->actionType != EActionType::WALK_AND_ATTACK;
assert(defender);
StackAttackedInfo to_put = {defender, elem.damageAmount, elem.killedAmount, attacker, remoteAttack, elem.killed(), elem.willRebirth(), elem.cloneKilled()};
arg.push_back(to_put);
StackAttackedInfo info;
info.defender = defender;
info.attacker = attacker;
info.damageDealt = elem.damageAmount;
info.amountKilled = elem.killedAmount;
info.spellEffect = SpellID::NONE;
info.indirectAttack = ranged;
info.killed = elem.killed();
info.rebirth = elem.willRebirth();
info.cloneKilled = elem.cloneKilled();
info.fireShield = elem.fireShield();
if (elem.isSpell())
info.spellEffect = elem.spellID;
arg.push_back(info);
}
battleInt->stacksAreAttacked(arg);
}
@@ -1002,96 +983,36 @@ void CPlayerInterface::battleAttack(const BattleAttack * ba)
assert(curAction);
const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
StackAttackInfo info;
info.attacker = cb->battleGetStackByID(ba->stackAttacking);
info.defender = nullptr;
info.indirectAttack = ba->shot();
info.lucky = ba->lucky();
info.unlucky = ba->unlucky();
info.deathBlow = ba->deathBlow();
info.lifeDrain = ba->lifeDrain();
info.tile = ba->tile;
info.spellEffect = SpellID::NONE;
if(!attacker)
{
logGlobal->error("Attacking stack not found");
return;
}
if (ba->spellLike())
info.spellEffect = ba->spellID;
if(ba->lucky()) //lucky hit
for(auto & elem : ba->bsa)
{
battleInt->console->addText(attacker->formatGeneralMessage(-45));
battleInt->displayEffect(18, attacker->getPosition());
CCS->soundh->playSound(soundBase::GOODLUCK);
}
if(ba->unlucky()) //unlucky hit
{
battleInt->console->addText(attacker->formatGeneralMessage(-44));
battleInt->displayEffect(48, attacker->getPosition());
CCS->soundh->playSound(soundBase::BADLUCK);
}
if(ba->deathBlow())
{
battleInt->console->addText(attacker->formatGeneralMessage(365));
for(auto & elem : ba->bsa)
if(!elem.isSecondary())
{
const CStack * attacked = cb->battleGetStackByID(elem.stackAttacked);
battleInt->displayEffect(73, attacked->getPosition());
assert(info.defender == nullptr);
info.defender = cb->battleGetStackByID(elem.stackAttacked);
}
CCS->soundh->playSound(soundBase::deathBlow);
}
battleInt->displayCustomEffects(ba->customEffects);
battleInt->waitForAnims();
auto actionTarget = curAction->getTarget(cb.get());
if(actionTarget.empty() || (actionTarget.size() < 2 && !ba->shot()))
{
logNetwork->error("Invalid current action: no destination.");
return;
}
if(ba->shot())
{
for(auto & elem : ba->bsa)
else
{
if(!elem.isSecondary()) //display projectile only for primary target
{
const CStack * attacked = cb->battleGetStackByID(elem.stackAttacked);
battleInt->stackAttacking(attacker, attacked->getPosition(), attacked, true);
}
info.secondaryDefender.push_back(cb->battleGetStackByID(elem.stackAttacked));
}
}
else
{
auto attackTarget = actionTarget.at(1).hexValue;
assert(info.defender != nullptr);
assert(info.attacker != nullptr);
//TODO: use information from BattleAttack but not curAction
int shift = 0;
if(ba->counter() && BattleHex::mutualPosition(attackTarget, attacker->getPosition()) < 0)
{
int distp = BattleHex::getDistance(attackTarget + 1, attacker->getPosition());
int distm = BattleHex::getDistance(attackTarget - 1, attacker->getPosition());
if(distp < distm)
shift = 1;
else
shift = -1;
}
if(!ba->bsa.empty())
{
const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
battleInt->stackAttacking(attacker, ba->counter() ? BattleHex(attackTarget + shift) : attackTarget, attacked, false);
}
}
//battleInt->waitForAnims(); //FIXME: freeze
if(ba->spellLike())
{
//TODO: use information from BattleAttack but not curAction
auto destination = actionTarget.at(0).hexValue;
//display hit animation
SpellID spellID = ba->spellID;
battleInt->displaySpellHit(spellID, destination);
}
battleInt->stackAttacking(info);
}
void CPlayerInterface::battleGateStateChanged(const EGateState state)
@@ -1573,7 +1494,7 @@ void CPlayerInterface::newObject( const CGObjectInstance * obj )
//we might have built a boat in shipyard in opened town screen
if (obj->ID == Obj::BOAT
&& LOCPLINT->castleInt
&& obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
&& obj->visitablePos() == LOCPLINT->castleInt->town->bestLocation())
{
CCS->soundh->playSound(soundBase::newBuilding);
LOCPLINT->castleInt->addBuilding(BuildingID::SHIP);
@@ -1625,7 +1546,7 @@ void CPlayerInterface::playerBlocked(int reason, bool start)
GH.curInt = this;
adventureInt->selection = nullptr;
adventureInt->setPlayer(playerID);
std::string msg = CGI->generaltexth->localizedTexts["adventureMap"]["playerAttacked"].String();
std::string msg = CGI->generaltexth->translate("vcmi.adventureMap.playerAttacked");
boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
std::vector<std::shared_ptr<CComponent>> cmp;
cmp.push_back(std::make_shared<CComponent>(CComponent::flag, playerID.getNum(), 0));
@@ -2008,7 +1929,7 @@ CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
}
else
{
assert(h->getPosition(false) == path.startPos());
assert(h->visitablePos() == path.startPos());
//update the hero path in case of something has changed on map
if (LOCPLINT->cb->getPathsInfo(h)->getPath(path, path.endPos()))
return &path;
@@ -2112,7 +2033,7 @@ void CPlayerInterface::acceptTurn()
void CPlayerInterface::tryDiggging(const CGHeroInstance * h)
{
int msgToShow = -1;
const bool isBlocked = CGI->mh->hasObjectHole(h->getPosition(false)); // Don't dig in the pit.
const bool isBlocked = CGI->mh->hasObjectHole(h->visitablePos()); // Don't dig in the pit.
const auto diggingStatus = isBlocked
? EDiggingStatus::TILE_OCCUPIED
@@ -2409,7 +2330,7 @@ void CPlayerInterface::doMoveHero(const CGHeroInstance * h, CGPath path)
int i = 1;
auto getObj = [&](int3 coord, bool ignoreHero)
{
return cb->getTile(CGHeroInstance::convertPosition(coord,false))->topVisitableObj(ignoreHero);
return cb->getTile(h->convertToVisitablePos(coord))->topVisitableObj(ignoreHero);
};
auto isTeleportAction = [&](CGPathNode::ENodeAction action) -> bool
@@ -2448,7 +2369,9 @@ void CPlayerInterface::doMoveHero(const CGHeroInstance * h, CGPath path)
};
{
path.convert(0);
for (auto & elem : path.nodes)
elem.coord = h->convertFromVisitablePos(elem.coord);
TerrainId currentTerrain = Terrain::BORDER; // not init yet
TerrainId newTerrain;
int sh = -1;
@@ -2505,7 +2428,7 @@ void CPlayerInterface::doMoveHero(const CGHeroInstance * h, CGPath path)
sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
#endif
{
newTerrain = cb->getTile(CGHeroInstance::convertPosition(currentCoord, false))->terType->id;
newTerrain = cb->getTile(h->convertToVisitablePos(currentCoord))->terType->id;
if(newTerrain != currentTerrain)
{
CCS->soundh->stopSound(sh);