Ejemplo n.º 1
0
        private void CheckState(IActionSelection actionSelection)
        {
            if (this.actualState.IsEqual("Defense"))
            {
                if (npc.TargetedEnemy == null)
                {
                    Logger.AddWarning("Villager- " + npc.GetCharacter().name + ": conflict, ale target je null");
                    return;
                }

                try
                {
                    Status npcStatus = npc.GetStatus();
                    //Status enemyStatus = npc.TargetedEnemy.GetStatus();
                    Logger.AddInfo(this.npc.character.name + ": " + npcStatus.ToString());
                    //ActionType type = ActionType.Defense; //(this.actualState.IsEqual("Confict") ? ActionType.Attack : ActionType.Defense);
                    Action selectedAction;

                    ConflictState state = actionSelection.GetNextAction(npc, npc.TargetedEnemy, npc.character.priors, out selectedAction);

                    this.npc.DoAction(selectedAction, state);
                }
                catch (Exception ex)
                {
                    Logger.AddWarning("Chyba pri vybirani akce: " + ex.ToString());
                }
            }
            else if (this.actualState.IsEqual("Stand"))
            {
                Point goal = this.npc.GetStatus().position;

                double random  = rand.NextDouble();
                double random2 = rand.NextDouble();
                int    offset1 = 0;
                int    offset2 = 0;

                if (random > 0.66)
                {
                    offset1 = 1;
                }
                else if (random > 0.33)
                {
                    offset1 = -1;
                }

                if (random2 > 0.66)
                {
                    offset2 = 1;
                }
                else if (random2 > 0.33)
                {
                    offset2 = -1;
                }

                //move randomly with NPC
                goal.Offset(offset1, offset2);

                goal = LocationCorrection(goal);
                if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                {
                    this.npc.DoFightMove(goal);
                }
            }
            else if (this.actualState.IsEqual("Talk"))
            {
                //TODO let's talk :)
            }
        }
