1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-15 11:46:56 +02:00
Should also fix #152.
This commit is contained in:
Michał W. Urbańczyk 2012-08-28 12:28:13 +00:00
parent 3b1ddfa3ca
commit 830d94064e
7 changed files with 110 additions and 84 deletions

View File

@ -123,7 +123,6 @@ BattleAction CStupidAI::activeStack( const CStack * stack )
else
{
std::vector<BattleHex> avHexes = cb->battleGetAvailableHexes(stack, false);
boost::copy(stack->getHexes(), std::back_inserter(avHexes)); //add current stack position - we can attack from it
BOOST_FOREACH(BattleHex hex, avHexes)
{

View File

@ -78,10 +78,20 @@ signed char BattleHex::mutualPosition(BattleHex hex1, BattleHex hex2)
}
char BattleHex::getDistance(BattleHex hex1, BattleHex hex2)
{
int xDst = std::abs(hex1 % GameConstants::BFIELD_WIDTH - hex2 % GameConstants::BFIELD_WIDTH),
yDst = std::abs(hex1 / GameConstants::BFIELD_WIDTH - hex2 / GameConstants::BFIELD_WIDTH);
return std::max(xDst, yDst) + std::min(xDst, yDst) - (yDst + (yDst + xDst < 2 ? 0 : 1))/2;
{
int y1 = hex1.getY(),
y2 = hex2.getY();
int x1 = hex1.getX() + y1 / 2.0,
x2 = hex2.getX() + y2 / 2.0;
int xDst = x2 - x1,
yDst = y2 - y1;
if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0))
return std::max(std::abs(xDst), std::abs(yDst));
else
return std::abs(xDst) + std::abs(yDst);
}
void BattleHex::checkAndPush(BattleHex tile, std::vector<BattleHex> & ret)

View File

