Distance() public method

public Distance ( Vector2i, a ) : float
a Vector2i,
return float
Ejemplo n.º 1
0
    public static IEnumerable <Vector2i> GetLine(Vector2i initialGridCoordinate, Vector2i endGridCoordinate)
    {
        List <Vector2i> line     = new List <Vector2i>();
        int             distance = initialGridCoordinate.Distance(endGridCoordinate) * 2;

        // Debug.Log(initialGridCoordinate + " " + endGridCoordinate + " " + distance);
        // TODO: Reduce complexity. (now 1 iteration in GetLine then an interation in the calling function).
        for (int i = 0; i < distance; i++)
        {
            line.Add(Vector2i.Lerp(initialGridCoordinate, endGridCoordinate, (float)i / distance));
        }
        return(line);
    }
    public void destroy()
    {
        Vector2i pos  = GLOBAL.worldToGrid(transform.position);
        Unit     unit = CombatSequence.Instance.attacker;//PlayerManager.Instance.getCurrentPlayer().selectedObject.GetComponent<Unit>();

        if (pos.Distance(unit.pos) == max)
        {
            // last shot, administer damage (TODO: only administer once regardless of how many max distance tiles there are)
            CombatSequence.Instance.AoEDamage();
        }

        Destroy(gameObject);
    }
        /// <summary>
        /// Get chunk positions order by valuable
        /// </summary>
        /// <returns></returns>
        public IEnumerable <ChunkPositionValue> ValuableChunkPos(float range)
        {
            var chunkCenterPos = Chunk.GetPositionFromWorld(new Vector2(Position.x, Position.z));
            var chunkRange     = (int)(range / Chunk.Size);

            var result = new List <ChunkPositionValue>();

            //Get all chunk positions in a range
            foreach (var chunkPos in new Bounds2i(chunkCenterPos, chunkRange))
            {
                if (Vector2i.Distance(chunkCenterPos, chunkPos) * Chunk.Size < range)
                {
                    result.Add(new ChunkPositionValue()
                    {
                        Position = chunkPos,
                        Value    = GetChunkPositionValue(chunkPos, range)
                    });
                }
            }

            result.Sort();
            return(result);
        }
Ejemplo n.º 4
0
    // finds the closest room and returns its index
    private int findClosestRoom(int room, List <int> ignoreRooms)
    {
        Vector2i sourceRoomCenter = roomCenters[room];
        float    smallestDistance = float.MaxValue;
        int      index            = 0;

        for (int i = 0; i < roomCenters.Count; i++)
        {
            if (i == room || ignoreRooms.Contains(i))
            {
                continue;
            }
            Vector2i curRoomCenter = roomCenters[i];
            float    curDist       = Vector2i.Distance(sourceRoomCenter, curRoomCenter);
            if (curDist < smallestDistance)
            {
                smallestDistance = curDist;
                index            = i;
            }
        }

        return(index);
    }
