Ejemplo n.º 1
0
        public void InitializePathfindingSearch(Vector3 startPosition, Vector3 goalPosition)
        {
            this.StartPosition = startPosition;
            this.GoalPosition  = goalPosition;
            this.StartNode     = this.Quantize(this.StartPosition);
            this.GoalNode      = this.Quantize(this.GoalPosition);

            //if it is not possible to quantize the positions and find the corresponding nodes, then we cannot proceed
            if (this.StartNode == null || this.GoalNode == null)
            {
                return;
            }

            //I need to do this because in Recast NavMesh graph, the edges of polygons are considered to be nodes and not the connections.
            //Theoretically the Quantize method should then return the appropriate edge, but instead it returns a polygon
            //Therefore, we need to create one explicit connection between the polygon and each edge of the corresponding polygon for the search algorithm to work
            ((NavMeshPoly)this.StartNode).AddConnectedPoly(this.StartPosition);
            ((NavMeshPoly)this.GoalNode).AddConnectedPoly(this.GoalPosition);

            this.InProgress          = true;
            this.TotalProcessedNodes = 0;
            this.TotalProcessingTime = 0.0f;
            this.MaxOpenNodes        = 0;

            var initialNode = new NodeRecord
            {
                gValue = 0,
                hValue = this.Heuristic.H(this.StartNode, this.GoalNode),
                node   = this.StartNode
            };

            initialNode.fValue = AStarPathfinding.F(initialNode);

            this.Open.Initialize();
            this.Open.AddToOpen(initialNode);
            this.Closed.Initialize();
        }
Ejemplo n.º 2
0
 public PathFindingDecomposer(SteeringPipeline pipeline, AStarPathfinding pathfindingAlgorithm)
     : base(pipeline)
 {
     this.PathfindingAlgorithm = pathfindingAlgorithm;
 }
