Esempio n. 1
0
        public static IPathSearch FromPoints(World world, Locomotor locomotor, Actor self, IEnumerable <CPos> froms, CPos target, BlockedByActor check)
        {
            var graph  = new PathGraph(LayerPoolForWorld(world), locomotor, self, world, check);
            var search = new PathSearch(graph);

            search.heuristic = search.DefaultEstimator(target);

            // The search will aim for the shortest path by default, a weight of 100%.
            // We can allow the search to find paths that aren't optimal by changing the weight.
            // We provide a weight that limits the worst case length of the path,
            // e.g. a weight of 110% will find a path no more than 10% longer than the shortest possible.
            // The benefit of allowing the search to return suboptimal paths is faster computation time.
            // The search can skip some areas of the search space, meaning it has less work to do.
            // We allow paths up to 25% longer than the shortest, optimal path, to improve pathfinding time.
            search.heuristicWeightPercentage = 125;

            search.isGoal = loc =>
            {
                var locInfo = search.Graph[loc];
                return(locInfo.EstimatedTotal - locInfo.CostSoFar == 0);
            };

            foreach (var sl in froms)
            {
                if (world.Map.Contains(sl))
                {
                    search.AddInitialCell(sl);
                }
            }

            return(search);
        }
Esempio n. 2
0
        public PathGraph(CellInfoLayerPool layerPool, Locomotor locomotor, Actor actor, World world, BlockedByActor check,
                         Func <CPos, int> customCost, Actor ignoreActor, bool inReverse, bool laneBias)
        {
            customMovementLayers = world.GetCustomMovementLayers();
            customMovementLayersEnabledForLocomotor = customMovementLayers.Count(cml => cml != null && cml.EnabledForLocomotor(locomotor.Info));
            this.locomotor     = locomotor;
            this.world         = world;
            this.actor         = actor;
            this.check         = check;
            this.customCost    = customCost;
            this.ignoreActor   = ignoreActor;
            this.inReverse     = inReverse;
            this.laneBias      = laneBias;
            checkTerrainHeight = world.Map.Grid.MaximumTerrainHeight > 0;

            // As we support a search over the whole map area,
            // use the pool to grab the CellInfos we need to track the graph state.
            // This allows us to avoid the cost of allocating large arrays constantly.
            // PERF: Avoid LINQ
            pooledLayer         = layerPool.Get();
            cellInfoForLayer    = new CellLayer <CellInfo> [customMovementLayers.Length];
            cellInfoForLayer[0] = pooledLayer.GetLayer();
            foreach (var cml in customMovementLayers)
            {
                if (cml != null && cml.EnabledForLocomotor(locomotor.Info))
                {
                    cellInfoForLayer[cml.Index] = pooledLayer.GetLayer();
                }
            }
        }
Esempio n. 3
0
        public PathGraph(CellInfoLayerPool layerPool, Locomotor locomotor, Actor actor, World world, BlockedByActor check)
        {
            this.locomotor = locomotor;

            // As we support a search over the whole map area,
            // use the pool to grab the CellInfos we need to track the graph state.
            // This allows us to avoid the cost of allocating large arrays constantly.
            // PERF: Avoid LINQ
            var cmls = world.GetCustomMovementLayers();

            pooledLayer         = layerPool.Get();
            cellInfoForLayer    = new CellLayer <CellInfo> [cmls.Length];
            cellInfoForLayer[0] = pooledLayer.GetLayer();
            foreach (var cml in cmls)
            {
                if (cml != null && cml.EnabledForLocomotor(locomotor.Info))
                {
                    cellInfoForLayer[cml.Index] = pooledLayer.GetLayer();
                }
            }

            World              = world;
            Actor              = actor;
            LaneBias           = 1;
            checkConditions    = check;
            checkTerrainHeight = world.Map.Grid.MaximumTerrainHeight > 0;
        }
Esempio n. 4
0
 void Awake()
 {
     Graphic = GetComponent<Graphic>();
     Locomotor = GetComponent<Locomotor>();
     Animator = GetComponentInChildren<Animator>();
     Sprite = GetComponentInChildren<SpriteRenderer>();
     GetBrain();
 }
            public MinerTraitWrapper(Actor actor)
            {
                Actor = actor;
                var mobile = actor.Trait <Mobile>();

                Locomotor  = mobile.Locomotor;
                Transforms = actor.Trait <Transforms>();
            }
Esempio n. 6
0
            public HarvesterTraitWrapper(Actor actor)
            {
                Actor     = actor;
                Harvester = actor.Trait <Harvester>();
                var mobile = actor.Trait <Mobile>();

                Locomotor = mobile.Locomotor;
            }
Esempio n. 7
0
        public static IPathSearch Search(World world, Locomotor locomotor, Actor self, BlockedByActor check, Func <CPos, bool> goalCondition)
        {
            var graph  = new PathGraph(LayerPoolForWorld(world), locomotor, self, world, check);
            var search = new PathSearch(graph);

            search.isGoal    = goalCondition;
            search.heuristic = loc => 0;
            return(search);
        }
