コード例 #1
0
    /// <summary>
    /// This method will be called automaticaly each time when AntAIAgent decide to update the plan.
    /// You should attach this script to the same object where is AntAIAgent.
    /// </summary>
    public void CollectConditions(AntAIAgent aAgent, AntAICondition aWorldState)
    {
        // 1. Classic setup of the World Conditions for planner.
        // -------------------------------------------------------
        // aWorldState.BeginUpdate(aAgent.planner);
        // {
        //  aWorldState.Set("Is Cargo Delivered", false);
        //  aWorldState.Set("See Cargo", IsSeeCargo());
        //  aWorldState.Set("Has Cargo", _control.HasCargo);
        //  aWorldState.Set("See Base", IsSeeBase());
        //  aWorldState.Set("Near Base", IsNearBase());
        // }
        // aWorldState.EndUpdate();

        // 2. Optimized setup of the World Conditions for the planner.
        // -----------------------------------------------------------
        aWorldState.BeginUpdate(aAgent.planner);
        {
            aWorldState.Set(DeliveryBot.IsCargoDelivered, false);
            aWorldState.Set(DeliveryBot.SeeCargo, IsSeeCargo());
            aWorldState.Set(DeliveryBot.HasCargo, _control.HasCargo);
            aWorldState.Set(DeliveryBot.SeeBase, IsSeeBase());
            aWorldState.Set(DeliveryBot.NearBase, IsNearBase());
        }
        aWorldState.EndUpdate();

        // HINT: When you have finished the AI Scenario, just export all conditions
        // as enum and use it to set conditions from the code.
    }
コード例 #2
0
        public AntAICondition post;         // Последующие условия.

        public AntAIAction(string aName, int aCost = 1)
        {
            cost  = aCost;
            name  = aName;
            state = aName;
            pre   = new AntAICondition();
            post  = new AntAICondition();
        }
コード例 #3
0
 public void Act(AntAICondition aPost)
 {
     for (int i = 0; i < AntAIPlanner.MAX_ATOMS; i++)
     {
         mask[i] = mask[i] || aPost.mask[i];
         if (aPost.mask[i])
         {
             values[i] = aPost.values[i];
         }
     }
 }
コード例 #4
0
        public AntAICondition Clone()
        {
            AntAICondition clone = new AntAICondition();

            for (int i = 0; i < AntAIPlanner.MAX_ATOMS; i++)
            {
                clone.values[i] = values[i];
                clone.mask[i]   = mask[i];
            }
            return(clone);
        }