Ejemplo n.º 2
0
        internal void CheckState(IActionSelection actionSelection)
        {
            if (this.actualState.IsEqual("Go"))
            {
                npc.Go();
            }
            else if (this.actualState.IsEqual("Conflict") || this.actualState.IsEqual("Defense"))
            {
                if (npc.TargetedEnemy == null)
                {
                    Logger.AddWarning(npc.GetCharacter().name + ": conflict, ale target je null");
                    return;
                }

                try
                {
                    Status npcStatus = npc.GetStatus();
                    //Status enemyStatus = npc.TargetedEnemy.GetStatus();
                    //Logger.AddInfo(this.npc.character.name + ": " + npcStatus.ToString());
                    //ActionType type = (this.actualState.IsEqual("Confict") ? ActionType.Attack : ActionType.Defense);
                    Action selectedAction;

                    ConflictState state = actionSelection.GetNextAction(npc, npc.TargetedEnemy, npc.character.priors, out selectedAction);

                    //do moving
                    #region moving
                    //if (rand.Next(100) < 80)
                    {
                        Point npcPos   = this.npc.GetStatus().position;
                        Point enemyPos = this.npc.GetTargetedEnemy().GetStatus().position;
                        int   dist     = this.npc.CalculateDistance(npcPos, enemyPos);

                        if (dist > 10 && this.npc.TargetedEnemy.GetCharacter().name == "Player")
                        {
                            this.npc.DoFightMove(enemyPos);
                        }
                        else if (dist > 4)
                        {
                            Point diff = new Point();
                            diff.X = enemyPos.X - npcPos.X;
                            diff.Y = enemyPos.Y - npcPos.Y;

                            Point goal = this.npc.GetStatus().position;

                            //move with npc, try to stay at the "ideal" distance
                            if (diff.X != 0 && diff.Y != 0)
                            {
                                goal.Offset(new Point(diff.X / Math.Abs(diff.X), diff.Y / Math.Abs(diff.Y)));
                            }
                            //don't want to divide by zero
                            else if (diff.X == 0)
                            {
                                //diff.X++;
                                goal.Offset(0, diff.Y / Math.Abs(diff.Y));
                            }
                            else if (diff.Y == 0)
                            {
                                diff.Y++;
                                goal.Offset(new Point(diff.X / Math.Abs(diff.X), 0));
                            }

                            goal = LocationCorrection(goal);
                            if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                            {
                                this.npc.DoFightMove(goal);
                            }
                        }
                        else
                        {
                            Point  goal    = this.npc.GetStatus().position;
                            double random  = this.rand.NextDouble();
                            double random2 = this.rand.NextDouble();
                            int    offset1 = 0;
                            int    offset2 = 0;

                            if (random > 0.66)
                            {
                                offset1 = 1;
                            }
                            else if (random > 0.33)
                            {
                                offset1 = -1;
                            }

                            if (random2 > 0.66)
                            {
                                offset2 = 1;
                            }
                            else if (random2 > 0.33)
                            {
                                offset2 = -1;
                            }

                            //move randomly with NPC
                            goal.Offset(offset1, offset2);

                            goal = LocationCorrection(goal);
                            if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                            {
                                this.npc.DoFightMove(goal);
                            }
                        }
                        #endregion
                    }

                    this.npc.DoAction(selectedAction, state, this.actualState.GetName());
                }
                catch (Exception ex)
                {
                    Logger.AddWarning("Chyba pri vybirani akce: " + ex.ToString());
                }
            }
            else if (this.actualState.IsEqual("RunAway"))
            {
                Point goal = new Point();
                //Point p = new Point();
                //p.X = this.npc.GetPosition2D().X - this.npc.GetTargetedEnemy().GetStatus().position.X;
                //p.Y = this.npc.GetPosition2D().Y - this.npc.GetTargetedEnemy().GetStatus().position.Y;
                //goal = this.npc.GetPosition2D();
                //goal.Offset(p);
                int          offset    = 25;
                Point        current   = this.npc.GetPosition2D();
                List <Point> locations = new List <Point>();

                Point up = current;
                up.Offset(0, offset);
                up = LocationCorrection(up);
                locations.Add(up);
                Point down = current;
                down.Offset(0, -offset);
                down = LocationCorrection(down);
                locations.Add(down);
                Point left = current;
                left.Offset(-offset, 0);
                left = LocationCorrection(left);
                locations.Add(left);
                Point right = current;
                right.Offset(offset, 0);
                right = LocationCorrection(right);
                locations.Add(right);

                Point rightUp = current;
                rightUp.Offset(offset, offset);
                rightUp = LocationCorrection(rightUp);
                locations.Add(rightUp);
                Point leftUp = current;
                leftUp.Offset(-offset, offset);
                leftUp = LocationCorrection(leftUp);
                locations.Add(leftUp);
                Point leftDown = current;
                leftDown.Offset(-offset, -offset);
                leftDown = LocationCorrection(leftDown);
                locations.Add(leftDown);
                Point rightDown = current;
                rightDown.Offset(offset, -offset);
                rightDown = LocationCorrection(rightDown);
                locations.Add(rightDown);

                //find safest new location to run to
                int eval   = 0;
                int bigest = 0;
                foreach (Point loc in locations)
                {
                    foreach (IActing enemy in this.npc.GetStatus().nearEnemies)
                    {
                        eval += this.npc.CalculateDistance(enemy.GetStatus().position, loc);
                    }
                    if (eval > bigest)
                    {
                        bigest = eval;
                        goal   = loc;
                    }
                    eval = 0;
                }

                goal = LocationCorrection(goal);

                npc.DoFightMove(goal);
            }
            else if (this.actualState.IsEqual("Stand"))
            {
                //TODO - what to do in stand state?
            }
            else if (this.actualState.IsEqual("Seek"))
            {
                Point goal = this.npc.GetStatus().position;
                goal = this.npc.TargetedEnemy.GetStatus().position;
                //goal = LocationCorrection(goal);

                //if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                {
                    this.npc.DoFightMove(goal);
                }
            }
            else if (this.actualState.IsEqual("Weakness"))
            {
                this.npc.Rest();
            }
            else
            {
                Logger.AddWarning("BasicFSM: neznamy stav automatu - " + this.actualState);
            }
        }
