1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00

Drop battle elapsed time measurement, restore avHexes.

Github validation report fixes.
This commit is contained in:
MichalZr6 2025-01-03 00:11:41 +01:00
parent dad6437661
commit 4031006317
17 changed files with 60 additions and 62 deletions

View File

@ -814,21 +814,11 @@ void ApplyClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRound, pack.battleID);
}
uint64_t timeElapsed(std::chrono::time_point<std::chrono::high_resolution_clock> start)
{
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
void ApplyClientNetPackVisitor::visitBattleSetActiveStack(BattleSetActiveStack & pack)
{
if(!pack.askPlayerInterface)
return;
auto start = std::chrono::steady_clock::now();
static uint64_t duration = 0;
const CStack *activated = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
PlayerColor playerToCall; //pack.player that will move activated stack
if(activated->isHypnotized())
@ -843,8 +833,6 @@ void ApplyClientNetPackVisitor::visitBattleSetActiveStack(BattleSetActiveStack &
}
cl.startPlayerBattleAction(pack.battleID, playerToCall);
duration += timeElapsed(start);
logGlobal->warn("Battle elapsed for %ld ms", duration);
}
void ApplyClientNetPackVisitor::visitBattleLogMessage(BattleLogMessage & pack)

View File

@ -11,9 +11,9 @@
#include "BattleHex.h"
#include "BattleHexArray.h"
VCMI_LIB_NAMESPACE_BEGIN
BattleHex BattleHex::getClosestTile(const BattleHexArray & hexes, BattleSide side, BattleHex initialPos)
VCMI_LIB_NAMESPACE_BEGIN
BattleHex BattleHex::getClosestTile(BattleSide side, BattleHex initialPos, const BattleHexArray & hexes)
{
if(hexes.empty())
return BattleHex();
@ -46,7 +46,7 @@ BattleHex BattleHex::getClosestTile(const BattleHexArray & hexes, BattleSide sid
auto bestTile = std::min_element(closestTiles.begin(), closestTiles.end(), compareHorizontal);
return (bestTile != closestTiles.end()) ? *bestTile : BattleHex();
}
}
const BattleHexArray & BattleHex::getAllNeighbouringTiles() const
{
@ -61,7 +61,7 @@ const BattleHexArray & BattleHex::getNeighbouringTiles() const
const BattleHexArray & BattleHex::getNeighbouringTilesDblWide(BattleSide side) const
{
return BattleHexArray::getNeighbouringTilesDblWide(*this, side);
}
}
std::ostream & operator<<(std::ostream & os, const BattleHex & hex)
{

View File

@ -205,7 +205,7 @@ public:
return std::abs(xDst) + std::abs(yDst);
}
static BattleHex getClosestTile(const BattleHexArray & hexes, BattleSide side, BattleHex initialPos);
static BattleHex getClosestTile(BattleSide side, BattleHex initialPos, const BattleHexArray & hexes);
//Constexpr defined array with all directions used in battle
static constexpr auto hexagonalDirections()

View File

@ -176,7 +176,7 @@ public:
/// get (precomputed) only valid and available surrounding tiles for double wide creatures
static const BattleHexArray & getNeighbouringTilesDblWide(BattleHex hex, BattleSide side)
{
assert(hex.isValid() && (side == BattleSide::ATTACKER || BattleSide::DEFENDER));
assert(hex.isValid() && (side == BattleSide::ATTACKER || side == BattleSide::DEFENDER));
return neighbouringTilesDblWide.at(side)[hex];
}
@ -275,9 +275,17 @@ public:
return const_reverse_iterator(begin());
}
bool operator ==(const BattleHexArray & other) const noexcept
{
if(internalStorage != other.internalStorage || presenceFlags != other.presenceFlags)
return false;
return true;
}
private:
StorageType internalStorage;
std::bitset<totalSize> presenceFlags = {};
std::bitset<totalSize> presenceFlags;
[[nodiscard]] inline bool isNotValidForInsertion(BattleHex hex) const
{

View File

@ -204,7 +204,7 @@ bool CBattleInfoCallback::battleHasPenaltyOnLine(BattleHex from, BattleHex dest,
while (next != dest)
{
next = BattleHex::getClosestTile(next.getNeighbouringTiles(), direction, dest);
next = BattleHex::getClosestTile(direction, dest, next.getNeighbouringTiles());
ret.insert(next);
}
assert(!ret.empty());
@ -1159,6 +1159,7 @@ BattleHexArray CBattleInfoCallback::getStoppers(BattleSide whichSidePerspective)
std::pair<const battle::Unit *, BattleHex> CBattleInfoCallback::getNearestStack(const battle::Unit * closest) const
{
auto reachability = getReachability(closest);
auto avHexes = battleGetAvailableHexes(reachability, closest, false);
// I hate std::pairs with their undescriptive member names first / second
struct DistStack
@ -1177,7 +1178,7 @@ std::pair<const battle::Unit *, BattleHex> CBattleInfoCallback::getNearestStack(
for(const battle::Unit * st : possible)
{
for(BattleHex hex : battleGetAvailableHexes(reachability, closest, false))
for(BattleHex hex : avHexes)
if(CStack::isMeleeAttackPossible(closest, st, hex))
{
DistStack hlp = {reachability.distances[hex], hex, st};
@ -1222,7 +1223,7 @@ BattleHex CBattleInfoCallback::getAvailableHex(const CreatureID & creID, BattleS
return BattleHex::INVALID; //all tiles are covered
}
return BattleHex::getClosestTile(occupyable, side, pos);
return BattleHex::getClosestTile(side, pos, occupyable);
}
si8 CBattleInfoCallback::battleGetTacticDist() const

View File

@ -609,9 +609,7 @@ std::vector<Destination> BattleSpellMechanics::getPossibleDestinations(size_t in
for(auto stack : stacks)
{
hexesToCheck.insert(stack->getPosition());
for(auto adjacent : stack->getPosition().getNeighbouringTiles())
hexesToCheck.insert(adjacent);
hexesToCheck.insert(stack->getPosition().getNeighbouringTiles());
}
for(auto hex : hexesToCheck)

View File

@ -228,7 +228,7 @@ EffectTarget UnitEffect::transformTargetByChain(const Mechanics * m, const Targe
if(possibleHexes.empty())
break;
destHex = BattleHex::getClosestTile(possibleHexes, unit->unitSide(), destHex);
destHex = BattleHex::getClosestTile(unit->unitSide(), destHex, possibleHexes);
}
return effectTarget;

View File

@ -58,7 +58,7 @@ void LuaSpellEffect::adjustTargetTypes(std::vector<TargetType> & types) const
}
void LuaSpellEffect::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
void LuaSpellEffect::adjustAffectedHexes(BattleHexArray & hexes, const Mechanics * m, const Target & spellTarget) const
{
}
@ -98,7 +98,7 @@ bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m, const Ef
for(const auto & dest : target)
{
JsonNode targetData;
targetData.Vector().emplace_back(dest.hexValue.hex);
targetData.Vector().emplace_back(static_cast<si16>(dest.hexValue));
if(dest.unitValue)
targetData.Vector().emplace_back(dest.unitValue->unitId());
@ -141,7 +141,7 @@ void LuaSpellEffect::apply(ServerCallback * server, const Mechanics * m, const E
for(const auto & dest : target)
{
JsonNode targetData;
targetData.Vector().emplace_back(dest.hexValue.hex);
targetData.Vector().emplace_back(static_cast<si16>(dest.hexValue));
if(dest.unitValue)
targetData.Vector().emplace_back(dest.unitValue->unitId());

View File

@ -49,7 +49,7 @@ public:
void adjustTargetTypes(std::vector<TargetType> & types) const override;
void adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const override;
void adjustAffectedHexes(BattleHexArray & hexes, const Mechanics * m, const Target & spellTarget) const override;
bool applicable(Problem & problem, const Mechanics * m) const override;
bool applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const override;

View File

@ -99,11 +99,13 @@ int BattleCbProxy::getUnitByPos(lua_State * L)
if(!S.tryGet(1, object))
return S.retVoid();
BattleHex hex;
si16 hexVal;
if(!S.tryGet(2, hex.hex))
if(!S.tryGet(2, hexVal))
return S.retNil();
BattleHex hex(hexVal);
bool onlyAlive;
if(!S.tryGet(3, onlyAlive))

View File

@ -47,7 +47,7 @@ int BattleStackMovedProxy::addTileToMove(lua_State * L)
lua_Integer hex = 0;
if(!S.tryGetInteger(2, hex))
return S.retVoid();
object->tilesToMove.emplace_back(hex);
object->tilesToMove.insert(hex);
return S.retVoid();
}

View File

@ -9,29 +9,29 @@
*/
#include "StdInc.h"
#include "../lib/battle/BattleHex.h"
#include "../lib/battle/BattleHexArray.h"
TEST(BattleHexTest, getNeighbouringTiles)
{
BattleHex mainHex;
std::vector<BattleHex> neighbouringTiles;
BattleHexArray neighbouringTiles;
mainHex.setXY(16,0);
neighbouringTiles = mainHex.neighbouringTiles();
neighbouringTiles = mainHex.getNeighbouringTiles();
EXPECT_EQ(neighbouringTiles.size(), 1);
mainHex.setXY(0,0);
neighbouringTiles = mainHex.neighbouringTiles();
neighbouringTiles = mainHex.getNeighbouringTiles();
EXPECT_EQ(neighbouringTiles.size(), 2);
mainHex.setXY(15,2);
neighbouringTiles = mainHex.neighbouringTiles();
neighbouringTiles = mainHex.getNeighbouringTiles();
EXPECT_EQ(neighbouringTiles.size(), 3);
mainHex.setXY(2,0);
neighbouringTiles = mainHex.neighbouringTiles();
neighbouringTiles = mainHex.getNeighbouringTiles();
EXPECT_EQ(neighbouringTiles.size(), 4);
mainHex.setXY(1,2);
neighbouringTiles = mainHex.neighbouringTiles();
neighbouringTiles = mainHex.getNeighbouringTiles();
EXPECT_EQ(neighbouringTiles.size(), 5);
mainHex.setXY(8,5);
neighbouringTiles = mainHex.neighbouringTiles();
neighbouringTiles = mainHex.getNeighbouringTiles();
EXPECT_EQ(neighbouringTiles.size(), 6);
ASSERT_TRUE(neighbouringTiles.size()==6 && mainHex==93);
@ -85,7 +85,7 @@ TEST(BattleHexTest, mutualPositions)
TEST(BattleHexTest, getClosestTile)
{
BattleHex mainHex(0);
std::set<BattleHex> possibilities;
BattleHexArray possibilities;
possibilities.insert(3);
possibilities.insert(170);
possibilities.insert(100);

View File

@ -17,7 +17,7 @@ TEST(battle_Unit_getSurroundingHexes, oneWide)
auto actual = battle::Unit::getSurroundingHexes(position, false, BattleSide::ATTACKER);
EXPECT_EQ(actual, position.neighbouringTiles());
EXPECT_EQ(actual, position.getNeighbouringTiles());
}
TEST(battle_Unit_getSurroundingHexes, oneWideLeftCorner)
@ -26,7 +26,7 @@ TEST(battle_Unit_getSurroundingHexes, oneWideLeftCorner)
auto actual = battle::Unit::getSurroundingHexes(position, false, BattleSide::ATTACKER);
EXPECT_EQ(actual, position.neighbouringTiles());
EXPECT_EQ(actual, position.getNeighbouringTiles());
}
TEST(battle_Unit_getSurroundingHexes, oneWideRightCorner)
@ -35,7 +35,7 @@ TEST(battle_Unit_getSurroundingHexes, oneWideRightCorner)
auto actual = battle::Unit::getSurroundingHexes(position, false, BattleSide::ATTACKER);
EXPECT_EQ(actual, position.neighbouringTiles());
EXPECT_EQ(actual, position.getNeighbouringTiles());
}
TEST(battle_Unit_getSurroundingHexes, doubleWideAttacker)
@ -44,7 +44,7 @@ TEST(battle_Unit_getSurroundingHexes, doubleWideAttacker)
auto actual = battle::Unit::getSurroundingHexes(position, true, BattleSide::ATTACKER);
static const std::vector<BattleHex> expected =
static const BattleHexArray expected =
{
60,
61,
@ -65,7 +65,7 @@ TEST(battle_Unit_getSurroundingHexes, doubleWideLeftCorner)
auto actualAtt = battle::Unit::getSurroundingHexes(position, true, BattleSide::ATTACKER);
static const std::vector<BattleHex> expectedAtt =
static const BattleHexArray expectedAtt =
{
35,
53,
@ -76,7 +76,7 @@ TEST(battle_Unit_getSurroundingHexes, doubleWideLeftCorner)
auto actualDef = battle::Unit::getSurroundingHexes(position, true, BattleSide::DEFENDER);
static const std::vector<BattleHex> expectedDef =
static const BattleHexArray expectedDef =
{
35,
36,
@ -94,7 +94,7 @@ TEST(battle_Unit_getSurroundingHexes, doubleWideRightCorner)
auto actualAtt = battle::Unit::getSurroundingHexes(position, true, BattleSide::ATTACKER);
static const std::vector<BattleHex> expectedAtt =
static const BattleHexArray expectedAtt =
{
116,
117,
@ -109,7 +109,7 @@ TEST(battle_Unit_getSurroundingHexes, doubleWideRightCorner)
auto actualDef = battle::Unit::getSurroundingHexes(position, true, BattleSide::DEFENDER);
static const std::vector<BattleHex> expectedDef =
static const BattleHexArray expectedDef =
{
116,
117,
@ -128,7 +128,7 @@ TEST(battle_Unit_getSurroundingHexes, doubleWideDefender)
auto actual = battle::Unit::getSurroundingHexes(position, true, BattleSide::DEFENDER);
static const std::vector<BattleHex> expected =
static const BattleHexArray expected =
{
60,
61,

View File

@ -40,7 +40,7 @@ public:
MOCK_CONST_METHOD0(getPlayerID, std::optional<PlayerColor>());
MOCK_CONST_METHOD2(battleGetAllObstaclesOnPos, std::vector<std::shared_ptr<const CObstacleInstance>>(BattleHex, bool));
MOCK_CONST_METHOD2(getAllAffectedObstaclesByStack, std::vector<std::shared_ptr<const CObstacleInstance>>(const battle::Unit *, const std::set<BattleHex> &));
MOCK_CONST_METHOD2(getAllAffectedObstaclesByStack, std::vector<std::shared_ptr<const CObstacleInstance>>(const battle::Unit *, const BattleHexArray &));
};

View File

@ -12,6 +12,7 @@
#include "../../lib/spells/ISpellMechanics.h"
#include "../../lib/CGameInfoCallback.h"
#include "../../lib/battle/BattleHexArray.h"
namespace spells
{
@ -22,7 +23,7 @@ public:
MOCK_CONST_METHOD2(adaptProblem, bool(ESpellCastProblem, Problem &));
MOCK_CONST_METHOD1(adaptGenericProblem, bool(Problem &));
MOCK_CONST_METHOD1(rangeInHexes, std::vector<BattleHex>(BattleHex));
MOCK_CONST_METHOD1(rangeInHexes, BattleHexArray(BattleHex));
MOCK_CONST_METHOD1(getAffectedStacks, std::vector<const CStack *>(const Target &));
MOCK_CONST_METHOD1(canBeCast, bool(Problem &));

View File

@ -84,7 +84,7 @@ TEST_F(LuaSpellEffectAPITest, DISABLED_ApplicableOnLeftSideOfField)
BattleHex hex(2,2);
JsonNode first;
first.Vector().emplace_back(hex.hex);
first.Vector().emplace_back(static_cast<si16>(hex));
first.Vector().emplace_back();
JsonNode targets;
@ -113,7 +113,7 @@ TEST_F(LuaSpellEffectAPITest, DISABLED_NotApplicableOnRightSideOfField)
BattleHex hex(11,2);
JsonNode first;
first.Vector().emplace_back(hex.hex);
first.Vector().emplace_back(static_cast<si16>(hex));
first.Vector().emplace_back(-1);
JsonNode targets;
@ -138,13 +138,13 @@ TEST_F(LuaSpellEffectAPITest, DISABLED_ApplyMoveUnit)
BattleHex hex1(11,2);
JsonNode unit;
unit.Vector().emplace_back(hex1.hex);
unit.Vector().emplace_back(static_cast<si16>(hex1));
unit.Vector().emplace_back(42);
BattleHex hex2(5,4);
JsonNode destination;
destination.Vector().emplace_back(hex2.hex);
destination.Vector().emplace_back(static_cast<si16>(hex2));
destination.Vector().emplace_back(-1);
JsonNode targets;
@ -163,7 +163,7 @@ TEST_F(LuaSpellEffectAPITest, DISABLED_ApplyMoveUnit)
EXPECT_EQ(pack.teleporting, true);
EXPECT_EQ(pack.distance, 0);
std::vector<BattleHex> toMove(1, hex2);
BattleHexArray toMove = { hex2 };
EXPECT_EQ(pack.tilesToMove, toMove);
};

View File

@ -154,11 +154,11 @@ TEST_F(LuaSpellEffectTest, ApplicableTargetRedirected)
JsonNode first;
first.Vector().emplace_back(hex1.hex);
first.Vector().emplace_back(static_cast<si16>(hex1));
first.Vector().emplace_back(id1);
JsonNode second;
second.Vector().emplace_back(hex2.hex);
second.Vector().emplace_back(static_cast<si16>(hex2));
second.Vector().emplace_back(-1);
JsonNode targets;
@ -193,7 +193,7 @@ TEST_F(LuaSpellEffectTest, ApplyRedirected)
subject->apply(&serverMock, &mechanicsMock, target);
JsonNode first;
first.Vector().emplace_back(hex1.hex);
first.Vector().emplace_back(static_cast<si16>(hex1));
first.Vector().emplace_back(id1);
JsonNode targets;