Advertisement
sdini

FarmBotSourceMarch11_3:41pm

Mar 11th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.70 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.  
  17. namespace FarmingBotMod
  18. {
  19.     [Mod]
  20.     public class FarmingBotMod : DiskResource
  21.     {
  22.         public static List<Vector2> savedChestLocations = new List<Vector2>();// create list of chest locations
  23.  
  24.         // bots crafting ingrediants 1 iridium ingot, 50 coal, 1 battery, 1 refined crystal
  25.         // bot starts with 50 coal
  26.         // setting bot down
  27.         // allow interaction of the bot
  28.         // configuration of the bot 3x3 and 5x5
  29.         // bot needs coal to run
  30.         // bot uses coal every 7.5 seconds
  31.  
  32.         [Subscribe]
  33.         public void PreNewDayEventCallback(PreNewDayEvent @event)
  34.         {
  35.             savedChestLocations.Clear();
  36.  
  37.             Console.WriteLine(savedChestLocations.Count + " <= number should be 0");
  38.         }
  39.  
  40.         [Subscribe]
  41.         public void PreObjectDayUpdateCallback(PreObjectDayUpdateEvent @event)// using as a place holder
  42.         {
  43.             //get location
  44.             Storm.StardewValley.Wrapper.ObjectItem obj = @event.This;
  45.             GameLocation location = @event.ArgLocation;
  46.  
  47.             // chest locations to search
  48.             //Microsoft.Xna.Framework.Vector2 chestLocations = new Vector2();
  49.  
  50.             int savedCounter = 0;
  51.             //int chestCounter = 0;
  52.  
  53.             //Dictionary<int, Vector2> savedLocationHarvestableCrops = new Dictionary<int, Vector2>(); // using dictionary as a list
  54.             var savedLocationHarvestableCrops = new List<Vector2>(); // create list to store tileLocations that are plants that have product.
  55.             //var savedChestLocations = new List<Vector2>();// create list of chest locations
  56.  
  57.             // bot senses when crops are done
  58.             //check if chest is behind bot
  59.             if (obj.Name == "Chest")
  60.             {
  61.                 //find locations
  62.                 Microsoft.Xna.Framework.Vector2 chestLocation = obj.TileLocation;
  63.                 //debuging
  64.                 Console.WriteLine("obj location at " + obj.TileLocation.X + ", " + obj.TileLocation.Y);
  65.  
  66.                 savedChestLocations.Add(chestLocation);
  67.                 Console.WriteLine("----------------");
  68.                 Console.WriteLine("Finding chests and saving location at " + chestLocation.X + ", " + chestLocation.Y);
  69.                 Console.WriteLine("----------------");
  70.                
  71.                
  72.                 Console.WriteLine(savedChestLocations.Count + " total chests found");
  73.             }
  74.  
  75.             if (obj.Name == "Hardwood Fence")
  76.             {
  77.                 //debugging
  78.                 Console.WriteLine();
  79.                 Console.WriteLine("----------New day Start---------");
  80.  
  81.                 // find location of the bot and save the default location
  82.                 Microsoft.Xna.Framework.Vector2 tileLocation = obj.TileLocation;
  83.                 Microsoft.Xna.Framework.Vector2 defLocation = tileLocation;
  84.  
  85.                
  86.  
  87.                 // tileLocation is where the search for crops start
  88.                 // 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
  89.                 // an increase in y is lower on the screen. ex plots start top left at 0,0 to bottom right at 9999,9999
  90.                 tileLocation.X -= 4;
  91.                 tileLocation.Y += 1;
  92.  
  93.                 //debugging
  94.                 //Console.WriteLine("First Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
  95.                 //Console.WriteLine("default Location for x = " + defLocation.X + " default Location for y = " + defLocation.Y);
  96.  
  97.                 int counter = 0;
  98.                 int actuallyHarvestableCropsCounter = 0;
  99.  
  100.                 //counts the crops to out put for debugging
  101.                 int cropCounter = 0;
  102.                 //count the crops
  103.  
  104.                 //debugging
  105.                 //Console.WriteLine("before running the while statement.");
  106.  
  107.  
  108.                 while (counter < 5)
  109.                 {
  110.                     //debugging
  111.                     Console.WriteLine("Starting Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
  112.  
  113.                     //debugging
  114.                     /*Console.WriteLine("before running the if statement.");
  115.                     Console.WriteLine(location.TerrainFeatures.ContainsKey(tileLocation));
  116.                     try
  117.                     {
  118.                         //Console.WriteLine(location.TerrainFeatures[tileLocation].IsHoeDirt()); //old
  119.                         Console.WriteLine(location.TerrainFeatures[tileLocation].Is<HoeDirtAccessor>());
  120.                     }
  121.                     catch
  122.                     {
  123.                         Console.WriteLine("Not hoedirt");
  124.                     }*/
  125.  
  126.                     //HoeDirt[] hoeDirts = location.TerrainFeatures.Where(x => x.Value. is HoeDirtAccessor).ToArray<HoeDirt>();
  127.                     //ObjectItem[] items = location.Objects.Where(x => x.Value is ObjectAccessor).ToList<ObjectItem>; // not correct
  128.                    
  129.                     //Console.WriteLine(hoeDirts[0].ToString());
  130.  
  131.                     Console.WriteLine("Before verifiying if tileLocation is hoeDirt.");
  132.  
  133.                     //if (location.TerrainFeatures.ContainsKey(tileLocation) && location.TerrainFeatures[tileLocation].IsHoeDirt()) // old
  134.                     if (location.TerrainFeatures.ContainsKey(tileLocation) && location.TerrainFeatures[tileLocation].Is<HoeDirtAccessor>())
  135.                     {
  136.                         Console.WriteLine("Succesfully verified hoeDirt");
  137.                        
  138.                         HoeDirt crop = location.TerrainFeatures[tileLocation].As<HoeDirt,HoeDirtAccessor>();
  139.  
  140.                         //degbuging
  141.                         //Console.WriteLine("Before checking crops is true or not.");
  142.  
  143.                         //check if crops are fully grown
  144.                         try
  145.                         {
  146.  
  147.                             // check if fully grown
  148.                             if (crop.Crop.IsFullyGrown == true)
  149.                             {
  150.                                 Console.WriteLine("Found fully grown crop.");
  151.                                 ++cropCounter;
  152.  
  153.                                 // if fully grown check if harvestable
  154.                                 if(crop.Crop.CurrentPhase >= crop.Crop.PhaseDays.Count - 1 && (!crop.Crop.IsFullyGrown || crop.Crop.DayOfCurrentPhase <= 0))
  155.                                 {
  156.                                     Console.WriteLine("Found a crop that can be harvested located at " + tileLocation.X + " and " + tileLocation.Y);
  157.                                     ++actuallyHarvestableCropsCounter;
  158.  
  159.                                     // save location
  160.                                     savedLocationHarvestableCrops.Add(tileLocation);
  161.                                     ++savedCounter;
  162.                                 }
  163.                             }
  164.                         } catch (Exception e) //(System.NullReferenceException) this crashes the game when ran in concecutive days
  165.                         {
  166.                             Console.WriteLine("--------------------------");
  167.                             Console.WriteLine(e);
  168.                             Console.WriteLine("hoedirt but no crop");
  169.                             Console.WriteLine("--------------------------");
  170.                         }
  171.                         //debuging
  172.                         //Console.WriteLine("After checking if crops are true or not.");
  173.                     }
  174.                    
  175.                     //debugging
  176.                     //Console.WriteLine("before check 1");
  177.  
  178.                     // check if tileLocation is equal to default location, x will go to default x, y will go to default y + 5
  179.                     // may not be needed
  180.                     if (tileLocation.X == defLocation.X && tileLocation.Y == defLocation.Y + 5)
  181.                        
  182.                     {
  183.                         //debugging
  184.                         //Console.WriteLine("Finished checking tiles");
  185.  
  186.                         ++counter;
  187.                     }
  188.                     //check if tilelocation.x is the default location
  189.                     else if (tileLocation.X == defLocation.X)
  190.                     {
  191.                         tileLocation.X -= 4;
  192.                         tileLocation.Y += 1;
  193.                         counter = 0;
  194.  
  195.                         //debugging
  196.                         //Console.WriteLine("reseting pos to 1 down 4 to the left");
  197.                     }
  198.                     // if nether previous are true continue counting.
  199.                     else
  200.                     {
  201.                         //debugging
  202.                         //Console.WriteLine("inside check 3");
  203.                         tileLocation.X += 1;
  204.                         ++counter;
  205.                     }
  206.                     //debugging
  207.                     //Console.WriteLine("after check 3");
  208.                     //debugging
  209.                     //Console.WriteLine("Ending Tile Location for x = " + tileLocation.X + " Tile Location for y = " + tileLocation.Y);
  210.                 }
  211.  
  212.  
  213.  
  214.                 Console.WriteLine(cropCounter + " total fully grown crops and " + actuallyHarvestableCropsCounter + " total crops ready to be harvested.");
  215.  
  216.                 //harvest find chest
  217.                 //check if chest is behind
  218.                 if (savedChestLocations.Count > 0)// && (obj.TileLocation.X == defLocation.X && obj.TileLocation.Y == defLocation.Y - 1))
  219.                 {
  220.                     int c = 0;
  221.                     while (c < savedChestLocations.Count)
  222.                     {
  223.                         //debuging
  224.                         Console.WriteLine();
  225.                         Console.WriteLine(savedChestLocations[c].Y);
  226.                         Console.WriteLine(defLocation.Y - 1 );
  227.                         Console.WriteLine();
  228.  
  229.                         if (savedChestLocations[c].X == defLocation.X && savedChestLocations[c].Y == defLocation.Y - 1)
  230.                         {
  231.                             Console.WriteLine("There is a chest behind the bot located at " + defLocation.X);
  232.                         }
  233.                         ++c;
  234.                     }
  235.                 }
  236.                 @event.ReturnEarly = true;
  237.             }
  238.            
  239.            
  240.            
  241.  
  242.             //debugging
  243.             /*
  244.             if (savedCounter != 0)
  245.             {
  246.                 int test = 0;
  247.                 Console.WriteLine();
  248.                 Console.WriteLine(savedCounter + " saved locations");
  249.  
  250.                 while (test < savedCounter)
  251.                 {
  252.  
  253.                     Console.WriteLine("{0}, {1}", savedLocationHarvestableCrops[test].X, savedLocationHarvestableCrops[test].Y);
  254.                     ++test;
  255.                 }
  256.             }*///end debugging
  257.  
  258.         }
  259.  
  260.         [Subscribe]
  261.         public void PostNewDayCallback(PostNewDayEvent @event)
  262.         {
  263.             Console.WriteLine(savedChestLocations.Count + " Total chests found");    
  264.         }
  265.  
  266.             // bot finds a path to crops (going around sprinklers and scarecrows)
  267.             // bot farms the crops and contains them in a chest.
  268.             // bot return to the spot it was placed.
  269.  
  270.  
  271.         }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement