浏览代码

Drop battle elapsed time measurement, restore avHexes.
Github validation report fixes.

MichalZr6 9 月之前
父节点
当前提交
4031006317

+ 0 - 12
client/NetPacksClient.cpp

@@ -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)

+ 5 - 5
lib/battle/BattleHex.cpp

@@ -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)
 {

+ 1 - 1
lib/battle/BattleHex.h

@@ -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() 

+ 10 - 2
lib/battle/BattleHexArray.h

@@ -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
 	{

+ 4 - 3
lib/battle/CBattleInfoCallback.cpp

@@ -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

+ 1 - 3
lib/spells/BattleSpellMechanics.cpp

@@ -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)

+ 1 - 1
lib/spells/effects/UnitEffect.cpp

@@ -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;

+ 3 - 3
scripting/lua/LuaSpellEffect.cpp

@@ -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());

+ 1 - 1
scripting/lua/LuaSpellEffect.h

@@ -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;

+ 4 - 2
scripting/lua/api/BattleCb.cpp

@@ -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))

+ 1 - 1
scripting/lua/api/netpacks/BattleStackMoved.cpp

@@ -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();
 }
 

+ 9 - 9
test/battle/BattleHexTest.cpp

@@ -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);

+ 9 - 9
test/battle/battle_UnitTest.cpp

@@ -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,

+ 1 - 1
test/mock/mock_IBattleInfoCallback.h

@@ -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 &));
 
 };
 

+ 2 - 1
test/mock/mock_spells_Mechanics.h

@@ -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 &));

+ 5 - 5
test/scripting/LuaSpellEffectAPITest.cpp

@@ -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);
 	};

+ 3 - 3
test/scripting/LuaSpellEffectTest.cpp

@@ -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;