Skip to content

Commit ba94da2

Browse files
committed
Add warnings on map load for objects being outside of the map, fix edge case where building was processed when even if its origin cell was outside of the map
1 parent da332b3 commit ba94da2

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/TSMapEditor/Config/Translations/en/Translation_en.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ MapLoader.ReadBuildings.UpgradeInvalid=Building {0} has an upgrade {1}, but@{2}d
7777
MapLoader.ReadBuildings.UpgradeCountMismatch=Building {0} at {1} has more upgrades ({2}) than specified by its Upgrades= value ({3}) in Rules. Skipping adding one or more of the building's upgrades.
7878
MapLoader.CheckFoundationCell.TileOutOfBounds=Building {0} has been placed outside of the map at {1}. Skipping adding it to map.
7979
MapLoader.ReadAircraft.AircraftTypeNotFound=Unable to find aircraft type {0} - skipping adding it to map.
80+
MapLoader.ReadAircraft.AircraftOutsideOfMap=Warning: The map has an aircraft "{0}" ({1}) placed outside of the valid map area at {2}, {3}.
8081
MapLoader.ReadUnits.UnitTypeNotFound=Unable to find unit type {0} - skipping adding it to map.
82+
MapLoader.ReadUnits.UnitOutsideOfMap=Warning: The map has a unit "{0}" ({1}) placed outside of the valid map area at {2}, {3}.
8183
MapLoader.ReadInfantry.InfantryTypeNotFound=Unable to find infantry type {0} - skipping adding it to map.
84+
MapLoader.ReadInfantry.InfantryOutsideOfMap=Warning: The map has an infantry "{0}" ({1}) placed outside of the valid map area at {2}, {3}.
8285
MapLoader.ReadSmudges.InvalidSmudgeSyntax=Invalid syntax in smudge defined in map: {0}
8386
MapLoader.ReadSmudges.InvalidSmudgeSyntaxAtPosition=Invalid syntax in smudge at {0},{1}: {2}
8487
MapLoader.ReadSmudges.SmudgeTypeNotFound=Cell at {0},{1} contains a smudge '{2}' that does not exist in Rules.ini. Ignoring it.

src/TSMapEditor/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace TSMapEditor
44
{
55
public static class Constants
66
{
7-
public const string ReleaseVersion = "1.7.7";
7+
public const string ReleaseVersion = "1.7.8";
88

99
public static int CellSizeX = 48;
1010
public static int CellSizeY = 24;

src/TSMapEditor/Initialization/MapLoader.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,20 +398,25 @@ public static void ReadBuildings(IMap map, IniFile mapIni)
398398

399399
bool isClear = false;
400400

401-
void CheckFoundationCell(Point2D cellCoords)
401+
void ApplyFoundationCell(Point2D cellCoords)
402402
{
403403
var tile = map.GetTile(cellCoords);
404404
if (tile != null)
405405
{
406-
isClear = true;
407406
tile.Structures.Add(building);
408407
}
409408
}
410409

411-
// Go through foundation cells and register the building to all tiles that are valid on its foundation.
412-
// If the building was added to no cells (all cells on its foundation were nonexistent),
413-
// then the building is outside of the map and we should not consider it as belonging to the map.
414-
buildingType.ArtConfig.DoForFoundationCoordsOrOrigin(offset => CheckFoundationCell(building.Position + offset));
410+
bool IsFoundationCellOnMap(Point2D cellCoords) => map.GetTile(cellCoords) != null;
411+
412+
// Check that the building's origin cell is within the map. If it is not, we should not add the building to the map at all.
413+
if (IsFoundationCellOnMap(building.Position))
414+
{
415+
isClear = true;
416+
417+
// Go through foundation cells and register the building to all tiles that are valid on its foundation.
418+
buildingType.ArtConfig.DoForFoundationCoordsOrOrigin(offset => ApplyFoundationCell(building.Position + offset));
419+
}
415420

416421
if (!isClear)
417422
{
@@ -487,7 +492,15 @@ public static void ReadAircraft(IMap map, IniFile mapIni)
487492
map.Aircraft.Add(aircraft);
488493
var tile = map.GetTile(x, y);
489494
if (tile != null)
495+
{
490496
tile.Aircraft.Add(aircraft);
497+
}
498+
else
499+
{
500+
AddMapLoadError(string.Format(Translate("MapLoader.ReadAircraft.AircraftOutsideOfMap",
501+
"Warning: The map has an aircraft \"{0}\" ({1}) placed outside of the valid map area at {2}, {3}."),
502+
aircraftType.GetEditorDisplayName(), aircraftTypeId, x, y));
503+
}
491504
}
492505

493506
Logger.Log("Aircraft read successfully.");
@@ -553,7 +566,15 @@ public static void ReadUnits(IMap map, IniFile mapIni)
553566
map.Units.Add(unit);
554567
var tile = map.GetTile(x, y);
555568
if (tile != null)
569+
{
556570
tile.Vehicles.Add(unit);
571+
}
572+
else
573+
{
574+
AddMapLoadError(string.Format(Translate("MapLoader.ReadUnits.UnitOutsideOfMap",
575+
"Warning: The map has a unit \"{0}\" ({1}) placed outside of the valid map area at {2}, {3}."),
576+
unitType.GetEditorDisplayName(), unitTypeId, x, y));
577+
}
557578
}
558579

559580
// Process follow IDs
@@ -628,7 +649,15 @@ public static void ReadInfantry(IMap map, IniFile mapIni)
628649
map.Infantry.Add(infantry);
629650
var tile = map.GetTile(x, y);
630651
if (tile != null)
652+
{
631653
tile.Infantry[(int)subCell] = infantry;
654+
}
655+
else
656+
{
657+
AddMapLoadError(string.Format(Translate("MapLoader.ReadInfantry.InfantryOutsideOfMap",
658+
"Warning: The map has an infantry \"{0}\" ({1}) placed outside of the valid map area at {2}, {3}."),
659+
infantryType.GetEditorDisplayName(), infantryTypeId, x, y));
660+
}
632661
}
633662

634663
Logger.Log("Infantry read successfully.");

0 commit comments

Comments
 (0)