Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple objects in cell #48

Merged
merged 15 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/TSMapEditor/Initialization/MapLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,9 @@ void CheckFoundationCell(Point2D cellCoords)
return;
}

if (tile.Structure != null)
if (tile.Structures.Count > 0)
{
isClear = false;
AddMapLoadError($"Building {buildingType.ININame} exists on a cell ({cellCoords}) that already has another building ({tile.Structure.ObjectType.ININame}). Skipping adding it to map.");
Logger.Log($"NOTE: Building {buildingType.ININame} exists in the cell {cellCoords} that already has other buildings.");
Rampastring marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -366,7 +365,7 @@ void CheckFoundationCell(Point2D cellCoords)
buildingType.ArtConfig.DoForFoundationCoordsOrOrigin(offset =>
{
var tile = map.GetTile(building.Position + offset);
tile.Structure = building;
tile.Structures.Add(building);
});
}
}
Expand Down Expand Up @@ -424,7 +423,7 @@ public static void ReadAircraft(IMap map, IniFile mapIni)
map.Aircraft.Add(aircraft);
var tile = map.GetTile(x, y);
if (tile != null)
tile.Aircraft = aircraft;
tile.Aircraft.Add(aircraft);
}
}

Expand Down Expand Up @@ -485,7 +484,7 @@ public static void ReadUnits(IMap map, IniFile mapIni)
map.Units.Add(unit);
var tile = map.GetTile(x, y);
if (tile != null)
tile.Vehicle = unit;
tile.Vehicles.Add(unit);
}

// Process follow IDs
Expand Down Expand Up @@ -673,10 +672,10 @@ public static void ReadWaypoints(IMap map, IniFile mapIni)
AddMapLoadError($"Waypoint {waypoint.Identifier} at {waypoint.Position} is not within the valid map area.");
continue;
}
if (mapCell.Waypoint != null)

if (mapCell.Waypoints.Count > 0)
{
AddMapLoadError($"Cell at {waypoint.Position} has multiple waypoints placed on it. Skipping adding waypoint #{waypoint.Identifier} there.");
Logger.Log($"NOTE: Waypoint {waypoint.Identifier} exists in the cell {waypoint.Position} that already has other waypoints.");
Rampastring marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand Down
103 changes: 51 additions & 52 deletions src/TSMapEditor/Models/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using System.IO;
using System.Linq;
using TSMapEditor.CCEngine;
using TSMapEditor.Extensions;
using TSMapEditor.GameMath;
using TSMapEditor.Initialization;
using TSMapEditor.Rendering;
using TSMapEditor.Extensions;

namespace TSMapEditor.Models
{
Expand Down Expand Up @@ -597,31 +597,34 @@ public void AddWaypoint(Waypoint waypoint)
{
Waypoints.Add(waypoint);
var cell = GetTile(waypoint.Position.X, waypoint.Position.Y);
if (cell.Waypoint != null)
if (cell.Waypoints.Count > 0)
{
throw new InvalidOperationException($"Cell at {cell.CoordsToPoint()} already has a waypoint, skipping adding waypoint {waypoint.Identifier}");
Logger.Log($"NOTE: Waypoint {waypoint.Identifier} exists in the cell {cell.CoordsToPoint()} that already has other waypoints.");
ZivDero marked this conversation as resolved.
Show resolved Hide resolved
}

cell.Waypoint = waypoint;
cell.Waypoints.Add(waypoint);
}

public void RemoveWaypoint(Waypoint waypoint)
{
var tile = GetTile(waypoint.Position);
if (tile.Waypoint == waypoint)
if (tile.Waypoints.Contains(waypoint))
{
Waypoints.Remove(waypoint);
tile.Waypoint = null;
tile.Waypoints.Remove(waypoint);
}
}

public void RemoveWaypointFrom(Point2D cellCoords)
public void RemoveWaypointsFrom(Point2D cellCoords)
{
var tile = GetTile(cellCoords);
if (tile.Waypoint != null)
if (tile.Waypoints.Count > 0)
{
Waypoints.Remove(tile.Waypoint);
tile.Waypoint = null;
foreach (var waypoint in tile.Waypoints)
{
Waypoints.Remove(waypoint);
}
tile.Waypoints.Clear();
}
}

Expand Down Expand Up @@ -747,26 +750,23 @@ public void PlaceBuilding(Structure structure)
if (cell == null)
return;

if (cell.Structure != null)
throw new InvalidOperationException("Cannot place a structure on a cell that already has a structure!");

cell.Structure = structure;
cell.Structures.Add(structure);
});

if (structure.ObjectType.ArtConfig.Foundation.Width == 0 && structure.ObjectType.ArtConfig.Foundation.Height == 0)
{
GetTile(structure.Position).Structure = structure;
GetTile(structure.Position).Structures.Add(structure);
}

Structures.Add(structure);
}

public void RemoveBuilding(Point2D cellCoords)
public void RemoveBuildingsFrom(Point2D cellCoords)
{
var cell = GetTile(cellCoords);

if (cell.Structure != null)
RemoveBuilding(cell.Structure);
if (cell.Structures.Count > 0)
cell.DoForAllBuildings(structure => RemoveBuilding(structure));
}

public void RemoveBuilding(Structure structure)
Expand All @@ -777,13 +777,13 @@ public void RemoveBuilding(Structure structure)
if (cell == null)
return;

