public void MoveToParent(LosCell _cell, LosCell _parent) { var val = Cells[_cell]; _parent.Add(_cell.Point, val, _cell); Cells.Remove(_cell); }
public void Add(Point _pnt, float _closedByParent, LosCell _cell) { float value; if (Cells.TryGetValue(_cell, out value)) { _closedByParent += value; } Cells[_cell] = _closedByParent; }
public LosManager(int _radius) { unchecked { m_root = new LosCell(Point.Zero, _radius); var alreadyDone = new Dictionary <Point, LosCell> { { Point.Zero, m_root } }; var dVectors = new List <Vector2>(); for (var di = -DIVIDER / 2 + 1; di < DIVIDER / 2; di++) { for (var dj = -DIVIDER / 2 + 1; dj < DIVIDER / 2; dj++) { var dv = new Vector2(di, dj) / DIVIDER; if (dv.Length() > Math.Sqrt(2)) { continue; } dVectors.Add(dv); } } var dividedPart = 1f / dVectors.Count; for (var i = _radius; i >= -_radius; --i) { for (var j = -_radius; j <= _radius; ++j) { var pnt = new Point(i, j); if (pnt.Equals(Point.Zero)) { continue; } LosCell cell; if (!alreadyDone.TryGetValue(pnt, out cell)) { cell = new LosCell(pnt, _radius); if (cell.Point.Lenght > _radius) { continue; } alreadyDone.Add(pnt, cell); } foreach (var dv in dVectors) { var v = new Vector2(pnt.X, pnt.Y) + dv; var parentPoint = Point.Zero; foreach (var lineV in v.GetLineToPoints(Vector2.Zero, 0.5f)) { var point = new Point((int)Math.Round(lineV.X), (int)Math.Round(lineV.Y)); if (point.Equals(pnt) || point.Lenght > _radius) { continue; } parentPoint = point; break; } LosCell parent; if (!alreadyDone.TryGetValue(parentPoint, out parent)) { parent = new LosCell(parentPoint, _radius); alreadyDone.Add(parentPoint, parent); } parent.Add(pnt, dividedPart * 1f, cell); } } } m_inOrder = alreadyDone.Values.OrderByDescending(_cell => _cell.DistanceCoefficient).ToArray(); foreach (var losCell in m_inOrder) { var dictionary = losCell.Cells.ToArray(); foreach (var pair in dictionary) { var cell = pair.Key; var delta = cell.Point.Lenght - losCell.Point.Lenght; if (delta >= 0 && delta < 0.1f) { var parent = m_inOrder.Where( _cell => _cell.Cells.Keys.Contains(cell) && _cell.DistanceCoefficient > losCell.DistanceCoefficient); losCell.MoveToParent(cell, parent.OrderByDescending(_cell => _cell.DistanceCoefficient).First()); } } } foreach (var losCell in m_inOrder) { losCell.BuildCellIndexes(m_inOrder); } } }
public void BuildCellIndexes(LosCell[] _inOrder) { CellIndexes = new Dictionary<int, float>(); foreach (var cell in Cells) { CellIndexes.Add(Array.IndexOf(_inOrder, cell.Key), cell.Value*cell.Key.DistanceCoefficient); } }
public LosManager(int _radius) { unchecked { m_root = new LosCell(Point.Zero, _radius); var alreadyDone = new Dictionary<Point, LosCell> {{Point.Zero, m_root}}; var dVectors = new List<Vector2>(); for (var di = -DIVIDER/2 + 1; di < DIVIDER/2; di++) { for (var dj = -DIVIDER/2 + 1; dj < DIVIDER/2; dj++) { var dv = new Vector2(di, dj)/DIVIDER; if (dv.Length() > Math.Sqrt(2)) continue; dVectors.Add(dv); } } var dividedPart = 1f/dVectors.Count; for (var i = _radius; i >= -_radius; --i) { for (var j = -_radius; j <= _radius; ++j) { var pnt = new Point(i, j); if (pnt.Equals(Point.Zero)) continue; LosCell cell; if (!alreadyDone.TryGetValue(pnt, out cell)) { cell = new LosCell(pnt, _radius); if (cell.Point.Lenght > _radius) { continue; } alreadyDone.Add(pnt, cell); } foreach (var dv in dVectors) { var v = new Vector2(pnt.X, pnt.Y) + dv; var parentPoint = Point.Zero; foreach (var lineV in v.GetLineToPoints(Vector2.Zero, 0.5f)) { var point = new Point((int) Math.Round(lineV.X), (int) Math.Round(lineV.Y)); if (point.Equals(pnt) || point.Lenght > _radius) continue; parentPoint = point; break; } LosCell parent; if (!alreadyDone.TryGetValue(parentPoint, out parent)) { parent = new LosCell(parentPoint, _radius); alreadyDone.Add(parentPoint, parent); } parent.Add(pnt, dividedPart*1f, cell); } } } m_inOrder = alreadyDone.Values.OrderByDescending(_cell => _cell.DistanceCoefficient).ToArray(); foreach (var losCell in m_inOrder) { var dictionary = losCell.Cells.ToArray(); foreach (var pair in dictionary) { var cell = pair.Key; var delta = cell.Point.Lenght - losCell.Point.Lenght; if (delta >= 0 && delta < 0.1f) { var parent = m_inOrder.Where( _cell => _cell.Cells.Keys.Contains(cell) && _cell.DistanceCoefficient > losCell.DistanceCoefficient); losCell.MoveToParent(cell, parent.OrderByDescending(_cell => _cell.DistanceCoefficient).First()); } } } foreach (var losCell in m_inOrder) { losCell.BuildCellIndexes(m_inOrder); } } }