Esempio n. 8
0
 public static PathSearch ToTargetCell(
     World world, Locomotor locomotor, Actor self, CPos from, CPos target, BlockedByActor check,
     Func <CPos, int> customCost = null,
     Actor ignoreActor           = null,
     bool inReverse = false,
     bool laneBias  = DefaultLaneBias)
 {
     return(ToTargetCell(world, locomotor, self, new[] { from }, target, check, customCost, ignoreActor, inReverse, laneBias));
 }
Esempio n. 9
0
 // Start is called before the first frame update
 void Start()
 {
     locomotor = GetComponent <Locomotor>();
     bomber    = GetComponent <Bomber>();
     turret    = this.transform.GetChild(2).gameObject;
     if (pcControl)
     {
         moveJoystick.gameObject.SetActive(false);
         aimJoystick.gameObject.SetActive(false);
     }
 }
Esempio n. 10
0
 protected DensePathGraph(Locomotor locomotor, Actor actor, World world, BlockedByActor check,
                          Func <CPos, int> customCost, Actor ignoreActor, bool laneBias, bool inReverse)
 {
     CustomMovementLayers = world.GetCustomMovementLayers();
     customMovementLayersEnabledForLocomotor = CustomMovementLayers.Count(cml => cml != null && cml.EnabledForLocomotor(locomotor.Info));
     this.locomotor     = locomotor;
     this.world         = world;
     this.actor         = actor;
     this.check         = check;
     this.customCost    = customCost;
     this.ignoreActor   = ignoreActor;
     this.laneBias      = laneBias;
     this.inReverse     = inReverse;
     checkTerrainHeight = world.Map.Grid.MaximumTerrainHeight > 0;
 }
Esempio n. 11
0
        public static PathSearch ToTargetCellByPredicate(
            World world, Locomotor locomotor, Actor self, IEnumerable <CPos> froms, Func <CPos, bool> targetPredicate, BlockedByActor check,
            Func <CPos, int> customCost = null)
        {
            var graph  = new PathGraph(LayerPoolForWorld(world), locomotor, self, world, check, customCost, null, false, DefaultLaneBias);
            var search = new PathSearch(graph, loc => 0, DefaultHeuristicWeightPercentage, targetPredicate);

            foreach (var sl in froms)
            {
                if (world.Map.Contains(sl))
                {
                    search.AddInitialCell(sl);
                }
            }

            return(search);
        }
Esempio n. 12
0
        /// <summary>
        /// Default: Diagonal distance heuristic. More information:
        /// http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
        /// Layers are ignored and incur no additional cost.
        /// </summary>
        /// <param name="locomotor">Locomotor used to provide terrain costs.</param>
        /// <returns>A delegate that calculates the cost estimation between the given cells.</returns>
        public static Func <CPos, CPos, int> DefaultCostEstimator(Locomotor locomotor)
        {
            // Determine the minimum possible cost for moving horizontally between cells based on terrain speeds.
            // The minimum possible cost diagonally is then Sqrt(2) times more costly.
            var cellCost         = locomotor.Info.TerrainSpeeds.Values.Min(ti => ti.Cost);
            var diagonalCellCost = Exts.MultiplyBySqrtTwo(cellCost);

            return((here, destination) =>
            {
                var diag = Math.Min(Math.Abs(here.X - destination.X), Math.Abs(here.Y - destination.Y));
                var straight = Math.Abs(here.X - destination.X) + Math.Abs(here.Y - destination.Y);

                // According to the information link, this is the shape of the function.
                // We just extract factors to simplify.
                // Possible simplification: var h = Constants.CellCost * (straight + (Constants.Sqrt2 - 2) * diag);
                return cellCost * straight + (diagonalCellCost - 2 * cellCost) * diag;
            });
        }
Esempio n. 13
0
 public MapPathGraph(CellInfoLayerPool layerPool, Locomotor locomotor, Actor actor, World world, BlockedByActor check,
                     Func <CPos, int> customCost, Actor ignoreActor, bool laneBias, bool inReverse)
     : base(locomotor, actor, world, check, customCost, ignoreActor, laneBias, inReverse)
 {
     // As we support a search over the whole map area,
     // use the pool to grab the CellInfos we need to track the graph state.
     // This allows us to avoid the cost of allocating large arrays constantly.
     // PERF: Avoid LINQ
     pooledLayer         = layerPool.Get();
     cellInfoForLayer    = new CellLayer <CellInfo> [CustomMovementLayers.Length];
     cellInfoForLayer[0] = pooledLayer.GetLayer();
     foreach (var cml in CustomMovementLayers)
     {
         if (cml != null && cml.EnabledForLocomotor(locomotor.Info))
         {
             cellInfoForLayer[cml.Index] = pooledLayer.GetLayer();
         }
     }
 }
