Allow resource placement outside the map cordon.

This commit is contained in:
Paul Chote
2015-06-07 22:59:40 +01:00
parent 55cea73cf4
commit 6e09c62fdd
6 changed files with 52 additions and 29 deletions

View File

@@ -65,10 +65,11 @@ namespace OpenRA.Mods.Common.Widgets
var underCursor = editorLayer.PreviewsAt(worldRenderer.Viewport.ViewToWorldPx(mi.Location)) var underCursor = editorLayer.PreviewsAt(worldRenderer.Viewport.ViewToWorldPx(mi.Location))
.FirstOrDefault(); .FirstOrDefault();
var mapResources = world.Map.MapResources.Value;
ResourceType type; ResourceType type;
if (underCursor != null) if (underCursor != null)
editorWidget.SetTooltip(underCursor.Tooltip); editorWidget.SetTooltip(underCursor.Tooltip);
else if (world.Map.Contains(cell) && resources.TryGetValue(world.Map.MapResources.Value[cell].Type, out type)) else if (mapResources.Contains(cell) && resources.TryGetValue(mapResources[cell].Type, out type))
editorWidget.SetTooltip(type.Info.Name); editorWidget.SetTooltip(type.Info.Name);
else else
editorWidget.SetTooltip(null); editorWidget.SetTooltip(null);
@@ -84,8 +85,8 @@ namespace OpenRA.Mods.Common.Widgets
if (underCursor != null) if (underCursor != null)
editorLayer.Remove(underCursor); editorLayer.Remove(underCursor);
if (world.Map.MapResources.Value[cell].Type != 0) if (mapResources.Contains(cell) && mapResources[cell].Type != 0)
world.Map.MapResources.Value[cell] = new ResourceTile(); mapResources[cell] = new ResourceTile();
} }
else if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down) else if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down)
{ {

View File

@@ -82,7 +82,8 @@ namespace OpenRA.Mods.Common.Widgets
public bool AllowResourceAt(CPos cell) public bool AllowResourceAt(CPos cell)
{ {
if (!world.Map.Contains(cell)) var mapResources = world.Map.MapResources.Value;
if (!mapResources.Contains(cell))
return false; return false;
var tile = world.Map.MapTiles.Value[cell]; var tile = world.Map.MapTiles.Value[cell];
@@ -92,7 +93,7 @@ namespace OpenRA.Mods.Common.Widgets
var terrainType = world.TileSet.TerrainInfo[tileInfo.TerrainType]; var terrainType = world.TileSet.TerrainInfo[tileInfo.TerrainType];
if (world.Map.MapResources.Value[cell].Type == ResourceType.ResourceType) if (mapResources[cell].Type == ResourceType.ResourceType)
return false; return false;
if (!ResourceType.AllowedTerrainTypes.Contains(terrainType.Type)) if (!ResourceType.AllowedTerrainTypes.Contains(terrainType.Type))

View File

@@ -86,11 +86,7 @@ namespace OpenRA.Mods.Common.Traits
// so we must also touch all the neighbouring tiles // so we must also touch all the neighbouring tiles
Dirty.Add(cell); Dirty.Add(cell);
foreach (var d in CVec.Directions) foreach (var d in CVec.Directions)
{ Dirty.Add(cell + d);
var c = cell + d;
if (Map.Contains(c))
Dirty.Add(c);
}
} }
protected virtual string ChooseRandomVariant(ResourceType t) protected virtual string ChooseRandomVariant(ResourceType t)
@@ -103,10 +99,16 @@ namespace OpenRA.Mods.Common.Traits
// Set density based on the number of neighboring resources // Set density based on the number of neighboring resources
var adjacent = 0; var adjacent = 0;
var type = Tiles[c].Type; var type = Tiles[c].Type;
var resources = Map.MapResources.Value;
for (var u = -1; u < 2; u++) for (var u = -1; u < 2; u++)
{
for (var v = -1; v < 2; v++) for (var v = -1; v < 2; v++)
if (Map.MapResources.Value[c + new CVec(u, v)].Type == type.Info.ResourceType) {
var cell = c + new CVec(u, v);
if (resources.Contains(cell) && resources[cell].Type == type.Info.ResourceType)
adjacent++; adjacent++;
}
}
return Math.Max(int2.Lerp(0, type.Info.MaxDensity, adjacent, 9), 1); return Math.Max(int2.Lerp(0, type.Info.MaxDensity, adjacent, 9), 1);
} }
@@ -139,7 +141,8 @@ namespace OpenRA.Mods.Common.Traits
return; return;
foreach (var c in Dirty) foreach (var c in Dirty)
Tiles[c] = UpdateDirtyTile(c); if (Tiles.Contains(c))
Tiles[c] = UpdateDirtyTile(c);
Dirty.Clear(); Dirty.Clear();

View File

