Esempio n. 1
0
        public void Cursor()
        {
            if (player.whoAmI != Main.myPlayer)
            {
                return;
            }
            Main.SmartCursorShowing = false;
            if (!Main.SmartCursorEnabled)
            {
                return;
            }
            Item    item  = player.inventory[player.selectedItem];
            Vector2 mouse = Main.screenPosition + new Vector2(Main.mouseX, Main.mouseY);

            if (player.gravDir == -1f)
            {
                mouse.Y = Main.screenPosition.Y + Main.screenHeight - Main.mouseY;
            }
            int  targetX       = (int)MathHelper.Clamp(Player.tileTargetX, 10, Main.maxTilesX - 10);
            int  targetY       = (int)MathHelper.Clamp(Player.tileTargetY, 10, Main.maxTilesY - 10);
            bool disableCursor = false;

            if (Main.tile[targetX, targetY] == null)
            {
                return;
            }
            if (Main.tile[targetX, targetY].active())
            {
                switch (Main.tile[targetX, targetY].type)
                {
                case 4:
                case 10:
                case 11:
                case 13:
                case 21:
                case 29:
                case 33:
                case 49:
                case 50:
                case 55:
                case 79:
                case 85:
                case 88:
                case 97:
                case 104:
                case 125:
                case 132:
                case 136:
                case 139:
                case 144:
                case 174:
                case 207:
                case 209:
                case 212:
                case 216:
                case 219:
                case 237:
                case 287:
                case 334:
                case 335:
                case 338:
                case 354:
                case 386:
                case 387:
                case 388:
                case 389:
                case 411:
                case 425:
                case 441:
                case 463:
                case 467:
                case 468:
                    disableCursor = true;
                    break;

                case 314:
                    if (player.gravDir == 1f)
                    {
                        disableCursor = true;
                    }
                    break;
                }
            }
            TileLoader.DisableSmartCursor(Main.tile[targetX, targetY], ref disableCursor);
            int tileBoost = item.tileBoost;
            int maxLeft   = (int)(player.position.X / 16f) - Player.tileRangeX - tileBoost + 1;
            int maxRight  = (int)((player.position.X + player.width) / 16f) + Player.tileRangeX + tileBoost - 1;
            int maxUp     = (int)(player.position.Y / 16f) - Player.tileRangeY - tileBoost + 1;
            int maxDown   = (int)((player.position.Y + player.height) / 16f) + Player.tileRangeY + tileBoost - 2;

            maxLeft  = Utils.Clamp(maxLeft, 10, Main.maxTilesX - 10);
            maxRight = Utils.Clamp(maxRight, 10, Main.maxTilesX - 10);
            maxDown  = Utils.Clamp(maxDown, 10, Main.maxTilesY - 10);
            maxUp    = Utils.Clamp(maxUp, 10, Main.maxTilesY - 10);
            if (disableCursor && targetX >= maxLeft && targetX <= maxRight && targetY <= maxDown && targetY >= maxUp)
            {
                return;
            }
            List <Tuple <int, int> > listTargets = new List <Tuple <int, int> >();

            for (int xCheck = maxLeft; xCheck <= maxRight; xCheck++)
            {
                for (int yCheck = maxUp; yCheck <= maxDown; yCheck++)
                {
                    Tile checkTile       = Main.tile[xCheck, yCheck];
                    bool isBloomingPlant = checkTile.type == TileID.BloomingHerbs;
                    if (checkTile.type == TileID.MatureHerbs)
                    {
                        switch (checkTile.frameX / 18)
                        {
                        case 0:
                        {
                            if (Main.dayTime)
                            {
                                isBloomingPlant = true;
                            }
                            break;
                        }

                        case 1:
                        {
                            if (!Main.dayTime)
                            {
                                isBloomingPlant = true;
                            }
                            break;
                        }

                        case 3:
                        {
                            if (!Main.dayTime && (Main.bloodMoon || Main.moonPhase == 0))
                            {
                                isBloomingPlant = true;
                            }
                            break;
                        }

                        case 4:
                        {
                            if (Main.raining || Main.cloudAlpha > 0f)
                            {
                                isBloomingPlant = true;
                            }
                            break;
                        }

                        case 5:
                        {
                            if (!Main.raining && Main.dayTime && Main.time > 40500.00)
                            {
                                isBloomingPlant = true;
                            }
                            break;
                        }

                        default:
                            break;
                        }
                    }
                    if (isBloomingPlant)
                    {
                        listTargets.Add(new Tuple <int, int>(xCheck, yCheck));
                    }
                }
            }
            if (listTargets.Count > 0)
            {
                float            distanceToMouse = -1f;
                Tuple <int, int> tupleCoords     = listTargets[0];
                for (int target = 0; target < listTargets.Count; target++)
                {
                    float distanceNew = Vector2.Distance(new Vector2(listTargets[target].Item1, listTargets[target].Item2) * 16f + Vector2.One * 8f, mouse);
                    if (distanceToMouse == -1f || distanceNew < distanceToMouse)
                    {
                        distanceToMouse = distanceNew;
                        tupleCoords     = listTargets[target];
                    }
                }
                if (Collision.InTileBounds(tupleCoords.Item1, tupleCoords.Item2, maxLeft, maxUp, maxRight, maxDown))
                {
                    Main.SmartCursorX       = (Player.tileTargetX = tupleCoords.Item1);
                    Main.SmartCursorY       = (Player.tileTargetY = tupleCoords.Item2);
                    Main.SmartCursorShowing = true;
                }
                listTargets.Clear();
            }
        }
