Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Storm.ExternalEvent;
- using Storm.StardewValley;
- using Storm.StardewValley.Event;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Storm.StardewValley.Wrapper;
- using Storm.StardewValley.Proxy;
- using System.Collections;
- using Storm.StardewValley.Accessor;
- using System.Diagnostics;
- namespace FarmingBotMod
- {
- [Mod]
- public class FarmingBotMod : DiskResource
- {
- public static List<Vector2> savedChestLocations = new List<Vector2>();// create list of chest locations
- public static List<Crop> savedCrops = new List<Crop>();
- public static List<Chest> savedChest = new List<Chest>();
- public static List<Chest> chestToBeUsed = new List<Chest>();
- public static int canHarvest = 0;
- public static int chestCounter1 = 0;
- // bots crafting ingrediants 1 iridium ingot, 50 coal, 1 battery, 1 refined crystal
- // bot starts with 50 coal
- // setting bot down
- // allow interaction of the bot
- // configuration of the bot 3x3 and 5x5
- // bot needs coal to run
- // bot uses coal every 7.5 seconds
- [Subscribe]
- public void PreNewDayEventCallback(PreNewDayEvent @event)
- {
- //reset all to 0
- chestCounter1 = 0;
- savedChestLocations.Clear();
- savedCrops.Clear();
- Console.WriteLine(savedChestLocations.Count + " <= number should be 0");
- }
- [Subscribe]
- public void PreObjectDayUpdateCallback(PreObjectDayUpdateEvent @event)// using as a place holder
- {
- //get location
- Storm.StardewValley.Wrapper.ObjectItem obj = @event.This;
- GameLocation location = @event.ArgLocation;
- // chest locations to search
- //Microsoft.Xna.Framework.Vector2 chestLocations = new Vector2();
- int savedCounter = 0;
- //int chestCounter = 0;
- //Dictionary<int, Vector2> savedLocationHarvestableCrops = new Dictionary<int, Vector2>(); // using dictionary as a list
- var savedLocationHarvestableCrops = new List<Vector2>(); // create list to store tileLocations that are plants that have product.
- //var savedChestLocations = new List<Vector2>();// create list of chest locations
- // bot senses when crops are done
- //check if you are on the farm
- if (location.Name == "Farm")
- {
- //check if chest is behind bot
- if (obj.Name == "Chest")
- {
- Microsoft.Xna.Framework.Vector2 chestLocation = new Vector2();
- savedChest.Add(obj.As<ChestAccessor, Chest>());
- chestLocation = obj.TileLocation;
- chestLocation.X = obj.BoundingBox.X / 64;
- chestLocation.Y = obj.BoundingBox.Y / 64;
- //if (location.Objects.ContainsKey(chestLocation) && location.Objects[chestLocation].Is<ChestAccessor>())
- //{
- //try
- //{
- //Storm.StardewValley.Wrapper.Chest obj2 = location.Objects[chestLocation].As<ChestAccessor, Chest>();
- //savedChest.Add(obj2);
- // } catch (Exception e)
- //{
- // Console.WriteLine();
- //Console.WriteLine("Tray catch 1");
- //Console.WriteLine(e);
- //Console.WriteLine();
- //}
- //Debugger.Break();
- //find locations
- //debugging
- //chestLocation.X = obj.BoundingBox.X;
- //chestLocation.Y = obj.BoundingBox.Y;
- //chestLocation.X = obj.BoundingBox.X / 64;
- //chestLocation.Y = obj.BoundingBox.Y / 64;
- //debuging
- Console.WriteLine();
- Console.WriteLine(chestCounter1 + 1 + " individual saved chest information");
- try
- {
- //Console.WriteLine(string.Join(", ", savedChest[chestCounter1].Items));
- //Console.WriteLine("testing Chest");
- }
- catch (Exception e)
- {
- Console.WriteLine("Tray catch 2");
- Console.WriteLine();
- Console.WriteLine(e);
- Console.WriteLine();
- }
- //Console.WriteLine(location.Name);
- //Console.WriteLine("TILELOCATION x is " + obj.TileLocation.X + ". y is " + obj.TileLocation.Y);
- //Console.WriteLine("BOUNDINGBOX x is " + obj.BoundingBox.X + ". y is " + obj.BoundingBox.Y);
- //Console.WriteLine("Width is " + obj.BoundingBox.Width + ". Height is " + obj.BoundingBox.Height);
- //Console.WriteLine();
- //Console.WriteLine("obj location at " + obj.BoundingBox.X + ", " + obj.BoundingBox.Y);
- //Console.WriteLine("obj location at " + obj.BoundingBox.X / 64 + ", " + obj.BoundingBox.Y / 64);
- savedChestLocations.Add(chestLocation);
- Console.WriteLine("----------------");
- Console.WriteLine("Finding chests and saving location at " + chestLocation.X + ", " + chestLocation.Y);
- Console.WriteLine("----------------");
- Console.WriteLine(savedChestLocations.Count + " total chests found");
- ++chestCounter1;
- //Debugger.Break();
- //}
- }
- if (obj.Name == "Hardwood Fence")
- {
- //debugging
- Console.WriteLine();
- Console.WriteLine("----------New day Start---------");
- // find location of the bot and save the default location
- Microsoft.Xna.Framework.Vector2 tileLocation = obj.TileLocation;
- Microsoft.Xna.Framework.Vector2 defLocation = tileLocation;
- // tileLocation is where the search for crops start
- // because we want to cover a 5*5 spread includ ing the first x value. x will start at -= 4 and end at += 4, y will start at -= 1 and end at -= 5
- // an increase in y is lower on the screen. ex plots start top left at 0,0 to bottom right at 9999,9999
- tileLocation.X -= 4;
- tileLocation.Y += 1;
- //debugging
- //Console.WriteLine("First Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
- //Console.WriteLine("default Location for x = " + defLocation.X + " default Location for y = " + defLocation.Y);
- int counter = 0;
- int actuallyHarvestableCropsCounter = 0;
- //counts the crops to out put for debugging
- int cropCounter = 0;
- //count the crops
- //debugging
- //Console.WriteLine("before running the while statement.");
- while (counter < 5)
- {
- //debugging
- Console.WriteLine("Starting Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
- //debugging
- /*Console.WriteLine("before running the if statement.");
- Console.WriteLine(location.TerrainFeatures.ContainsKey(tileLocation));
- try
- {
- //Console.WriteLine(location.TerrainFeatures[tileLocation].IsHoeDirt()); //old
- Console.WriteLine(location.TerrainFeatures[tileLocation].Is<HoeDirtAccessor>());
- }
- catch
- {
- Console.WriteLine("Not hoedirt");
- }*/
- //HoeDirt[] hoeDirts = location.TerrainFeatures.Where(x => x.Value. is HoeDirtAccessor).ToArray<HoeDirt>();
- //ObjectItem[] items = location.Objects.Where(x => x.Value is ObjectAccessor).ToList<ObjectItem>; // not correct
- //Console.WriteLine(hoeDirts[0].ToString());
- Console.WriteLine("Before verifiying if tileLocation is hoeDirt.");
- //if (location.TerrainFeatures.ContainsKey(tileLocation) && location.TerrainFeatures[tileLocation].IsHoeDirt()) // old
- if (location.TerrainFeatures.ContainsKey(tileLocation) && location.TerrainFeatures[tileLocation].Is<HoeDirtAccessor>())
- {
- Console.WriteLine("Succesfully verified hoeDirt");
- HoeDirt crop = location.TerrainFeatures[tileLocation].As<HoeDirtAccessor, HoeDirt>();
- //degbuging
- //Console.WriteLine("Before checking crops is true or not.");
- //check if crops are fully grown
- try
- {
- // check if fully grown
- if (crop.Crop.IsFullyGrown == true)
- {
- Console.WriteLine("Found fully grown crop.");
- ++cropCounter;
- // if fully grown check if harvestable
- if (crop.Crop.CurrentPhase >= crop.Crop.PhaseDays.Count - 1 && (!crop.Crop.IsFullyGrown || crop.Crop.DayOfCurrentPhase <= 0))
- {
- Console.WriteLine("Found a crop that can be harvested located at " + tileLocation.X + " and " + tileLocation.Y);
- ++actuallyHarvestableCropsCounter;
- // save location
- savedLocationHarvestableCrops.Add(tileLocation);
- savedCrops.Add(crop.Crop);
- canHarvest = 1;
- ++savedCounter;
- }
- }
- }
- catch (Exception e) //(System.NullReferenceException) this crashes the game when ran in concecutive days
- {
- Console.WriteLine("Tray catch 3");
- Console.WriteLine("--------------------------");
- Console.WriteLine(e);
- Console.WriteLine("hoedirt but no crop");
- Console.WriteLine("--------------------------");
- }
- //debuging
- //Console.WriteLine("After checking if crops are true or not.");
- }
- //debugging
- //Console.WriteLine("before check 1");
- // check if tileLocation is equal to default location, x will go to default x, y will go to default y + 5
- // may not be needed
- if (tileLocation.X == defLocation.X && tileLocation.Y == defLocation.Y + 5)
- {
- //debugging
- //Console.WriteLine("Finished checking tiles");
- ++counter;
- }
- //check if tilelocation.x is the default location
- else if (tileLocation.X == defLocation.X)
- {
- tileLocation.X -= 4;
- tileLocation.Y += 1;
- counter = 0;
- //debugging
- //Console.WriteLine("reseting pos to 1 down 4 to the left");
- }
- // if nether previous are true continue counting.
- else
- {
- //debugging
- //Console.WriteLine("inside check 3");
- tileLocation.X += 1;
- ++counter;
- }
- //debugging
- //Console.WriteLine("after check 3");
- //debugging
- //Console.WriteLine("Ending Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
- }
- Console.WriteLine(cropCounter + " total fully grown crops and " + actuallyHarvestableCropsCounter + " total crops ready to be harvested.");
- //harvest find chest
- //check if chest is behind
- if (savedChestLocations.Count > 0)// && (obj.TileLocation.X == defLocation.X && obj.TileLocation.Y == defLocation.Y - 1))
- {
- int c = 0;
- while (c < savedChestLocations.Count)
- {
- //debuging
- Console.WriteLine();
- Console.WriteLine(savedChestLocations[c].Y);
- Console.WriteLine(defLocation.Y - 1);
- Console.WriteLine();
- if (savedChestLocations[c].X == defLocation.X && savedChestLocations[c].Y == defLocation.Y - 1)
- {
- Console.WriteLine("There is a chest behind the bot located at " + defLocation.X);
- chestToBeUsed.Add(savedChest[c]);
- //try
- //{
- // Console.WriteLine("Adding to chest");
- // foreach (Crop crop in savedCrops)
- // {
- // var itemDelegate = new StandardObjectDelegate(Vector2.Zero, crop.IndexOfHarvest, 1);
- // var nItemDelegate = @event.Proxy<ObjectAccessor, ObjectItem>(itemDelegate, new ObjectItem());
- // savedChest[c].Items.Add(nItemDelegate);
- // Debugger.Break();
- // }
- //}catch (Exception e)
- //{
- // Console.WriteLine(e);
- //}
- }
- ++c;
- }
- }
- @event.ReturnEarly = true;
- }
- //debugging
- /*
- if (savedCounter != 0)
- {
- int test = 0;
- Console.WriteLine();
- Console.WriteLine(savedCounter + " saved locations");
- while (test < savedCounter)
- {
- Console.WriteLine("{0}, {1}", savedLocationHarvestableCrops[test].X, savedLocationHarvestableCrops[test].Y);
- ++test;
- }
- }*///end debugging
- }
- }
- [Subscribe]
- public void PostNewDayCallback(PostNewDayEvent @event)
- {
- Console.WriteLine(savedChestLocations.Count + " Total chests found");
- //try
- //{
- // Console.WriteLine("Adding to chest");
- // foreach (Crop crop in savedCrops)
- // {
- // var itemDelegate = new StandardObjectDelegate(Vector2.Zero, crop.IndexOfHarvest, 1);
- // var nItemDelegate = @event.Proxy<ObjectAccessor, ObjectItem>(itemDelegate, new ObjectItem());
- // chestToBeUsed[0].Items.Add(nItemDelegate);
- // Debugger.Break();
- // }
- //}
- //catch (Exception e)
- //{
- // Console.WriteLine(e);
- //}
- }
- [Subscribe]
- public void WarpFarmerEventCallback(WarpFarmerEvent @event)
- {
- int itemLocation = 0;
- int itemCount = 0;
- var trash = new StandardObjectDelegate(Vector2.Zero, 0, 1);
- var finalItemsToBeAdded = @event.Proxy<ObjectAccessor, ObjectItem>(trash, new ObjectItem());
- Console.WriteLine("testing PostObjectDayUpdateEvent");
- GameLocation location = @event.Location;
- if (location.Name == "Farm")
- {
- if (canHarvest == 1)
- {
- try
- {
- Console.WriteLine("Adding to chest");
- foreach (Crop crop in savedCrops)
- {
- var itemDelegate = new StandardObjectDelegate(Vector2.Zero, crop.IndexOfHarvest, 1);
- var nItemDelegate = @event.Proxy<ObjectAccessor, ObjectItem>(itemDelegate, new ObjectItem());
- finalItemsToBeAdded = nItemDelegate;
- itemCount++;
- //if (chestToBeUsed[0].Items.Contains(nItemDelegate))
- //{
- // itemLocation = chestToBeUsed[0].Items.IndexOf(nItemDelegate);
- // nItemDelegate.Stack += 1;
- // Console.WriteLine(nItemDelegate.Stack + "is the initial items that will be added stack number");
- // finalItemsToBeAdded = nItemDelegate;
- // Console.WriteLine(finalItemsToBeAdded.Stack + "is the final items that will be added stack number");
- //}
- //else
- //{
- // Console.WriteLine("testing else");
- // chestToBeUsed[0].Items.Add(nItemDelegate);
- //}
- Debugger.Break();
- }
- finalItemsToBeAdded.Stack = itemCount;
- finalItemsToBeAdded.Description = "";
- chestToBeUsed[0].Items.Add(finalItemsToBeAdded);
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- canHarvest = 0;
- }
- }
- }
- // bot finds a path to crops (going around sprinklers and scarecrows)
- // bot farms the crops and contains them in a chest.
- // bot return to the spot it was placed.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement