Browse Source

Merge branch 'develop' into handicap

Laserlicht 1 year ago
parent
commit
4f0c3a3608

+ 8 - 16
client/CServerHandler.cpp

@@ -161,11 +161,6 @@ CServerHandler::CServerHandler()
 	registerTypesLobbyPacks(*applier);
 }
 
-void CServerHandler::setHighScoreCalc(const std::shared_ptr<HighScoreCalculation> &newHighScoreCalc)
-{
-	campaignScoreCalculator = newHighScoreCalc;
-}
-
 void CServerHandler::threadRunNetwork()
 {
 	logGlobal->info("Starting network thread");
@@ -663,7 +658,7 @@ void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameSta
 		break;
 	case EStartMode::CAMPAIGN:
 		if(si->campState->conqueredScenarios().empty())
-			campaignScoreCalculator.reset();
+			si->campState->highscoreParameters.clear();
 		client->newGame(gameState);
 		break;
 	case EStartMode::LOAD_GAME:
@@ -694,12 +689,12 @@ HighScoreParameter CServerHandler::prepareHighScores(PlayerColor player, bool vi
 	for(const CGTownInstance * t : playerState->towns)
 		if(t->builtBuildings.count(BuildingID::GRAIL))
 			param.hasGrail = true;
-	param.allDefeated = true;
+	param.allEnemiesDefeated = true;
 	for (PlayerColor otherPlayer(0); otherPlayer < PlayerColor::PLAYER_LIMIT; ++otherPlayer)
 	{
 		auto ps = gs->getPlayerState(otherPlayer, false);
 		if(ps && otherPlayer != player && !ps->checkVanquished())
-			param.allDefeated = false;
+			param.allEnemiesDefeated = false;
 	}
 	param.scenarioName = gs->getMapHeader()->name.toString();
 	param.playerName = gs->getStartInfo()->playerInfos.find(player)->second.name;
@@ -764,19 +759,16 @@ void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared
 	if (!cs)
 		ourCampaign = si->campState;
 
-	if(campaignScoreCalculator == nullptr)
-	{
-		campaignScoreCalculator = std::make_shared<HighScoreCalculation>();
-		campaignScoreCalculator->isCampaign = true;
-		campaignScoreCalculator->parameters.clear();
-	}
 	param.campaignName = cs->getNameTranslated();
-	campaignScoreCalculator->parameters.push_back(param);
+	cs->highscoreParameters.push_back(param);
+	auto campaignScoreCalculator = std::make_shared<HighScoreCalculation>();
+	campaignScoreCalculator->isCampaign = true;
+	campaignScoreCalculator->parameters = cs->highscoreParameters;
 
 	endGameplay();
 
 	auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog;
-	auto finisher = [this, ourCampaign]()
+	auto finisher = [ourCampaign, campaignScoreCalculator]()
 	{
 		if(ourCampaign->campaignSet != "" && ourCampaign->isCampaignFinished())
 		{

+ 2 - 3
client/CServerHandler.h

@@ -28,6 +28,8 @@ struct CPack;
 struct CPackForLobby;
 struct CPackForClient;
 
+class HighScoreParameter;
+
 template<typename T> class CApplier;
 
 VCMI_LIB_NAMESPACE_END
@@ -39,7 +41,6 @@ class GameChatHandler;
 class IServerRunner;
 
 class HighScoreCalculation;
-class HighScoreParameter;
 
 enum class ESelectionScreen : ui8;
 enum class ELoadMode : ui8;
@@ -106,7 +107,6 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
 	std::unique_ptr<IServerRunner> serverRunner;
 	std::shared_ptr<CMapInfo> mapToStart;
 	std::vector<std::string> localPlayerNames;
-	std::shared_ptr<HighScoreCalculation> campaignScoreCalculator;
 
 	boost::thread threadNetwork;
 
@@ -221,7 +221,6 @@ public:
 
 	void visitForLobby(CPackForLobby & lobbyPack);
 	void visitForClient(CPackForClient & clientPack);
-	void setHighScoreCalc(const std::shared_ptr<HighScoreCalculation> &newHighScoreCalc);
 };
 
 extern CServerHandler * CSH;

+ 2 - 1
client/mainmenu/CHighScoreScreen.cpp

@@ -32,6 +32,7 @@
 #include "../../lib/CConfigHandler.h"
 #include "../../lib/CCreatureHandler.h"
 #include "../../lib/constants/EntityIdentifiers.h"
+#include "../../lib/gameState/HighScore.h"
 
 auto HighScoreCalculation::calculate()
 {
@@ -48,7 +49,7 @@ auto HighScoreCalculation::calculate()
 	const std::array<double, 5> difficultyMultipliers{0.8, 1.0, 1.3, 1.6, 2.0}; 
 	for(auto & param : parameters)
 	{
-		double tmp = 200 - (param.day + 10) / (param.townAmount + 5) + (param.allDefeated ? 25 : 0) + (param.hasGrail ? 25 : 0);
+		double tmp = 200 - (param.day + 10) / (param.townAmount + 5) + (param.allEnemiesDefeated ? 25 : 0) + (param.hasGrail ? 25 : 0);
 		firstResult = Result{static_cast<int>(tmp), static_cast<int>(tmp * difficultyMultipliers.at(param.difficulty)), param.day, param.usedCheat};
 		summary.basic += firstResult.basic * 5.0 / parameters.size();
 		summary.total += firstResult.total * 5.0 / parameters.size();

+ 2 - 15
client/mainmenu/CHighScoreScreen.h

@@ -9,6 +9,7 @@
  */
 #pragma once
 #include "../windows/CWindowObject.h"
+#include "../../lib/gameState/HighScore.h"
 
 class CButton;
 class CLabel;
@@ -20,24 +21,10 @@ class CFilledTexture;
 
 class TransparentFilledRectangle;
 
-class HighScoreParameter
-{
-public:
-	int difficulty;
-	int day;
-	int townAmount;
-	bool usedCheat;
-	bool hasGrail;
-	bool allDefeated;
-	std::string campaignName;
-	std::string scenarioName;
-	std::string playerName;
-};
-
 class HighScoreCalculation
 {
 public:
-	std::vector<HighScoreParameter> parameters = std::vector<HighScoreParameter>();
+	std::vector<HighScoreParameter> parameters;
 	bool isCampaign = false;
 
 	auto calculate();

+ 2 - 2
client/mapView/MapRendererContext.cpp

@@ -113,7 +113,7 @@ const CGPath * MapRendererBaseContext::currentPath() const
 
 size_t MapRendererBaseContext::objectGroupIndex(ObjectInstanceID objectID) const
 {
-	static const std::vector<size_t> idleGroups = {0, 13, 0, 1, 2, 3, 4, 15, 14};
+	static const std::array<size_t, 9> idleGroups = {0, 13, 0, 1, 2, 3, 4, 15, 14};
 	return idleGroups[getObjectRotation(objectID)];
 }
 
@@ -403,7 +403,7 @@ size_t MapRendererAdventureMovingContext::objectGroupIndex(ObjectInstanceID obje
 {
 	if(target == objectID)
 	{
-		static const std::vector<size_t> moveGroups = {0, 10, 5, 6, 7, 8, 9, 12, 11};
+		static const std::array<size_t, 9> moveGroups = {0, 10, 5, 6, 7, 8, 9, 12, 11};
 		return moveGroups[getObjectRotation(objectID)];
 	}
 	return MapRendererAdventureContext::objectGroupIndex(objectID);

+ 1 - 0
lib/CMakeLists.txt

@@ -463,6 +463,7 @@ set(lib_MAIN_HEADERS
 	gameState/CGameState.h
 	gameState/CGameStateCampaign.h
 	gameState/EVictoryLossCheckResult.h
+	gameState/HighScore.h
 	gameState/InfoAboutArmy.h
 	gameState/RumorState.h
 	gameState/SThievesGuildInfo.h

+ 1 - 1
lib/bonuses/Bonus.h

@@ -53,7 +53,7 @@ public:
 	JsonNode toJsonNode() const;
 };
 
-#define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
+#define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.loadingGamestate) deserializationFix();
 
 /// Struct for handling bonuses of several types. Can be transferred to any hero
 struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>, public Serializeable

+ 5 - 0
lib/campaign/CampaignState.h

@@ -15,6 +15,7 @@
 #include "../texts/TextLocalizationContainer.h"
 #include "CampaignConstants.h"
 #include "CampaignScenarioPrologEpilog.h"
+#include "../gameState/HighScore.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 
@@ -318,6 +319,8 @@ public:
 
 	std::string campaignSet;
 
+	std::vector<HighScoreParameter> highscoreParameters;
+
 	template <typename Handler> void serialize(Handler &h)
 	{
 		h & static_cast<Campaign&>(*this);
@@ -330,6 +333,8 @@ public:
 		h & campaignSet;
 		if (h.version >= Handler::Version::CAMPAIGN_MAP_TRANSLATIONS)
 			h & mapTranslations;
+		if (h.version >= Handler::Version::HIGHSCORE_PARAMETERS)
+			h & highscoreParameters;
 	}
 };
 

+ 41 - 0
lib/gameState/HighScore.h

@@ -0,0 +1,41 @@
+/*
+ * HighScore.h, 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
+ *
+ */
+#pragma once
+
+VCMI_LIB_NAMESPACE_BEGIN
+
+class DLL_LINKAGE HighScoreParameter
+{
+public:
+	int difficulty;
+	int day;
+	int townAmount;
+	bool usedCheat;
+	bool hasGrail;
+	bool allEnemiesDefeated;
+	std::string campaignName;
+	std::string scenarioName;
+	std::string playerName;
+
+	template <typename Handler> void serialize(Handler &h)
+	{
+		h & difficulty;
+		h & day;
+		h & townAmount;
+		h & usedCheat;
+		h & hasGrail;
+		h & allEnemiesDefeated;
+		h & campaignName;
+		h & scenarioName;
+		h & playerName;
+	}
+};
+
+VCMI_LIB_NAMESPACE_END

+ 4 - 0
lib/networkPacks/PacksForLobby.h

@@ -155,9 +155,13 @@ struct DLL_LINKAGE LobbyStartGame : public CLobbyPackToPropagate
 
 	template <typename Handler> void serialize(Handler &h)
 	{
+		if (!h.saving)
+			h.loadingGamestate = true;
 		h & clientId;
 		h & initializedStartInfo;
 		h & initializedGameState;
+		if (!h.saving)
+			h.loadingGamestate = false;
 	}
 };
 

+ 0 - 2
lib/serializer/BinaryDeserializer.cpp

@@ -15,9 +15,7 @@ VCMI_LIB_NAMESPACE_BEGIN
 
 BinaryDeserializer::BinaryDeserializer(IBinaryReader * r): CLoaderBase(r)
 {
-	saving = false;
 	version = Version::NONE;
-	smartPointerSerialization = true;
 	reverseEndianness = false;
 
 	registerTypes(*this);

+ 5 - 4
lib/serializer/BinaryDeserializer.h

@@ -165,8 +165,9 @@ public:
 	std::map<uint32_t, Serializeable*> loadedPointers;
 	std::map<const Serializeable*, std::shared_ptr<Serializeable>> loadedSharedPointers;
 	IGameCallback * cb = nullptr;
-	bool smartPointerSerialization;
-	bool saving;
+	static constexpr bool trackSerializedPointers = true;
+	static constexpr bool saving = false;
+	bool loadingGamestate = false;
 
 	bool hasFeature(Version what) const
 	{
@@ -342,7 +343,7 @@ public:
 		}
 
 		uint32_t pid = 0xffffffff; //pointer id (or maybe rather pointee id)
-		if(smartPointerSerialization)
+		if(trackSerializedPointers)
 		{
 			load( pid ); //get the id
 			auto i = loadedPointers.find(pid); //lookup
@@ -383,7 +384,7 @@ public:
 	template <typename T>
 	void ptrAllocated(T *ptr, uint32_t pid)
 	{
-		if(smartPointerSerialization && pid != 0xffffffff)
+		if(trackSerializedPointers && pid != 0xffffffff)
 			loadedPointers[pid] = const_cast<Serializeable*>(dynamic_cast<const Serializeable*>(ptr)); //add loaded pointer to our lookup map; cast is to avoid errors with const T* pt
 	}
 

+ 0 - 2
lib/serializer/BinarySerializer.cpp

@@ -15,8 +15,6 @@ VCMI_LIB_NAMESPACE_BEGIN
 
 BinarySerializer::BinarySerializer(IBinaryWriter * w): CSaverBase(w)
 {
-	saving=true;
-	smartPointerSerialization = true;
 	registerTypes(*this);
 }
 

+ 4 - 3
lib/serializer/BinarySerializer.h

@@ -118,8 +118,9 @@ public:
 	std::map<const Serializeable*, uint32_t> savedPointers;
 
 	Version version = Version::CURRENT;
-	bool smartPointerSerialization;
-	bool saving;
+	static constexpr bool trackSerializedPointers = true;
+	static constexpr bool saving = true;
+	bool loadingGamestate = false;
 
 	bool hasFeature(Version what) const
 	{
@@ -257,7 +258,7 @@ public:
 				return;
 		}
 
-		if(smartPointerSerialization)
+		if(trackSerializedPointers)
 		{
 			// We might have an object that has multiple inheritance and store it via the non-first base pointer.
 			// Therefore, all pointers need to be normalized to the actual object address.

+ 1 - 0
lib/serializer/CLoadFile.cpp

@@ -29,6 +29,7 @@ int CLoadFile::read(std::byte * data, unsigned size)
 
 void CLoadFile::openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion)
 {
+	serializer.loadingGamestate = true;
 	assert(!serializer.reverseEndianness);
 	assert(minimalVersion <= ESerializationVersion::CURRENT);
 

+ 0 - 12
lib/serializer/Connection.cpp

@@ -151,18 +151,6 @@ void CConnection::enterGameplayConnectionMode(CGameState * gs)
 	enableSmartVectorMemberSerializatoin(gs);
 }
 
-void CConnection::disableSmartPointerSerialization()
-{
-	deserializer->smartPointerSerialization = false;
-	serializer->smartPointerSerialization = false;
-}
-
-void CConnection::enableSmartPointerSerialization()
-{
-	deserializer->smartPointerSerialization = true;
-	serializer->smartPointerSerialization = true;
-}
-
 void CConnection::disableSmartVectorMemberSerialization()
 {
 	packReader->smartVectorMembersSerialization = false;

+ 0 - 2
lib/serializer/Connection.h

@@ -38,8 +38,6 @@ class DLL_LINKAGE CConnection : boost::noncopyable
 
 	void disableStackSendingByID();
 	void enableStackSendingByID();
-	void disableSmartPointerSerialization();
-	void enableSmartPointerSerialization();
 	void disableSmartVectorMemberSerialization();
 	void enableSmartVectorMemberSerializatoin(CGameState * gs);
 

+ 2 - 1
lib/serializer/ESerializationVersion.h

@@ -57,7 +57,8 @@ enum class ESerializationVersion : int32_t
 	SIMPLE_TEXT_CONTAINER_SERIALIZATION, // 847 - text container is serialized using common routine instead of custom approach
 	MAP_FORMAT_ADDITIONAL_INFOS, // 848 - serialize new infos in map format
 	REMOVE_LIB_RNG, // 849 - removed random number generators from library classes
-	PLAYER_HANDICAP, // 850 - player handicap selection at game start
+	HIGHSCORE_PARAMETERS, // 850 - saves parameter for campaign
+  PLAYER_HANDICAP, // 851 - player handicap selection at game start
 
 	CURRENT = PLAYER_HANDICAP
 };