Ejemplo n.º 1
0
    public List <string> Aalgorithm(State state)
    {
        path.Clear();
        came_from.Clear();
        cost_so_far.Clear();
        steps_so_far.Clear();
        came_from_name.Clear();

        initial_state                 = new State(state.position);
        came_from[initial_state]      = null;
        came_from_name[initial_state] = "";
        cost_so_far[initial_state]    = 0.0f;
        steps_so_far[initial_state]   = 0;
        path.Add("");
        Info initial = new Info("", initial_state);

        frontier.Enqueue(initial, 0);
        count = 0;

        while (frontier.Count != 0)
        {
            count++;
            Info current_info = frontier.Dequeue();
            state_name    = current_info.action_name;
            current_state = current_info.info_state;
            current_pos   = current_state.position;

            GameObject[] bulletArray = GameObject.FindGameObjectsWithTag("EnemyBullet");
            //Debug.Log ("Prior: " + bulletArray.Length);
            List <GameObject> nearbyThreats = keepThreatsIntoList(current_pos, bulletArray);
            //Debug.Log (eneList.Count);
            //find nearest enemy within X location amongst bulletArray
            GameObject[] enemyArray = GameObject.FindGameObjectsWithTag("Enemy");
            if (enemyArray.Length == 0 && bulletArray.Length == 0)
            {
                path.Add("doNothing");
                return(path);
            }
            float nearestEnemyX = findNearestEnemyX(enemyArray, current_pos);
            //Debug.Log ("nearest: " + nearestEnemyX);
            // 2 exit conditions; one checking for an enemy, another for no enemy in which case, remain in place dodging
            //Vector2 ene_pos = Enemy.transform.position;
            Vector2 dodge_pos = transform.position;

            if (steps_so_far[current_state] == 3)
            {
                path = creatingPath(current_state, state_name);
                return(path);
            }

            //print(current_state)
            //Debug.Log("good_moves.Count: "+good_moves.Count);
            for (int i = 0; i < good_moves.Count; i++)
            {
                //NEXT = Name, State effected by action, and Time cost
                action_name = good_moves[i];
                //Debug.Log (action_name);
                State newState = new State(current_state.position);
                AIBehavior.apply_move(action_name, newState);
                // Simulate state here
                //Debug.Log (newState.position);
                new_cost = cost_so_far[current_state] + 0.1f;

                float testValue;
                if ((cost_so_far.TryGetValue(newState, out testValue) == false) || new_cost < cost_so_far [newState])
                {
                    cost_so_far[newState] = new_cost;
                    priority = new_cost + heuristic(current_state, newState, nearestEnemyX, nearbyThreats, steps_so_far[current_state], action_name);
                    Info newer_info = new Info(action_name, newState);
                    steps_so_far[newState]   = steps_so_far[current_state] + 1;
                    came_from[newState]      = current_state;
                    came_from_name[newState] = action_name;
                    frontier.Enqueue(newer_info, priority);
                }
            }
        }
        frontier.Clear();
        return(path);
    }