@@ -50,9 +50,14 @@ namespace OpenRA.Mods.Common.Traits
{ {
var sum = 0; var sum = 0;
for (var u = -1; u < 2; u++) for (var u = -1; u < 2; u++)
{
for (var v = -1; v < 2; v++) for (var v = -1; v < 2; v++)
if (content[cell + new CVec(u, v)].Type == t) {
var c = cell + new CVec(u, v);
if (content.Contains(c) && content[c].Type == t)
++sum; ++sum;
}
}
return sum; return sum;
} }

View File

@@ -65,31 +65,36 @@ namespace OpenRA.Mods.D2k.Traits
return D2kResourceLayer.Variants.Keys.Random(Game.CosmeticRandom); return D2kResourceLayer.Variants.Keys.Random(Game.CosmeticRandom);
} }
bool CellContains(CPos c, ResourceType t)
{
return Tiles.Contains(c) && Tiles[c].Type == t;
}
ClearSides FindClearSides(ResourceType t, CPos p) ClearSides FindClearSides(ResourceType t, CPos p)
{ {
var ret = ClearSides.None; var ret = ClearSides.None;
if (Tiles[p + new CVec(0, -1)].Type != t) if (!CellContains(p + new CVec(0, -1), t))
ret |= ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight; ret |= ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight;
if (Tiles[p + new CVec(-1, 0)].Type != t) if (!CellContains(p + new CVec(-1, 0), t))
ret |= ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft; ret |= ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft;
if (Tiles[p + new CVec(1, 0)].Type != t) if (!CellContains(p + new CVec(1, 0), t))
ret |= ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight; ret |= ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight;
if (Tiles[p + new CVec(0, 1)].Type != t) if (!CellContains(p + new CVec(0, 1), t))
ret |= ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight; ret |= ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight;
if (Tiles[p + new CVec(-1, -1)].Type != t) if (!CellContains(p + new CVec(-1, -1), t))
ret |= ClearSides.TopLeft; ret |= ClearSides.TopLeft;
if (Tiles[p + new CVec(1, -1)].Type != t) if (!CellContains(p + new CVec(1, -1), t))
ret |= ClearSides.TopRight; ret |= ClearSides.TopRight;
if (Tiles[p + new CVec(-1, 1)].Type != t) if (!CellContains(p + new CVec(-1, 1), t))
ret |= ClearSides.BottomLeft; ret |= ClearSides.BottomLeft;
if (Tiles[p + new CVec(1, 1)].Type != t) if (!CellContains(p + new CVec(1, 1), t))
ret |= ClearSides.BottomRight; ret |= ClearSides.BottomRight;
return ret; return ret;

View File

@@ -96,31 +96,36 @@ namespace OpenRA.Mods.D2k.Traits
{ ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 }, { ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 },
}; };
bool CellContains(CPos c, ResourceType t)
{
return render.Contains(c) && render[c].Type == t;
}
ClearSides FindClearSides(ResourceType t, CPos p) ClearSides FindClearSides(ResourceType t, CPos p)
{ {
var ret = ClearSides.None; var ret = ClearSides.None;
if (render[p + new CVec(0, -1)].Type != t) if (!CellContains(p + new CVec(0, -1), t))
ret |= ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight; ret |= ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight;
if (render[p + new CVec(-1, 0)].Type != t) if (!CellContains(p + new CVec(-1, 0), t))
ret |= ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft; ret |= ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft;
if (render[p + new CVec(1, 0)].Type != t) if (!CellContains(p + new CVec(1, 0), t))
ret |= ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight; ret |= ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight;
if (render[p + new CVec(0, 1)].Type != t) if (!CellContains(p + new CVec(0, 1), t))
ret |= ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight; ret |= ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight;
if (render[p + new CVec(-1, -1)].Type != t) if (!CellContains(p + new CVec(-1, -1), t))
ret |= ClearSides.TopLeft; ret |= ClearSides.TopLeft;
if (render[p + new CVec(1, -1)].Type != t) if (!CellContains(p + new CVec(1, -1), t))
ret |= ClearSides.TopRight; ret |= ClearSides.TopRight;
if (render[p + new CVec(-1, 1)].Type != t) if (!CellContains(p + new CVec(-1, 1), t))
ret |= ClearSides.BottomLeft; ret |= ClearSides.BottomLeft;
if (render[p + new CVec(1, 1)].Type != t) if (!CellContains(p + new CVec(1, 1), t))
ret |= ClearSides.BottomRight; ret |= ClearSides.BottomRight;
return ret; return ret;
@@ -128,6 +133,9 @@ namespace OpenRA.Mods.D2k.Traits
void UpdateRenderedTileInner(CPos p) void UpdateRenderedTileInner(CPos p)
{ {
if (!render.Contains(p))
return;
var t = render[p]; var t = render[p];
if (t.Density > 0) if (t.Density > 0)
{ {