Esempio n. 14
0
	void Awake()
	{
		// Setup Dependancies
		animator = GetComponent<Animation>();
		audioSource = GetComponent<AudioSource>();
		locomotor = GetComponent<Locomotor>();
		acquirer = GetComponent<Acquirer>();
		stats = GetComponent<Stats>();
		
		// Setup Components
		if (animationClip != null)
		{
			animationClip.wrapMode = WrapMode.Loop;
			animator.clip = animationClip;
			animator.Play();
		}
		rigidbody.freezeRotation = true;
		//rigidbody.useGravity = false;
		Hungry = true;
		
	}
Esempio n. 15
0
        public static IPathSearch FromPoints(World world, Locomotor locomotor, Actor self, IEnumerable <CPos> froms, CPos target, bool checkForBlocked)
        {
            var graph  = new PathGraph(LayerPoolForWorld(world), locomotor, self, world, checkForBlocked);
            var search = new PathSearch(graph)
            {
                heuristic = DefaultEstimator(target)
            };

            search.isGoal = loc =>
            {
                var locInfo = search.Graph[loc];
                return(locInfo.EstimatedTotal - locInfo.CostSoFar == 0);
            };

            foreach (var sl in froms.Where(sl => world.Map.Contains(sl)))
            {
                search.AddInitialCell(sl);
            }

            return(search);
        }
Esempio n. 16
0
        public static IPathSearch FromPoint(World world, Locomotor locomotor, Actor self, CPos @from, CPos target, bool checkForBlocked)
        {
            var graph  = new PathGraph(LayerPoolForWorld(world), locomotor, self, world, checkForBlocked);
            var search = new PathSearch(graph)
            {
                heuristic = DefaultEstimator(target)
            };

            search.isGoal = loc =>
            {
                var locInfo = search.Graph[loc];
                return(locInfo.EstimatedTotal - locInfo.CostSoFar == 0);
            };

            if (world.Map.Contains(from))
            {
                search.AddInitialCell(from);
            }

            return(search);
        }
Esempio n. 17
0
        public PathGraph(CellInfoLayerPool layerPool, Locomotor locomotor, Actor actor, World world, BlockedByActor check)
        {
            pooledLayer = layerPool.Get();
            groundInfo  = pooledLayer.GetLayer();
            var locomotorInfo = locomotor.Info;

            this.locomotor = locomotor;
            var layers = world.GetCustomMovementLayers().Values
                         .Where(cml => cml.EnabledForActor(actor.Info, locomotorInfo));

            foreach (var cml in layers)
            {
                customLayerInfo[cml.Index] = Pair.New(cml, pooledLayer.GetLayer());
            }

            World              = world;
            worldMovementInfo  = locomotorInfo.GetWorldMovementInfo(world);
            Actor              = actor;
            LaneBias           = 1;
            checkConditions    = check;
            checkTerrainHeight = world.Map.Grid.MaximumTerrainHeight > 0;
        }
Esempio n. 18
0
        public static PathSearch ToTargetCell(
            World world, Locomotor locomotor, Actor self, IEnumerable <CPos> froms, CPos target, BlockedByActor check,
            Func <CPos, int> customCost = null,
            Actor ignoreActor           = null,
            bool laneBias                 = true,
            bool inReverse                = false,
            Func <CPos, int> heuristic    = null,
            int heuristicWeightPercentage = DefaultHeuristicWeightPercentage)
        {
            var graph = new MapPathGraph(LayerPoolForWorld(world), locomotor, self, world, check, customCost, ignoreActor, laneBias, inReverse);

            heuristic = heuristic ?? DefaultCostEstimator(locomotor, target);
            var search = new PathSearch(graph, heuristic, heuristicWeightPercentage, loc => loc == target);

            foreach (var sl in froms)
            {
                if (world.Map.Contains(sl))
                {
                    search.AddInitialCell(sl);
                }
            }

            return(search);
        }
Esempio n. 19
0
        /// <summary>
        /// Default: Diagonal distance heuristic. More information:
        /// http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
        /// Layers are ignored and incur no additional cost.
        /// </summary>
        /// <param name="locomotor">Locomotor used to provide terrain costs.</param>
        /// <param name="destination">The cell for which costs are to be given by the estimation function.</param>
        /// <returns>A delegate that calculates the cost estimation between the <paramref name="destination"/> and the given cell.</returns>
        public static Func <CPos, int> DefaultCostEstimator(Locomotor locomotor, CPos destination)
        {
            var estimator = DefaultCostEstimator(locomotor);

            return(here => estimator(here, destination));
        }
Esempio n. 20
0
 public static IPathSearch FromPoint(World world, Locomotor locomotor, Actor self, CPos @from, CPos target, BlockedByActor check)
 {
     return(FromPoints(world, locomotor, self, new[] { from }, target, check));
 }
 void Start()
 {
     SetupJoysticks();
     locomotor = GetComponent<Locomotor> ();
     acquirer = GetComponent<Acquirer> ();
     thrower = GetComponent<InventoryThrower> ();
     Screen.showCursor = false;
 }
 void Start()
 {
     locomotor = GetComponent<Locomotor>();
     player = GetComponent<PlayerGravityBody>();
     attractors = new Cycler(FindObjectsOfType(typeof(GravityAttractor)));
 }