Example #1
0
        public void CalculatePath(LiMap.Cell firstCellWithTransition, bool hasUnknown)
        {
            Logger.instance.Assert(null != pathLastCell, "Don't set last cell. Please call SetupEnvironment");

            this.hasUnknown = hasUnknown;
            if (null != transition && !transition.Cell.Pos.Equals(firstCellWithTransition.Pos))
            {
                pathLastCell = transition.Cell;
                transition   = transition.Next;
            }

            if (car.Durability < 1.0e-9 || car.Speed() < 5)
            {
                pathLastCell = currentDirLastCell();
                transition   = null;
            }

            beginDir = new TilePos(car.X, car.Y) - pathLastCell.Pos;

            HashSet <LiMap.Cell> visited = new HashSet <LiMap.Cell>();
            int depth = Math.Min(3, (int)(car.Speed() / 10));

            transition = mergePath(firstCellWithTransition, pathLastCell, transition, depth);

            Logger.instance.Assert(null != transition, "Can't find path.");

            path = createPathFromTransition(transition).ToArray();
            Logger.instance.Assert(3 <= path.Length, "Can't find full path.");
        }
Example #2
0
        private CellTransition calculatePath(LiMap.Cell cell, Cell lastCell, HashSet <LiMap.Cell> visited, int depth)
        {
            if (visited.Contains(cell) || depth > Constant.PathMaxDepth)
            {
                return(null);
            }
            visited.Add(cell);

            Cell resultCell = new Cell();

            resultCell.Pos   = cell.Pos;
            resultCell.DirIn = cell.Pos - lastCell.Pos;

            CellTransition max = null;

            foreach (LiMap.Transition transition in cell.Transitions)
            {
                TileDir dir = transition.ToCell.Pos - cell.Pos;
                resultCell.DirOut = dir;

                CellTransition newTransition = calculatePath(transition.ToCell, resultCell, visited, depth + 1);
                if (null != newTransition)
                {
                    newTransition.TransitionPriority = cellTransitionPriority(lastCell, resultCell, newTransition.Cell, transition.Weight);

                    int checkDepth = transition.isCheckpoint ? 0 : 3;
                    if (null == max || newTransition.Priority(checkDepth) > max.Priority(checkDepth))
                    {
                        max = newTransition;
                    }
                }
            }

            List <TileDir> dirOuts = new List <TileDir>();

            foreach (TileDir dir in cell.Dirs)
            {
                if (dir != resultCell.DirIn.Negative())
                {
                    dirOuts.Add(dir);
                }
            }
            resultCell.DirOuts = dirOuts.ToArray();

            resultCell.DirOut = null;
            if (0 != dirOuts.Count)
            {
                if (null != max)
                {
                    resultCell.DirOut = max.Cell.Pos - cell.Pos;
                }
            }

            CellTransition result = new CellTransition(resultCell, max, cellPriority(resultCell));

            visited.Remove(cell);

            return(result);
        }
Example #3
0
    // Start is called before the first frame update
    void Start()
    {
        var prefab        = GameObjectConversionUtility.ConvertGameObjectHierarchy(cellPrefab, World.Active);
        var entityManager = World.Active.EntityManager;

        for (int x = 0; x < gridWidth; x++)
        {
            for (int y = 0; y < gridHeight; y++)
            {
                for (int z = 0; z < gridDepth; z++)
                {
                    // Instantiate our cell prefab as a entity
                    var cellInstance = entityManager.Instantiate(prefab);

                    var position = transform.TransformPoint(x * 1.2f - 4.5f, y * 1.2f, z * 1.2f);

                    CellTransition cTran = new CellTransition {
                        value = 0.0f
                    };

                    CellIndex cIndex;

                    if (x == 0 || x == gridWidth - 1 || y == 0 || y == gridHeight - 1 || z == 0 || z == gridDepth - 1)
                    {
                        cIndex = new CellIndex {
                            deadCell = true, index = cellCounter
                        };
                    }
                    else
                    {
                        cIndex = new CellIndex {
                            deadCell = false, index = cellCounter
                        };
                    }

                    cellCounter++;

                    entityManager.AddComponent <CellIndex>(cellInstance);
                    entityManager.AddComponent <CellTransition>(cellInstance);
                    entityManager.AddComponent <CellStatus>(cellInstance);
                    entityManager.AddComponent <Scale>(cellInstance);
                    //entityManager.AddComponent<CellNeighbors>(cellInstance);

                    entityManager.SetComponentData(cellInstance, cTran);
                    entityManager.SetComponentData(cellInstance, cIndex);
                    entityManager.SetComponentData(cellInstance, new Translation {
                        Value = position
                    });
                    entityManager.SetComponentData(cellInstance, new Scale {
                        Value = cIndex.deadCell ? 0.0f:1.0f
                    });
                }
            }
        }
    }
Example #4
0
        private List <Cell> createPathFromTransition(CellTransition transition)
        {
            List <Cell> result = new List <Cell>();

            result.Add(transition.Cell);
            if (null != transition.Next)
            {
                result.AddRange(createPathFromTransition(transition.Next));
            }
            return(result);
        }
Example #5
0
        private CellTransition mergePath(LiMap.Cell mapCell, Cell lastCell, CellTransition iter, int depth)
        {
            if (null != iter && depth > 0)
            {
                if (iter.Cell.Pos == mapCell.Pos && null != iter.Next)
                {
                    foreach (LiMap.Transition transition in mapCell.Transitions)
                    {
                        if (iter.Next.Cell.Pos.Equals(transition.ToCell.Pos))
                        {
                            iter.Next = mergePath(transition.ToCell, iter.Cell, iter.Next, depth - 1);
                            return(iter);
                        }
                    }
                }
            }

            HashSet <LiMap.Cell> visited = new HashSet <LiMap.Cell>();

            return(calculatePath(mapCell, lastCell, visited, 0));
        }
Example #6
0
 public CellTransitionShould()
 {
     _cellTransition = new CellTransition();
 }
Example #7
0
 public CellTransition(Cell cell, CellTransition next, double cellPriority)
 {
     this.Cell         = cell;
     this.Next         = next;
     this.CellPriority = cellPriority;
 }