Advertisement
sdini

farmingbotsourceMarch15_2:10am

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