Example #1
0
 public override void MakePlanRequest(SenseEventType EventType)
 {
     if (EventType != SenseEventType.yWall)
     {
         newPlanRequest = true;
     }
 }
Example #2
0
 public ModuleState Start()
 {
     currentActionDirective.Action = AHEntities.Action.LEAVE;
     currentEvent = SenseEventType.NoEvent;
     currentActionDirective.TimeStamp = DateTime.Now;
     Scheduler = new Thread(new ThreadStart(PlanningScheduler.PlanningScheduler));
     Scheduler.Start();
     if (internalState == ModuleState.Inactive)
     {
         internalState = ModuleState.Active;
         PlanningScheduler.Activate();
     }
     return(internalState);
 }
Example #3
0
        public override AHEntities.ActionDirective SelectAction(SenseEventType planReason)
        {
            Dictionary <string, double> physicalState = WM.GetPhysicalState();

            double[] crossParamsGoal, crossParamsAttack, crossParamsDefense;
            bool     isThreat     = false;
            double   time2goal    = 0;
            double   time2attack  = 0;
            double   time2defense = 0;

            AHEntities.ActionDirective action = new ActionDirective();
            action.TimeStamp = DateTime.Now;

            #region stuck events handling
            if (planReason == SenseEventType.StuckPlayer)
            {
                action.Action   = AHEntities.Action.PREPARE;
                action.Duration = TimeSpan.FromSeconds(1);
                return(action);
            }
            if (planReason == SenseEventType.StuckAgent)
            {
                action.Action   = AHEntities.Action.STUCK_ATTACK;
                action.Duration = TimeSpan.FromSeconds(0.3);
                return(action);
            }

            #endregion stuck events handling

            #region Crossing calculations
            Point puckP = new Point(physicalState["PuckX"], physicalState["PuckY"]);
            Point puckV = new Point(physicalState["PuckVx"], physicalState["PuckVy"]);
            try
            {
                // (y, T)
                crossParamsGoal = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, 32,
                                                                           AHEntities.EstimateLineCrossing.goalLine, (int)global["Tablewidth"], (int)global["Tableheight"]);

                crossParamsDefense = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, 32,
                                                                              AHEntities.EstimateLineCrossing.defenseAttackLine, (int)global["Tablewidth"], (int)global["Tableheight"]);

                crossParamsAttack = AHEntities.EstimateLineCrossing.Estimate(puckP, puckV, 32,
                                                                             AHEntities.EstimateLineCrossing.attackLine, (int)global["Tablewidth"], (int)global["Tableheight"]);
            }
            catch (Exception)
            {
                action.Action   = AHEntities.Action.LEAVE;
                action.Duration = TimeSpan.FromSeconds(0);
                return(action);
            }
            #endregion Crossing calculations

            // not directed to the goal
            if (crossParamsGoal == null)
            {
                action.Action   = AHEntities.Action.PREPARE;
                action.Duration = TimeSpan.FromSeconds(1);
                return(action);
            }

            // is directed to the goal
            if ((crossParamsGoal[0] < 220) && (crossParamsGoal[0] > -220))
            {
                isThreat = true;
            }

            // time to reach the goal and attack lines
            time2goal    = crossParamsGoal[1];
            time2defense = crossParamsDefense[1];
            time2attack  = crossParamsAttack[1];

            #region emergency Block/Leave
            // puck is too fast, immidiate BLOCK or LEAVE
            if (time2goal < 0.2)
            {
                if (isThreat)
                {
                    action.Action   = AHEntities.Action.BLOCK;
                    action.Duration = TimeSpan.FromSeconds(time2goal);
                    return(action);
                }
                else
                {
                    action.Action   = AHEntities.Action.LEAVE;
                    action.Duration = TimeSpan.FromSeconds(0);
                    return(action);
                }
            }
            #endregion emergency Block/Leave

            if (time2attack < 0.5)      // defense attack (no time to calcualte elaborated attack
            {
                action.Action   = AHEntities.Action.DEFENSE_ATTACK;
                action.Duration = TimeSpan.FromSeconds(time2defense);
                return(action);
            }
            else // Directed attack
            {
                int    height = (int)global["Tableheight"] / 2;
                double rim    = height * 0.95;
                action.Duration = TimeSpan.FromSeconds(time2attack);

                // puck is crossing on the rim of the table
                if (Math.Abs(crossParamsAttack[0]) > rim)
                {
                    action.Action   = AHEntities.Action.DEFENSE_ATTACK;
                    action.Duration = TimeSpan.FromSeconds(time2defense);
                    return(action);
                }
                else // crossing on the inside of the table
                {
                    Random random = new Random();
                    AHEntities.Action[] attacks;
                    double outerTable = height * 0.65;
                    if (Math.Abs(crossParamsAttack[0]) > outerTable)    // outer part
                    {
                        if (crossParamsAttack[2] > height - outerTable) // Agent's left part
                        {
                            attacks = new AHEntities.Action[] { AHEntities.Action.ATTACK_RIGHT,
                                                                AHEntities.Action.ATTACK_MIDDLE };
                        }
                        else // Agent's right part
                        {
                            attacks = new AHEntities.Action[] { AHEntities.Action.ATTACK_LEFT,
                                                                AHEntities.Action.ATTACK_MIDDLE };
                        }
                    }
                    else // inner part
                    {
                        attacks = new AHEntities.Action[] { AHEntities.Action.ATTACK_LEFT,
                                                            AHEntities.Action.ATTACK_MIDDLE,
                                                            AHEntities.Action.ATTACK_RIGHT };
                    }
                    action.Action = attacks[random.Next(attacks.Length)];
                    return(action);
                }
            }
        }
 public abstract void MakePlanRequest(SenseEventType EventType);
