Advertisement
sdini

FarmbotSource_March11_4:21AM

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