コード例 #5
0
 public bool Match(AntAICondition aOther)
 {
     for (int i = 0; i < AntAIPlanner.MAX_ATOMS; i++)
     {
         if ((mask[i] && aOther.mask[i]) && (values[i] != aOther.values[i]))
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #6
0
 public bool Equals(AntAICondition aCondition)
 {
     for (int i = 0; i < AntAIPlanner.MAX_ATOMS; i++)
     {
         if (values[i] != aCondition.values[i])
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #7
0
        public int Heuristic(AntAICondition aOther)
        {
            int dist = 0;

            for (int i = 0; i < AntAIPlanner.MAX_ATOMS; i++)
            {
                if (aOther.mask[i] && values[i] != aOther.values[i])
                {
                    dist++;
                }
            }
            return(dist);
        }
コード例 #8
0
        private void Start()
        {
            // 1. Create the AI planner.
            // -------------------------
            var planner = new AntAIPlanner();

            // Load scenario for planner.
            A.Assert(scenario == null, "Scenario not selected!");
            planner.LoadScenario(scenario);

            // 2. Create world state.
            // ----------------------
            var worldState = new AntAICondition();

            worldState.BeginUpdate(planner);
            worldState.Set("Is Cargo Delivered", false);
            worldState.Set("See Cargo", false);
            worldState.Set("Has Cargo", false);
            worldState.Set("See Base", false);
            worldState.Set("Near Base", false);
            worldState.EndUpdate();

            // 3. Build plan.
            // --------------
            var plan = new AntAIPlan();

            planner.MakePlan(ref plan, worldState, planner.GetDefaultGoal());

            // Output plan.
            Debug.Log("<b>Plan:</b>");
            for (int i = 0; i < plan.Count; i++)
            {
                Debug.Log((i + 1) + ". " + plan[i]);
            }

            // 4. Change world state and rebuild plan.
            // ---------------------------------------
            worldState.BeginUpdate(planner);
            worldState.Set("Has Cargo", true);
            worldState.EndUpdate();

            planner.MakePlan(ref plan, worldState, planner.FindGoal("Delivery"));

            // Output plan.
            Debug.Log("<b>Plan:</b>");
            for (int i = 0; i < plan.Count; i++)
            {
                Debug.Log((i + 1) + ". " + plan[i]);
            }
        }
コード例 #9
0
        public void GetConditions(AntAIAgent aAgent, AntAICondition aWorldState)
        {
            // Получаем список всех объектов которые необходимо отслеживать.
            if (_visualNodes == null)
            {
                _visualNodes = AntEngine.Current.GetNodes <VisualNode>();
            }

            // 1. Обновляем информацию о себе.
            // -------------------------------
            aWorldState.Set(aAgent.planner, "ArmedWithGun", control.Tower.HasGun);
            aWorldState.Set(aAgent.planner, "ArmedWithBomb", control.Tower.HasBomb);
            aWorldState.Set(aAgent.planner, "HasAmmo", control.Tower.HasAmmo);
            aWorldState.Set(aAgent.planner, "Injured", (health.HP != health.maxHP));
            aWorldState.Set(aAgent.planner, "EnemyAlive", true);             // Наш враг всегда жив, потому что респавнится.
            aWorldState.Set(aAgent.planner, "Alive", (health.HP > 0.0f));
            aWorldState.Set(aAgent.planner, "HasObstacle", sensor.HasObstacle);

            if (sensor.HasObstacle)
            {
                sensor.HasObstacle = false;
            }

            // 2. Обрабатываем зрение.
            // -----------------------
            // Сбрасываем состояние всех возможных значений зрения.
            aWorldState.Set(aAgent.planner, "NearEnemy", false);
            aWorldState.Set(aAgent.planner, "EnemyVisible", false);
            aWorldState.Set(aAgent.planner, "GunInlineOfSight", false);
            aWorldState.Set(aAgent.planner, "AmmoInlineOfSight", false);
            aWorldState.Set(aAgent.planner, "BombInlineOfSight", false);
            aWorldState.Set(aAgent.planner, "HealInlineOfSight", false);

            VisualNode visual;

            // Перебираем все объекты которые можно просматривать.
            for (int i = 0, n = _visualNodes.Count; i < n; i++)
            {
                visual = _visualNodes[i];
                // Если объект из другой группы.
                if (vision.group != visual.Visual.group)
                {
                    // Если объект попадает в область зрения.
                    if (vision.IsSee(visual.entity.Position))
                    {
                        blackboard[visual.Visual.conditionName].AsBool = true;
                        blackboard[string.Concat(visual.Visual.conditionName, "_Pos")].AsVector2 = visual.entity.Position;

                        // Отмечаем что видим.
                        aWorldState.Set(aAgent.planner, visual.Visual.conditionName, true);

                        if (vision.enemyGroup == visual.Visual.group &&
                            AntMath.Distance(control.Position, visual.entity.Position) < 1.5f)
                        {
                            aWorldState.Set(aAgent.planner, "NearEnemy", true);
                        }
                    }
                }
            }

            // 3. Обрабатываем наведение пушки на врага.
            // -----------------------------------------
            aWorldState.Set(aAgent.planner, "EnemyLinedUp", false);
            if (aWorldState.Has(aAgent.planner, "EnemyVisible") && control.Tower.HasGun)
            {
                if (blackboard["EnemyVisible"].AsBool)
                {
                    float angle = AntMath.AngleDeg((Vector2)control.Position, blackboard["EnemyVisible_Pos"].AsVector2);
                    if (AntMath.Equal(AntMath.Angle(control.Tower.Angle), AntMath.Angle(angle), 10.0f))
                    {
                        aWorldState.Set(aAgent.planner, "EnemyLinedUp", true);
                    }
                }
            }
        }
コード例 #10
0
        public void CollectConditions(AntAIAgent aAgent, AntAICondition aWorldState)
        {
            // 1. Remove all stored data from previous ticks.
            // ----------------------------------------------
            ClearSensors();

            // 2. Get fresh Vision and Touch data.
            // -----------------------------
            UpdateVision();
            UpdateTouch();

            // 3. Update World State.
            // ----------------------
            aWorldState.Set(TankScenario.Alive, true);
            aWorldState.Set(TankScenario.Injured, (_control.Health < _control.maxHealth));
            aWorldState.Set(TankScenario.EnemyAlive, true);             // All time is true ;)

            aWorldState.Set(TankScenario.HasGun, _control.Tower.IsHasGun);
            aWorldState.Set(TankScenario.HasAmmo, _control.Tower.IsHasAmmo);
            aWorldState.Set(TankScenario.HasBomb, _control.Tower.IsHasBomb);

            // Reset items visibility.
            aWorldState.Set(TankScenario.SeeGun, false);
            aWorldState.Set(TankScenario.SeeAmmo, false);
            aWorldState.Set(TankScenario.SeeBomb, false);
            aWorldState.Set(TankScenario.SeeRepair, false);

            // Update vision for items.
            for (int i = 0; i < SEE_ITEMS_COUNT; i++)
            {
                if (_seeItems[i] != null)
                {
                    // Adds founded spawner and item to the our memory.
                    // This data maybe will be required by bot in the near future.
                    _blackboard.TrackSpawner(_seeItems[i].Position, _seeItems[i].kind);
                    _blackboard.TrackItem(_seeItems[i], _seeItems[i].kind);

                    // Update the world state.
                    switch (_seeItems[i].kind)
                    {
                    case ItemKind.Gun:
                        aWorldState.Set(TankScenario.SeeGun, true);
                        break;

                    case ItemKind.Ammo:
                        aWorldState.Set(TankScenario.SeeAmmo, true);
                        break;

                    case ItemKind.Bomb:
                        aWorldState.Set(TankScenario.SeeBomb, true);
                        break;

                    case ItemKind.Repair:
                        aWorldState.Set(TankScenario.SeeRepair, true);
                        break;
                    }
                }
            }

            // Update vision for enemies.
            aWorldState.Set(TankScenario.SeeEnemy, false);

            for (int i = 0; i < TANKS_COUNT; i++)
            {
                if (_seeTanks[i] != null)
                {
                    _blackboard.TrackTank(_seeTanks[i]);
                    aWorldState.Set(TankScenario.SeeEnemy, true);
                }
            }

            // Check the enemy on line of the fire.
            aWorldState.Set(TankScenario.OnLineofFire, false);

            if (_blackboard.HasEnemy)
            {
                float angle = AntMath.AngleDeg(_control.Position, _blackboard.EnemyTank.Position);
                if (AntMath.Equal(AntMath.Angle(angle), AntMath.Angle(_control.Tower.Angle), 10.0f))
                {
                    aWorldState.Set(TankScenario.OnLineofFire, true);
                }
            }

            // Check the touching of enemy tanks.
            aWorldState.Set(TankScenario.NearEnemy, false);

            for (int i = 0; i < TANKS_COUNT; i++)
            {
                if (_touchTanks[i] != null)
                {
                    aWorldState.Set(TankScenario.NearEnemy, true);
                    break;
                }
            }
        }