private void waterAllFields(object sender, EventArgs e)
        {
            GameLocation farm    = Game1.getFarm();
            int          watered = 0;

            foreach (var tf in farm.terrainFeatures)
            {
                TerrainFeature feature = tf.Value;
                if (feature is HoeDirt)
                {
                    this.Monitor.Log("test");
                    watered++;
                    StardewValley.Tools.WateringCan wc = Game1.player.items.OfType <StardewValley.Tools.WateringCan>().FirstOrDefault();
                    feature.performToolAction(wc, 0, tf.Key);
                }
            }
            this.Monitor.Log("Watered " + watered + " tiles");
        }
        public override IActiveEffect onCast(Farmer player, int level, int targetX, int targetY)
        {
            level   += 1;
            targetX /= Game1.tileSize;
            targetY /= Game1.tileSize;
            Vector2 target = new Vector2(targetX, targetY);

            Tool dummyAxe  = new Axe(); dummyAxe.UpgradeLevel = level;
            Tool dummyPick = new Pickaxe(); dummyPick.UpgradeLevel = level;

            Mod.instance.Helper.Reflection.GetField <Farmer>(dummyAxe, "lastUser").SetValue(player);
            Mod.instance.Helper.Reflection.GetField <Farmer>(dummyPick, "lastUser").SetValue(player);

            GameLocation loc = player.currentLocation;

            for (int ix = targetX - level; ix <= targetX + level; ++ix)
            {
                for (int iy = targetY - level; iy <= targetY + level; ++iy)
                {
                    if (player.getCurrentMana() <= 0)
                    {
                        return(null);
                    }

                    Vector2 pos = new Vector2(ix, iy);

                    if (loc.objects.ContainsKey(pos))
                    {
                        var obj = loc.objects[pos];
                        if (obj.performToolAction(dummyAxe, loc))
                        {
                            if (obj.Type == "Crafting" && obj.Fragility != 2)
                            {
                                loc.debris.Add(new Debris(obj.bigCraftable.Value ? -obj.ParentSheetIndex : obj.ParentSheetIndex, pos, pos));
                            }
                            obj.performRemoveAction(pos, loc);
                            loc.objects.Remove(pos);
                            player.addMana(-3);
                            player.AddCustomSkillExperience(Magic.Skill, 1);
                        }
                        else
                        {
                            var oldStam = player.stamina;
                            dummyPick.DoFunction(loc, ix * Game1.tileSize, iy * Game1.tileSize, 0, player);
                            player.stamina = oldStam;
                            player.addMana(-3);
                            player.AddCustomSkillExperience(Magic.Skill, 1);
                        }
                    }

                    // Trees
                    if (level >= 2)
                    {
                        if (loc.terrainFeatures.ContainsKey(pos) && !(loc.terrainFeatures[pos] is HoeDirt))
                        {
                            TerrainFeature tf = loc.terrainFeatures[pos];
                            if (tf is Tree)
                            {
                                player.addMana(-3);
                            }
                            if (tf.performToolAction(dummyAxe, 0, pos, loc) || tf is Grass || (tf is Tree && tf.performToolAction(dummyAxe, 0, pos, loc)))
                            {
                                if (tf is Tree)
                                {
                                    player.AddCustomSkillExperience(Magic.Skill, 5);
                                }
                                loc.terrainFeatures.Remove(pos);
                            }
                            if (tf is Grass && loc is Farm)
                            {
                                (loc as Farm).tryToAddHay(1);
                                Game1.playSound("swordswipe");
                                loc.temporarySprites.Add(new TemporaryAnimatedSprite(28, pos * (float)Game1.tileSize + new Vector2((float)Game1.random.Next(-Game1.pixelZoom * 4, Game1.pixelZoom * 4), (float)Game1.random.Next(-Game1.pixelZoom * 4, Game1.pixelZoom * 4)), Color.Green, 8, Game1.random.NextDouble() < 0.5, (float)Game1.random.Next(60, 100), 0, -1, -1f, -1, 0));
                                loc.temporarySprites.Add(new TemporaryAnimatedSprite(Game1.objectSpriteSheetName, Game1.getSourceRectForStandardTileSheet(Game1.objectSpriteSheet, 178, 16, 16), 750f, 1, 0, player.position - new Vector2(0.0f, (float)(Game1.tileSize * 2)), false, false, player.position.Y / 10000f, 0.005f, Color.White, (float)Game1.pixelZoom, -0.005f, 0.0f, 0.0f, false)
                                {
                                    motion     = { Y = -1f },
                                    layerDepth = (float)(1.0 - (double)Game1.random.Next(100) / 10000.0),
                                    delayBeforeAnimationStart = Game1.random.Next(350)
                                });
                            }
                        }
                    }

                    if (level >= 3)
                    {
                        ICollection <ResourceClump> clumps = (NetCollection <ResourceClump>)loc.GetType().GetField("resourceClumps", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(loc);
                        if (loc is Woods)
                        {
                            clumps = (loc as Woods).stumps;
                        }
                        if (clumps != null)
                        {
                            foreach (var rc in clumps)
                            {
                                if (new Rectangle((int)rc.tile.X, (int)rc.tile.Y, rc.width.Value, rc.height.Value).Contains(ix, iy))
                                {
                                    player.addMana(-3);
                                    if (rc.performToolAction(dummyAxe, 1, pos, loc) || rc.performToolAction(dummyPick, 1, pos, loc))
                                    {
                                        clumps.Remove(rc);
                                        player.AddCustomSkillExperience(Magic.Skill, 10);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 3
0
        //this was almost entirely stolen from spacechase0 with very little contribution on my part.
        internal static void HandleToolTransmute(Tool tool)
        {
            int alchemyLevel = (EquivalentExchange.IsShiftKeyPressed() ? 0 : Alchemy.GetToolTransmuteRadius());
            int toolLevel    = tool.UpgradeLevel;

            //set last user to dodge a null pointer
            var toolPlayerFieldReflector = tool.GetType().GetField("lastUser", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            toolPlayerFieldReflector.SetValue(tool, Game1.player);

            Point hitLocation = GetMouseHitLocation();

            GameLocation location = Game1.player.currentLocation;

            bool performedAction = false;

            //getting this out of the way, helps with easily determining tool types
            bool isScythe      = tool is MeleeWeapon && tool.Name.ToLower().Contains("scythe");
            bool isAxe         = tool is StardewValley.Tools.Axe;
            bool isPickaxe     = tool is StardewValley.Tools.Pickaxe;
            bool isHoe         = tool is StardewValley.Tools.Hoe;
            bool isWateringCan = tool is StardewValley.Tools.WateringCan;

            for (int xOffset = -alchemyLevel; xOffset <= alchemyLevel; xOffset++)
            {
                for (int yOffset = -alchemyLevel; yOffset <= alchemyLevel; yOffset++)
                {
                    if (!isScythe)
                    {
                        if (!IsCapableOfWithstandingToolTransmuteCost(Game1.player, 2F))
                        {
                            return;
                        }
                    }

                    Vector2 offsetPosition = new Vector2(xOffset + hitLocation.X, yOffset + hitLocation.Y);

                    if (location.objects.ContainsKey(offsetPosition))
                    {
                        if (isAxe || isScythe || isPickaxe || isHoe)
                        {
                            var snapshotPlayerExperience = Game1.player.experiencePoints;
                            performedAction = DoToolFunction(location, Game1.player, tool, (int)offsetPosition.X, (int)offsetPosition.Y);
                            RestorePlayerExperience(snapshotPlayerExperience);
                            if (performedAction && !isScythe)
                            {
                                HandleToolTransmuteConsequence(2F);
                            }
                        }
                    }
                    else if (location.terrainFeatures.ContainsKey(offsetPosition))
                    {
                        //a terrain feature, rather than a tool check, might respond to the tool
                        TerrainFeature terrainFeature = location.terrainFeatures[offsetPosition];

                        //don't break stumps unless the player is in precision mode.
                        if (terrainFeature is Tree && isAxe && (!(terrainFeature as Tree).stump || EquivalentExchange.IsShiftKeyPressed()))
                        {
                            Netcode.NetArray <int, Netcode.NetInt> snapshotPlayerExperience = Game1.player.experiencePoints;
                            //trees get removed automatically
                            performedAction = DoToolFunction(location, Game1.player, tool, (int)offsetPosition.X, (int)offsetPosition.Y);
                            RestorePlayerExperience(snapshotPlayerExperience);

                            if (performedAction)
                            {
                                HandleToolTransmuteConsequence(2F);
                            }
                        }
                        else if (terrainFeature is Grass && location is Farm && isScythe)
                        {
                            int oldHay = (location as Farm).piecesOfHay;
                            var snapshotPlayerExperience = Game1.player.experiencePoints;
                            if (terrainFeature.performToolAction(tool, 0, offsetPosition, location))
                            {
                                location.terrainFeatures.Remove(offsetPosition);
                                //HandleToolTransmuteConsequence(); Scythe transmute is special and doesn't cost anything, but you don't get experience.
                                performedAction = true;
                            }
                            RestorePlayerExperience(snapshotPlayerExperience);

                            //hay get! spawn the sprite animation for acquisition of hay
                            if (oldHay < (location as Farm).piecesOfHay)
                            {
                                SpawnHayAnimationSprite(location, offsetPosition, Game1.player);
                            }
                        }
                        else if (terrainFeature is HoeDirt && isWateringCan && (tool as WateringCan).WaterLeft > 0)
                        {
                            //state of 0 is unwatered.
                            if ((terrainFeature as HoeDirt).state != 1)
                            {
                                var snapshotPlayerExperience = Game1.player.experiencePoints;
                                terrainFeature.performToolAction(tool, 0, offsetPosition, location);
                                RestorePlayerExperience(snapshotPlayerExperience);
                                (tool as WateringCan).WaterLeft = (tool as WateringCan).WaterLeft - 1;
                                SpawnWateringCanAnimationSprite(location, offsetPosition);
                                HandleToolTransmuteConsequence(2F);
                                performedAction = true;
                            }
                        }
                        else if (isPickaxe && terrainFeature is HoeDirt)
                        {
                            var snapshotPlayerExperience = Game1.player.experiencePoints;
                            performedAction = DoToolFunction(location, Game1.player, tool, (int)offsetPosition.X, (int)offsetPosition.Y);
                            RestorePlayerExperience(snapshotPlayerExperience);

                            if (performedAction)
                            {
                                HandleToolTransmuteConsequence(2F);
                            }
                        }
                    }
                    else if ((isPickaxe || isAxe))
                    {
                        ICollection <ResourceClump> largeResourceClusters = null;
                        if (location is Farm)
                        {
                            largeResourceClusters = (NetCollection <ResourceClump>)location.GetType().GetField("resourceClumps", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(location);
                        }
                        else if (location is MineShaft)
                        {
                            largeResourceClusters = (NetObjectList <ResourceClump>)location.GetType().GetField("resourceClumps", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(location);
                        }
                        else if (location is Woods)
                        {
                            largeResourceClusters = (location as Woods).stumps;
                        }
                        DoLargeResourceClusterAction(largeResourceClusters, tool, offsetPosition, performedAction);
                    }
                    else if (isHoe)
                    {
                        var snapshotPlayerExperience = Game1.player.experiencePoints;
                        performedAction = DoToolFunction(location, Game1.player, tool, (int)offsetPosition.X, (int)offsetPosition.Y);
                        RestorePlayerExperience(snapshotPlayerExperience);

                        if (performedAction)
                        {
                            HandleToolTransmuteConsequence(2F);
                        }
                    }
                }
            }

            if (performedAction)
            {
                SoundUtil.PlayMagickySound();
            }
        }