Esempio n. 2
0
        // Adapted from the vanilla Player class method
        public static void SmartCursorLookup(Player player)
        {
            if (player.whoAmI != Main.myPlayer || !Main.SmartCursorEnabled || Main.SmartInteractShowingGenuine)
            {
                return;
            }
            try
            {
                Item item = player.inventory[player.selectedItem];
                if (Main.SmartCursorShowing && item.pick > 0)
                {
                    return;
                }

                int shovel = (item.modItem as BaseShovel)?.Shovel ?? 0;
                if (shovel <= 0)
                {
                    return;
                }

                Vector2 MousePoss = Main.ReverseGravitySupport(Main.MouseScreen) + Main.screenPosition;
                int     x         = Utils.Clamp((int)(MousePoss.X / 16), 10, Main.maxTilesX - 10);
                int     y         = Utils.Clamp((int)(MousePoss.Y / 16), 10, Main.maxTilesY - 10);
                if (Main.tile[x, y] == null)
                {
                    return;
                }

                bool DisableSmart = false;
                if (Main.tile[x, y].active())
                {
                    DisableSmart = CanDigTile(Main.tile[x, y].type, shovel);
                }

                TileLoader.DisableSmartCursor(Main.tile[x, y], ref DisableSmart);
                int tileBoost = item.tileBoost;
                int minX      = (int)(player.position.X / 16) - Player.tileRangeX - tileBoost + 1;
                int maxX      = (int)((player.position.X + player.width) / 16) + Player.tileRangeX + tileBoost - 1;
                int minY      = (int)(player.position.Y / 16) - Player.tileRangeY - tileBoost + 1;
                int maxY      = (int)((player.position.Y + player.height) / 16) + Player.tileRangeY + tileBoost - 2;
                minX = Utils.Clamp(minX, 10, Main.maxTilesX - 10);
                maxX = Utils.Clamp(maxX, 10, Main.maxTilesX - 10);
                minY = Utils.Clamp(minY, 10, Main.maxTilesY - 10);
                maxY = Utils.Clamp(maxY, 10, Main.maxTilesY - 10);
                if (DisableSmart && x >= minX && x <= maxX && y >= minY && y <= maxY)
                {
                    return;
                }

                List <Tuple <int, int> > grapledTiles = new List <Tuple <int, int> >();
                for (int i = 0; i < player.grapCount; i++)
                {
                    Projectile projectile = Main.projectile[player.grappling[i]];
                    grapledTiles.Add(new Tuple <int, int>((int)(projectile.Center.X / 16), (int)(projectile.Center.Y / 16)));
                }
                int smartX = -1;
                int smartY = -1;
                if (PlayerInput.UsingGamepad)
                {
                    Vector2 navigatorDirections    = PlayerInput.Triggers.Current.GetNavigatorDirections();
                    Vector2 gamepadThumbstickLeft  = PlayerInput.GamepadThumbstickLeft;
                    Vector2 gamepadThumbstickRight = PlayerInput.GamepadThumbstickRight;
                    if (navigatorDirections == Vector2.Zero && gamepadThumbstickLeft.Length() < 0.05f && gamepadThumbstickRight.Length() < 0.05f)
                    {
                        MousePoss = player.Center + new Vector2(player.direction * 1000, 0f);
                    }
                }
                Vector2 MouseDir     = MousePoss - player.Center;
                int     leftOrRight  = Math.Sign(MouseDir.X);
                int     avobeOrBelow = Math.Sign(MouseDir.Y);
                if (Math.Abs(MouseDir.X) > Math.Abs(MouseDir.Y) * 3f)
                {
                    avobeOrBelow = 0;
                    MousePoss.Y  = player.Center.Y;
                }
                if (Math.Abs(MouseDir.Y) > Math.Abs(MouseDir.X) * 3f)
                {
                    leftOrRight = 0;
                    MousePoss.X = player.Center.X;
                }
                int playerTileX = (int)(player.Center.X / 16);
                int playerTileY = (int)(player.Center.Y / 16);
                List <Tuple <int, int> > startTiles = new List <Tuple <int, int> >();
                List <Tuple <int, int> > endTiles   = new List <Tuple <int, int> >();
                int notUpperCorners = avobeOrBelow == -1 && leftOrRight != 0 ? -1 : 1;
                int sideSearchX     = (int)((player.position.X + player.width / 2 + ((player.width / 2 - 1) * leftOrRight)) / 16);
                int topSearchY      = (int)(notUpperCorners == -1 ? (player.position.Y + player.height - 1f) / 16 : (player.position.Y + 0.1f) / 16);
                int widthInTiles    = player.width / 16 + ((player.width % 16 == 0) ? 0 : 1);
                int heightInTiles   = player.height / 16 + ((player.height % 16 == 0) ? 0 : 1);
                if (leftOrRight != 0)
                {
                    for (int i = 0; i < heightInTiles; i++)
                    {
                        if (Main.tile[sideSearchX, topSearchY + i * notUpperCorners] == null)
                        {
                            return;
                        }

                        startTiles.Add(new Tuple <int, int>(sideSearchX, topSearchY + i * notUpperCorners));
                    }
                }
                if (avobeOrBelow != 0)
                {
                    for (int i = 0; i < widthInTiles; i++)
                    {
                        if (Main.tile[(int)(player.position.X / 16f) + i, topSearchY] == null)
                        {
                            return;
                        }

                        startTiles.Add(new Tuple <int, int>((int)(player.position.X / 16f) + i, topSearchY));
                    }
                }
                int endX = (int)((MousePoss.X + ((player.width / 2 - 1) * leftOrRight)) / 16);
                int endY = (int)((MousePoss.Y + 0.1f - (player.height / 2 + 1)) / 16);
                if (notUpperCorners == -1)
                {
                    endY = (int)((MousePoss.Y + (player.height / 2) - 1f) / 16);
                }

                if (player.gravDir == -1f && avobeOrBelow == 0)
                {
                    endY++;
                }

                if (endY < 10)
                {
                    endY = 10;
                }

                if (endY > Main.maxTilesY - 10)
                {
                    endY = Main.maxTilesY - 10;
                }

                if (leftOrRight != 0)
                {
                    for (int i = 0; i < heightInTiles; i++)
                    {
                        if (Main.tile[endX, endY + i * notUpperCorners] == null)
                        {
                            return;
                        }

                        endTiles.Add(new Tuple <int, int>(endX, endY + i * notUpperCorners));
                    }
                }
                if (avobeOrBelow != 0)
                {
                    for (int i = 0; i < widthInTiles; i++)
                    {
                        if (Main.tile[(int)((MousePoss.X - (player.width / 2)) / 16f) + i, endY] == null)
                        {
                            return;
                        }

                        endTiles.Add(new Tuple <int, int>((int)((MousePoss.X - (player.width / 2)) / 16f) + i, endY));
                    }
                }
                List <Tuple <int, int> > validTiles = new List <Tuple <int, int> >();
                while (startTiles.Count > 0)
                {
                    Tuple <int, int> tuple  = startTiles[0];
                    Tuple <int, int> tuple2 = endTiles[0];
                    if (!Collision.TupleHitLine(tuple.Item1, tuple.Item2, tuple2.Item1, tuple2.Item2, leftOrRight * (int)player.gravDir, -avobeOrBelow * (int)player.gravDir, grapledTiles, out Tuple <int, int> tuple3))
                    {
                        startTiles.Remove(tuple);
                        endTiles.Remove(tuple2);
                    }
                    else
                    {
                        if (tuple3.Item1 != tuple2.Item1 || tuple3.Item2 != tuple2.Item2)
                        {
                            validTiles.Add(tuple3);
                        }

                        Tile tile2 = Main.tile[tuple3.Item1, tuple3.Item2];
                        if (!tile2.inActive() && tile2.active() && Main.tileSolid[tile2.type] && !Main.tileSolidTop[tile2.type] && !grapledTiles.Contains(tuple3))
                        {
                            validTiles.Add(tuple3);
                        }

                        startTiles.Remove(tuple);
                        endTiles.Remove(tuple2);
                    }
                }
                List <Tuple <int, int> > tilesToRemove = new List <Tuple <int, int> >();
                for (int i = 0; i < validTiles.Count; i++)
                {
                    if (DigPower(Main.tile[validTiles[i].Item1, validTiles[i].Item2].type, shovel) < 100 || !WorldGen.CanKillTile(validTiles[i].Item1, validTiles[i].Item2))
                    {
                        tilesToRemove.Add(validTiles[i]);
                    }
                }

                for (int i = 0; i < tilesToRemove.Count; i++)
                {
                    validTiles.Remove(tilesToRemove[i]);
                }

                tilesToRemove.Clear();
                if (validTiles.Count > 0)
                {
                    float            distance = -1f;
                    Tuple <int, int> tuple4   = validTiles[0];
                    for (int i = 0; i < validTiles.Count; i++)
                    {
                        float cdistance = Vector2.DistanceSquared(new Vector2(validTiles[i].Item1, validTiles[i].Item2) * 16 + Vector2.One * 8, player.Center);
                        if (distance == -1f || cdistance < distance)
                        {
                            distance = cdistance;
                            tuple4   = validTiles[i];
                        }
                    }
                    if (Collision.InTileBounds(tuple4.Item1, tuple4.Item2, minX, minY, maxX, maxY))
                    {
                        smartX = tuple4.Item1;
                        smartY = tuple4.Item2;
                    }
                }
                startTiles.Clear();
                endTiles.Clear();
                validTiles.Clear();
                if (smartX != -1 && smartY != -1)
                {
                    Main.SmartCursorX       = (Player.tileTargetX = smartX);
                    Main.SmartCursorY       = (Player.tileTargetY = smartY);
                    Main.SmartCursorShowing = true;
                }
                grapledTiles.Clear();
            }
            catch
            {
                Main.SmartCursorEnabled = false;
            }
        }