Example #5
0
        public void StartNewPlan(object sender, EventArgs e)
        {
            ActionDirective             tempA;
            SenseEventArgs              senseE    = (SenseEventArgs)e;
            Dictionary <string, double> gameState = WM.GetPhysicalState();
            Point puckV = new Point(gameState["PuckVx"], gameState["PuckVy"]);

            if ((senseE.EventType == SenseEventType.yWall) && (puckV.X > 0))
            {
                // no replan (prepare action)
                currentEvent = senseE.EventType;
                mLogger.AddLogMessage("Tactics: No New Action Required: " + currentActionDirective.Action.ToString());
            }
            else
            {
                //new plan
                if ((currentEvent == senseE.EventType) &&
                    ((currentEvent == SenseEventType.StuckPlayer) || (currentEvent == SenseEventType.StuckAgent)))
                {
                    // no replan
                    mLogger.AddLogMessage("Tactics: No New Action Required: " + currentActionDirective.Action.ToString());
                }
                else if ((senseE.EventType == SenseEventType.yWall) && (puckV.X < 0))
                {
                    // need to replan current action
                    currentEvent = senseE.EventType;
                    mLogger.AddLogMessage("Tactics: Refining Old Action: " + currentActionDirective.Action.ToString());
                    PlanningScheduler.MakePlanRequest(senseE.EventType);
                }
                else
                {
                    currentEvent = senseE.EventType;
                    tempA        = actionSelectionStrategy.SelectAction(senseE.EventType);
                    //if (tempA.Action != currentActionDirective.Action)
                    //{
                    lock (currentActionDirective.ActionDirectiveLock)
                    {
                        currentActionDirective.Action    = tempA.Action;
                        currentActionDirective.TimeStamp = senseE.TimeStamp;
                        currentActionDirective.Duration  = tempA.Duration - (DateTime.Now - senseE.TimeStamp);
                    }
                    // log action - new action
                    mLogger.AddLogMessage("Tactics: New Action Selected: " + currentActionDirective.Action.ToString());
                    PlanningScheduler.MakePlanRequest(senseE.EventType);

                    /*}
                     * else
                     * {
                     *  mLogger.AddLogMessage("Tactics: Old Action Selected: " + currentActionDirective.Action.ToString());
                     *  PlanningScheduler.MakePlanRequest(senseE.EventType);
                     * }*/
                }
            }


            /*
             * // new plan required
             * if ((senseE.EventType == SenseEventType.AgentCollision) || (senseE.EventType == SenseEventType.OpponentCollision) ||
             *  (senseE.EventType == SenseEventType.xWall) || (senseE.EventType == SenseEventType.yWall) && (puckV.X < 0))
             * {
             *  tempA = actionSelectionStrategy.SelectAction(senseE.EventType);
             *  if (tempA.Action != currentActionDirective.Action)
             *  {
             *      lock (currentActionDirective.ActionDirectiveLock)
             *      {
             *          currentActionDirective.Action = tempA.Action;
             *          currentActionDirective.TimeStamp = senseE.TimeStamp;
             *          currentActionDirective.Duration = tempA.Duration - (DateTime.Now - senseE.TimeStamp);
             *      }
             *      // log action - new action
             *      mLogger.AddLogMessage("Tactics: New Action Selected: " + currentActionDirective.Action.ToString());
             *      PlanningScheduler.MakePlanRequest(senseE.EventType);
             *  }
             *  else
             *  {
             *      mLogger.AddLogMessage("Tactics: Old Action Selected: " + currentActionDirective.Action.ToString());
             *      PlanningScheduler.MakePlanRequest(senseE.EventType);
             *  }
             * }
             * else
             * {
             *  // log action
             *  mLogger.AddLogMessage("Tactics: Refinement Of Active Action: " + currentActionDirective.Action.ToString());
             *  PlanningScheduler.MakePlanRequest(senseE.EventType);
             * }*/



            /*
             * if (senseE.PlanType == SensePlanArg.Plan)
             * {
             *  tempA = actionSelectionStrategy.SelectAction(senseE.EventType);
             *  // construct the action directive object
             *  if (tempA.Action != currentActionDirective.Action)
             *  {
             *      lock (currentActionDirective.ActionDirectiveLock)
             *      {
             *          currentActionDirective.Action = tempA.Action;
             *          currentActionDirective.TimeStamp = senseE.TimeStamp;
             *          currentActionDirective.Duration = tempA.Duration - (DateTime.Now - senseE.TimeStamp);
             *      }
             *      // log action - new action
             *      mLogger.AddLogMessage("Tactics: New Action Selected: " + currentActionDirective.Action.ToString());
             *  }
             *  else
             *  {
             *      if ((currentActionDirective.Action != AHEntities.Action.PREPARE))
             *      {
             *          // log action - refinement of current action.
             *          mLogger.AddLogMessage("Tactics: New (reselection) Action Selected: " + currentActionDirective.Action.ToString());
             *      }
             *  }
             *  PlanningScheduler.MakePlanRequest(senseE.EventType);
             * }
             * else
             * {
             *  // current directive is still valid, need to schedule replanning
             *  // log action
             *  mLogger.AddLogMessage("Tactics: Refinement Of Active Action: " + currentActionDirective.Action.ToString());
             *  PlanningScheduler.MakePlanRequest(senseE.EventType);
             * }*/
        }