@ -187,7 +187,7 @@ const CStack * BattleInfo::getNextStack() const
// };
//
BattleHex BattleInfo::getClosestTile (bool attackerOwned, int initialPos, std::set<BattleHex> & possibilities) const
BattleHex BattleInfo::getClosestTile(bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities) const
{
std::vector<BattleHex> sortedTiles (possibilities.begin(), possibilities.end()); //set can't be sorted properly :(
@ -206,14 +206,22 @@ BattleHex BattleInfo::getClosestTile (bool attackerOwned, int initialPos, std::s
return closestDistance < here.getDistance (initialPos, here);
};
sortedTiles.erase (boost::remove_if (sortedTiles, notClosest), sortedTiles.end()); //only closest tiles are interesting
vstd::erase_if(sortedTiles, notClosest); //only closest tiles are interesting
auto compareHorizontal = [attackerOwned](const BattleHex left, const BattleHex right) -> bool
auto compareHorizontal = [attackerOwned, initialPos](const BattleHex left, const BattleHex right) -> bool
{
if (attackerOwned)
return left.getX() > right.getX(); //find furthest right
if(left.getX() != right.getX())
{
if (attackerOwned)
return left.getX() > right.getX(); //find furthest right
else
return left.getX() < right.getX(); //find furthest left
}
else
return left.getX() < right.getX(); //find furthest left
{
//Prefer tiles in the same row.
return std::abs(left.getY() - initialPos.getY()) < std::abs(right.getY() - initialPos.getY());
}
};
boost::sort (sortedTiles, compareHorizontal);
@ -438,14 +446,17 @@ CStack * BattleInfo::generateNewStack(const CStackInstance &base, bool attackerO
CStack * ret = new CStack(&base, owner, stackID, attackerOwned, slot);
ret->position = getAvaliableHex (base.getCreatureID(), attackerOwned, position); //TODO: what if no free tile on battlefield was found?
ret->state.insert(EBattleStackState::ALIVE); //alive state indication
return ret;
}
CStack * BattleInfo::generateNewStack(const CStackBasicDescriptor &base, bool attackerOwned, int slot, BattleHex position) const
{
int stackID = getIdForNewStack();
int owner = attackerOwned ? sides[0] : sides[1];
CStack * ret = new CStack(&base, owner, stackID, attackerOwned, slot);
ret->position = position;
ret->state.insert(EBattleStackState::ALIVE); //alive state indication
return ret;
}
@ -921,6 +932,30 @@ BattleInfo * BattleInfo::setupBattle( int3 tile, int terrain, int battlefieldTyp
commanderBank.push_back (position.Float());
}
//adding war machines
if(!creatureBank)
{
//Checks if hero has artifact and create appropriate stack
auto handleWarMachine= [&](int side, int artid, int cretype, int hex)
{
if(heroes[side] && heroes[side]->getArt(artid))
stacks.push_back(curB->generateNewStack(CStackBasicDescriptor(cretype, 1), true, 255, hex));
};
handleWarMachine(0, 13, 146, 52); //ballista
handleWarMachine(0, 14, 148, 18); //ammo cart
handleWarMachine(0, 15, 147, 154);//first aid tent
if(town && town->hasFort())
handleWarMachine(0, 3, 145, 120);//catapult
if(!town) //defending hero shouldn't receive ballista (bug #551)
handleWarMachine(1, 13, 146, 66); //ballista
handleWarMachine(1, 14, 148, 32); //ammo cart
handleWarMachine(1, 15, 147, 168); //first aid tent
}
//war machines added
//battleStartpos read
int k = 0; //stack serial
for(TSlots::const_iterator i = armies[0]->Slots().begin(); i!=armies[0]->Slots().end(); i++, k++)
@ -951,69 +986,22 @@ BattleInfo * BattleInfo::setupBattle( int3 tile, int terrain, int battlefieldTyp
CStack * stack = curB->generateNewStack(*i->second, false, i->first, pos);
stacks.push_back(stack);
}
// //shifting positions of two-hex creatures
// for(unsigned g=0; g<stacks.size(); ++g)
// {
// //we should do that for creature bank too
// if(stacks[g]->doubleWide() && stacks[g]->attackerOwned)
// {
// stacks[g]->position += BattleHex::RIGHT;
// }
// else if(stacks[g]->doubleWide() && !stacks[g]->attackerOwned)
// {
// if (stacks[g]->position.getX() > 1)
// stacks[g]->position += BattleHex::LEFT;
// }
// }
//shifting positions of two-hex creatures
for(unsigned g=0; g<stacks.size(); ++g)
{
//we should do that for creature bank too
if(stacks[g]->doubleWide() && stacks[g]->attackerOwned)
{
stacks[g]->position += BattleHex::RIGHT;
}
else if(stacks[g]->doubleWide() && !stacks[g]->attackerOwned)
{
if (stacks[g]->position.getX() > 1)
stacks[g]->position += BattleHex::LEFT;
}
}
//adding war machines
if(!creatureBank)
{
if(heroes[0])
{
if(heroes[0]->getArt(13)) //ballista
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(146, 1), true, 255, 52);
stacks.push_back(stack);
}
if(heroes[0]->getArt(14)) //ammo cart
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(148, 1), true, 255, 18);
stacks.push_back(stack);
}
if(heroes[0]->getArt(15)) //first aid tent
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(147, 1), true, 255, 154);
stacks.push_back(stack);
}
}
if(heroes[1])
{
//defending hero shouldn't receive ballista (bug #551)
if(heroes[1]->getArt(13) && !town) //ballista
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(146, 1), false, 255, 66);
stacks.push_back(stack);
}
if(heroes[1]->getArt(14)) //ammo cart
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(148, 1), false, 255, 32);
stacks.push_back(stack);
}
if(heroes[1]->getArt(15)) //first aid tent
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(147, 1), false, 255, 168);
stacks.push_back(stack);
}
}
if(town && heroes[0] && town->hasFort()) //catapult
{
CStack * stack = curB->generateNewStack(CStackBasicDescriptor(145, 1), true, 255, 120);
stacks.push_back(stack);
}
}
//war machines added
//adding commanders
for (int i = 0; i < 2; ++i)
@ -1338,7 +1326,6 @@ void CStack::postInit()
shots = getCreature()->valOfBonuses(Bonus::SHOTS);
counterAttacks = 1 + valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
casts = valOfBonuses(Bonus::CASTS);
state.insert(EBattleStackState::ALIVE); //alive state indication
}
ui32 CStack::Speed( int turn /*= 0*/ , bool useBind /* = false*/) const

View File

