Browse Source

abstraction layer

Laserlicht 1 year ago
parent
commit
ebeeff5aa3

+ 8 - 8
AI/Nullkiller/Analyzers/BuildAnalyzer.cpp

@@ -31,16 +31,16 @@ void BuildAnalyzer::updateTownDwellings(TownDevelopmentInfo & developmentInfo)
 		}
 	}
 
-	BuildingID prefixes[] = {BuildingID::DWELL_UP_FIRST, BuildingID::DWELL_FIRST};
+	bool prefixes[] = {true, false};
 
 	for(int level = 0; level < developmentInfo.town->town->creatures.size(); level++)
 	{
 		logAi->trace("Checking dwelling level %d", level);
 		BuildingInfo nextToBuild = BuildingInfo();
 
-		for(BuildingID prefix : prefixes)
+		for(bool prefix : prefixes)
 		{
-			BuildingID building = BuildingID(prefix + level);
+			BuildingID building = BuildingID(BuildingID::getDwelling(level, prefix));
 
 			if(!vstd::contains(buildings, building))
 				continue; // no such building in town
@@ -209,10 +209,10 @@ BuildingInfo BuildAnalyzer::getBuildingOrPrerequisite(
 	int creatureLevel = -1;
 	int creatureUpgrade = 0;
 
-	if(BuildingID::DWELL_FIRST <= toBuild && toBuild <= BuildingID::DWELL_UP_LAST)
+	if(BuildingID::getLevel(toBuild) != -1)
 	{
-		creatureLevel = (toBuild - BuildingID::DWELL_FIRST) % GameConstants::CREATURES_PER_TOWN;
-		creatureUpgrade = (toBuild - BuildingID::DWELL_FIRST) / GameConstants::CREATURES_PER_TOWN;
+		creatureLevel = BuildingID::getLevel(toBuild) % GameConstants::CREATURES_PER_TOWN;
+		creatureUpgrade = BuildingID::getLevel(toBuild) / GameConstants::CREATURES_PER_TOWN;
 	}
 	else if(toBuild == BuildingID::HORDE_1 || toBuild == BuildingID::HORDE_1_UPGR)
 	{
@@ -262,7 +262,7 @@ BuildingInfo BuildAnalyzer::getBuildingOrPrerequisite(
 
 			auto otherDwelling = [](const BuildingID & id) -> bool
 			{
-				return BuildingID::DWELL_FIRST <= id && id <= BuildingID::DWELL_UP_LAST;
+				return BuildingID::getLevel(id) != -1;
 			};
 
 			if(vstd::contains_if(missingBuildings, otherDwelling))
@@ -405,7 +405,7 @@ BuildingInfo::BuildingInfo(
 		}
 		else
 		{
-			if(BuildingID::DWELL_FIRST <= id && id <= BuildingID::DWELL_UP_LAST)
+			if(BuildingID::getLevel(id) != -1)
 			{
 				creatureGrows = creature->getGrowth();
 

+ 6 - 6
client/windows/CCastleInterface.cpp

@@ -163,7 +163,7 @@ void CBuildingRect::showPopupWindow(const Point & cursorPosition)
 	}
 	else
 	{
-		int level = ( bid - BuildingID::DWELL_FIRST ) % bld->town->creatures.size();
+		int level = BuildingID::getLevel(bid) % bld->town->creatures.size();
 		GH.windows().createAndPushWindow<CDwellingInfoBox>(parent->pos.x+parent->pos.w / 2, parent->pos.y+parent->pos.h  /2, town, level);
 	}
 }
@@ -688,7 +688,7 @@ void CCastleBuildings::buildingClicked(BuildingID building, BuildingSubID::EBuil
 
 	if (building >= BuildingID::DWELL_FIRST)
 	{
-		enterDwelling((building-BuildingID::DWELL_FIRST)%town->town->creatures.size());
+		enterDwelling((BuildingID::getLevel(building))%town->town->creatures.size());
 	}
 	else
 	{
@@ -1771,12 +1771,12 @@ CFortScreen::CFortScreen(const CGTownInstance * town):
 		BuildingID buildingID;
 		if(fortSize == town->town->creatures.size())
 		{
-			BuildingID dwelling = BuildingID::DWELL_UP_FIRST+i;
+			BuildingID dwelling = BuildingID::getDwelling(i, true);
 
 			if(vstd::contains(town->builtBuildings, dwelling))
-				buildingID = BuildingID(BuildingID::DWELL_UP_FIRST+i);
+				buildingID = BuildingID(BuildingID::getDwelling(i, true));
 			else
-				buildingID = BuildingID(BuildingID::DWELL_FIRST+i);
+				buildingID = BuildingID(BuildingID::getDwelling(i));
 		}
 		else
 		{
@@ -1878,7 +1878,7 @@ const CCreature * CFortScreen::RecruitArea::getMyCreature()
 
 const CBuilding * CFortScreen::RecruitArea::getMyBuilding()
 {
-	BuildingID myID = BuildingID(BuildingID::DWELL_FIRST + level);
+	BuildingID myID = BuildingID(BuildingID::getDwelling(level));
 
 	if (level == town->town->creatures.size())
 		return town->town->getSpecialBuilding(BuildingSubID::PORTAL_OF_SUMMONING);

+ 22 - 2
lib/constants/EntityIdentifiers.h

@@ -314,8 +314,28 @@ public:
 
 	};
 
-	const std::vector<Type> dwellings = { DWELL_LVL_1, DWELL_LVL_2, DWELL_LVL_3, DWELL_LVL_4, DWELL_LVL_5, DWELL_LVL_6, DWELL_LVL_7, DWELL_LVL_8 };
-	const std::vector<Type> dwellingsUp = { DWELL_LVL_1_UP, DWELL_LVL_2_UP, DWELL_LVL_3_UP, DWELL_LVL_4_UP, DWELL_LVL_5_UP, DWELL_LVL_6_UP, DWELL_LVL_7_UP, DWELL_LVL_8_UP };
+	static Type getDwelling(int level, bool up = false)
+	{
+		const std::vector<Type> dwellings = { DWELL_LVL_1, DWELL_LVL_2, DWELL_LVL_3, DWELL_LVL_4, DWELL_LVL_5, DWELL_LVL_6, DWELL_LVL_7, DWELL_LVL_8 };
+		const std::vector<Type> dwellingsUp = { DWELL_LVL_1_UP, DWELL_LVL_2_UP, DWELL_LVL_3_UP, DWELL_LVL_4_UP, DWELL_LVL_5_UP, DWELL_LVL_6_UP, DWELL_LVL_7_UP, DWELL_LVL_8_UP };
+
+		return up ? dwellingsUp[level] : dwellings[level];
+	}
+
+	static int getLevel(BuildingIDBase level)
+	{
+		const std::vector<Type> dwellings = { DWELL_LVL_1, DWELL_LVL_2, DWELL_LVL_3, DWELL_LVL_4, DWELL_LVL_5, DWELL_LVL_6, DWELL_LVL_7, DWELL_LVL_8 };
+		const std::vector<Type> dwellingsUp = { DWELL_LVL_1_UP, DWELL_LVL_2_UP, DWELL_LVL_3_UP, DWELL_LVL_4_UP, DWELL_LVL_5_UP, DWELL_LVL_6_UP, DWELL_LVL_7_UP, DWELL_LVL_8_UP };
+
+		auto it = std::find(dwellings.begin(), dwellings.end(), level);
+		if (it != dwellings.end())
+			return std::distance(dwellings.begin(), it);
+		it = std::find(dwellingsUp.begin(), dwellingsUp.end(), level);
+		if (it != dwellingsUp.end())
+			return std::distance(dwellingsUp.begin(), it);
+
+		return -1;
+	}
 
 	bool IsSpecialOrGrail() const
 	{

+ 2 - 2
lib/entities/building/CBuildingHandler.cpp

@@ -56,7 +56,7 @@ BuildingID CBuildingHandler::campToERMU(int camp, FactionID townType, const std:
 				{
 					if (hordeLvlsPerTType[townType.getNum()][0] == i)
 					{
-						BuildingID dwellingID(BuildingID::DWELL_UP_FIRST + hordeLvlsPerTType[townType.getNum()][0]);
+						BuildingID dwellingID(BuildingID::getDwelling(hordeLvlsPerTType[townType.getNum()][0], true));
 
 						if(vstd::contains(builtBuildings, dwellingID)) //if upgraded dwelling is built
 							return BuildingID::HORDE_1_UPGR;
@@ -67,7 +67,7 @@ BuildingID CBuildingHandler::campToERMU(int camp, FactionID townType, const std:
 					{
 						if(hordeLvlsPerTType[townType.getNum()].size() > 1)
 						{
-							BuildingID dwellingID(BuildingID::DWELL_UP_FIRST + hordeLvlsPerTType[townType.getNum()][1]);
+							BuildingID dwellingID(BuildingID::getDwelling(hordeLvlsPerTType[townType.getNum()][1], true));
 
 							if(vstd::contains(builtBuildings, dwellingID)) //if upgraded dwelling is built
 								return BuildingID::HORDE_2_UPGR;

+ 1 - 1
lib/mapObjects/CGTownInstance.cpp

@@ -520,7 +520,7 @@ void CGTownInstance::initObj(vstd::RNG & rand) ///initialize town structures
 
 	for (int level = 0; level < town->creatures.size(); level++)
 	{
-		BuildingID buildID = BuildingID(BuildingID::DWELL_FIRST + level);
+		BuildingID buildID = BuildingID(BuildingID::getDwelling(level));
 		int upgradeNum = 0;
 
 		for (; town->buildings.count(buildID); upgradeNum++, buildID.advance(town->creatures.size()))

+ 3 - 3
server/CGameHandler.cpp

@@ -2360,10 +2360,10 @@ bool CGameHandler::buildStructure(ObjectInstanceID tid, BuildingID requestedID,
 	//Performs stuff that has to be done before new building is built
 	auto processBeforeBuiltStructure = [t, this](const BuildingID buildingID)
 	{
-		if(buildingID >= BuildingID::DWELL_FIRST) //dwelling
+		if(BuildingID::getLevel(buildingID) > -1) //dwelling
 		{
-			int level = (buildingID - BuildingID::DWELL_FIRST) % t->town->creatures.size();
-			int upgradeNumber = (buildingID - BuildingID::DWELL_FIRST) / t->town->creatures.size();
+			int level = BuildingID::getLevel(buildingID) % t->town->creatures.size();
+			int upgradeNumber = BuildingID::getLevel(buildingID) / t->town->creatures.size();
 
 			if(upgradeNumber >= t->town->creatures.at(level).size())
 			{