-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIWriter.cpp
More file actions
124 lines (103 loc) · 3.67 KB
/
Copy pathAIWriter.cpp
File metadata and controls
124 lines (103 loc) · 3.67 KB
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
#include "AIWriter.h"
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
#include <QDir>
namespace AI {
AIWriter::AIWriter() {}
bool AIWriter::writeInitParameters(const QString& filePath, const InitParameters& params) {
QFileInfo fileInfo(filePath);
QDir dir = fileInfo.dir();
if (!dir.exists()) {
if (!dir.mkpath(".")) {
m_lastError = QString("Cannot create directory: %1").arg(dir.path());
return false;
}
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
m_lastError = QString("Cannot open file for writing: %1").arg(filePath);
return false;
}
QTextStream out(&file);
// Write header comment
out << "// " << fileInfo.fileName() << "\n";
out << "\n";
// Get known parameter names for ordering
QStringList knownNames = getKnownInitParamNames();
QStringList writtenNames;
// Write known parameters first (preserves expected order)
for (const QString& name : knownNames) {
if (params.params.contains(name)) {
const InitParameter& p = params.params[name];
if (p.isFloat) {
out << p.name << "\t" << QString::number(p.value, 'f', 2)
<< "\t" << QString::number(p.variance, 'f', 2) << "\n";
} else {
out << p.name << "\t" << static_cast<int>(p.value)
<< "\t" << static_cast<int>(p.variance) << "\n";
}
writtenNames.append(name);
}
}
// Write any custom parameters
for (auto it = params.params.begin(); it != params.params.end(); ++it) {
if (!writtenNames.contains(it.key())) {
const InitParameter& p = it.value();
if (p.isFloat) {
out << p.name << "\t" << QString::number(p.value, 'f', 2)
<< "\t" << QString::number(p.variance, 'f', 2) << "\n";
} else {
out << p.name << "\t" << static_cast<int>(p.value)
<< "\t" << static_cast<int>(p.variance) << "\n";
}
}
}
file.close();
return true;
}
bool AIWriter::writeBuildFitness(const QString& filePath, const BuildFitness& fitness) {
QFileInfo fileInfo(filePath);
QDir dir = fileInfo.dir();
if (!dir.exists()) {
if (!dir.mkpath(".")) {
m_lastError = QString("Cannot create directory: %1").arg(dir.path());
return false;
}
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
m_lastError = QString("Cannot open file for writing: %1").arg(filePath);
return false;
}
QTextStream out(&file);
// Write entries in TAB-separated format: UnitName\tFitnessValue
for (const BuildFitnessEntry& entry : fitness.entries) {
out << entry.unitName << "\t" << entry.fitness << "\n";
}
file.close();
return true;
}
bool AIWriter::writeMaxUnits(const QString& filePath, const MaxUnits& maxUnits) {
QFileInfo fileInfo(filePath);
QDir dir = fileInfo.dir();
if (!dir.exists()) {
if (!dir.mkpath(".")) {
m_lastError = QString("Cannot create directory: %1").arg(dir.path());
return false;
}
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
m_lastError = QString("Cannot open file for writing: %1").arg(filePath);
return false;
}
QTextStream out(&file);
// Write entries in TAB-separated format: UnitName\tMaxCount
for (const MaxUnitsEntry& entry : maxUnits.entries) {
out << entry.unitName << "\t" << entry.maxCount << "\n";
}
file.close();
return true;
}
} // namespace AI