-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameData.cs
372 lines (344 loc) · 15 KB
/
GameData.cs
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cardamom.Interface;
using Cardamom.Planar;
using Cardamom.Serialization;
using Cardamom.Utilities.Markov;
using SFML.Graphics;
namespace PanzerBlitz
{
public static class GameData
{
enum Attribute
{
UNIT_MOVEMENT_RULES,
FACTIONS,
FACTION_RENDER_DETAILS,
UNIT_CONFIGURATIONS,
UNIT_RENDER_DETAILS,
UNIT_CONFIGURATION_LINKS,
UNIT_CONFIGURATION_LOCKS,
FORMATION_TEMPLATES,
SCENARIOS,
TILE_COMPONENT_RULES,
ENVIRONMENTS,
TILE_RENDERERS,
NAME_GENERATORS,
TERRAIN_GENERATORS,
MATCH_SETTINGS
};
public static ushort OnlinePort = 1000;
public static Player Player =
new Player(OnlineId.Temporary(DateTime.Now.Ticks), "Player " + DateTime.Now.Ticks.ToString());
public static string LoadedModule;
public static Dictionary<string, UnitMovementRules> UnitMovementRules;
public static Dictionary<string, TileComponentRules> TileComponentRules;
public static Dictionary<string, Environment> Environments;
public static Dictionary<string, Faction> Factions;
public static Dictionary<string, FactionRenderDetails> FactionRenderDetails;
public static Dictionary<string, UnitConfiguration> UnitConfigurations;
public static Dictionary<string, UnitRenderDetails> UnitRenderDetails;
public static Dictionary<string, UnitConfigurationLink> UnitConfigurationLinks;
public static Dictionary<string, UnitConfigurationLock> UnitConfigurationLocks;
public static Dictionary<string, FormationTemplate> FormationTemplates;
public static List<Scenario> Scenarios;
public static Dictionary<string, TileRenderer> TileRenderers;
public static Dictionary<string, MarkovGenerator<char>> NameGenerators;
public static Dictionary<string, TerrainGeneratorConfiguration> TerrainGenerators;
public static Dictionary<string, MatchSetting> MatchSettings;
public static void Load(string Module)
{
LoadedModule = Module;
string path = "./Modules/" + Module;
ClassLibrary.Instance.ReadBlock(new ParseBlock(new ParseBlock[]
{
ParseBlock.FromFile(path + "/Theme/Fonts.blk"),
new ParseBlock(
"class<>",
"classes",
Enumerable.Repeat(path + "/Theme/Base.blk", 1)
.Concat(Directory.EnumerateFiles(
path + "/Theme/Components", "*", SearchOption.AllDirectories))
.SelectMany(i => ParseBlock.FromFile(i).Break()))
}));
var block = new ParseBlock(new ParseBlock[] {
ParseBlock.FromFile(path + "/UnitMovementRules.blk"),
ParseBlock.FromFile(path + "/Factions.blk"),
ParseBlock.FromFile(path + "/FactionRenderDetails.blk"),
ParseBlock.FromFile(path + "/Terrain.blk"),
ParseBlock.FromFile(path + "/Environments.blk"),
ParseBlock.FromFile(path + "/TerrainRenderers.blk"),
ParseBlock.FromFile(path + "/NameGenerators.blk"),
ParseBlock.FromFile(path + "/TerrainGenerators.blk"),
ParseBlock.FromFile(path + "/MatchSettings.blk"),
new ParseBlock(
"unit-configuration<>",
"unit-configurations",
LoadDirectory(path + "/UnitConfigurations")),
new ParseBlock(
"unit-configuration-link<>",
"unit-configuration-links",
LoadDirectory(path + "/UnitConfigurationLinks")),
new ParseBlock(
"unit-configuration-lock<>",
"unit-configuration-locks",
LoadDirectory(path + "/UnitConfigurationLocks")),
new ParseBlock(
"formation-template<>",
"formation-templates",
LoadDirectory(path + "/FormationTemplates")),
new ParseBlock(
"unit-render-details<>",
"unit-render-details",
LoadDirectory(path + "/UnitRenderDetails")),
new ParseBlock(
"scenario[]",
"scenarios",
LoadDirectory(path + "/Scenarios"))
});
Load(path, block);
}
static IEnumerable<ParseBlock> LoadDirectory(string Path)
{
return new ParseBlock(
string.Join(
string.Empty,
Directory
.EnumerateFiles(Path, "*", SearchOption.AllDirectories)
.OrderBy(i => i)
.Select(File.ReadAllText)))
.Break();
}
static void Load(string Path, ParseBlock Block)
{
Cence.Module.AddParsers(Block);
Block.AddParser<Color>("color", i => ClassLibrary.Instance.ParseColor(i.String), false);
Block.AddParser<List<Color>>("color[]", i => ClassLibrary.Instance.ParseColors(i.String), false);
Block.AddParser<Dictionary<string, Color>>("color<>", i => i.BreakToDictionary<Color>(), false);
Block.AddParser<Polygon>("zone", i => new Polygon(i));
Block.AddParser<Coordinate>();
Block.AddParser<TileBase>();
Block.AddParser<TileEdge>();
Block.AddParser<TilePathOverlay>();
Block.AddParser<WeaponClass>();
Block.AddParser<UnitClass>();
Block.AddParser<UnitWeight>();
Block.AddParser<UnitStatus>();
Block.AddParser<BlockType>();
Block.AddParser<Front>();
Block.AddParser<Weapon>();
Block.AddParser<MovementRule>();
Block.AddParser<UnitMovementRules>();
Block.AddParser<Faction>();
Block.AddParser<UnitConfiguration>();
Block.AddParser<UnitConstraints>();
Block.AddParser<UnitConfigurationLink>();
Block.AddParser<UnitConfigurationLock>();
Block.AddParser<FormationFeature>();
Block.AddParser<FormationTemplate>("formation-template", i => null);
Block.AddParser<CompositeFormationTemplate>();
Block.AddParser<AtomicFormationTemplate>();
Block.AddParser<BindingFormationTemplate>();
Block.AddParser<FilteringFormationTemplate>();
Block.AddParser<ParameterizingFormationTemplate>();
Block.AddParser<ReplicatingFormationTemplate>();
Block.AddParser<SelectingFormationTemplate>();
Block.AddParser<SwitchingFormationTemplate>();
Block.AddParser<Direction>();
Block.AddParser<TerrainAttribute>();
Block.AddParser<TileComponentRules>();
Block.AddParser<TileRuleSet>();
Block.AddParser<Environment>();
Block.AddParsers<object>(MatcherSerializer.Instance.GetParsers());
Block.AddParser<UnitGroup>();
Block.AddParser<UnitCount>();
Block.AddParser<ConvoyMovementAutomator>();
Block.AddParser<PositionalDeploymentConfiguration>();
Block.AddParser<ConvoyDeploymentConfiguration>();
Block.AddParser<ObjectiveSuccessLevel>();
Block.AddParser<VictoryCondition>();
Block.AddParser<ObjectiveSuccessTrigger>();
Block.AddParsers<Objective>(ObjectiveSerializer.Instance.GetParsers());
Block.AddParser<ArmyConfiguration>();
Block.AddParser<BoardConfiguration>();
Block.AddParser<BoardCompositeMapConfiguration>(
"map-configuration", i => new BoardCompositeMapConfiguration(i));
Block.AddParser<RandomMapConfiguration>();
Block.AddParsers<Sequence>(SequenceSerializer.Instance.GetParsers());
Block.AddParser<TurnConfiguration>();
Block.AddParser<ScenarioRules>();
Block.AddParser<Scenario>();
Block.AddParser<FactionRenderDetails>(
"faction-render-details", i => new FactionRenderDetails(i, Path + "/FactionSymbols/"));
Block.AddParser<UnitRenderDetails>(
"unit-render-details", i => new UnitRenderDetails(i, Path + "/UnitSprites/"));
Block.AddParser<TileRenderer>();
Block.AddParser<MapGeneratorConfiguration>();
Block.AddParser<TerrainGeneratorConfiguration>();
Block.AddParser<MatchSetting>();
Block.AddParser<MarkovGenerator<char>>(
"name-generator", i => FileUtils.LoadLanguage(Path + "/NameGenerators/" + i.String));
Block.AddParser<FeatureGenerator>();
var attributes = Block.BreakToAttributes<object>(typeof(Attribute), true);
UnitMovementRules = (Dictionary<string, UnitMovementRules>)attributes[(int)Attribute.UNIT_MOVEMENT_RULES];
TileComponentRules =
(Dictionary<string, TileComponentRules>)attributes[(int)Attribute.TILE_COMPONENT_RULES];
Environments = (Dictionary<string, Environment>)attributes[(int)Attribute.ENVIRONMENTS];
Factions = (Dictionary<string, Faction>)attributes[(int)Attribute.FACTIONS];
FactionRenderDetails =
(Dictionary<string, FactionRenderDetails>)attributes[(int)Attribute.FACTION_RENDER_DETAILS];
UnitConfigurations = (Dictionary<string, UnitConfiguration>)attributes[(int)Attribute.UNIT_CONFIGURATIONS];
UnitConfigurationLinks =
(Dictionary<string, UnitConfigurationLink>)attributes[(int)Attribute.UNIT_CONFIGURATION_LINKS];
UnitConfigurationLocks =
(Dictionary<string, UnitConfigurationLock>)attributes[(int)Attribute.UNIT_CONFIGURATION_LOCKS];
FormationTemplates = (Dictionary<string, FormationTemplate>)attributes[(int)Attribute.FORMATION_TEMPLATES];
UnitRenderDetails = (Dictionary<string, UnitRenderDetails>)attributes[(int)Attribute.UNIT_RENDER_DETAILS];
Scenarios = (List<Scenario>)attributes[(int)Attribute.SCENARIOS];
TileRenderers = (Dictionary<string, TileRenderer>)attributes[(int)Attribute.TILE_RENDERERS];
NameGenerators = (Dictionary<string, MarkovGenerator<char>>)attributes[(int)Attribute.NAME_GENERATORS];
MatchSettings = (Dictionary<string, MatchSetting>)attributes[(int)Attribute.MATCH_SETTINGS];
// Emit warnings for units without configured render details.
foreach (UnitConfiguration unit in UnitConfigurations.Values)
{
if (!UnitRenderDetails.ContainsKey(unit.UniqueKey))
Console.WriteLine(
"[WARNING]: No render details configured for UnitConfiguration {0}", unit.UniqueKey);
else if (!File.Exists(UnitRenderDetails[unit.UniqueKey].ImagePath))
Console.WriteLine(
"[WARNING]: Image {0} missing for UnitConfiguration {1}",
UnitRenderDetails[unit.UniqueKey].ImagePath,
unit.UniqueKey);
}
// Emit warnings for factions without configured render details.
foreach (Faction faction in Factions.Values)
{
if (!FactionRenderDetails.ContainsKey(faction.UniqueKey))
Console.WriteLine(
"[WARNING]: No render details configured for Faction {0}", faction.UniqueKey);
else if (!File.Exists(FactionRenderDetails[faction.UniqueKey].ImagePath))
Console.WriteLine(
"[WARNING]: Image {0} missing for Faction {1}",
FactionRenderDetails[faction.UniqueKey].ImagePath,
faction.UniqueKey);
}
// Debug scenario configurations.
foreach (Scenario s in Scenarios)
{
using (MemoryStream m = new MemoryStream())
{
try
{
var stream = new SerializationOutputStream(m);
stream.Write(s);
}
catch (Exception e)
{
Console.WriteLine("There was an error writing scenario {0}\n\n{1}", s.Name, e.Message);
}
m.Seek(0, SeekOrigin.Begin);
try
{
var stream = new SerializationInputStream(m);
var copy = new Scenario(stream);
}
catch (Exception e)
{
Console.WriteLine("There was an error reading scenario {0}\n\n{1}", s.Name, e);
Console.WriteLine(new HexDumpWriter(16).Write(m.GetBuffer()));
}
}
}
var random = new Random();
var parameters =
new ArmyParameters(
Factions["german"],
0,
0,
new ScenarioParameters(1940, MatchSettings["russia-winter"], 0, new Coordinate(0, 0), false));
foreach (var template in FormationTemplates.Values)
{
Console.WriteLine(
string.Join(
"\n",
template.Generate(random, new FormationParameters(parameters)).Select(i => i.ToString())));
}
}
public static void Load(string Module, SerializationInputStream Stream)
{
LoadedModule = Module;
string Path = "./Modules/" + Module;
ClassLibrary.Instance.ReadBlock(new ParseBlock(new ParseBlock[]
{
ParseBlock.FromFile(Path + "/Theme/Fonts.blk"),
new ParseBlock(
"class<>",
"classes",
Enumerable.Repeat(Path + "/Theme/Base.blk", 1)
.Concat(Directory.EnumerateFiles(
Path + "/Theme/Components", "*", SearchOption.AllDirectories))
.SelectMany(i => ParseBlock.FromFile(i).Break()))
}));
UnitMovementRules = Stream.ReadEnumerable(
i => i.ReadObject(j => new UnitMovementRules(j), false, true)).ToDictionary(i => i.UniqueKey);
TileComponentRules = Stream.ReadEnumerable(
i => i.ReadObject(j => new TileComponentRules(j), false, true)).ToDictionary(i => i.UniqueKey);
Environments = Stream.ReadEnumerable(
i => i.ReadObject(j => new Environment(j), false, true)).ToDictionary(i => i.UniqueKey);
Factions = Stream.ReadEnumerable(
i => i.ReadObject(j => new Faction(j), false, true)).ToDictionary(i => i.UniqueKey);
FactionRenderDetails = Stream.ReadEnumerable(
i => new KeyValuePair<string, FactionRenderDetails>(
i.ReadString(),
new FactionRenderDetails(i, Path + "/FactionSymbols/"))).ToDictionary(i => i.Key, i => i.Value);
UnitConfigurations = Stream.ReadEnumerable(
i => i.ReadObject(j => new UnitConfiguration(j), false, true)).ToDictionary(i => i.UniqueKey);
UnitRenderDetails = Stream.ReadEnumerable(
i => new KeyValuePair<string, UnitRenderDetails>(
i.ReadString(),
new UnitRenderDetails(i, Path + "/UnitSprites/"))).ToDictionary(i => i.Key, i => i.Value);
UnitConfigurationLinks =
Stream.ReadEnumerable(i => new UnitConfigurationLink(i)).ToDictionary(i => i.UniqueKey);
UnitConfigurationLocks =
Stream.ReadEnumerable(i => new UnitConfigurationLock(i)).ToDictionary(i => i.UniqueKey);
Scenarios = Stream.ReadEnumerable(i => new Scenario(i)).ToList();
TileRenderers = Stream.ReadEnumerable(i => new TileRenderer(i)).ToDictionary(i => i.UniqueKey);
NameGenerators = Stream.ReadEnumerable(
i => new KeyValuePair<string, MarkovGenerator<char>>(
Stream.ReadString(), Stream.ReadObject(j => new MarkovGenerator<char>(j))))
.ToDictionary(i => i.Key, i => i.Value);
TerrainGenerators = Stream.ReadEnumerable(
i => new KeyValuePair<string, TerrainGeneratorConfiguration>(
Stream.ReadString(), Stream.ReadObject(j => new TerrainGeneratorConfiguration(j))))
.ToDictionary(i => i.Key, i => i.Value);
MatchSettings = Stream.ReadEnumerable(i => new MatchSetting(i)).ToDictionary(i => i.UniqueKey);
}
public static void Serialize(SerializationOutputStream Stream)
{
Stream.Write(UnitMovementRules, i => Stream.Write(i.Value, false, true));
Stream.Write(TileComponentRules, i => Stream.Write(i.Value, false, true));
Stream.Write(Environments, i => Stream.Write(i.Value, false, true));
Stream.Write(Factions, i => Stream.Write(i.Value, false, true));
Stream.Write(FactionRenderDetails, i =>
{
Stream.Write(i.Key);
Stream.Write(i.Value);
});
Stream.Write(UnitConfigurations, i => Stream.Write(i.Value, false, true));
Stream.Write(UnitRenderDetails, i =>
{
Stream.Write(i.Key);
Stream.Write(i.Value);
});
Stream.Write(UnitConfigurationLinks.Values);
Stream.Write(UnitConfigurationLocks.Values);
Stream.Write(Scenarios);
Stream.Write(TileRenderers, i => Stream.Write(i.Value));
Stream.Write(NameGenerators, i => { Stream.Write(i.Key); Stream.Write(i.Value, false, true); });
Stream.Write(TerrainGenerators, i => { Stream.Write(i.Key); Stream.Write(i.Value, false, true); });
Stream.Write(MatchSettings, i => Stream.Write(i.Value));
}
}
}