-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCvBuildingProductionAI.cpp
239 lines (208 loc) · 6.48 KB
/
CvBuildingProductionAI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -------------------------------------------------------------------------------------------------------
© 1991-2012 Take-Two Interactive Software and its subsidiaries. Developed by Firaxis Games.
Sid Meier's Civilization V, Civ, Civilization, 2K Games, Firaxis Games, Take-Two Interactive Software
and their respective logos are all trademarks of Take-Two interactive Software, Inc.
All other marks and trademarks are the property of their respective owners.
All rights reserved.
------------------------------------------------------------------------------------------------------- */
#include "CvGameCoreDLLPCH.h"
#include "CvGameCoreDLLUtil.h"
#include "CvBuildingProductionAI.h"
// include after all other headers
#include "LintFree.h"
/// Constructor
CvBuildingProductionAI::CvBuildingProductionAI (CvCity *pCity, CvCityBuildings *pCityBuildings):
m_pCity (pCity),
m_pCityBuildings (pCityBuildings)
{
}
/// Destructor
CvBuildingProductionAI::~CvBuildingProductionAI(void)
{
}
/// Clear out AI local variables
void CvBuildingProductionAI::Reset()
{
CvAssertMsg (m_pCityBuildings != NULL, "Building Production AI init failure: city buildings are NULL");
m_BuildingAIWeights.clear();
// Loop through reading each one and add an entry with 0 weight to our vector
if(m_pCityBuildings)
{
for (int i = 0; i < m_pCityBuildings->GetBuildings()->GetNumBuildings(); i++)
{
m_BuildingAIWeights.push_back(i, 0);
}
}
}
/// Serialization read
void CvBuildingProductionAI::Read(FDataStream& kStream)
{
int iWeight;
// Version number to maintain backwards compatibility
uint uiVersion;
kStream >> uiVersion;
// Reset vector
m_BuildingAIWeights.clear();
// Loop through reading each one and adding it to our vector
if(m_pCityBuildings)
{
if (uiVersion >= 2)
{
for (int i = 0; i < m_pCityBuildings->GetBuildings()->GetNumBuildings(); i++)
{
m_BuildingAIWeights.push_back(i, 0);
}
int iNumEntries;
FStringFixedBuffer(sTemp, 64);
int iType;
kStream >> iNumEntries;
for (int iI = 0; iI < iNumEntries; iI++)
{
kStream >> sTemp;
if (sTemp != "NO_BUILDING")
{
iType = GC.getInfoTypeForString(sTemp);
kStream >> iWeight;
if (iType != -1)
{
m_BuildingAIWeights.IncreaseWeight(iType, iWeight);
}
else
{
CvString szError;
szError.Format("LOAD ERROR: Building Type not found: %s", sTemp);
GC.LogMessage(szError.GetCString());
CvAssertMsg(false, szError);
}
}
}
}
else
{
for (int i = 0; i < 89 /* Buildings in gold master */; i++)
{
kStream >> iWeight;
m_BuildingAIWeights.push_back(i, iWeight);
}
}
}
else
{
CvAssertMsg (m_pCityBuildings != NULL, "Building Production AI init failure: city buildings are NULL");
}
}
/// Serialization write
void CvBuildingProductionAI::Write(FDataStream& kStream)
{
CvAssertMsg (m_pCityBuildings != NULL, "Building Production AI init failure: city buildings are NULL");
FStringFixedBuffer(sTemp, 64);
// Current version number
uint uiVersion = 2;
kStream << uiVersion;
if (m_pCityBuildings)
{
int iNumBuildings = m_pCityBuildings->GetBuildings()->GetNumBuildings();
kStream << iNumBuildings;
// Loop through writing each entry
for (int iI = 0; iI < iNumBuildings; iI++)
{
const BuildingTypes eBuilding = static_cast<BuildingTypes>(iI);
CvBuildingEntry* pkBuildingInfo = GC.getBuildingInfo(eBuilding);
if(pkBuildingInfo)
{
sTemp = pkBuildingInfo->GetType();
kStream << sTemp;
kStream << m_BuildingAIWeights.GetWeight(iI);
}
else
{
sTemp = "NO_BUILDING";
kStream << sTemp;
}
}
}
}
/// Establish weights for one flavor; can be called multiple times to layer strategies
void CvBuildingProductionAI::AddFlavorWeights(FlavorTypes eFlavor, int iWeight)
{
CvBuildingXMLEntries* pkBuildings = m_pCityBuildings->GetBuildings();
// Loop through all buildings
for (int iBuilding = 0; iBuilding < m_pCityBuildings->GetBuildings()->GetNumBuildings(); iBuilding++)
{
CvBuildingEntry *entry = pkBuildings->GetEntry(iBuilding);
if(entry)
{
// Set its weight by looking at building's weight for this flavor and using iWeight multiplier passed in
m_BuildingAIWeights.IncreaseWeight (iBuilding, entry->GetFlavorValue(eFlavor) * iWeight);
}
}
}
/// Retrieve sum of weights on one item
int CvBuildingProductionAI::GetWeight(BuildingTypes eBuilding)
{
return m_BuildingAIWeights.GetWeight(eBuilding);
}
/// Recommend highest-weighted building
BuildingTypes CvBuildingProductionAI::RecommendBuilding()
{
int iBldgLoop;
int iWeight;
int iTurnsLeft;
// Reset list of all the possible buildings
m_Buildables.clear();
// Loop through adding the available buildings
for (iBldgLoop = 0; iBldgLoop < GC.GetGameBuildings()->GetNumBuildings(); iBldgLoop++)
{
// Make sure this building can be built now
if (m_pCity->canConstruct((BuildingTypes)iBldgLoop))
{
// Update weight based on turns to construct
iTurnsLeft = m_pCity->getProductionTurnsLeft((BuildingTypes) iBldgLoop, 0);
iWeight = CityStrategyAIHelpers::ReweightByTurnsLeft(m_BuildingAIWeights.GetWeight((BuildingTypes)iBldgLoop), iTurnsLeft);
m_Buildables.push_back (iBldgLoop, iWeight);
}
}
// Sort items and grab the first one
if (m_Buildables.size() > 0)
{
m_Buildables.SortItems();
LogPossibleBuilds();
return (BuildingTypes)m_Buildables.GetElement(0);
}
// Unless we didn't find any
else
{
return NO_BUILDING;
}
}
/// Log all potential builds
void CvBuildingProductionAI::LogPossibleBuilds()
{
if (GC.getLogging() && GC.getAILogging())
{
CvString strOutBuf;
CvString strBaseString;
CvString strTemp;
CvString playerName;
CvString cityName;
CvString strDesc;
CvString strLogName;
// Find the name of this civ and city
playerName = GET_PLAYER(m_pCity->getOwner()).getCivilizationShortDescription();
cityName = m_pCity->getName();
// Open the log file
FILogFile *pLog;
pLog = LOGFILEMGR.GetLog(m_pCity->GetCityStrategyAI()->GetLogFileName(playerName, cityName), FILogFile::kDontTimeStamp);
// Get the leading info for this line
strBaseString.Format ("%03d, ", GC.getGame().getElapsedGameTurns());
strBaseString += playerName + ", " + cityName + ", ";
// Dump out the weight of each buildable item
for (int iI = 0; iI < m_Buildables.size(); iI++)
{
strDesc = GC.GetGameBuildings()->GetEntry(m_Buildables.GetElement(iI))->GetDescription();
strTemp.Format("Building, %s, %d", strDesc.GetCString(), m_Buildables.GetWeight(iI));
strOutBuf = strBaseString + strTemp;
pLog->Msg(strOutBuf);
}
}
}