/
World.cs
495 lines (422 loc) · 22.2 KB
/
World.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using Json;
using Maps.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
namespace Maps
{
public class World
{
internal Sector Sector { get; set; }
public string Name { get; set; } = string.Empty;
public string Hex { get => hex.ToString(); set => hex = new Hex(value); }
internal string SubsectorHex => hex.ToSubsectorString();
[XmlElement("UWP"), JsonName("UWP")]
public string UWP { get; set; } = "X000000-0";
[XmlElement("PBG"), JsonName("PBG")]
public string PBG { get; set; } = "000";
public string Zone
{
get => zone;
set => zone = (value == " " || value == "G") ? string.Empty : value;
}
private string zone = string.Empty;
public string Bases { get; set; } = string.Empty;
public string Allegiance { get; set; } = "Na";
public string Stellar { get; set; } = string.Empty;
// T5
public string SS => "" + (char)('A' + Subsector);
[XmlElement("Ix"), JsonName("Ix")]
public string Importance { get; set; }
internal int? ImportanceValue
{
get
{
int? value = null;
string ix = Importance;
if (!string.IsNullOrWhiteSpace(ix) && int.TryParse(ix.Replace('{', ' ').Replace('}', ' '),
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.Integer,
CultureInfo.InvariantCulture, out int tmp))
{
value = tmp;
}
return value;
}
}
[XmlElement("Ex"), JsonName("Ex")]
public string Economic { get; set; }
[XmlElement("Cx"), JsonName("Cx")]
public string Cultural { get; set; }
public string Nobility { get; set; }
public byte Worlds { get; set; }
public int ResourceUnits { get; set; }
private Hex hex;
internal byte X => hex.X;
internal byte Y => hex.Y;
public int Subsector => ((X - 1) / Astrometrics.SubsectorWidth) + ((Y - 1) / Astrometrics.SubsectorHeight) * 4;
public int Quadrant => ((X - 1) / (Astrometrics.SubsectorWidth * 2)) + ((Y - 1) / (Astrometrics.SubsectorHeight * 2)) * 4;
internal Point Coordinates
{
get
{
if (Sector == null)
throw new InvalidOperationException("Can't get coordinates for a world not assigned to a sector");
return Astrometrics.LocationToCoordinates(Sector.Location, new Hex(X, Y));
}
}
// For API data
public int WorldX => Coordinates.X;
public int WorldY => Coordinates.Y;
public const int PBG_P = 0;
public const int PBG_B = 1;
public const int PBG_G = 2;
public const int UWP_St = 0;
public const int UWP_S = 1;
public const int UWP_A = 2;
public const int UWP_H = 3;
public const int UWP_P = 4;
public const int UWP_G = 5;
public const int UWP_L = 6;
public const int UWP_TL = 8;
internal char Starport => UWP[UWP_St];
internal int Size => Char.ToUpperInvariant(UWP[UWP_S]) == 'S' ? -1 : SecondSurvey.FromHex(UWP[UWP_S], valueIfUnknown: -1);
internal int Atmosphere => SecondSurvey.FromHex(UWP[UWP_A], valueIfUnknown: -1);
internal int Hydrographics => SecondSurvey.FromHex(UWP[UWP_H], valueIfUnknown: -1);
internal int PopulationExponent => SecondSurvey.FromHex(UWP[UWP_P], valueIfUnknown: 0);
internal int Government => SecondSurvey.FromHex(UWP[UWP_G], valueIfUnknown: 0);
internal int Law => SecondSurvey.FromHex(UWP[UWP_L], valueIfUnknown: 0);
internal int TechLevel => SecondSurvey.FromHex(UWP[UWP_TL], valueIfUnknown: 0);
internal int PopulationMantissa
{
get
{
int mantissa = SecondSurvey.FromHex(PBG[PBG_P], valueIfUnknown: 0);
// Hack for legacy data w/o PBG
if (mantissa == 0 && PopulationExponent > 0)
return 1;
return mantissa;
}
}
internal int Belts => SecondSurvey.FromHex(PBG[PBG_B], valueIfUnknown: 0);
internal int GasGiants => SecondSurvey.FromHex(PBG[PBG_G], valueIfUnknown: 0);
internal double Population => Math.Pow(10, PopulationExponent) * PopulationMantissa;
internal bool WaterPresent => (Hydrographics > 0) && (Atmosphere.InRange(2, 9) || Atmosphere.InRange(0xD, 0xF));
// Only define what is needed.
// TODO: Unify with validation checks.
// Planetary
internal bool IsAs => Size == 0;
internal bool IsVa => Atmosphere == 0;
// Population
internal bool IsHi => PopulationExponent >= 9;
// Economic
internal bool IsAg => Atmosphere.InRange(4, 9) && Hydrographics.InRange(4, 8) && PopulationExponent.InRange(5, 7);
internal bool IsIn => Atmosphere.InList(0, 1, 2, 4, 7, 9, 10, 11, 12) && PopulationExponent >= 9;
internal bool IsRi => Atmosphere.InList(6, 8) && PopulationExponent.InList(6, 7, 8);
internal bool IsCp => HasCode("Cp");
internal bool IsCs => HasCode("Cs");
internal bool IsCx => HasCode("Cx");
internal bool IsCapital => codes.Any(s => s == "Cp" || s == "Cs" || s == "Cx" || s == "Capital");
internal bool IsPenalColony => HasCode("Pe");
internal bool IsPrisonExileCamp => HasCode("Px") || HasCode("Ex"); // TODO: "Pr" is used in some legacy files, conflicts with T5 "Pre-Rich" - convert codes on import/export
internal bool IsReserve => HasCode("Re");
internal string ResearchStation => GetCodePrefix("Rs");
internal bool IsPlaceholder => UWP == "XXXXXXX-X" || UWP == "???????-?";
internal bool IsAnomaly => HasCode("{Anomaly}");
public bool HasCode(string code)
{
if (code == null)
throw new ArgumentNullException(nameof(code));
return codes.Any(s => s.Equals(code, StringComparison.InvariantCultureIgnoreCase));
}
public string GetCodePrefix(string code)
{
if (code == null)
throw new ArgumentNullException(nameof(code));
return codes.Where(s => s.StartsWith(code, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
}
public bool HasCodePrefix(string code)
{
if (code == null)
throw new ArgumentNullException(nameof(code));
return codes.Any(s => s.StartsWith(code, StringComparison.InvariantCultureIgnoreCase));
}
// Keep storage as a string and parse on demand to reduce memory consumption.
// (Many worlds * many codes = lots of strings and container overhead)
// "[Sophont]" - major race homeworld
// "(Sophont)" - minor race homeworld
// "(Sophont)0" - minor race homeworld (population in tenths)
// "Di(Sophont)" - minor race (dieback)
// "{comment ... }" - arbitrary comment
// "xyz" - other code
private static readonly Tuple<string, char>[] MARKERS = new Tuple<string, char>[]
{
Tuple.Create("[", ']'),
Tuple.Create("(", ')'),
Tuple.Create("{", '}'),
Tuple.Create("Di(", ')')
};
private class CodeList : IEnumerable<string>
{
public CodeList(string codes = "") { this.codes = codes; }
private string codes;
public IEnumerator<string> GetEnumerator()
{
int pos = 0;
while (pos < codes.Length)
{
if (codes[pos] == ' ')
{
++pos;
continue;
}
int begin = pos;
bool found = false;
foreach (var tuple in MARKERS)
{
string start = tuple.Item1;
char endchar = tuple.Item2;
if (codes.MatchAt(start, pos))
{
pos += start.Length;
pos = codes.IndexOf(endchar, pos);
if (pos != -1)
pos = codes.IndexOf(' ', pos);
if (pos == -1)
pos = codes.Length;
yield return codes.Substring(begin, pos - begin);
found = true;
break;
}
}
if (found)
continue;
pos = codes.IndexOf(' ', pos);
if (pos == -1)
pos = codes.Length;
yield return codes.Substring(begin, pos - begin);
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public string Remarks
{
get => string.Join(" ", codes);
set => codes = new CodeList(value ?? throw new ArgumentNullException(nameof(value)));
}
internal IEnumerable<string> Codes => codes;
private CodeList codes = new CodeList();
public string LegacyBaseCode
{
get => SecondSurvey.EncodeLegacyBases(Allegiance, Bases);
set => Bases = SecondSurvey.DecodeLegacyBases(Allegiance, value);
}
internal bool IsAmber => Zone == "A" || Zone == "U";
internal bool IsRed => Zone == "R" || Zone == "F";
internal bool IsTNEBalkanized => Zone == "B";
[XmlAttribute("Sector"), JsonName("Sector")]
public string SectorName => Sector.Names[0].Text;
public string SubsectorName => Sector.Subsector(Subsector)?.Name ?? "";
public string SectorAbbreviation => Sector.Abbreviation;
public string AllegianceName => Sector?.GetAllegianceFromCode(Allegiance)?.Name ?? "";
internal string BaseAllegiance => Sector?.AllegianceCodeToBaseAllegianceCode(Allegiance) ?? Allegiance;
internal string LegacyAllegiance => SecondSurvey.T5AllegianceCodeToLegacyCode(Allegiance);
internal void Validate(ErrorLogger errors, int lineNumber, string line)
{
// TODO: Validate partial UWPs
if (UWP.Contains('?') || UWP == "XXXXXXX-X") return;
#region Helpers
Action<string> Error = (string message) => { errors.Warning(message, lineNumber, line); };
Action<bool, string> ErrorIf = (bool test, string message) => { if (test) Error(message); };
Action<bool, string> ErrorUnless = (bool test, string message) => { if (!test) Error(message); };
Func<int, string, bool> Check = (int value, string hex) =>
{
foreach (char c in hex)
{
if (value == SecondSurvey.FromHex(c))
return true;
}
return false;
};
Func<string, bool, bool> CC = (string code, bool calc) =>
{
if (calc)
ErrorUnless(HasCode(code), $"Missing code: {code}");
else
ErrorUnless(!HasCode(code), $"Extraneous code: {code}");
return calc;
};
#endregion
#region UWP
// UWP
ErrorIf(Atmosphere > 15,
$"UWP: Atm={Atmosphere} out of range; should be: 0...F");
ErrorIf(Hydrographics > 10,
$"UWP: Hyd={Hydrographics} out of range; should be: 0...A");
ErrorIf(PopulationExponent > 15,
$"UWP: Pop={PopulationExponent} out of range; should be: 0...F");
ErrorUnless(Government.InRange(PopulationExponent - 5, Math.Max(15, PopulationExponent + 5)),
$"UWP: Gov={Government} out of range; should be: Pop(={PopulationExponent}) + Flux");
ErrorUnless(Law.InRange(Government - 5, Math.Max(18, Government + 5)),
$"UWP: Law={Law} out of range; should be: Gov(={Government}) + Flux");
int tlmod =
(Starport == 'A' ? 6 : 0) + (Starport == 'B' ? 4 : 0) + (Starport == 'C' ? 2 : 0) + (Starport == 'X' ? -4 : 0) +
(Size == 0 || Size == 1 ? 2 : 0) + (Size == 2 || Size == 3 || Size == 4 ? 1 : 0) +
(Atmosphere <= 3 ? 1 : 0) + (Atmosphere >= 10 ? 1 : 0) +
(Hydrographics == 9 ? 1 : 0) + (Hydrographics == 10 ? 2 : 0) +
(PopulationExponent.InRange(1, 5) ? 1 : 0) + (PopulationExponent == 9 ? 2 : 0) + (PopulationExponent >= 10 ? 4 : 0) +
(Government == 0 || Government == 5 ? 1 : 0) + (Government == 13 ? -2 : 0);
ErrorUnless(TechLevel.InRange(tlmod + 1, tlmod + 6) ||
(PopulationExponent == 0 && TechLevel == 0),
$"UWP: TL={TechLevel} out of range; should be: mods(={tlmod}) + 1D");
#endregion
#region Codes
// Planetary
bool As = CC("As", Check(Size, "0") /*&& Check(Atmosphere, "0") && Check(Hydrographics, "0")*/);
bool De = CC("De", Check(Atmosphere, "23456789") && Check(Hydrographics, "0"));
bool Fl = CC("Fl", Check(Atmosphere, "ABC") && Check(Hydrographics, "123456789A"));
bool Ga = CC("Ga", Check(Size, "678") && Check(Atmosphere, "568") && Check(Hydrographics, "567"));
bool He = CC("He", Check(Size, "3456789ABC") && Check(Atmosphere, "2479ABC") && Check(Hydrographics, "012"));
bool Ic = CC("Ic", Check(Atmosphere, "01") && Check(Hydrographics, "123456789A"));
bool Oc = CC("Oc", Check(Size, "ABCDEF") && Check(Atmosphere, "3456789DEF") && Check(Hydrographics, "A"));
bool Va = CC("Va", Check(Atmosphere, "0"));
bool Wa = CC("Wa", Check(Size, "3456789") && Check(Atmosphere, "3456789DEF") && Check(Hydrographics, "A"));
// Population
bool Di = (PopulationExponent == 0 /*&& Government == 0 && Law == 0*/ && TechLevel > 0);
ErrorIf(Di && !HasCodePrefix("Di"), "Missing code: Di or Di(sophont) is required if Pop=0 and TL>0");
bool Ba = CC("Ba", PopulationExponent == 0 /*&& Government == 0 && Law == 0*/ && TechLevel == 0);
bool Lo = CC("Lo", Check(PopulationExponent, "123"));
bool Ni = CC("Ni", Check(PopulationExponent, "456"));
bool Ph = CC("Ph", Check(PopulationExponent, "8"));
bool Hi = CC("Hi", Check(PopulationExponent, "9ABCDEF"));
// Economic
bool Pa = CC("Pa", Check(Atmosphere, "456789") && Check(Hydrographics, "45678") && Check(PopulationExponent, "48"));
bool Ag = CC("Ag", Check(Atmosphere, "456789") && Check(Hydrographics, "45678") && Check(PopulationExponent, "567"));
bool Na = CC("Na", Check(Atmosphere, "0123") && Check(Hydrographics, "0123") && Check(PopulationExponent, "6789ABCDEF"));
bool Pi = CC("Pi", Check(Atmosphere, "012479") && Check(PopulationExponent, "78"));
bool In = CC("In", Check(Atmosphere, "012479ABC") && Check(PopulationExponent, "9ABCDEF"));
bool Po = CC("Po", Check(Atmosphere, "2345") && Check(Hydrographics, "0123"));
bool Pr = CC("Pr", Check(Atmosphere, "68") && Check(PopulationExponent, "59"));
bool Ri = CC("Ri", Check(Atmosphere, "68") && Check(PopulationExponent, "678"));
ErrorUnless(As == IsAs, "Internal code failure: As/IsAs definitions");
ErrorUnless(Va == IsVa, "Internal code failure: Va/IsVa definitions");
ErrorUnless(Ag == IsAg, "Internal code failure: Ag/IsAg definitions");
ErrorUnless(In == IsIn, "Internal code failure: In/IsIn definitions");
ErrorUnless(Ri == IsRi, "Internal code failure: Ri/IsRi definitions");
#endregion
// {Ix}
int imp = 0;
if (!string.IsNullOrWhiteSpace(Importance))
{
string ix = Importance.Replace('{', ' ').Replace('}', ' ').Trim();
if (ix != "")
{
if ("AB".Contains(Starport)) ++imp;
if ("DEX".Contains(Starport)) --imp;
if (TechLevel >= 10) ++imp;
if (TechLevel >= 16) ++imp;
if (TechLevel <= 8) --imp;
if (PopulationExponent <= 6) --imp;
if (PopulationExponent >= 9) ++imp;
if (Ag) ++imp;
if (Ri) ++imp;
if (In) ++imp;
if (Bases == "NS" || Bases == "NW" || Bases == "W" || Bases == "X" || Bases == "D" || Bases == "RT" || Bases == "CK" || Bases == "KM" || Bases == "KV") ++imp;
ErrorUnless(Int32.Parse(ix) == imp,
$"{{Ix}} Importance={ix} incorrect; should be: {imp}");
}
}
// (Ex)
if (!string.IsNullOrWhiteSpace(Economic))
{
string ex = Economic.Replace('(', ' ').Replace(')', ' ').Trim();
if (ex != "")
{
int resources = SecondSurvey.FromHex(ex[0]);
int labor = SecondSurvey.FromHex(ex[1]);
int infrastructure = SecondSurvey.FromHex(ex[2]);
int efficiency = Int32.Parse(ex.Substring(3));
if (TechLevel < 8)
ErrorUnless(resources.InRange(2, 12),
$"(Ex) Resources={resources} out of range; should be: 2D if TL(={TechLevel})<8");
else
ErrorUnless(resources.InRange(2 + GasGiants + Belts, 12 + GasGiants + Belts),
$"(Ex) Resources={resources} out of range; should be: 2D + GG(={GasGiants}) + Belts(={Belts}) if TL(={TechLevel})=8+");
ErrorUnless(labor == Math.Max(0, PopulationExponent - 1),
$"(Ex) Labor={labor} incorrect; should be: Pop(={PopulationExponent}) - 1");
if (Ba)
ErrorUnless(infrastructure == 0,
$"(Ex) Infrastructure={infrastructure} incorrect; should be: 0 if Ba");
else if (Lo)
ErrorUnless(infrastructure == 1,
$"(Ex) Infrastructure={infrastructure} incorrect; should be: 1 if Lo");
else if (Ni)
ErrorUnless(infrastructure.InRange(Math.Max(0, imp + 1), Math.Max(0, imp + 6)),
$"(Ex) Infrastructure={infrastructure} out of range for Ni; should be: Imp(={imp}) + 1D");
else
ErrorUnless(infrastructure.InRange(Math.Max(0, imp + 2), Math.Max(0, imp + 12)),
$"(Ex) Infrastructure={infrastructure} out of range; should be: Imp(={imp}) + 2D");
ErrorUnless(efficiency.InRange(-5, 5),
$"(Ex) Efficiency={efficiency} out of range; should be: Flux");
}
}
// [Cx]
if (!string.IsNullOrWhiteSpace(Cultural))
{
string cx = Cultural.Replace('[', ' ').Replace(']', ' ').Trim();
if (cx != "")
{
int homogeneity = SecondSurvey.FromHex(cx[0]);
int acceptance = SecondSurvey.FromHex(cx[1]);
int strangeness = SecondSurvey.FromHex(cx[2]);
int symbols = SecondSurvey.FromHex(cx[3]);
if (PopulationExponent == 0)
{
ErrorUnless(homogeneity == 0,
$"[Cx] Homogeneity={homogeneity} incorrect; should be: 0 if Pop=0");
ErrorUnless(acceptance == 0,
$"[Cx] Acceptance={acceptance} incorrect; should be: 0 if Pop=0");
ErrorUnless(strangeness == 0,
$"[Cx] Strangeness={strangeness} incorrect; should be: 0 if Pop=0");
ErrorUnless(symbols == 0,
$"[Cx] Symbols={symbols} incorrect; should be: 0 if Pop=0");
}
else
{
ErrorUnless(homogeneity.InRange(Math.Max(1, PopulationExponent - 5), Math.Max(1, PopulationExponent + 5)),
$"[Cx] Homogeneity={homogeneity} out of range; should be: Pop(={PopulationExponent}) + Flux");
ErrorUnless(acceptance == Math.Max(1, PopulationExponent + imp),
$"[Cx] Acceptance={acceptance} incorrect; should be: Pop(={PopulationExponent}) + Imp(={imp})");
ErrorUnless(strangeness.InRange(Math.Max(1, 5 - 5), Math.Max(1, 5 + 5)),
$"[Cx] Strangeness={strangeness} out of range; should be: Flux + 5");
ErrorUnless(symbols.InRange(Math.Max(1, TechLevel - 5), Math.Max(1, TechLevel + 5)),
$"[Cx] Symbols={symbols} out of range; should be: TL(={TechLevel}) + Flux");
}
}
}
// Ownership
if (Government == 6 && !(HasCodePrefix("O:") || HasCodePrefix("Mr") ||
HasCode("Re") || HasCode("Px") || HasCode("Pe") || HasCode("Cy")))
errors.Warning("Gov 6 (captive/colony) missing one of: O:/Mr/Re/Px", lineNumber, line);
// PBG
ErrorIf(SecondSurvey.FromHex(PBG[PBG_P]) == 0 && PopulationExponent > 0,
$"PBG: Pop Multiplier = 0 but Population Exponent (={PopulationExponent}) > 0");
// TODO: Nobility
}
private string routes = null;
[XmlIgnore,JsonIgnore]
public string Routes
{
get
{
if (routes == null)
routes = string.Join(" ", Sector.RoutesForWorld(this).OrderBy(s => s));
return routes;
}
}
}
}