Пример #1
0
        private Node OpenNode(Node[] nodes, int node_index, PanelModel start, PanelModel goal)
        {
            // 添字から座標に
            var center = _stage[node_index];

            int center_cost = nodes[node_index].cost;

            foreach (Direction direction in Enum.GetValues(typeof(Direction)))
            {
                var open_position = OpenPosition(center, direction);

                if (open_position == goal)
                {
                    nodes[_stageHash[open_position.GetEntity().X][open_position.GetEntity().Y]].parentIndex = node_index;
                    return(nodes[_stageHash[goal.GetEntity().X][goal.GetEntity().Y]]);
                }

                if (open_position == null || !open_position.Invasive())
                {
                    continue;
                }

                // コスト計算
                int cost = costs[_stageHash[open_position.GetEntity().X][open_position.GetEntity().Y]] + center_cost +
                           1;
                int heuristic = math.abs(goal.GetEntity().X - open_position.GetEntity().X) +
                                math.abs(goal.GetEntity().Y - open_position.GetEntity().Y);
                int score = cost + heuristic + 1;

                int next_index = _stageHash[open_position.GetEntity().X][open_position.GetEntity().Y];
                if (nodes[next_index].status == STATUS_FREE || nodes[next_index].score > score)
                {
                    nodes[next_index].status = STATUS_OPEN;
                    nodes[next_index].cost   = cost;
                    nodes[next_index].length = math.abs(start.GetEntity().X - open_position.GetEntity().X) +
                                               math.abs(start.GetEntity().Y - open_position.GetEntity().Y);
                    nodes[next_index].heuristic   = heuristic;
                    nodes[next_index].parentIndex = node_index;
                }
            }

            nodes[node_index].status = STATUS_CLOSE;

            return(nodes[node_index]);
        }
Пример #2
0
        private PanelModel OpenPosition(PanelModel center, Direction direction)
        {
            var x = direction == Direction.Right ? 1 : 0;

            x = direction == Direction.Left ? -1 : 0;
            var y = direction == Direction.Up ? 1 : 0;

            y = direction == Direction.Down ? -1 : 0;

            if (_stageHash.Count <= center.GetEntity().X + x || _stageHash[0].Count <= center.GetEntity().Y + y)
            {
                return(null);
            }

            var index = _stageHash[center.GetEntity().X + x][center.GetEntity().Y + y];

            return(_stage[index]);
        }