@ -90,7 +90,7 @@ struct DLL_LINKAGE BattleInfo : public CBonusSystemNode, public CBattleInfoCallb
//void getAccessibilityMap(bool *accessibility, bool twoHex, bool attackerOwned, bool addOccupiable, std::set<BattleHex> & occupyable, bool flying, const CStack* stackToOmmit = NULL) const; //send pointer to at least 187 allocated bytes
//static bool isAccessible(BattleHex hex, bool * accessibility, bool twoHex, bool attackerOwned, bool flying, bool lastPos); //helper for makeBFS
BattleHex getClosestTile (bool attackerOwned, int initialPos, std::set<BattleHex> & possibilities) const; //TODO: vector or set? copying one to another is bad
BattleHex getClosestTile (bool attackerOwned, BattleHex initialPos, std::set<BattleHex> & possibilities) const; //TODO: vector or set? copying one to another is bad
int getAvaliableHex(TCreature creID, bool attackerOwned, int initialPos = -1) const; //find place for summon / clone effects
//void makeBFS(BattleHex start, bool*accessibility, BattleHex *predecessor, int *dists, bool twoHex, bool attackerOwned, bool flying, bool fillPredecessors) const; //*accessibility must be prepared bool[187] array; last two pointers must point to the at least 187-elements int arrays - there is written result
std::pair< std::vector<BattleHex>, int > getPath(BattleHex start, BattleHex dest, const CStack *stack); //returned value: pair<path, length>; length may be different than number of elements in path since flying vreatures jump between distant hexes

View File

@ -398,7 +398,7 @@ si8 CBattleInfoCallback::battleHasWallPenalty( const CStack * stack, BattleHex d
si8 CBattleInfoCallback::battleCanTeleportTo(const CStack * stack, BattleHex destHex, int telportLevel) const
{
RETURN_IF_NOT_BATTLE(false);
if(getAccesibility().accessible(destHex, stack))
if(getAccesibility(stack).accessible(destHex, stack))
return false;
if (battleGetSiegeLevel() && telportLevel < 2) //check for wall
@ -1079,6 +1079,20 @@ AccessibilityInfo CBattleInfoCallback::getAccesibility() const
return ret;
}
AccessibilityInfo CBattleInfoCallback::getAccesibility(const CStack *stack) const
{
return getAccesibility(stack->getHexes());
}
AccessibilityInfo CBattleInfoCallback::getAccesibility(const std::vector<BattleHex> &accessibleHexes) const
{
auto ret = getAccesibility();
BOOST_FOREACH(auto hex, accessibleHexes)
ret[hex] = EAccessibility::ACCESSIBLE;
return ret;
}
ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibility, const ReachabilityInfo::Parameters params) const
{
ReachabilityInfo ret;
@ -1131,7 +1145,7 @@ ReachabilityInfo CBattleInfoCallback::makeBFS(const AccessibilityInfo &accessibi
ReachabilityInfo CBattleInfoCallback::makeBFS(const CStack *stack) const
{
return makeBFS(getAccesibility(), ReachabilityInfo::Parameters(stack));
return makeBFS(getAccesibility(stack), ReachabilityInfo::Parameters(stack));
}
std::set<BattleHex> CBattleInfoCallback::getStoppers(BattlePerspective::BattlePerspective whichSidePerspective) const
@ -1216,7 +1230,8 @@ ReachabilityInfo CBattleInfoCallback::getReachability(const CStack *stack) const
if(!battleDoWeKnowAbout(!stack->attackerOwned))
{
//Stack is held by enemy, we can't use his perspective to check for reachability.
tlog3 << "Falling back to our perspective for reachability lookup for " << stack->nodeName() << std::endl;
// Happens ie. when hovering enemy stack for its range. The arg could be set properly, but it's easier to fix it here.
//tlog3 << "Falling back to our perspective for reachability lookup for " << stack->nodeName() << std::endl;
params.perspective = battleGetMySide();
}
@ -1228,13 +1243,13 @@ ReachabilityInfo CBattleInfoCallback::getReachability(const ReachabilityInfo::Pa
if(params.flying)
return getFlyingReachability(params);
else
return makeBFS(getAccesibility(), params);
return makeBFS(getAccesibility(params.knownAccessible), params);
}
ReachabilityInfo CBattleInfoCallback::getFlyingReachability(const ReachabilityInfo::Parameters params) const
{
ReachabilityInfo ret;
ret.accessibility = getAccesibility();
ret.accessibility = getAccesibility(params.knownAccessible);
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
{
@ -1965,6 +1980,7 @@ ReachabilityInfo::Parameters::Parameters(const CStack *Stack)
doubleWide = stack->doubleWide();
attackerOwned = stack->attackerOwned;
flying = stack->hasBonusOfType(Bonus::FLYING);
knownAccessible = stack->getHexes();
}
ESpellCastProblem::ESpellCastProblem CPlayerBattleCallback::battleCanCastThisSpell(const CSpell * spell) const

View File

@ -113,6 +113,7 @@ struct DLL_LINKAGE ReachabilityInfo
bool attackerOwned;
bool doubleWide;
bool flying;
std::vector<BattleHex> knownAccessible; //hexes that will be treated as accessible, even if they're occupied by stack (by default - tiles occupied by stack we do reachability for, so it doesn't block itself)
BattleHex startPosition; //assumed position of stack
BattlePerspective::BattlePerspective perspective; //some obstacles (eg. quicksands) may be invisible for some side
@ -134,7 +135,7 @@ struct DLL_LINKAGE ReachabilityInfo
bool isReachable(BattleHex hex) const
{
return predecessors[hex].isValid();
return distances[hex] < INFINITE_DIST;
}
};
@ -192,7 +193,7 @@ public:
void battleGetStackCountOutsideHexes(bool *ac) const; // returns hexes which when in front of a stack cause us to move the amount box back
std::vector<BattleHex> battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<BattleHex> * attackable = NULL) const; //returns hexes reachable by creature with id ID (valid movement destinations), does not contain stack current position
std::vector<BattleHex> battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<BattleHex> * attackable = NULL) const; //returns hexes reachable by creature with id ID (valid movement destinations), DOES contain stack current position
int battleGetSurrenderCost(int Player) const; //returns cost of surrendering battle, -1 if surrendering is not possible
ReachabilityInfo::TDistances battleGetDistances(const CStack * stack, BattleHex hex = BattleHex::INVALID, BattleHex * predecessors = NULL) const; //returns vector of distances to [dest hex number]
@ -245,6 +246,8 @@ public:
ReachabilityInfo getReachability(const CStack *stack) const;
ReachabilityInfo getReachability(const ReachabilityInfo::Parameters &params) const;
AccessibilityInfo getAccesibility() const;
AccessibilityInfo getAccesibility(const CStack *stack) const; //Hexes ocupied by stack will be marked as accessible.
AccessibilityInfo getAccesibility(const std::vector<BattleHex> &accessibleHexes) const; //given hexes will be marked as accessible
std::pair<const CStack *, BattleHex> getNearestStack(const CStack * closest, boost::logic::tribool attackerOwned) const;
protected:
ReachabilityInfo getFlyingReachability(const ReachabilityInfo::Parameters params) const;

