diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index f4921e1498..a962476d3e 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -138,7 +138,9 @@ namespace OpenRA { ++RenderFrame; viewport.DrawRegions(worldRenderer, new DefaultInputHandler(orderManager.world)); - Sound.SetListenerPosition(worldRenderer.Position(viewport.CenterLocation.ToInt2())); + + if (worldRenderer != null) + Sound.SetListenerPosition(worldRenderer.Position(viewport.CenterLocation.ToInt2())); } PerfHistory.items["render"].Tick(); diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index 26a845fb51..ea940a2876 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2013 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, @@ -26,15 +26,21 @@ namespace OpenRA.Traits public object Create(ActorInitializer init) { return new ActorMap(init.world, this); } } - public class ActorMap + public class ActorMap : ITick { class InfluenceNode { - public InfluenceNode next; - public SubCell subCell; - public Actor actor; + public InfluenceNode Next; + public SubCell SubCell; + public Actor Actor; } + static readonly SubCell[] SubCells = + { + SubCell.TopLeft, SubCell.TopRight, SubCell.Center, + SubCell.BottomLeft, SubCell.BottomRight + }; + readonly ActorMapInfo info; readonly Map map; InfluenceNode[,] influence; @@ -42,6 +48,11 @@ namespace OpenRA.Traits List[] actors; int rows, cols; + // Position updates are done in one pass + // to ensure consistency during a tick + List addActorPosition; + List removeActorPosition; + public ActorMap(World world, ActorMapInfo info) { this.info = info; @@ -54,6 +65,9 @@ namespace OpenRA.Traits for (var j = 0; j < rows; j++) for (var i = 0; i < cols; i++) actors[j * cols + i] = new List(); + + addActorPosition = new List(); + removeActorPosition = new List(); } public IEnumerable GetUnitsAt(CPos a) @@ -61,9 +75,9 @@ namespace OpenRA.Traits if (!map.IsInMap(a)) yield break; - for (var i = influence[a.X, a.Y]; i != null; i = i.next) - if (!i.actor.Destroyed) - yield return i.actor; + for (var i = influence[a.X, a.Y]; i != null; i = i.Next) + if (!i.Actor.Destroyed) + yield return i.Actor; } public IEnumerable GetUnitsAt(CPos a, SubCell sub) @@ -71,9 +85,9 @@ namespace OpenRA.Traits if (!map.IsInMap(a)) yield break; - for (var i = influence[a.X, a.Y]; i != null; i = i.next) - if (!i.actor.Destroyed && (i.subCell == sub || i.subCell == SubCell.FullCell)) - yield return i.actor; + for (var i = influence[a.X, a.Y]; i != null; i = i.Next) + if (!i.Actor.Destroyed && (i.SubCell == sub || i.SubCell == SubCell.FullCell)) + yield return i.Actor; } public bool HasFreeSubCell(CPos a) @@ -81,8 +95,7 @@ namespace OpenRA.Traits if (!AnyUnitsAt(a)) return true; - return new[] { SubCell.TopLeft, SubCell.TopRight, SubCell.Center, - SubCell.BottomLeft, SubCell.BottomRight }.Any(b => !AnyUnitsAt(a,b)); + return SubCells.Any(b => !AnyUnitsAt(a, b)); } public SubCell? FreeSubCell(CPos a) @@ -90,11 +103,9 @@ namespace OpenRA.Traits if (!HasFreeSubCell(a)) return null; - return new[] { SubCell.TopLeft, SubCell.TopRight, SubCell.Center, - SubCell.BottomLeft, SubCell.BottomRight }.First(b => !AnyUnitsAt(a,b)); + return SubCells.First(b => !AnyUnitsAt(a, b)); } - public bool AnyUnitsAt(CPos a) { return influence[a.X, a.Y] != null; @@ -102,8 +113,8 @@ namespace OpenRA.Traits public bool AnyUnitsAt(CPos a, SubCell sub) { - for (var i = influence[a.X, a.Y]; i != null; i = i.next) - if (i.subCell == sub || i.subCell == SubCell.FullCell) + for (var i = influence[a.X, a.Y]; i != null; i = i.Next) + if (i.SubCell == sub || i.SubCell == SubCell.FullCell) return true; return false; @@ -112,7 +123,7 @@ namespace OpenRA.Traits public void AddInfluence(Actor self, IOccupySpace ios) { foreach (var c in ios.OccupiedCells()) - influence[c.First.X, c.First.Y] = new InfluenceNode { next = influence[c.First.X, c.First.Y], subCell = c.Second, actor = self }; + influence[c.First.X, c.First.Y] = new InfluenceNode { Next = influence[c.First.X, c.First.Y], SubCell = c.Second, Actor = self }; } public void RemoveInfluence(Actor self, IOccupySpace ios) @@ -125,25 +136,41 @@ namespace OpenRA.Traits { if (influenceNode == null) return; - else if (influenceNode.actor == toRemove) - influenceNode = influenceNode.next; + else if (influenceNode.Actor == toRemove) + influenceNode = influenceNode.Next; if (influenceNode != null) - RemoveInfluenceInner(ref influenceNode.next, toRemove); + RemoveInfluenceInner(ref influenceNode.Next, toRemove); + } + + public void Tick(Actor self) + { + // Position updates are done in one pass + // to ensure consistency during a tick + foreach (var bin in actors) + bin.RemoveAll(removeActorPosition.Contains); + + removeActorPosition.Clear(); + + foreach (var a in addActorPosition) + { + var pos = a.OccupiesSpace.CenterPosition; + var i = (pos.X / info.BinSize).Clamp(0, cols - 1); + var j = (pos.Y / info.BinSize).Clamp(0, rows - 1); + actors[j * cols + i].Add(a); + } + + addActorPosition.Clear(); } public void AddPosition(Actor a, IOccupySpace ios) { - var pos = ios.CenterPosition; - var i = (pos.X / info.BinSize).Clamp(0, cols - 1); - var j = (pos.Y / info.BinSize).Clamp(0, rows - 1); - actors[j*cols + i].Add(a); + addActorPosition.Add(a); } public void RemovePosition(Actor a, IOccupySpace ios) { - foreach (var bin in actors) - bin.Remove(a); + removeActorPosition.Add(a); } public void UpdatePosition(Actor a, IOccupySpace ios) @@ -163,18 +190,21 @@ namespace OpenRA.Traits var j1 = (top / info.BinSize).Clamp(0, rows - 1); var j2 = (bottom / info.BinSize).Clamp(0, rows - 1); + var actorsInBox = new List(); for (var j = j1; j <= j2; j++) { for (var i = i1; i <= i2; i++) { - foreach (var actor in actors[j*cols + i]) + foreach (var actor in actors[j * cols + i]) { var c = actor.CenterPosition; - if (left <= c.X && c.X <= right && top <= c.Y && c.Y <= bottom) - yield return actor; + if (actor.IsInWorld && left <= c.X && c.X <= right && top <= c.Y && c.Y <= bottom) + actorsInBox.Add(actor); } } } + + return actorsInBox.Distinct(); } } }