Ejemplo n.º 5
0
        /// <summary>
        /// Attempts to move towards a position. This function will perform pathfinding logic and take into consideration move distance
        /// to try and smoothly move towards a point.
        /// </summary>
        /// <param name="position">The position to move towards.</param>
        /// <param name="user">A user object passed.</param>
        /// <returns>true if the position was moved towards, and false if there was a pathfinding error.</returns>
        public bool MoveTowards(Vector2i position, params dynamic[] user)
        {
            var myPosition = LokiPoe.MyPosition;

            if (
                _cmd == null ||                                                                                   // No command yet
                _cmd.Path == null ||
                _cmd.EndPoint != position ||                                                                      // Moving to a new position
                LokiPoe.CurrentWorldArea.IsTown ||                                                                // In town, always generate new paths
                (_sw.IsRunning && _sw.ElapsedMilliseconds > OldPlayerMoverSettings.Instance.PathRefreshRateMs) || // New paths on interval
                _cmd.Path.Count <= 2 ||                                                                           // Not enough points
                _cmd.Path.All(p => myPosition.Distance(p) > 7))
            // Try and find a better path to follow since we're off course
            {
                _cmd = new PathfindingCommand(myPosition, position, 3, OldPlayerMoverSettings.Instance.AvoidWallHugging);
                if (!ExilePather.FindPath(ref _cmd))
                {
                    _sw.Restart();
                    Log.ErrorFormat("[OldPlayerMover.MoveTowards] ExilePather.FindPath failed from {0} to {1}.",
                                    myPosition, position);
                    return(false);
                }
                //Log.InfoFormat("[OldPlayerMover.MoveTowards] Finding new path.");
                _sw.Restart();
                //_originalPath = new IndexedList<Vector2i>(_cmd.Path);
            }

            // Eliminate points until we find one within a good moving range.
            while (_cmd.Path.Count > 1)
            {
                if (_cmd.Path[0].Distance(myPosition) < OldPlayerMoverSettings.Instance.MoveRange)
                {
                    _cmd.Path.RemoveAt(0);
                }
                else
                {
                    break;
                }
            }

            var point = _cmd.Path[0];

            point += new Vector2i(LokiPoe.Random.Next(-2, 3), LokiPoe.Random.Next(-2, 3));

            if (_useForceAdjustments)
            {
                var negX = 0;
                var posX = 0;

                var tmp1 = point;
                var tmp2 = point;

                for (var i = 0; i < 10; i++)
                {
                    tmp1.X--;
                    if (!ExilePather.IsWalkable(tmp1))
                    {
                        negX++;
                    }

                    tmp2.X++;
                    if (!ExilePather.IsWalkable(tmp2))
                    {
                        posX++;
                    }
                }

                if (negX > 5 && posX == 0)
                {
                    point.X += 10;
                    if (OldPlayerMoverSettings.Instance.DebugAdjustments)
                    {
                        Log.WarnFormat("[OldPlayerMover.MoveTowards] X-Adjustments being made!");
                    }
                    _cmd.Path[0] = point;
                }
                else if (posX > 5 && negX == 0)
                {
                    point.X -= 10;
                    if (OldPlayerMoverSettings.Instance.DebugAdjustments)
                    {
                        Log.WarnFormat("[OldPlayerMover.MoveTowards] X-Adjustments being made!");
                    }
                    _cmd.Path[0] = point;
                }

                var negY = 0;
                var posY = 0;

                tmp1 = point;
                tmp2 = point;

                for (var i = 0; i < 10; i++)
                {
                    tmp1.Y--;
                    if (!ExilePather.IsWalkable(tmp1))
                    {
                        negY++;
                    }

                    tmp2.Y++;
                    if (!ExilePather.IsWalkable(tmp2))
                    {
                        posY++;
                    }
                }

                if (negY > 5 && posY == 0)
                {
                    point.Y += 10;
                    if (OldPlayerMoverSettings.Instance.DebugAdjustments)
                    {
                        Log.WarnFormat("[OldPlayerMover.MoveTowards] Y-Adjustments being made!");
                    }
                    _cmd.Path[0] = point;
                }
                else if (posY > 5 && negY == 0)
                {
                    point.Y -= 10;
                    if (OldPlayerMoverSettings.Instance.DebugAdjustments)
                    {
                        Log.WarnFormat("[OldPlayerMover.MoveTowards] Y-Adjustments being made!");
                    }
                    _cmd.Path[0] = point;
                }
            }

            // Le sigh...
            if (_useAct3TownAdjustments)
            {
                var seed = LokiPoe.LocalData.AreaHash;
                if (_tgtSeed != seed || _tgts == null)
                {
                    Log.InfoFormat("[OldPlayerMover.MoveTowards] Now building TGT info.");
                    _tgts    = LokiPoe.TerrainData.TgtEntries;
                    _tgtSeed = seed;
                }
                if (TgtUnderPlayer.TgtName.Equals("Art/Models/Terrain/Act3Town/Act3_town_01_01_c16r7.tgt"))
                {
                    Log.InfoFormat("[OldPlayerMover.MoveTowards] Act 3 Town force adjustment being made!");
                    point.Y += 5;
                }
            }

            var move = LokiPoe.InGameState.SkillBarHud.LastBoundMoveSkill;

            if (move == null)
            {
                Log.ErrorFormat("[OldPlayerMover.MoveTowards] Please assign the \"Move\" skill to your skillbar!");
                return(false);
            }

            if ((LokiPoe.ProcessHookManager.GetKeyState(move.BoundKeys.Last()) & 0x8000) != 0 &&
                LokiPoe.Me.HasCurrentAction)
            {
                if (myPosition.Distance(position) < OldPlayerMoverSettings.Instance.SingleUseDistance)
                {
                    LokiPoe.ProcessHookManager.ClearAllKeyStates();
                    LokiPoe.InGameState.SkillBarHud.UseAt(move.Slots.Last(), false, point);
                    if (OldPlayerMoverSettings.Instance.DebugInputApi)
                    {
                        Log.WarnFormat("[SkillBarHud.UseAt] {0}", point);
                    }
                    _lastPoint = point;
                }
                else
                {
                    if (OldPlayerMoverSettings.Instance.UseMouseSmoothing)
                    {
                        var d = _lastPoint.Distance(point);
                        if (d >= OldPlayerMoverSettings.Instance.MouseSmoothDistance)
                        {
                            MouseManager.SetMousePos("OldPlayerMover.MoveTowards", point, false);
                            if (OldPlayerMoverSettings.Instance.DebugInputApi)
                            {
                                Log.WarnFormat("[MouseManager.SetMousePos] {0} [{1}]", point, d);
                            }
                            _lastPoint = point;
                        }
                        else
                        {
                            if (OldPlayerMoverSettings.Instance.DebugInputApi)
                            {
                                Log.WarnFormat("[MouseManager.SetMousePos] Skipping moving mouse to {0} because [{1}] < [{2}]", point, d, OldPlayerMoverSettings.Instance.MouseSmoothDistance);
                            }
                        }
                    }
                    else
                    {
                        MouseManager.SetMousePos("OldPlayerMover.MoveTowards", point, false);
                    }
                }
            }
            else
            {
                LokiPoe.ProcessHookManager.ClearAllKeyStates();
                if (myPosition.Distance(position) < OldPlayerMoverSettings.Instance.SingleUseDistance)
                {
                    LokiPoe.InGameState.SkillBarHud.UseAt(move.Slots.Last(), false, point);
                    if (OldPlayerMoverSettings.Instance.DebugInputApi)
                    {
                        Log.WarnFormat("[SkillBarHud.UseAt] {0}", point);
                    }
                }
                else
                {
                    LokiPoe.InGameState.SkillBarHud.BeginUseAt(move.Slots.Last(), false, point);
                    if (OldPlayerMoverSettings.Instance.DebugInputApi)
                    {
                        Log.WarnFormat("[BeginUseAt] {0}", point);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 6
0
 /// <summary>Get the closest distance between this <see cref="Box2i"/> and the <see cref="Vector2i"/>.</summary>
 public double Distance( ref  Vector2i point)
 {
     Vector2i nearest;
         NearestPointTo(ref point, out nearest);
         return point.Distance(ref nearest);
 }
Ejemplo n.º 7
0
 public static float Distance(Vector2i a, Vector2i b)
 {
     return(Vector2i.Distance(a, b));
 }
Ejemplo n.º 8
0
    // marks PURPLE TILES of all possible coverages for selection
    public override void markAoEAim(Vector2i root)
    {
        TileMarker tileMarker = TileMarker.Instance;

        unit.calcCombatStats();
        int range = Mathf.Max((int)(unit.combatEnergyAtk * 0.5f), 3); // minimum beam size is 3x3 (length extendable)

        // mark left beam
        for (int i = -1; i <= 1; i++)
        {
            if (unit.pos.y + i < 0 || unit.pos.y + i > MapScript.Instance.mapHeight)
            {
                continue; // don't process tiles outside of map
            }

            for (int j = Mathf.Max(root.x - range, 0); j < root.x; j++)
            {
                Vector2i mark = new Vector2i(j, root.y + i);

                if (mark.Distance(unit.pos) == 2 && mark.x != unit.pos.x && mark.y != unit.pos.y)
                {
                    if (!tileMarker.attackTiles.ContainsKey(mark))
                    {
                        //tiles directly diagonal to unit are ambiguous; mark with attack tile instead of AoE
                        tileMarker.addAttackTile(mark);
                    }
                }
                else
                {
                    tileMarker.addAoETile(mark);
                }
            }
        }
        // mark right beam
        for (int i = -1; i <= 1; i++)
        {
            if (unit.pos.y + i < 0 || unit.pos.y + i > MapScript.Instance.mapHeight)
            {
                continue; // don't process tiles outside of map
            }

            for (int j = Mathf.Min(root.x + range, MapScript.Instance.mapWidth); j > root.x; j--)
            {
                Vector2i mark = new Vector2i(j, root.y + i);

                if (mark.Distance(unit.pos) == 2 && mark.x != unit.pos.x && mark.y != unit.pos.y)
                {
                    //tiles directly diagonal to unit are ambiguous; mark with attack tile instead of AoE
                    if (!tileMarker.attackTiles.ContainsKey(mark))
                    {
                        //tiles directly diagonal to unit are ambiguous; mark with attack tile instead of AoE
                        tileMarker.addAttackTile(mark);
                    }
                }
                else
                {
                    tileMarker.addAoETile(mark);
                }
            }
        }
        // mark up beam
        for (int i = -1; i <= 1; i++) // i represents x now instead of y
        {
            if (unit.pos.x + i < 0 || unit.pos.x + i > MapScript.Instance.mapWidth)
            {
                continue; // don't process tiles outside of map
            }

            for (int j = Mathf.Min(root.y + 1, MapScript.Instance.Width); j <= root.y + range; j++)
            {
                Vector2i mark = new Vector2i(root.x + i, j);

                if (mark.Distance(unit.pos) == 2 && mark.x != unit.pos.x && mark.y != unit.pos.y)
                {
                    if (!tileMarker.attackTiles.ContainsKey(mark))
                    {
                        //tiles directly diagonal to unit are ambiguous; mark with attack tile instead of AoE
                        tileMarker.addAttackTile(mark);
                    }
                }
                else
                {
                    tileMarker.addAoETile(mark);
                }
            }
        }
        // mark down beam
        for (int i = -1; i <= 1; i++) // i represents x now instead of y
        {
            if (unit.pos.x + i < 0 || unit.pos.x + i > MapScript.Instance.mapWidth)
            {
                continue; // don't process tiles outside of map
            }

            for (int j = Mathf.Max(root.y - 1, 0); j >= root.y - range; j--)
            {
                Vector2i mark = new Vector2i(root.x + i, j);

                if (mark.Distance(unit.pos) == 2 && mark.x != unit.pos.x && mark.y != unit.pos.y)
                {
                    if (!tileMarker.attackTiles.ContainsKey(mark))
                    {
                        //tiles directly diagonal to unit are ambiguous; mark with attack tile instead of AoE
                        tileMarker.addAttackTile(mark);
                    }
                }
                else
                {
                    tileMarker.addAoETile(mark);
                }
            }
        }
    }
Ejemplo n.º 9
0
    public override void markAoEPattern(Vector2i root) // root is redundant (always this weapon's unit)
    {
        TileMarker tileMarker = TileMarker.Instance;

        //mark all diagonal tiles from unit

        Vector2i curr = new Vector2i(unit.pos.x - 1, unit.pos.y + 1);

        // up left
        while (curr.x >= 0 && curr.y < MapScript.Instance.mapHeight)
        {
            if (curr.Distance(unit.pos) > max)
            { // keep track of maximum distance attack
                max = curr.Distance(unit.pos);
            }

            tileMarker.addAttackTile(curr);
            curr.x--;
            curr.y++;
        }

        // up right
        curr = new Vector2i(unit.pos.x + 1, unit.pos.y + 1);
        while (curr.x < MapScript.Instance.mapWidth && curr.y < MapScript.Instance.mapHeight)
        {
            if (curr.Distance(unit.pos) > max)
            { // keep track of maximum distance attack
                max = curr.Distance(unit.pos);
            }

            tileMarker.addAttackTile(curr);
            curr.x++;
            curr.y++;
        }

        // down left
        curr = new Vector2i(unit.pos.x - 1, unit.pos.y - 1);
        while (curr.x >= 0 && curr.y >= 0)
        {
            if (curr.Distance(unit.pos) > max)
            { // keep track of maximum distance attack
                max = curr.Distance(unit.pos);
            }

            tileMarker.addAttackTile(curr);
            curr.x--;
            curr.y--;
        }

        // down right
        curr = new Vector2i(unit.pos.x + 1, unit.pos.y - 1);
        while (curr.x < MapScript.Instance.mapWidth && curr.y >= 0)
        {
            if (curr.Distance(unit.pos) > max)
            { // keep track of maximum distance attack
                max = curr.Distance(unit.pos);
            }

            tileMarker.addAttackTile(curr);
            curr.x++;
            curr.y--;
        }
    }
Ejemplo n.º 10
0
        public override Blocks GenerateBlock2(Vector2i position, Heights macroHeight)
        {
            var block = base.GenerateBlock2(position, macroHeight);

            if (Zone.Biome.Type == BiomeType.TestWaves)
            {
                var height  = Mathf.Sin(position.X / 5f) * 5;
                var heights = new Heights(macroHeight.Main + height, macroHeight.Underground + height, macroHeight.Base + height);
                block = block.MutateHeight(heights);
            }
            else if (Zone.Biome.Type == BiomeType.TestBaseOreAndGround)
            {
                var baseHeight      = _generator.GetSimplex(position.X / 20f, position.Z / 20f) * 3 + macroHeight.Base;
                var resourcesHeight = Interpolation.SmoothestStep(Mathf.Clamp01((float)_generator.GetSimplex(position.X / 10f, position.Z / 10f))) * 10 + macroHeight.Underground - 1;
                var groundHeight    = Interpolation.SmoothestStep((_generator.GetSimplex(position.Z / 40f, position.X / 40f) + 1) / 2) * 10 - 12 + macroHeight.Main;
                //var groundHeight = -1;

                //var underground = resourcesHeight > baseHeight ? BlockType.GoldOre : BlockType.Empty;
                var       underground = BlockType.GoldOre;
                BlockType ground;

                /*
                 * if (groundHeight > resourcesHeight && groundHeight > baseHeight)
                 *  ground = BlockType.Grass;
                 * else
                 *  ground = BlockType.Empty;
                 */
                ground = BlockType.Grass;

                block = new Blocks(ground, underground, new Heights((float)groundHeight, resourcesHeight, (float)baseHeight));
            }
            else if (Zone.Biome.Type == BiomeType.TestBaseCavesAndGround)
            {
                //"Hourglass" shaped hole to the base layer
                //
                //-\        /---
                //  \      /
                //   \    /
                //   /    \
                //  /      \
                //-/--------\--
                var distance = Vector2.Distance(Vector2.Zero, position);

                if (distance < 15)
                {
                    var baseHeight   = -10;
                    var underHeight  = distance < 10 ? -10 + (10 - distance) : -10;
                    var groundHeight = distance < 10 ? -10 + distance : 0;

                    /*
                     * var baseHeight = _generator.GetSimplex(position.X / 30f, position.Z / 30f) * 3 + macroHeight.BaseHeight;
                     * var undergroundHeight = (_generator.GetSimplex(position.X / 20f, position.Z / 20f) + 1);
                     * undergroundHeight = Math.Pow(undergroundHeight, 4) + macroHeight.UndergroundHeight;
                     * var groundHeight = _generator.GetSimplex(position.X / 5f, position.Z / 5f) + macroHeight.Layer1Height;
                     */

                    BlockType underground;
                    if (underHeight > groundHeight || underHeight <= baseHeight)
                    {
                        underground = BlockType.Empty;
                    }
                    else
                    {
                        underground = BlockType.Cave;
                    }

                    BlockType ground = groundHeight >= underHeight ? BlockType.Grass : BlockType.Empty;

                    block = new Blocks(ground, underground, new Heights(groundHeight, underHeight, baseHeight));
                }
                else
                {
                    block = Blocks.Empty;
                }
            }
            else if (Zone.Biome.Type == BiomeType.TestBaseOreGroundColumn)
            {
                //Column made from ground and ore, to test very steep slopes (vertical)

                //Column has half-circle form
                if (position.X < 0 && Vector2i.Distance(position, Vector2i.Zero) < 5)
                {
                    //All blocks the same
                    //blocks = new Blocks(BlockType.Grass, BlockType.GoldOre, new Heights(10, 5, -5));

                    //All block has different heights
                    var baseHeight   = _random.Range(0, 5);
                    var underHeight  = baseHeight + _random.Range(0, 5);
                    var groundHeight = underHeight + _random.Range(0, 5);

                    block = new Blocks(BlockType.Grass, BlockType.GoldOre,
                                       new Heights(groundHeight, underHeight, baseHeight));
                }
                else
                {
                    block = new Blocks(BlockType.Empty, BlockType.Empty, new Heights(-10, -10, -10));
                }
            }
            else if (Zone.Biome.Type == BiomeType.TestBaseOrePyramidGround)
            {
                //Special test case
                if (position == (3, -21))
                {
                    return(new Blocks(Zone.Biome.DefaultMainBlock.Block, Zone.Biome.DefaultUndergroundBlock.Block,
                                      new Heights(0, 1, -5)));
                }

                if (position == (-15, -13) || position == (-15, -14))
                {
                    return(new Blocks(Zone.Biome.DefaultMainBlock.Block, Zone.Biome.DefaultUndergroundBlock.Block,
                                      new Heights(0, 1, -5)));
                }

                if (position == (-18, 12) || position == (-18, 13) || position == (-17, 13))
                {
                    return(new Blocks(Zone.Biome.DefaultMainBlock.Block, Zone.Biome.DefaultUndergroundBlock.Block,
                                      new Heights(0, 1, -5)));
                }

                if (position == (13, 14) || position == (13, 15) || position == (12, 14) || position == (12, 15))
                {
                    return(new Blocks(Zone.Biome.DefaultMainBlock.Block, Zone.Biome.DefaultUndergroundBlock.Block,
                                      new Heights(0, 1, -5)));
                }

                var distance = Vector2.Distance(Vector2.Zero, position);

                var baseHeight  = -5f;
                var oreHeight   = 7 - distance;
                var grassHeight = 0;

                block = new Blocks(Zone.Biome.DefaultMainBlock.Block, Zone.Biome.DefaultUndergroundBlock.Block,
                                   new Heights(grassHeight, oreHeight, baseHeight));
            }
            else
            {
                if (Zone.Biome.Type == BiomeType.TestTunnel)
                {
                    //Tunnel is line defined by ax + by + c = 0
                    const float a = 2f, b = -1f, c = 5f;
                    const float tunnelWidth        = 15;
                    const float tunnelCenterHeight = 0;

                    //Mountain defined as cone
                    Vector2     mountCenter = Vector2.Zero;
                    const float mountSize   = 20;


                    var distanceToTunnel = Mathf.Abs(a * position.X + b * position.Z + c) / Mathf.Sqrt(a * a + b * b);
                    var distanceToMount  = Vector2.Distance(position, mountCenter);
                    var tunnelTurbulence = (float)(_generator.GetSimplex(position.X / 10d, position.Z / 10d) * 2
                                                   + _generator.GetSimplex(position.Z / 70d, position.X / 70d) * 5);

                    //Make tunnel floor
                    var baseHeight = 0f;
                    if (distanceToTunnel < tunnelWidth)
                    {
                        baseHeight += tunnelCenterHeight - Mathf.Sqrt(tunnelWidth * tunnelWidth - distanceToTunnel * distanceToTunnel) + tunnelTurbulence;
                    }

                    var underHeight = 0f;
                    if (distanceToTunnel < tunnelWidth)
                    {
                        underHeight += tunnelCenterHeight + Mathf.Sqrt(tunnelWidth * tunnelWidth - distanceToTunnel * distanceToTunnel) + tunnelTurbulence;
                    }

                    var groundHeight = 5f;
                    if (distanceToMount < mountSize)
                    {
                        groundHeight += mountSize - distanceToMount;
                    }
                    block = new Blocks(Zone.Biome.DefaultMainBlock.Block, Zone.Biome.DefaultUndergroundBlock.Block,
                                       new Heights(groundHeight, underHeight, baseHeight));
                }
            }

            return(block);
        }