forked from CnCNet/WorldAlteringEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapTile.cs
More file actions
454 lines (372 loc) · 16.6 KB
/
MapTile.cs
File metadata and controls
454 lines (372 loc) · 16.6 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
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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using TSMapEditor.GameMath;
using TSMapEditor.Models.Enums;
using TSMapEditor.Models.MapFormat;
using TSMapEditor.Rendering;
namespace TSMapEditor.Models
{
/// <summary>
/// A cell on the map with additional logic properties for the map editor.
/// </summary>
public class MapTile : IsoMapPack5Tile
{
private const int SubCellCount = 5;
public MapTile() { }
public MapTile(byte[] data) : base(data) { }
/// <summary>
/// The cached image for this tile.
/// This should be cleared when the tile's terrain is changed.
/// </summary>
public TileImage TileImage { get; set; }
public TerrainObject TerrainObject { get; set; }
public List<Structure> Structures { get; set; } = new List<Structure>();
public List<Unit> Vehicles { get; set; } = new List<Unit>();
public List<Aircraft> Aircraft { get; set; } = new List<Aircraft>();
public Infantry[] Infantry { get; set; } = new Infantry[SubCellCount];
public TileImage PreviewTileImage { get; set; }
public int PreviewSubTileIndex { get; set; }
public int PreviewLevel { get; set; } = -1;
public Overlay Overlay { get; set; }
public Smudge Smudge { get; set; }
public List<Waypoint> Waypoints { get; set; } = new List<Waypoint>();
public CellTag CellTag { get; set; }
/// <summary>
/// A list of objects that graphically overlap with this tile.
/// When this tile is re-drawn, all the objects in the list should
/// be re-drawn as well.
/// </summary>
public List<AbstractObject> OverlapList { get; set; }
/// <summary>
/// The number of the screen refresh iteration when
/// this map tile was last rendered. If this value matches the current
/// rendering iteration, there is no need to draw this tile.
/// </summary>
public ulong LastRefreshIndex;
public MapColor CellLighting { get; set; } = new MapColor(1.0, 1.0, 1.0);
public List<(Structure Source, double DistanceInLeptons)> LightSources { get; set; } = new();
public void RefreshLighting(Lighting lighting, LightingPreviewMode lightingPreviewMode)
{
if (lightingPreviewMode == LightingPreviewMode.NoLighting)
{
CellLighting = new MapColor(1.0, 1.0, 1.0);
return;
}
double globalAmbient = lighting.GetAmbientComponent(lightingPreviewMode);
double globalLevel = lighting.GetLevelComponent(lightingPreviewMode);
double globalGround = lighting.GetGroundComponent(lightingPreviewMode);
double globalRed = lighting.GetRedComponent(lightingPreviewMode);
double globalGreen = lighting.GetGreenComponent(lightingPreviewMode);
double globalBlue = lighting.GetBlueComponent(lightingPreviewMode);
double redDivisor = globalRed >= 1.0 ? globalRed : 1.0;
double greenDivisor = globalGreen >= 1.0 ? globalGreen : 1.0;
double blueDivisor = globalBlue >= 1.0 ? globalBlue : 1.0;
double cellAmbient = globalAmbient;
double cellR = globalRed;
double cellG = globalGreen;
double cellB = globalBlue;
// Apply Ground
cellAmbient *= (1.0 - globalGround);
// Apply Level
cellAmbient += globalLevel * Level;
// Check all the light sources and how they affect this light
foreach (var source in LightSources)
{
// Sources with intensity of 0.0 don't get any light applied
if (source.Source.ObjectType.LightIntensity == 0.0)
continue;
var buildingType = source.Source.ObjectType;
double distanceRatio = 1.0 - (source.DistanceInLeptons / source.Source.ObjectType.LightVisibility);
// Intensity modifies the cell ambient value.
// For example, if Ambient=0.5 and LightIntensity=1.0, in a cell that is fully
// lit by the light post, the overall ambient level becomes 0.5 + 1.0 = 1.5
cellAmbient += source.Source.ObjectType.LightIntensity * distanceRatio;
double redStrength;
double greenStrength;
double blueStrength;
if (lightingPreviewMode == LightingPreviewMode.Normal)
{
// Apply tint. Tint does NOT depend on LightIntensity, but is independent of it
// (as long as LightIntensity != 0).
// Strength of tint depends on strength of global tint. For example, adding local red of 1.0
// to global red of 1.5 leads to a much smaller change than if the local red was added to global red of 0.5.
redStrength = (buildingType.LightRedTint / redDivisor) * distanceRatio;
greenStrength = (buildingType.LightGreenTint / greenDivisor) * distanceRatio;
blueStrength = (buildingType.LightBlueTint / blueDivisor) * distanceRatio;
}
else
{
// We are previewing Ion Storm or Psychic Dominator lighting.
// In this mode, the game normalizes all lighting color tints to the strongest tint.
double highest = Math.Max(Math.Max(buildingType.LightRedTint, buildingType.LightGreenTint), buildingType.LightBlueTint);
double highestDivisor = Math.Max(Math.Max(redDivisor, greenDivisor), blueDivisor);
redStrength = (highest / highestDivisor) * distanceRatio;
greenStrength = redStrength;
blueStrength = redStrength;
}
cellR += redStrength;
cellG += greenStrength;
cellB += blueStrength;
}
const double lightingComponentMax = 2.0;
// Apply Ambient to all components
cellR *= cellAmbient;
cellG *= cellAmbient;
cellB *= cellAmbient;
// In case the components exceed 2.0, they are all scaled down to fit within 0.0 to 2.0
double highestComponentValue = Math.Max(cellR, Math.Max(cellG, cellB));
if (highestComponentValue > lightingComponentMax)
{
double scale = lightingComponentMax / highestComponentValue;
cellR *= scale;
cellG *= scale;
cellB *= scale;
}
CellLighting = new MapColor(cellR, cellG, cellB);
}
public void ShiftPosition(int x, int y)
{
X += (short)x;
Y += (short)y;
// If we have overlay and/or a smudge, also move their position
if (Overlay != null)
Overlay.Position += new Point2D(x, y);
if (Smudge != null)
Smudge.Position += new Point2D(x, y);
}
public void AddObjectsToList(List<AbstractObject> objects)
{
if (Structures.Count > 0)
objects.AddRange(Structures);
if (Vehicles.Count > 0)
objects.AddRange(Vehicles);
}
public void AddInfantry(Infantry infantry)
{
Infantry[(int)infantry.SubCell] = infantry;
}
public void MoveInfantryToSubCell(Infantry infantry, SubCell newSubCell)
{
if (infantry.Position != CoordsToPoint())
throw new InvalidOperationException($"{nameof(MapTile)}.{nameof(MoveInfantryToSubCell)}: Cannot move infantry that is not on this cell.");
if (infantry.SubCell == SubCell.None)
throw new InvalidOperationException($"{nameof(MapTile)}.{nameof(MoveInfantryToSubCell)}: Given infantry is not on a subcell!");
// Sanity check just to be sure
if (Infantry[(int)infantry.SubCell] != infantry)
throw new InvalidOperationException($"{nameof(MapTile)}.{nameof(MoveInfantryToSubCell)}: Infantry data structure mismatch");
// One sub-cell can only contain one infantry.
// If the new sub-cell already has another infantry, move them to the argument infantry's old position.
Infantry otherInfantry = Infantry[(int)newSubCell];
if (otherInfantry != null)
{
Infantry[(int)infantry.SubCell] = otherInfantry;
otherInfantry.SubCell = infantry.SubCell;
}
else
{
// If the new sub-cell does not have infantry, simply clear the infantry information for the
// argument infantry's old position before applying them to the new position.
Infantry[(int)infantry.SubCell] = null;
}
// Finally, move the argument infantry to the given sub-cell.
infantry.SubCell = newSubCell;
Infantry[(int)newSubCell] = infantry;
}
public void DoForAllInfantry(Action<Infantry> action)
{
for (int i = 0; i < Infantry.Length; i++)
{
if (Infantry[i] != null)
action(Infantry[i]);
}
}
public void DoForAllVehicles(Action<Unit> action)
{
foreach (var unit in Vehicles)
{
action(unit);
}
}
public void DoForAllAircraft(Action<Aircraft> action)
{
foreach (var aircraft in Aircraft)
{
action(aircraft);
}
}
public void DoForAllBuildings(Action<Structure> action)
{
foreach (var structure in Structures)
{
action(structure);
}
}
public void DoForAllWaypoints(Action<Waypoint> action)
{
foreach (var waypoint in Waypoints)
{
action(waypoint);
}
}
public SubCell GetSubCellClosestToPosition(Point2D position, bool onlyOccupiedCells)
{
SubCell closestSubcell = SubCell.None;
float shortestDistance = float.MaxValue;
for (int i = 0; i < SubCellCount; i++)
{
SubCell subCell = (SubCell)i;
if (onlyOccupiedCells && GetInfantryFromSubCellSpot(subCell) == null)
continue;
var subCellCoords = GetTileCenter() + CellMath.GetSubCellOffset(subCell);
var distanceToSubCell = Vector2.Distance(position.ToXNAVector(), subCellCoords.ToXNAVector());
if (distanceToSubCell < shortestDistance)
{
closestSubcell = subCell;
shortestDistance = distanceToSubCell;
}
}
return closestSubcell;
}
public SubCell GetFreeSubCellSpot()
{
if (GetInfantryFromSubCellSpot(SubCell.Bottom) == null)
return SubCell.Bottom;
if (GetInfantryFromSubCellSpot(SubCell.Left) == null)
return SubCell.Left;
if (GetInfantryFromSubCellSpot(SubCell.Right) == null)
return SubCell.Right;
return SubCell.None;
}
public Infantry GetInfantryFromSubCellSpot(SubCell subCell)
{
return Infantry[(int)subCell];
}
public Infantry GetFirstInfantry()
{
for (int i = 0; i < Infantry.Length; i++)
{
if (Infantry[i] != null)
return Infantry[i];
}
return null;
}
public bool HasInfantry() => GetFirstInfantry() != null;
public bool HasTechno()
{
return Structures.Count > 0 || Vehicles.Count > 0 || Aircraft.Count > 0 || Array.Exists(Infantry, inf => inf != null);
}
public bool HasTechnoThatPassesCheck(Predicate<TechnoBase> predicate)
{
return GetFirstTechnoThatPassesCheck(predicate) != null;
}
public TechnoBase GetFirstTechnoThatPassesCheck(Predicate<TechnoBase> predicate)
{
var structure = Structures.Find(predicate);
if (structure != null) return structure;
var unit = Vehicles.Find(predicate);
if (unit != null) return unit;
var aircraft = Aircraft.Find(predicate);
if (aircraft != null) return aircraft;
return Array.Find(Infantry, inf => inf != null && predicate(inf));
}
public TechnoBase GetTechno(Point2D? position = null)
{
if (Structures.Count > 0)
return Structures[0];
if (Vehicles.Count > 0)
return Vehicles[0];
if (Aircraft.Count > 0)
return Aircraft[0];
if (position != null)
{
var closestSubcell = GetSubCellClosestToPosition((Point2D)position, true);
if (closestSubcell == SubCell.None)
return null;
return GetInfantryFromSubCellSpot(closestSubcell);
}
else
{
return GetFirstInfantry();
}
}
public GameObject GetObject(Point2D? position = null)
{
GameObject obj = GetTechno(position);
if (obj != null)
return obj;
if (TerrainObject != null)
return TerrainObject;
return null;
}
/// <summary>
/// Determines whether a specific game object can be assigned to this tile.
/// </summary>
public bool CanAddObject(GameObject gameObject, bool blocksSelf, bool overlapObjects)
{
switch (gameObject.WhatAmI())
{
case RTTIType.Building:
{
bool multipleStructuresExist = Structures.Count > 1;
bool anotherExists = Structures.Count == 1 && !Structures.Contains((Structure)gameObject);
bool clone = Structures.Count == 1 && Structures.Contains((Structure)gameObject) && blocksSelf;
if ((multipleStructuresExist || anotherExists || clone) && !overlapObjects)
return false;
return true;
}
case RTTIType.Unit:
return Vehicles.Count == 0 || overlapObjects;
case RTTIType.Aircraft:
return Aircraft.Count == 0 || overlapObjects;
case RTTIType.Infantry:
return GetFreeSubCellSpot() != SubCell.None;
case RTTIType.Terrain:
return TerrainObject == null;
}
return false;
}
public bool ContainsObject(AbstractObject abstractObject)
{
switch (abstractObject.WhatAmI())
{
case RTTIType.Aircraft:
return Aircraft.Contains((Aircraft)abstractObject);
case RTTIType.Terrain:
return TerrainObject == abstractObject;
case RTTIType.Building:
return Structures.Contains((Structure)abstractObject);
case RTTIType.Unit:
return Vehicles.Contains((Unit)abstractObject);
case RTTIType.Infantry:
return Array.Exists(Infantry, inf => inf == abstractObject);
case RTTIType.Overlay:
return Overlay == abstractObject;
case RTTIType.Smudge:
return Smudge == abstractObject;
case RTTIType.Waypoint:
return Waypoints.Contains((Waypoint)abstractObject);
}
return false;
}
public bool IsClearGround()
{
return TileIndex == 0;
}
/// <summary>
/// Returns a value that tells whether the cell has a harvestable resource (tiberium or ore) on it.
/// </summary>
public bool HasTiberium()
{
return Overlay != null && Overlay.OverlayType != null && Overlay.OverlayType.Tiberium;
}
public void ChangeTileIndex(int newTileIndex, byte newSubTileIndex)
{
TileImage = null;
TileIndex = newTileIndex;
SubTileIndex = newSubTileIndex;
}
public Point2D CoordsToPoint() => new Point2D(X, Y);
public Point2D GetTileCenter() => new Point2D(Constants.CellSizeX / 2, Constants.CellSizeY / 2);
}
}