Advertisement
sdini

farmingbotsourceMarch13_10:00pm

Mar 13th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.51 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using Storm.ExternalEvent;
  5. using Storm.StardewValley;
  6. using Storm.StardewValley.Event;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Storm.StardewValley.Wrapper;
  13. using Storm.StardewValley.Proxy;
  14. using System.Collections;
  15. using Storm.StardewValley.Accessor;
  16. using System.Diagnostics;
  17.  
  18. namespace FarmingBotMod
  19. {
  20.     [Mod]
  21.     public class FarmingBotMod : DiskResource
  22.     {
  23.         public static List<Vector2> savedChestLocations = new List<Vector2>();// create list of chest locations
  24.         public static List<Crop> savedCrops = new List<Crop>();
  25.         public static List<Chest> savedChest = new List<Chest>();
  26.         public static int chestCounter1 = 0;
  27.  
  28.         // bots crafting ingrediants 1 iridium ingot, 50 coal, 1 battery, 1 refined crystal
  29.         // bot starts with 50 coal
  30.         // setting bot down
  31.         // allow interaction of the bot
  32.         // configuration of the bot 3x3 and 5x5
  33.         // bot needs coal to run
  34.         // bot uses coal every 7.5 seconds
  35.  
  36.         [Subscribe]
  37.         public void PreNewDayEventCallback(PreNewDayEvent @event)
  38.         {
  39.             //reset all to 0
  40.             chestCounter1 = 0;
  41.             savedChestLocations.Clear();
  42.             savedCrops.Clear();
  43.             Console.WriteLine(savedChestLocations.Count + " <= number should be 0");
  44.         }
  45.  
  46.         [Subscribe]
  47.         public void PreObjectDayUpdateCallback(PreObjectDayUpdateEvent @event)// using as a place holder
  48.         {
  49.             //get location
  50.             Storm.StardewValley.Wrapper.ObjectItem obj = @event.This;
  51.             GameLocation location = @event.ArgLocation;
  52.            
  53.  
  54.             // chest locations to search
  55.             //Microsoft.Xna.Framework.Vector2 chestLocations = new Vector2();
  56.  
  57.             int savedCounter = 0;
  58.             //int chestCounter = 0;
  59.  
  60.             //Dictionary<int, Vector2> savedLocationHarvestableCrops = new Dictionary<int, Vector2>(); // using dictionary as a list
  61.             var savedLocationHarvestableCrops = new List<Vector2>(); // create list to store tileLocations that are plants that have product.
  62.             //var savedChestLocations = new List<Vector2>();// create list of chest locations
  63.  
  64.             // bot senses when crops are done
  65.            
  66.             //check if you are on the farm
  67.             if (location.Name == "Farm")
  68.             {
  69.  
  70.                 //check if chest is behind bot
  71.                 if (obj.Name == "Chest")
  72.                 {
  73.                     Microsoft.Xna.Framework.Vector2 chestLocation = new Vector2();
  74.                     savedChest.Add(obj.As<ChestAccessor, Chest>());
  75.                     chestLocation = obj.TileLocation;
  76.                     chestLocation.X = obj.BoundingBox.X / 64;
  77.                     chestLocation.Y = obj.BoundingBox.Y / 64;
  78.  
  79.                     //if (location.Objects.ContainsKey(chestLocation) && location.Objects[chestLocation].Is<ChestAccessor>())
  80.                     //{
  81.                     //try
  82.                         //{
  83.                             //Storm.StardewValley.Wrapper.Chest obj2 = location.Objects[chestLocation].As<ChestAccessor, Chest>();
  84.                             //savedChest.Add(obj2);
  85.  
  86.                        // } catch (Exception e)
  87.                         //{
  88.                            // Console.WriteLine();
  89.                             //Console.WriteLine("Tray catch 1");
  90.                             //Console.WriteLine(e);
  91.                             //Console.WriteLine();
  92.                         //}
  93.                         //Debugger.Break();
  94.                         //find locations
  95.                        
  96.                         //debugging
  97.                         //chestLocation.X = obj.BoundingBox.X;
  98.                         //chestLocation.Y = obj.BoundingBox.Y;
  99.                         //chestLocation.X = obj.BoundingBox.X / 64;
  100.                         //chestLocation.Y = obj.BoundingBox.Y / 64;
  101.                        
  102.  
  103.                         //debuging
  104.                         Console.WriteLine();
  105.                         Console.WriteLine(chestCounter1 + 1 + " individual saved chest information");
  106.  
  107.                         try
  108.                         {
  109.                             //Console.WriteLine(string.Join(", ", savedChest[chestCounter1].Items));
  110.                            
  111.                             //Console.WriteLine("testing Chest");
  112.                         }
  113.                         catch (Exception e)
  114.                         {
  115.                             Console.WriteLine("Tray catch 2");
  116.                             Console.WriteLine();
  117.                             Console.WriteLine(e);
  118.                             Console.WriteLine();
  119.                         }
  120.                        
  121.  
  122.                         //Console.WriteLine(location.Name);
  123.                         //Console.WriteLine("TILELOCATION x is " + obj.TileLocation.X + ". y is " + obj.TileLocation.Y);
  124.                         //Console.WriteLine("BOUNDINGBOX x is " + obj.BoundingBox.X + ". y is " + obj.BoundingBox.Y);
  125.                         //Console.WriteLine("Width is " + obj.BoundingBox.Width + ". Height is " + obj.BoundingBox.Height);
  126.                         //Console.WriteLine();
  127.  
  128.                         //Console.WriteLine("obj location at " + obj.BoundingBox.X + ", " + obj.BoundingBox.Y);
  129.                         //Console.WriteLine("obj location at " + obj.BoundingBox.X / 64 + ", " + obj.BoundingBox.Y / 64);
  130.  
  131.                         savedChestLocations.Add(chestLocation);
  132.                         Console.WriteLine("----------------");
  133.                         Console.WriteLine("Finding chests and saving location at " + chestLocation.X + ", " + chestLocation.Y);
  134.                         Console.WriteLine("----------------");
  135.  
  136.  
  137.                         Console.WriteLine(savedChestLocations.Count + " total chests found");
  138.                         ++chestCounter1;
  139.                         //Debugger.Break();
  140.                     //}
  141.                 }
  142.  
  143.  
  144.                 if (obj.Name == "Hardwood Fence")
  145.                 {
  146.                     //debugging
  147.                     Console.WriteLine();
  148.                     Console.WriteLine("----------New day Start---------");
  149.  
  150.                     // find location of the bot and save the default location
  151.                     Microsoft.Xna.Framework.Vector2 tileLocation = obj.TileLocation;
  152.                     Microsoft.Xna.Framework.Vector2 defLocation = tileLocation;
  153.  
  154.  
  155.  
  156.                     // tileLocation is where the search for crops start
  157.                     // 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
  158.                     // an increase in y is lower on the screen. ex plots start top left at 0,0 to bottom right at 9999,9999
  159.                     tileLocation.X -= 4;
  160.                     tileLocation.Y += 1;
  161.  
  162.                     //debugging
  163.                     //Console.WriteLine("First Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
  164.                     //Console.WriteLine("default Location for x = " + defLocation.X + " default Location for y = " + defLocation.Y);
  165.  
  166.                     int counter = 0;
  167.                     int actuallyHarvestableCropsCounter = 0;
  168.  
  169.                     //counts the crops to out put for debugging
  170.                     int cropCounter = 0;
  171.                     //count the crops
  172.  
  173.                     //debugging
  174.                     //Console.WriteLine("before running the while statement.");
  175.  
  176.  
  177.                     while (counter < 5)
  178.                     {
  179.                         //debugging
  180.                         Console.WriteLine("Starting Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
  181.  
  182.                         //debugging
  183.                         /*Console.WriteLine("before running the if statement.");
  184.                         Console.WriteLine(location.TerrainFeatures.ContainsKey(tileLocation));
  185.                         try
  186.                         {
  187.                             //Console.WriteLine(location.TerrainFeatures[tileLocation].IsHoeDirt()); //old
  188.                             Console.WriteLine(location.TerrainFeatures[tileLocation].Is<HoeDirtAccessor>());
  189.                         }
  190.                         catch
  191.                         {
  192.                             Console.WriteLine("Not hoedirt");
  193.                         }*/
  194.  
  195.                         //HoeDirt[] hoeDirts = location.TerrainFeatures.Where(x => x.Value. is HoeDirtAccessor).ToArray<HoeDirt>();
  196.                         //ObjectItem[] items = location.Objects.Where(x => x.Value is ObjectAccessor).ToList<ObjectItem>; // not correct
  197.  
  198.                         //Console.WriteLine(hoeDirts[0].ToString());
  199.  
  200.                         Console.WriteLine("Before verifiying if tileLocation is hoeDirt.");
  201.  
  202.                         //if (location.TerrainFeatures.ContainsKey(tileLocation) && location.TerrainFeatures[tileLocation].IsHoeDirt()) // old
  203.                         if (location.TerrainFeatures.ContainsKey(tileLocation) && location.TerrainFeatures[tileLocation].Is<HoeDirtAccessor>())
  204.                         {
  205.                             Console.WriteLine("Succesfully verified hoeDirt");
  206.  
  207.                             HoeDirt crop = location.TerrainFeatures[tileLocation].As<HoeDirtAccessor, HoeDirt>();
  208.  
  209.                             //degbuging
  210.                             //Console.WriteLine("Before checking crops is true or not.");
  211.  
  212.                             //check if crops are fully grown
  213.                             try
  214.                             {
  215.                                
  216.                                 // check if fully grown
  217.                                 if (crop.Crop.IsFullyGrown == true)
  218.                                 {
  219.                                     Console.WriteLine("Found fully grown crop.");
  220.                                     ++cropCounter;
  221.  
  222.                                     // if fully grown check if harvestable
  223.                                     if (crop.Crop.CurrentPhase >= crop.Crop.PhaseDays.Count - 1 && (!crop.Crop.IsFullyGrown || crop.Crop.DayOfCurrentPhase <= 0))
  224.                                     {
  225.                                         Console.WriteLine("Found a crop that can be harvested located at " + tileLocation.X + " and " + tileLocation.Y);
  226.                                         ++actuallyHarvestableCropsCounter;
  227.  
  228.                                         // save location
  229.                                         savedLocationHarvestableCrops.Add(tileLocation);
  230.                                         savedCrops.Add(crop.Crop);
  231.                                        
  232.  
  233.                                         ++savedCounter;
  234.                                     }
  235.                                 }
  236.                             }
  237.                             catch (Exception e) //(System.NullReferenceException) this crashes the game when ran in concecutive days
  238.                             {
  239.                                 Console.WriteLine("Tray catch 3");
  240.                                 Console.WriteLine("--------------------------");
  241.                                 Console.WriteLine(e);
  242.                                 Console.WriteLine("hoedirt but no crop");
  243.                                 Console.WriteLine("--------------------------");
  244.                             }
  245.                             //debuging
  246.                             //Console.WriteLine("After checking if crops are true or not.");
  247.                         }
  248.  
  249.                         //debugging
  250.                         //Console.WriteLine("before check 1");
  251.  
  252.                         // check if tileLocation is equal to default location, x will go to default x, y will go to default y + 5
  253.                         // may not be needed
  254.                         if (tileLocation.X == defLocation.X && tileLocation.Y == defLocation.Y + 5)
  255.  
  256.                         {
  257.                             //debugging
  258.                             //Console.WriteLine("Finished checking tiles");
  259.  
  260.                             ++counter;
  261.                         }
  262.                         //check if tilelocation.x is the default location
  263.                         else if (tileLocation.X == defLocation.X)
  264.                         {
  265.                             tileLocation.X -= 4;
  266.                             tileLocation.Y += 1;
  267.                             counter = 0;
  268.  
  269.                             //debugging
  270.                             //Console.WriteLine("reseting pos to 1 down 4 to the left");
  271.                         }
  272.                         // if nether previous are true continue counting.
  273.                         else
  274.                         {
  275.                             //debugging
  276.                             //Console.WriteLine("inside check 3");
  277.                             tileLocation.X += 1;
  278.                             ++counter;
  279.                         }
  280.                         //debugging
  281.                         //Console.WriteLine("after check 3");
  282.                         //debugging
  283.                         //Console.WriteLine("Ending Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
  284.                     }
  285.  
  286.  
  287.  
  288.                     Console.WriteLine(cropCounter + " total fully grown crops and " + actuallyHarvestableCropsCounter + " total crops ready to be harvested.");
  289.  
  290.                     //harvest find chest
  291.                     //check if chest is behind
  292.                     if (savedChestLocations.Count > 0)// && (obj.TileLocation.X == defLocation.X && obj.TileLocation.Y == defLocation.Y - 1))
  293.                     {
  294.                         int c = 0;
  295.                         while (c < savedChestLocations.Count)
  296.                         {
  297.                             //debuging
  298.                             Console.WriteLine();
  299.                             Console.WriteLine(savedChestLocations[c].Y);
  300.                             Console.WriteLine(defLocation.Y - 1);
  301.                             Console.WriteLine();
  302.  
  303.                             if (savedChestLocations[c].X == defLocation.X && savedChestLocations[c].Y == defLocation.Y - 1)
  304.                             {
  305.                                 Console.WriteLine("There is a chest behind the bot located at " + defLocation.X);
  306.  
  307.                                 try
  308.                                 {
  309.                                     Console.WriteLine("Adding to chest");
  310.                                     foreach (Crop crop in savedCrops)
  311.                                     {
  312.                                         var itemDelegate = new StandardObjectDelegate(Vector2.Zero, crop.IndexOfHarvest, 1);
  313.                                         var nItemDelegate = @event.Proxy<ObjectAccessor, ObjectItem>(itemDelegate, new ObjectItem());
  314.                                         savedChest[c].Items.Add(nItemDelegate);
  315.                                         Debugger.Break();
  316.                                     }
  317.                                 }catch (Exception e)
  318.                                 {
  319.                                     Console.WriteLine(e);
  320.                                 }
  321.                             }
  322.                             ++c;
  323.                         }
  324.                     }
  325.                     @event.ReturnEarly = true;
  326.                 }
  327.  
  328.  
  329.  
  330.  
  331.                 //debugging
  332.                 /*
  333.                 if (savedCounter != 0)
  334.                 {
  335.                     int test = 0;
  336.                     Console.WriteLine();
  337.                     Console.WriteLine(savedCounter + " saved locations");
  338.  
  339.                     while (test < savedCounter)
  340.                     {
  341.  
  342.                         Console.WriteLine("{0}, {1}", savedLocationHarvestableCrops[test].X, savedLocationHarvestableCrops[test].Y);
  343.                         ++test;
  344.                     }
  345.                 }*///end debugging
  346.  
  347.             }
  348.         }
  349.  
  350.         [Subscribe]
  351.         public void PostNewDayCallback(PostNewDayEvent @event)
  352.         {
  353.             Console.WriteLine(savedChestLocations.Count + " Total chests found");    
  354.         }
  355.  
  356.             // bot finds a path to crops (going around sprinklers and scarecrows)
  357.             // bot farms the crops and contains them in a chest.
  358.             // bot return to the spot it was placed.
  359.  
  360.  
  361.         }
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement