Repository navigation
Expand file tree
/
Copy pathMapProjection.cs
More file actions
246 lines (221 loc) · 10.7 KB
/
Copy pathMapProjection.cs
File metadata and controls
246 lines (221 loc) · 10.7 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
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
using OpenCiv2.Contracts;
using OpenCiv2.Core;
using OpenCiv2.Core.Rules;
using OpenCiv2.Core.State;
namespace OpenCiv2.Query;
/// <summary>P02 map information and W03 §1 knowledge channels. Only Session supplies the admitted identity.</summary>
public static class MapProjection
{
public static MapObservation? Observe(GameState world, ClientIdentity identity, ulong revision, RuleState rules)
{
var map = world.Map;
if (!map.Allocated)
{
return null;
}
if (identity.Runtime.Value == 0 || identity.Session.Value == 0
|| identity.Observer.Value >= EntityLimits.Civilizations)
{
throw new ArgumentException("Map projection requires an admitted observer", nameof(identity));
}
var geometry = map.Geometry;
int observer = identity.Observer.Value;
var memory = map.RememberedLayer(observer);
var tiles = new ObservedTile[geometry.Area];
for (int i = 0; i < tiles.Length; i++)
{
ref readonly var tile = ref map.Tiles[i];
if (!tile.RevealedTo.Contains(observer))
{
continue;
}
var point = geometry.Coordinates(i);
// A remembered unit bit is never current detection. City sites come from memory,
// independent of live foreign city names, size, ownership or lifetime.
tiles[i] = new((byte)tile.Terrain, i < memory.Length ? ObserveImprovements(memory[i]) : default,
tile.River, map.SpecialResource(point),
tile.Terrain == Terrain.Grassland && Geometry.GrasslandShield(point),
map.HutCandidate(point) ? MapFact<MapPresence>.Withheld : Present(false));
}
var units = new List<ObservedMapUnit>();
var pool = world.Entities.Units.Scanned;
var own = new bool[pool.Length];
for (int i = 0; i < pool.Length; i++)
{
ref readonly var unit = ref pool[i];
var point = unit.Position;
if (!unit.Token.Occupied || !geometry.Canonical(point))
{
continue;
}
if (unit.Owner == identity.Observer)
{
own[i] = true;
}
else if (unit.Visibility.Contains(observer)
&& tiles[geometry.TileIndex(point)!.Value].Terrain is not null)
{
units.Add(new(unit.Position, unit.Type, unit.Owner, null, MapUnitDetails.Withheld));
}
}
UnitTarget? active = null;
var ownPositions = new HashSet<int>();
// Display order follows consecutive own links only, with a bounded fallback for malformed
// links. Foreign links never contribute a rank, count, identity or traversal.
for (int pass = 0; pass < 2; pass++)
{
for (int i = 0; i < pool.Length; i++)
{
if (!own[i])
{
continue;
}
ref readonly var unit = ref pool[i];
int previous = (unit.Previous?.Value ?? -1);
if (pass == 0 && previous >= 0 && previous < own.Length && own[previous]
&& (pool[previous].Next?.Value ?? -1) == i
&&
pool[previous].Position.X == unit.Position.X && pool[previous].Position.Y == unit.Position.Y)
{
continue;
}
for (int j = i; j >= 0;)
{
ref readonly var member = ref pool[j];
own[j] = false;
var target = new UnitTarget(new((ushort)j), member.Token);
if ((world.Globals.ActiveUnit?.Value ?? -1) == j)
{
active = target;
}
int tileIndex = geometry.TileIndex(member.Position)!.Value;
ownPositions.Add(tileIndex);
var unitRule = member.Type.Value < rules.Definitions.Units.Length
? rules.Definitions.Units[member.Type.Value]
: null;
var tile = tiles[tileIndex];
bool airbase = (tile.Improvements & (MapFeatures.CitySite | MapFeatures.Fortification)) ==
(MapFeatures.CitySite | MapFeatures.Fortification);
bool stationary = airbase && unitRule is { Domain: RuleDomain.Air };
if (tile.Terrain is not null)
{
tiles[tileIndex] = tile with
{
Ownership = MapFact<MapTileOwnership>.Known(new(identity.Observer)),
Hut = Present(false),
AirbaseOccupant = stationary
? MapFact<MapTileOwnership>.Known(new(identity.Observer))
: tile.AirbaseOccupant
};
}
units.Add(new(member.Position, member.Type, member.Owner, target,
UnitDetails(member, unitRule, stationary, !world.Globals.Rules.NormalCombat)));
int next = (member.Next?.Value ?? -1);
j = next >= 0 && next < own.Length && own[next] && (pool[next].Previous?.Value ?? -1) == j &&
pool[next].Position.X == member.Position.X && pool[next].Position.Y == member.Position.Y ? next : -1;
}
}
}
var cities = new List<ObservedMapCity>();
var cityPool = world.Entities.Cities.Scanned;
for (int i = 0; i < cityPool.Length; i++)
{
ref readonly var city = ref cityPool[i];
if (!city.Token.Occupied || city.Owner != identity.Observer || !geometry.Canonical(city.Position))
{
continue;
}
int tileIndex = geometry.TileIndex(city.Position)!.Value;
if (tiles[tileIndex].Terrain is not null)
{
tiles[tileIndex] = tiles[tileIndex] with
{
Ownership = MapFact<MapTileOwnership>.Known(new(identity.Observer)),
Hut = Present(false)
};
}
cities.Add(new(city.Position, new((ushort)i), city.Token,
city.Name, city.Size,
CityDetails(world, rules, city, ownPositions.Contains(tileIndex))));
}
var admittedOwners = CivilizationSet.Only(observer);
foreach (var unit in units)
{
admittedOwners = admittedOwners.With(unit.Owner.Value);
}
var colors = new byte[EntityLimits.Civilizations];
for (byte i = 0; i < colors.Length; i++)
{
int tribe = world.Civilizations[i].Tribe;
// Uninstalled rules identify an authored state. Stock roles
// remain explicit in that profile. A selected role outside 0..7 stays diagnostic.
short role = admittedOwners.Contains(i) && rules.IsLoaded && tribe >= 0
&& tribe < rules.Leaders.Length
? rules.Leaders[tribe].Color
: i;
colors[i] = role is >= 0 and <= 255 ? (byte)role : byte.MaxValue;
}
return new(identity, revision, geometry.Width, geometry.Height, world.Globals.Rules.FlatWorld,
new(tiles), new(units), new(cities), new(new(colors), active));
}
// Remembered occupancy does not authorize disclosing a current unit.
private static MapFeatures ObserveImprovements(MapFeatures features) => features & ~MapFeatures.Unit;
private static MapFact<MapPresence> Present(bool value) =>
MapFact<MapPresence>.Known(value ? MapPresence.Present : MapPresence.Absent);
// P02 §§4–5. Own tall-object details are read-only. W04 counters retain the
// stored value and never imply a time to finish. Foreign AI values are withheld.
private static MapUnitDetails UnitDetails(Unit unit, UnitRule? rule, bool stationary, bool simplified)
{
bool defined = rule is not null && rule.Identity != RuleName.Empty;
var health = defined
? MapFact<MapHealth>.Known(new(unchecked((sbyte)rule!.HitPoints), unit.Damage, simplified))
: default;
var order = unit.Orders is UnitOrder.AiIdle or UnitOrder.AiGoTo
? MapFact<UnitOrder>.Withheld
: MapFact<UnitOrder>.Known(unit.Orders);
var work = defined && rule!.Role == RuleRole.Settle
? MapFact<int>.Known(unit.Counter) : default;
// P02 calls this awaiting input; F02/U03 name the same state Automated.
return new(health, order, Present(unit.Flags.Automated), Present(unit.Flags.Veteran),
default, work, defined ? Present(stationary) : default);
}
private static MapCityDetails CityDetails(GameState world, RuleState rules, City city, bool knownGarrison)
{
int owner = city.Owner.Value;
int tribe = world.Civilizations[owner].Tribe;
int style = tribe >= 0 && tribe < rules.Leaders.Length ? rules.Leaders[tribe].CityStyle : -1;
var appearance = style is >= 0 and <= 5
? MapFact<MapCityAppearance>.Known(MapCityAppearance.Resolve(style, city.Size,
CivilizationKnowledge.KnowsAdvance(world.Civilizations, owner, 37),
CivilizationKnowledge.KnowsAdvance(world.Civilizations, owner, 5),
CivilizationKnowledge.KnowsAdvance(world.Civilizations, owner, 24),
Improvement(city, 1), Improvement(city, 8) || HasGreatWall(world, rules, city.Owner)))
: default;
// Only own occupants are admitted here. No observed own garrison does not imply
// the absence of other occupants.
return new(city.Owner, CityKnowledge.Own, appearance, MapFact<int>.Known(city.Size),
knownGarrison ? Present(true) : default, Present(city.Flags.Disorder));
}
private static bool Improvement(City city, int index) => city.Improvements.Contains(index);
private static bool HasGreatWall(GameState world, RuleState rules, CivilizationSlot owner)
{
if (!rules.IsLoaded)
{
return false;
}
int slot = world.Globals.WonderCity[6];
if (slot < 0 || slot >= world.Entities.Cities.End || world.Entities.Cities.Storage[slot].Owner != owner)
{
return false;
}
int expiry = (int)rules.Definitions.WonderObsolescence[6];
for (int civ = 1; expiry >= 0 && civ < EntityLimits.Civilizations; civ++)
{
if (CivilizationKnowledge.KnowsAdvance(world.Civilizations, civ, expiry))
{
return false;
}
}
return true;
}
}