Ejemplo n.º 3
0
        private void CheckState(IActionSelection actionSelection)
        {
            if (this.actualState.IsEqual("Conflict") || this.actualState.IsEqual("Defense"))
            {
                if (npc.TargetedEnemy == null)
                {
                    Logger.AddWarning("Straz- " + npc.GetCharacter().name + ": conflict, ale target je null");
                    return;
                }

                try
                {
                    Status npcStatus   = npc.GetStatus();
                    Status enemyStatus = npc.TargetedEnemy.GetStatus();
                    //Logger.AddInfo(this.npc.character.name + ": " + npcStatus.ToString());
                    //ActionType type = (this.actualState.IsEqual("Confict") ? ActionType.Attack : ActionType.Defense);
                    Action selectedAction;

                    ConflictState state = actionSelection.GetNextAction(npc, npc.TargetedEnemy, npc.character.priors, out selectedAction);

                    //do moving
                    //if (rand.Next(100) < 95)
                    {
                        Point npcPos   = this.npc.GetStatus().position;
                        Point enemyPos = this.npc.GetTargetedEnemy().GetStatus().position;

                        if (this.npc.CalculateDistance(npcPos, enemyPos) > 4)
                        {
                            Point diff = new Point();
                            diff.X = enemyPos.X - npcPos.X;
                            diff.Y = enemyPos.Y - npcPos.Y;

                            Point goal = this.npc.GetStatus().position;

                            //move with npc, try to stay at the "ideal" distance
                            if (diff.X != 0 && diff.Y != 0)
                            {
                                goal.Offset(new Point(diff.X / Math.Abs(diff.X), diff.Y / Math.Abs(diff.Y)));
                            }
                            //don't want to divide by zero
                            else if (diff.X == 0)
                            {
                                goal.Offset(new Point(0, diff.Y / Math.Abs(diff.Y)));
                            }
                            else if (diff.Y == 0)
                            {
                                goal.Offset(new Point(diff.X / Math.Abs(diff.X), 0));
                            }

                            goal = LocationCorrection(goal);
                            if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                            {
                                this.npc.DoFightMove(goal);
                            }
                        }
                        else
                        {
                            Point goal = this.npc.GetStatus().position;
                            goal = RandomMove(goal);
                            if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                            {
                                this.npc.DoFightMove(goal);
                            }
                        }
                    }

                    this.npc.DoAction(selectedAction, state);
                }
                catch (Exception ex)
                {
                    Logger.AddWarning("Chyba pri vybirani akce: " + ex.ToString());
                }
            }
            else if (this.actualState.IsEqual("Seek"))
            {
                Point goal = this.npc.GetStatus().position;
                goal = this.npc.TargetedEnemy.GetStatus().position;
                //goal = LocationCorrection(goal);

                //if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                {
                    this.npc.DoFightMove(goal);
                }
            }
            else if (this.actualState.IsEqual("Stand"))
            {
                //Point goal = this.npc.GetStatus().position;
                //goal = RandomMove(goal);

                //if (!Map.GetInstance().CellMap[goal.X, goal.Y].Block)
                //{
                //    this.npc.DoFightMove(goal);
                //}
            }
            else if (this.actualState.IsEqual("Talk"))
            {
                //TODO let's talk :)
            }
            else if (this.actualState.IsEqual("Start"))
            {
                if (this.npc.CurrentTask.checkpoints != null)
                {
                    this.React(AiEvents.ready);
                }
            }
        }