Example #6
0
 public override void MakePlanRequest(SenseEventType EventType)
 {
     newPlanRequest = true;
 }
Example #7
0
 public abstract AHEntities.ActionDirective SelectAction(SenseEventType planReason);
Example #8
0
 public SenseEventArgs(SensePlanArg plan, SenseEventType type)
 {
     PlanType  = plan;
     EventType = type;
     timeStamp = DateTime.Now;
 }
Example #9
0
        public override SenseEventType Estimate(double agentX, double agentY, double agentVx, double agentVy,
                                                double puckX, double puckY, double puckVx, double puckVy, double PuckR,
                                                double oppX, double oppY, double oppVx, double oppVy, WorldModel worldModel)
        {
            Dictionary <string, double> oldState = worldModel.GetPhysicalState();

            SenseEventType event2return = SenseEventType.NoEvent;

            Point puckPold = new Point(oldState["PuckX"], oldState["PuckY"]);
            Point puckPnew = new Point(puckX, puckY);
            Point puckVold = new Point(oldState["PuckVx"], oldState["PuckVy"]);
            Point puckVnew = new Point(puckVx, puckVy);

            Point oppPold   = new Point(oldState["OpponentX"], oldState["OpponentY"]);
            Point oppPnew   = new Point(oppX, oppY);
            Point agentPold = new Point(oldState["AgentX"], oldState["AgentY"]);
            Point agentPnew = new Point(agentX, agentY);

            worldModel.UpdatePhysicalState(agentX, agentY, agentVx, agentVy, puckX, puckY, puckVx, puckVy, PuckR,
                                           oppX, oppY, oppVx, oppVy);

            if (VelocityChanged(puckVold, puckVnew))
            {
                if ((puckPold.Dist(agentPold) < 80 + eps) || (puckPnew.Dist(agentPnew) < 80 + eps))
                {
                    event2return = SenseEventType.AgentCollision;       // agent gave a hit
                }
                else if ((puckPold.Dist(oppPold) < 80 + eps) || (puckPnew.Dist(oppPnew) < 80 + eps))
                {
                    event2return = SenseEventType.OpponentCollision;      // player gave a hit
                }
                else if (Math.Sign(puckVold.Y) == -Math.Sign(puckVnew.Y)) // Vy_old == -Vy_new
                {
                    if (IsZero(puckVnew.X))                               // puck is stuck on a line (bouncing from side to side)
                    {
                        if (puckPnew.X < -eps)
                        {
                            event2return = SenseEventType.StuckAgent;   // stuck on the agent's side
                        }
                        else
                        {
                            event2return = SenseEventType.StuckPlayer;  // stuck on the player's side
                        }
                    }
                    else
                    {
                        event2return = SenseEventType.yWall;            // hit one of the side walls
                    }
                }
                else if ((IsZero(puckVnew.X)) && (IsZero(puckVnew.Y)))  // stuck (standing still) somewhere
                {
                    if (puckPnew.X < -eps)
                    {
                        event2return = SenseEventType.StuckAgent;       // standing still on the agent's side
                    }
                    else
                    {
                        event2return = SenseEventType.StuckPlayer;      // stading still on the player's side
                    }
                }
                else if (Math.Sign(puckVold.X) != Math.Sign(puckVnew.X))
                {
                    event2return = SenseEventType.xWall;                // hit one of the back/front walls
                }
                else
                {
                    event2return = SenseEventType.Disturbance;
                }
            }
            else
            {
                event2return = SenseEventType.NoEvent;
            }

            TrajectoryQueue puckTrajectory = EstimateLineCrossing.EstimatePuckTrajectory(puckPnew, puckVnew, (double)global["PuckRadius"],
                                                                                         (int)global["Tablewidth"], (int)global["Tableheight"], (double)global["TimeStep"], (double)global["TimeScale"]);

            worldModel.UpdatePuckTrajectory(puckTrajectory);

            return(event2return);
        }