-
Notifications
You must be signed in to change notification settings - Fork 26
/
blueprint_template.cpp
351 lines (306 loc) · 10.6 KB
/
blueprint_template.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "ai.h"
#include "blueprint.h"
bool blueprint_plan_template::apply(Json::Value data, std::string & error)
{
if (data.isMember("max_retries"))
{
Json::Value value = data["max_retries"];
data.removeMember("max_retries");
if (!value.isIntegral())
{
error = "max_retries has wrong type (should be integer)";
return false;
}
if (value.asInt() <= 0)
{
error = "max_retries must be positive";
return false;
}
max_retries = size_t(value.asInt());
}
if (data.isMember("max_failures"))
{
Json::Value value = data["max_failures"];
data.removeMember("max_failures");
if (!value.isIntegral())
{
error = "max_failures has wrong type (should be integer)";
return false;
}
if (value.asInt() <= 0)
{
error = "max_failures must be positive";
return false;
}
max_failures = size_t(value.asInt());
}
if (data.isMember("military_limit"))
{
Json::Value value = data["military_limit"];
data.removeMember("military_limit");
if (!value.isArray() || value.size() != 2 || !value[0].isIntegral() || !value[1].isIntegral())
{
error = "military_limit has wrong type (should be [min, max])";
return false;
}
if (value[0].asInt() < 0 || value[1].asInt() > 100 || value[0].asInt() > value[1].asInt())
{
error = "military_limit percentages out of range";
return false;
}
military_min = value[0].asInt();
military_max = value[1].asInt();
}
if (data.isMember("start"))
{
Json::Value value = data["start"];
data.removeMember("start");
if (!value.isString())
{
error = "start has wrong type (should be string)";
return false;
}
start = value.asString();
}
else
{
error = "missing start";
return false;
}
if (data.isMember("outdoor"))
{
Json::Value value = data["outdoor"];
data.removeMember("outdoor");
if (!value.isArray() || std::find_if(value.begin(), value.end(), [](Json::Value & v) -> bool { return !v.isString(); }) != value.end())
{
error = "outdoor has wrong type (should be array of strings)";
return false;
}
for (auto & v : value)
{
if (!outdoor.insert(v.asString()).second)
{
error = "duplicate outdoor tag: " + v.asString();
return false;
}
}
}
if (data.isMember("tags"))
{
Json::Value value = data["tags"];
data.removeMember("tags");
if (!value.isObject())
{
error = "tags has wrong type (should be object)";
return false;
}
std::vector<std::string> tag_names{ std::move(value.getMemberNames()) };
for (auto & tag_name : tag_names)
{
Json::Value & tag = value[tag_name];
if (!tag.isArray() || std::find_if(tag.begin(), tag.end(), [](Json::Value & v) -> bool { return !v.isString(); }) != tag.end())
{
error = "tag " + tag_name + " has wrong type (should be array of strings)";
return false;
}
auto & tag_set = tags[tag_name];
for (auto & v : tag)
{
if (!tag_set.insert(v.asString()).second)
{
error = "duplicate tag: " + tag_name + " -> " + v.asString();
return false;
}
}
}
}
if (data.isMember("count_as"))
{
Json::Value value = data["count_as"];
data.removeMember("count_as");
if (!value.isObject())
{
error = "count_as has wrong type (should be object)";
return false;
}
auto count_as_names = value.getMemberNames();
for (auto & count_as_name : count_as_names)
{
if (std::count(count_as_name.begin(), count_as_name.end(), '/') != 2)
{
error = "count_as " + count_as_name + " has wrong format (should contain exactly two slashes)";
return false;
}
Json::Value & count_as_values = value[count_as_name];
if (!count_as_values.isObject())
{
error = "count_as " + count_as_name + " has wrong type (should be object)";
return false;
}
for (auto & count_as_key : count_as_values.getMemberNames())
{
if (!count_as_values[count_as_key].isIntegral() || count_as_values[count_as_key].asInt() <= 0)
{
error = "count_as " + count_as_name + " " + count_as_key + " is invalid (must be positive integer)";
return false;
}
count_as[count_as_name][count_as_key] = size_t(count_as_values[count_as_key].asInt());
}
}
}
if (data.isMember("limits"))
{
Json::Value value = data["limits"];
data.removeMember("limits");
if (!value.isObject())
{
error = "limits has wrong type (should be object)";
return false;
}
auto limit_names = value.getMemberNames();
for (auto & limit_name : limit_names)
{
Json::Value & limit = value[limit_name];
if (!limit.isArray() || limit.size() != 2 || !limit[0].isIntegral() || !limit[1].isIntegral())
{
error = "limit " + limit_name + " has wrong type (should be [integer, integer])";
return false;
}
if (limit[0].asInt() < 0)
{
error = "limit " + limit_name + " has invalid minimum";
return false;
}
if (limit[1].asInt() < limit[0].asInt())
{
error = "limit " + limit_name + " has invalid maximum";
return false;
}
limits[limit_name] = std::make_pair(size_t(limit[0].asInt()), size_t(limit[1].asInt()));
}
}
if (data.isMember("instance_limits"))
{
Json::Value value = data["instance_limits"];
data.removeMember("instance_limits");
if (!value.isObject())
{
error = "instance_limits has wrong type (should be object)";
return false;
}
std::vector<std::string> type_names{ std::move(value.getMemberNames()) };
for (auto & type_name : type_names)
{
Json::Value & type = value[type_name];
if (!type.isObject())
{
error = "instance_limits " + type_name + " has wrong type (should be object)";
return false;
}
auto & limits = instance_limits[type_name];
std::vector<std::string> instance_names{ std::move(type.getMemberNames()) };
for (auto & instance_name : instance_names)
{
Json::Value & limit = type[instance_name];
if (!limit.isArray() || limit.size() != 2 || !limit[0].isIntegral() || !limit[1].isIntegral())
{
error = "instance_limit " + type_name + " " + instance_name + " has wrong type (should be [integer, integer])";
return false;
}
if (limit[0].asInt() < 0)
{
error = "instance_limit " + type_name + " " + instance_name + " has invalid minimum";
return false;
}
if (limit[1].asInt() < limit[0].asInt())
{
error = "instance_limit " + type_name + " " + instance_name + " has invalid maximum";
return false;
}
limits[instance_name] = std::make_pair(size_t(limit[0].asInt()), size_t(limit[1].asInt()));
}
}
}
if (data.isMember("variables"))
{
Json::Value vars = data["variables"];
data.removeMember("variables");
if (!vars.isObject())
{
error = "variables has wrong type (should be object)";
return false;
}
auto var_names = vars.getMemberNames();
for (auto & var_name : var_names)
{
Json::Value & var_value = vars[var_name];
if (!var_value.isString())
{
error = "variable " + var_name + " has wrong type (should be string)";
return false;
}
context.variables[var_name] = var_value.asString();
}
}
if (data.isMember("padding_x"))
{
Json::Value value = data["padding_x"];
data.removeMember("padding_x");
if (!value.isArray() || value.size() != 2 || !value[0].isIntegral() || !value[1].isIntegral())
{
error = "padding_x has wrong type (should be [integer, integer])";
return false;
}
if (value[0].asInt() > 0)
{
error = "padding_x[0] must not be positive";
return false;
}
if (value[1].asInt() < 0)
{
error = "padding_x[1] must not be negative";
return false;
}
padding_x = std::make_pair((int16_t)value[0].asInt(), (int16_t)value[1].asInt());
}
if (data.isMember("padding_y"))
{
Json::Value value = data["padding_y"];
data.removeMember("padding_y");
if (!value.isArray() || value.size() != 2 || !value[0].isIntegral() || !value[1].isIntegral())
{
error = "padding_y has wrong type (should be [integer, integer])";
return false;
}
if (value[0].asInt() > 0)
{
error = "padding_y[0] must not be positive";
return false;
}
if (value[1].asInt() < 0)
{
error = "padding_y[1] must not be negative";
return false;
}
padding_y = std::make_pair((int16_t)value[0].asInt(), (int16_t)value[1].asInt());
}
if (data.isMember("priorities"))
{
Json::Value p = data["priorities"];
data.removeMember("priorities");
if (!priorities_from_json(priorities, p, error))
{
return false;
}
}
if (data.isMember("stock_goals"))
{
Json::Value s = data["stock_goals"];
data.removeMember("stock_goals");
if (!stock_goals.from_json(s, error))
{
return false;
}
}
return apply_unhandled_properties(data, "", error);
}