View File

@ -939,7 +939,7 @@ int CGameHandler::moveStack(int stack, BattleHex dest)
return 0;
//initing necessary tables
auto accessibility = getAccesibility();
auto accessibility = getAccesibility(curStack);
//shifting destination (if we have double wide stack and we can occupy dest but not be exactly there)
if(!stackAtEnd && curStack->doubleWide() && !accessibility.accessible(dest, curStack))
@ -3217,6 +3217,7 @@ bool CGameHandler::makeBattleAction( BattleAction &ba )
const CStack *stack = battleGetStackByID(ba.stackNumber); //may be nullptr if action is not about stack
const bool isAboutActiveStack = stack && (stack == battleActiveStack());
switch(ba.actionType)
{
case BattleAction::WALK: //walk
@ -3239,7 +3240,16 @@ bool CGameHandler::makeBattleAction( BattleAction &ba )
complain("This stack is dead: " + stack->nodeName());
return false;
}
if(!isAboutActiveStack)
if(battleTacticDist())
{
if(stack && !stack->attackerOwned != battleGetTacticsSide())
{
complain("This is not a stack of side that has tactics!");
return false;
}
}
else if(!isAboutActiveStack)
{
complain("Action has to be about active stack!");
return false;
@ -3251,6 +3261,7 @@ bool CGameHandler::makeBattleAction( BattleAction &ba )
{
case BattleAction::END_TACTIC_PHASE: //wait
case BattleAction::BAD_MORALE:
case BattleAction::NO_ACTION:
{
StartAction start_action(ba);
sendAndApply(&start_action);