if (cell.Structure == structure)
cell.Structure = null;
if (cell.Structures.Contains(structure))
cell.Structures.Remove(structure);
});

if (structure.ObjectType.ArtConfig.Foundation.Width == 0 && structure.ObjectType.ArtConfig.Foundation.Height == 0)
{
GetTile(structure.Position).Structure = null;
GetTile(structure.Position).Structures.Remove(structure);
}

Structures.Remove(structure);
Expand All @@ -799,23 +799,24 @@ public void MoveBuilding(Structure structure, Point2D newCoords)
public void PlaceUnit(Unit unit)
{
var cell = GetTile(unit.Position);
if (cell.Vehicle != null)
throw new InvalidOperationException("Cannot place a vehicle on a cell that already has a vehicle!");

cell.Vehicle = unit;
cell.Vehicles.Add(unit);
Units.Add(unit);
}

public void RemoveUnit(Unit unit)
{
RemoveUnit(unit.Position);
var cell = GetTile(unit.Position);
Units.Remove(unit);
cell.Vehicles.Remove(unit);
}

public void RemoveUnit(Point2D cellCoords)
public void RemoveUnitsFrom(Point2D cellCoords)
{
var cell = GetTile(cellCoords);
Units.Remove(cell.Vehicle);
cell.Vehicle = null;
cell.DoForAllVehicles(unit => Units.Remove(unit));

cell.Vehicles.Clear();
}

public void MoveUnit(Unit unit, Point2D newCoords)
Expand Down Expand Up @@ -863,23 +864,24 @@ public void MoveInfantry(Infantry infantry, Point2D newCoords)
public void PlaceAircraft(Aircraft aircraft)
{
var cell = GetTile(aircraft.Position);
if (cell.Aircraft != null)
throw new InvalidOperationException("Cannot place an aircraft on a cell that already has an aircraft!");

cell.Aircraft = aircraft;
cell.Aircraft.Add(aircraft);
Aircraft.Add(aircraft);
}

public void RemoveAircraft(Aircraft aircraft)
{
RemoveAircraft(aircraft.Position);
var cell = GetTile(aircraft.Position);
cell.Aircraft.Remove(aircraft);
Aircraft.Remove(aircraft);
}

public void RemoveAircraft(Point2D cellCoords)
public void RemoveAircraftFrom(Point2D cellCoords)
{
var cell = GetTile(cellCoords);
Aircraft.Remove(cell.Aircraft);
cell.Aircraft = null;
cell.DoForAllAircraft(aircraft => Aircraft.Remove(aircraft));

cell.Aircraft.Clear();
}

public void MoveAircraft(Aircraft aircraft, Point2D newCoords)
Expand Down Expand Up @@ -930,9 +932,10 @@ public void MoveWaypoint(Waypoint waypoint, Point2D newCoords)
/// </summary>
/// <param name="movable">The object to move.</param>
/// <param name="newCoords">The new coordinates of the object.</param>
/// <param name="considerSelf">Determines whether the object itself can be considered as blocking placement of the object.</param>
/// <param name="blocksSelf">Determines whether the object itself can be considered as blocking placement of the object.</param>
/// <param name="overlapObjects">Determines whether multiple objects of the same type should be allowed to exist in the same cell.</param>
/// <returns>True if the object can be moved, otherwise false.</returns>
public bool CanPlaceObjectAt(IMovable movable, Point2D newCoords, bool considerSelf)
public bool CanPlaceObjectAt(IMovable movable, Point2D newCoords, bool blocksSelf, bool overlapObjects)
{
if (movable.WhatAmI() == RTTIType.Building)
{
Expand All @@ -944,19 +947,15 @@ public bool CanPlaceObjectAt(IMovable movable, Point2D newCoords, bool considerS
if (foundationCell == null)
return;

if (foundationCell.Structure != null && (considerSelf || foundationCell.Structure != movable))
if (!foundationCell.CanAddObject((GameObject)movable, blocksSelf, overlapObjects))
canPlace = false;
});

if (!canPlace)
return false;
return canPlace;
}

MapTile cell = GetTile(newCoords);
if (movable.WhatAmI() == RTTIType.Waypoint)
return cell.Waypoint == null;

return cell.CanAddObject((GameObject)movable);
return cell.CanAddObject((GameObject)movable, blocksSelf, overlapObjects);
}

public void DeleteObjectFromCell(Point2D cellCoords)
Expand All @@ -974,21 +973,21 @@ public void DeleteObjectFromCell(Point2D cellCoords)
}
}

if (tile.Aircraft != null)
if (tile.Aircraft.Count > 0)
{
RemoveAircraft(tile.Aircraft);
RemoveAircraftFrom(tile.CoordsToPoint());
return;
}

if (tile.Vehicle != null)
if (tile.Vehicles.Count > 0)
{
RemoveUnit(tile.Vehicle);
RemoveUnitsFrom(tile.CoordsToPoint());
return;
}

if (tile.Structure != null)
if (tile.Structures.Count > 0)
{
RemoveBuilding(tile.Structure);
RemoveBuildingsFrom(tile.CoordsToPoint());
return;
}

Expand All @@ -1004,9 +1003,9 @@ public void DeleteObjectFromCell(Point2D cellCoords)
return;
}

if (tile.Waypoint != null)
if (tile.Waypoints.Count > 0)
{
RemoveWaypoint(tile.Waypoint);
RemoveWaypointsFrom(tile.CoordsToPoint());
return;
}
}
Expand Down
Loading
Loading