Ejemplo n.º 3
0
        public void Start()
        {
            this.draw = true;
            this.influenceMapDebugMode = 0;

            this.navMesh = NavigationManager.Instance.NavMeshGraphs[0];
            this.Character = new DynamicCharacter(this.gameObject);

            //initialization of the Influence Maps
            this.RedInfluenceMap = new InfluenceMap(this.navMesh, new SimpleUnorderedList(), new ClosedLocationRecordDictionary(), new LinearInfluenceFunction(), 0.1f);
            this.GreenInfluenceMap = new InfluenceMap(this.navMesh, new SimpleUnorderedList(), new ClosedLocationRecordDictionary(), new LinearInfluenceFunction(), 0.1f);
            this.ResourceInfluenceMap = new InfluenceMap(this.navMesh, new SimpleUnorderedList(), new ClosedLocationRecordDictionary(), new LinearInfluenceFunction(), 0.1f);
            this.CombinedInfluence = new Dictionary<LocationRecord, float>();

            //initialization of the movement algorithms
            this.aStarPathFinding = new InfluenceMapAStarPathfinding(this.navMesh, new EuclideanDistanceHeuristic(), this.RedInfluenceMap, this.GreenInfluenceMap);
            this.aStarPathFinding.NodesPerSearch = 500;

            var steeringPipeline = new SteeringPipeline
            {
                MaxAcceleration = 40.0f,
                MaxConstraintSteps = 2,
                Character = this.Character.KinematicData,
            };

            this.decomposer = new PathFindingDecomposer(steeringPipeline, this.aStarPathFinding);
            this.Targeter = new FixedTargeter(steeringPipeline);
            steeringPipeline.Targeters.Add(this.Targeter);
            steeringPipeline.Decomposers.Add(this.decomposer);
            this.CharActuator = new ActuatorSwitch(steeringPipeline);
            var followPath = new FollowPathActuator(steeringPipeline);
            var standStill = new StandStillActuator(steeringPipeline);
            this.CharActuator.Actuators.Add(followPath.Name, followPath);
            this.CharActuator.Actuators.Add(standStill.Name, standStill);
            this.CharActuator.SwitchActuator(followPath.Name);
            steeringPipeline.Actuator = this.CharActuator;

            this.Character.Movement = steeringPipeline;

            //initialization of the GOB decision making
            //let's start by creating 5 main goals
            //the eat goal is the only goal that increases at a fixed rate per second, it increases at a rate of 0.1 per second
            this.SurviveGoal = new Goal(SURVIVE_GOAL, 1.5f);
            this.EatGoal = new Goal(EAT_GOAL, 2.5f)
            {
                ChangeRate = 0.1f
            };
            this.GetRichGoal = new Goal(GET_RICH_GOAL, 1.25f)
            {
                InsistenceValue = 5.0f,
                ChangeRate = 0.2f
            };
            this.RestGoal = new Goal(REST_GOAL, 0.25f);
            this.ConquerGoal = new Goal(CONQUER_GOAL, 1.5f)
            {
                InsistenceValue = 5.0f
            };

            this.Goals = new List<Goal>();
            this.Goals.Add(this.SurviveGoal);
            this.Goals.Add(this.EatGoal);
            this.Goals.Add(this.GetRichGoal);
            this.Goals.Add(this.RestGoal);
            this.Goals.Add(this.ConquerGoal);

            //initialize the available actions

            var restAction = new Rest(this);
            this.Actions = new List<Action>();
            this.Actions.Add(restAction);
            this.PlaceFlagAction = new PlaceFlag(this);
            this.Actions.Add(this.PlaceFlagAction);

            this.ActiveResources = new Dictionary<NavigationGraphNode, IInfluenceUnit>();

            foreach (var chest in GameObject.FindGameObjectsWithTag("Chest"))
            {
                this.Actions.Add(new PickUpChest(this, chest));
                this.AddResource(new Resource(this.navMesh.QuantizeToNode(chest.transform.position, 1.0f)));
            }

            foreach (var tree in GameObject.FindGameObjectsWithTag("Tree"))
            {
               this.Actions.Add(new GetArrows(this, tree));
                this.AddResource(new Resource(this.navMesh.QuantizeToNode(tree.transform.position, 1.0f)));
            }

            foreach (var bed in GameObject.FindGameObjectsWithTag("Bed"))
            {
                this.Actions.Add(new Sleep(this, bed));
                this.AddResource(new Resource(this.navMesh.QuantizeToNode(bed.transform.position, 1.0f)));
            }

            foreach (var boar in GameObject.FindGameObjectsWithTag("Boar"))
            {
                this.AddResource(new Resource(this.navMesh.QuantizeToNode(boar.transform.position, 1.0f)));
                this.Actions.Add(new MeleeAttack(this, boar));
                this.Actions.Add(new Shoot(this, boar));
            }

            this.ResourceInfluenceMap.Initialize(this.ActiveResources.Values.ToList());

            //flags used for the influence map
            this.RedFlags = new List<IInfluenceUnit>();
            foreach (var redFlag in GameObject.FindGameObjectsWithTag("RedFlag"))
            {
                this.RedFlags.Add(new Flag(this.navMesh.QuantizeToNode(redFlag.transform.position, 1.0f), FlagColor.Red));
            }

            this.GreenFlags = new List<IInfluenceUnit>();
            foreach (var greenFlag in GameObject.FindGameObjectsWithTag("GreenFlag"))
            {
                this.GreenFlags.Add(new Flag(this.navMesh.QuantizeToNode(greenFlag.transform.position, 1.0f), FlagColor.Green));
            }

            this.RedInfluenceMap.Initialize(this.RedFlags);
            this.GreenInfluenceMap.Initialize(this.GreenFlags);

            var worldModel = new CurrentStateWorldModel(this.GameManager, this.Actions, this.Goals);

            this.GOAPDecisionMaking = new DepthLimitedGOAPDecisionMaking(worldModel,this.Actions,this.Goals);
        }