Esempio n. 1
0
        public bool AddCell(List <Vector3> nodes, Area area, Passability passability, int layer)
        {
            CellContentData d = new CellContentData(nodes[0], nodes[1]);

            if (GetEdge(d).CellsContains(nodes) == false)
            {
                List <GenerationEdgeInfo> edges = new List <GenerationEdgeInfo>();

                for (int i = 0; i < nodes.Count - 1; i++)
                {
                    edges.Add(GetEdge(new CellContentData(nodes[i], nodes[i + 1])));
                }

                edges.Add(GetEdge(new CellContentData(nodes[0], nodes[nodes.Count - 1])));

                GenerationCellInfo newCell = new GenerationCellInfo(area, passability, layer, graph, nodes, edges);
                cells.Add(newCell);

                foreach (var edge in edges)
                {
                    edge.SetCellToEdge(newCell);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
 public void TryAddData(CellContentData d)
 {
     if (!_contentDictionary.ContainsKey(d))
     {
         _contentDictionary.Add(d, null);
     }
 }
Esempio n. 3
0
        public bool GetClosestToHull(float x, float y, float z, out Cell cell, out Vector3 closestToOutlinePos)
        {
            cell = null;
            closestToOutlinePos = new Vector3();

            if (empty || _mapCell == null)
            {
                return(false);
            }

            float sqrDist = float.MaxValue;

            foreach (var pair in _contourLib)
            {
                CellContentData val        = pair.Key;
                Vector3         curNearest = pair.Key.NearestPoint(x, y, z);
                float           curSqrDist = SomeMath.SqrDistance(curNearest.x, curNearest.y, curNearest.z, x, y, z);

                if (curSqrDist < sqrDist)
                {
                    sqrDist             = curSqrDist;
                    cell                = pair.Value;
                    closestToOutlinePos = curNearest;
                }
            }
            return(true);
        }
Esempio n. 4
0
            public GenerationEdgeInfo(CellContentData data)
            {
                this.data = data;
                Vector3 m = data.centerV3;

                //Debuger_K.AddLabel(SomeMath.MidPoint(m, data.leftV3), "Left");
                //Debuger_K.AddLabel(SomeMath.MidPoint(m, data.rightV3), "Right");
            }
Esempio n. 5
0
        private void AddEdgeToMap(Cell origin, Cell connection, CellContentData data)
        {
            //Debuger_K.AddLine(origin.centerV3, SomeMath.MidPoint(data.leftV3, data.centerV3), Color.blue);
            //Debuger_K.AddLine(origin.centerV3, SomeMath.MidPoint(data.rightV3, data.centerV3), Color.red);

            tempCellDataMapValue = new CellDataMapValue(origin, connection, data);
            DDARasterization.DrawLine(data.a.x - chunk.realX, data.a.z - chunk.realZ, data.b.x - chunk.realX, data.b.z - chunk.realZ, PathFinder.CELL_GRID_SIZE / PathFinder.gridSize, AddEdgeToMapDelegate);
        }
Esempio n. 6
0
 public CellContent(CellContentData cellData, Cell from, Cell to, bool interconnection, float costFrom, float costTo)
 {
     _cellData        = cellData;
     _from            = from;
     _to              = to;
     _interconnection = interconnection;
     _costFrom        = costFrom;
     _costTo          = costTo;
 }
Esempio n. 7
0
        public void AddCover(NodeCoverTemp coverInfo)
        {
            Cover cover = new Cover(coverInfo.positionV3, coverInfo.connection.positionV3, coverInfo.connectionType, coverInfo.normal);

            Vector3 agentNormalPoint = coverInfo.normal * properties.radius;

            foreach (var coverPoint in coverInfo.points)
            {
                Vector3        pointPlusAgentOffset = coverPoint.positionV3 + agentNormalPoint;
                HashSet <Cell> nearbyCells          = new HashSet <Cell>();

                foreach (var edge in coverPoint.edges)
                {
                    CellContentData data = new CellContentData(edge);
                    foreach (var cell in _cells)
                    {
                        if (cell.Contains(data))
                        {
                            nearbyCells.Add(cell);
                        }
                    }
                }

                float   closestSqrDistance = float.MaxValue;
                Cell    closestCell        = null;
                Vector3 closestPoint       = Vector3.zero;

                foreach (var cell in nearbyCells)
                {
                    bool    isOutside;
                    Vector3 currentPoint;
                    cell.GetClosestPointToCell(pointPlusAgentOffset, out currentPoint, out isOutside);

                    float curSqrDistance = SomeMath.SqrDistance(pointPlusAgentOffset, currentPoint);
                    if (curSqrDistance < closestSqrDistance)
                    {
                        closestCell        = cell;
                        closestPoint       = currentPoint;
                        closestSqrDistance = curSqrDistance;
                    }
                }

                if (closestCell == null)
                {
                    //Debuger3.AddDot(coverPoint.positionV3, Color.red);
                    continue;
                }

                NodeCoverPoint coverNode = new NodeCoverPoint(coverPoint.positionV3, closestPoint, closestCell, cover);
                cover.AddCoverPoint(coverNode);
            }

            covers.Add(cover);
        }
Esempio n. 8
0
        GenerationEdgeInfo GetEdge(CellContentData d)
        {
            GenerationEdgeInfo result;

            if (edges.TryGetValue(d, out result) == false)
            {
                result = new GenerationEdgeInfo(d);
                edges.Add(d, result);
            }
            return(result);
        }
Esempio n. 9
0
        public void Remove(CellContentData d)
        {
            CellContent c = _contentDictionary[d];

            if (c != null)
            {
                _connections.Remove(c);
            }
            if (!_contentDictionary.Remove(d))
            {
                Debug.LogError("false");
            }
        }
Esempio n. 10
0
        private static void SetInterconnection(Graph graph1, Graph graph2, Cell cell1, Cell cell2, Vector3 node1, Vector3 node2)
        {
            Vector3 intersection;

            SomeMath.ClampedRayIntersectXZ(cell1.centerVector3, cell2.centerVector3 - cell1.centerVector3, node1, node2, out intersection);
            float cell1Cost = Vector3.Distance(cell1.centerVector3, intersection) * cell1.area.cost;
            float cell2Cost = Vector3.Distance(cell2.centerVector3, intersection) * cell2.area.cost;

            Vector3 leftPos, rightPos;

            if (SomeMath.LinePointSideMathf(new Vector2(node1.x, node1.z), new Vector2(node2.x, node2.z), cell1.centerVector2) > 0)
            {
                leftPos  = node2;
                rightPos = node1;
            }
            else
            {
                leftPos  = node1;
                rightPos = node2;
            }

            //Debuger_K.AddLabel(SomeMath.MidPoint(leftPos, cell1.centerV3), "L");
            //Debuger_K.AddLabel(SomeMath.MidPoint(rightPos, cell1.centerV3), "R");

            //Debuger_K.AddLabel(SomeMath.MidPoint(rightPos, cell2.centerV3), "L");
            //Debuger_K.AddLabel(SomeMath.MidPoint(leftPos, cell2.centerV3), "R");

            CellContentData C1C2data = new CellContentData(leftPos, rightPos);
            CellContentData C2C1data = new CellContentData(rightPos, leftPos);

            CellContentGenericConnection C1C2 = new CellContentGenericConnection(C1C2data, cell1, cell2, true, cell1Cost, cell2Cost, intersection);
            CellContentGenericConnection C2C1 = new CellContentGenericConnection(C2C1data, cell2, cell1, true, cell2Cost, cell1Cost, intersection);

            cell1.SetContent(C1C2);
            cell2.SetContent(C2C1);

            cell1.graph.AddEdgeToMap(cell1, cell2, C1C2data);
            cell2.graph.AddEdgeToMap(cell2, cell1, C2C1data);

#if UNITY_EDITOR
            if (Debuger_K.doDebug)
            {
                Debuger_K.AddEdgesInterconnected(graph1.x, graph1.z, graph1.properties, C1C2);
                Debuger_K.AddEdgesInterconnected(graph2.x, graph2.z, graph2.properties, C2C1);
            }
#endif
        }
Esempio n. 11
0
        //we cant do this outside Graph so we store this data
        public void SetEdgeSide(CellContentData edge, Directions direction)
        {
            Cell cell = null;

            foreach (var curCell in cells)
            {
                if (curCell.Contains(edge))
                {
                    cell = curCell;
                    break;
                }
            }

            if (cell == null)
            {
                Debug.LogError("cell == null");
            }

            borderData[(int)direction].Add(new KeyValuePair <CellContentData, Cell>(edge, cell));
        }
Esempio n. 12
0
        public void RemoveAllConnections(Cell target)
        {
            for (int i = _connections.Count - 1; i >= 0; i--)
            {
                if (_connections[i].connection == target)
                {
                    CellContentData data = _connections[i].cellData;
                    _connections.RemoveAt(i);

                    //if edge was presented before we add this connection then it will remain in dictionary
                    //else we remove it
                    if (_originalEdges.Contains(data))
                    {
                        _contentDictionary[data] = null;
                    }
                    else
                    {
                        _contentDictionary.Remove(data);
                    }
                }
            }
        }
Esempio n. 13
0
            public void CalculateAngles()
            {
                foreach (var node in nodes)
                {
                    CellContentData?data1 = null, data2 = null;
                    bool            data1Full = false;

                    foreach (var edge in edges)
                    {
                        CellContentData curData = edge.data;
                        if (curData.Contains(node))
                        {
                            if (!data1Full)
                            {
                                data1     = curData;
                                data1Full = true;
                            }
                            else
                            {
                                data2 = curData;
                                break;//bouth data full
                            }
                        }
                    }

                    Vector3 a = data1.Value.leftV3 == node ? data1.Value.rightV3 : data1.Value.leftV3;
                    Vector3 b = data2.Value.leftV3 == node ? data2.Value.rightV3 : data2.Value.leftV3;

                    angles[node] = Vector2.Angle(new Vector2(a.x - node.x, a.z - node.z), new Vector2(b.x - node.x, b.z - node.z));

                    //Vector3 u = new Vector3(0, 0.1f, 0);
                    //Debuger_K.AddDot(node, Color.red);
                    //Debuger_K.AddLine(node + u, a, Color.blue);
                    //Debuger_K.AddLine(node + u, b, Color.red);
                    //Debuger_K.AddLabel(node, angles[node]);
                }
            }
Esempio n. 14
0
        //takes edges and axis. check if edge exist, if exist add closest point to cell
        public void AddPortal(IEnumerable <EdgeAbstract> edges, Vector3 axis)
        {
            Vector2 axisV2 = new Vector2(axis.x, axis.z);
            Dictionary <Cell, Vector3> cellMountPoints = new Dictionary <Cell, Vector3>();

            foreach (var abstractEdge in edges)
            {
                CellContentData data = new CellContentData(abstractEdge);

                Vector3 intersection;
                SomeMath.ClosestToSegmentTopProjection(data.a, data.b, axisV2, true, out intersection);

                foreach (var cell in _cells)
                {
                    if (cell.Contains(data))
                    {
                        if (cellMountPoints.ContainsKey(cell))
                        {
                            if (SomeMath.SqrDistance(cellMountPoints[cell], axis) > SomeMath.SqrDistance(intersection, axis))
                            {
                                cellMountPoints[cell] = intersection;
                            }
                        }
                        else
                        {
                            cellMountPoints.Add(cell, intersection);
                        }
                    }
                }
            }

            Vector2 normalRaw;

            switch (cellMountPoints.Count)
            {
            case 0:
                return;

            case 1:
                normalRaw = ToV2((cellMountPoints.First().Value - axis)).normalized * -1;
                break;

            case 2:

                normalRaw = (
                    ToV2(cellMountPoints.First().Value - axis).normalized +
                    ToV2(cellMountPoints.Last().Value - axis).normalized).normalized * -1;
                break;

            default:
                normalRaw = Vector2.left;
                Dictionary <Cell, float> cellAngles = new Dictionary <Cell, float>();
                Cell first = cellMountPoints.First().Key;
                cellAngles.Add(first, 0f);

                Vector3 firstDirV3 = cellMountPoints.First().Value - axis;
                Vector2 firstDirV2 = ToV2(firstDirV3);

                foreach (var pair in cellMountPoints)
                {
                    if (pair.Key == first)
                    {
                        continue;
                    }

                    Vector2 curDir = new Vector2(pair.Value.x - axis.x, pair.Value.z - axis.z);
                    cellAngles.Add(pair.Key, Vector2.Angle(firstDirV2, curDir) * Mathf.Sign(SomeMath.V2Cross(firstDirV2, curDir)));
                }

                normalRaw = (
                    ToV2(cellMountPoints[cellAngles.Aggregate((l, r) => l.Value > r.Value ? l : r).Key] - axis).normalized +
                    ToV2(cellMountPoints[cellAngles.Aggregate((l, r) => l.Value < r.Value ? l : r).Key] - axis).normalized).normalized * -1;
                break;
            }

            portalBases.Add(new JumpPortalBase(cellMountPoints, axis, new Vector3(normalRaw.x, 0, normalRaw.y)));
        }
Esempio n. 15
0
 public CellContentGenericConnection(
     CellContentData cellData, Cell from, Cell to, bool interconnection, float costFrom, float costTo,
     Vector3 intersection) : base(cellData, from, to, interconnection, costFrom, costTo)
 {
     this.intersection = intersection;
 }
Esempio n. 16
0
 public CellContentDataShort(CellContentData ccd) : this(ccd.leftV3, ccd.rightV3)
 {
 }
Esempio n. 17
0
 public void SetConnection(CellContentData data, Cell connectTo, float costFrom, float costTo, Vector3 intersection)
 {
     connections.Add(new CellContentGenericConnection(data, cell, connectTo, false, costFrom, costTo, intersection));
 }
Esempio n. 18
0
 private void RemoveEdgeFromMap(Cell connection, CellContentData data)
 {
     tempCellConnection = connection;
     DDARasterization.DrawLine(data.a.x - chunk.realX, data.a.z - chunk.realZ, data.b.x - chunk.realX, data.b.z - chunk.realZ, PathFinder.CELL_GRID_SIZE / PathFinder.gridSize, RemoveEdgeFromMapDelegate);
 }
Esempio n. 19
0
 //version for deserialization cause we already know cell in that case
 public void SetEdgeSide(CellContentData edge, Directions direction, Cell cell)
 {
     borderData[(int)direction].Add(new KeyValuePair <CellContentData, Cell>(edge, cell));
 }
Esempio n. 20
0
 public bool Contains(CellContentData data)
 {
     return(_contentDictionary.ContainsKey(data));
 }
Esempio n. 21
0
        private void MakeBorders(float yError)
        {
            for (int i = 0; i < 4; i++)
            {
                Directions directionFrom = (Directions)i;
                Directions directionTo   = Enums.Opposite(directionFrom);

                Graph neighbourGraph;
                if (TryGetNeighbour(directionFrom, out neighbourGraph) == false)
                {
                    continue;
                }

                IEnumerable <KeyValuePair <CellContentData, Cell> > edgesFrom = GetBorderEdges(directionFrom);
                IEnumerable <KeyValuePair <CellContentData, Cell> > edgesTo   = neighbourGraph.GetBorderEdges(directionTo);

                List <TempEdge> tempEdges = new List <TempEdge>();

                Axis projectionAxis;
                if (directionFrom == Directions.xMinus | directionFrom == Directions.xPlus)
                {
                    projectionAxis = Axis.z;
                }
                else
                {
                    projectionAxis = Axis.x;
                }


                foreach (var edgeFrom in edgesFrom)
                {
                    Cell cellFrom = edgeFrom.Value;

                    foreach (var edgeTo in edgesTo)
                    {
                        Cell cellTo = edgeTo.Value;

                        CellContentData intersection;
                        if (CellContentData.Project(edgeFrom.Key, edgeTo.Key, yError, projectionAxis, out intersection) == false || intersection.pointed)
                        {
                            continue;
                        }

                        //Debuger_K.AddLine(cellFrom.centerV3, edgeFrom.edge.aPositionV3, Color.green);
                        //Debuger_K.AddLine(cellFrom.centerV3, edgeFrom.edge.bPositionV3, Color.green);

                        //Debuger_K.AddLine(intersection.aPositionV3, edgeFrom.edge.aPositionV3, Color.blue);
                        //Debuger_K.AddLine(intersection.bPositionV3, edgeFrom.edge.bPositionV3, Color.blue);

                        TempEdge curTempEdge = null;
                        for (int e = 0; e < tempEdges.Count; e++)
                        {
                            if (tempEdges[e].from == cellFrom && tempEdges[e].to == cellTo)
                            {
                                curTempEdge = tempEdges[e];
                            }
                        }

                        if (curTempEdge == null)
                        {
                            tempEdges.Add(new TempEdge(cellFrom, cellTo, intersection.a, intersection.b));
                        }
                        else
                        {
                            curTempEdge.AddNodePos(intersection.a);
                            curTempEdge.AddNodePos(intersection.b);
                        }
                    }
                }
                foreach (var item in tempEdges)
                {
                    SetInterconnection(this, neighbourGraph, item.from, item.to, item.minus, item.plus);
                }
            }
        }
Esempio n. 22
0
 public GenerationEdgeInfo(CellContentData data, int direction) : this(data) {
     this.direction = direction;
 }