Advertisement
sdini

farming bot

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