Advertisement
Guest User

Lightbarrow Farm Bot 1.0

a guest
Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.91 KB | None | 0 0
  1.  
  2. // Lightbarrow Farmbot
  3. // October 31, 2014
  4. //
  5. // This bot works with seedless crops: potato, carrot
  6. //
  7. // To-do:
  8. // -> Empty items to a chest.
  9. // -> Skip columns that are not dirt (the semi-auto farm edge levels)
  10.  
  11. // ***********************************************************************
  12. // Settings — Change these to what you want to use.
  13. // ***********************************************************************
  14. &harvest_tool = diamond_axe;
  15. &repair_tool = stone_hoe;
  16. &food = carrot;
  17.  
  18. // Starting coordinates at North-East corner of the field
  19. // The autofarm/tiered-fields should run in the North-South direction.
  20.  
  21. // Test Field
  22. //#field_start_x = 14;
  23. //#field_start_z = 1;
  24.  
  25. // Autofarm North-side
  26. #autofarm_north_start_x = 6957;
  27. #autofarm_north_start_z = -3019;
  28. #autofarm_north_num_rows = 7;
  29. #autofarm_north_num_cols = 35;
  30. #autofarm_north_num_levels = 5;
  31. &autofarm_north_crop = potato;
  32.  
  33. // Autofarm South-side
  34. #autofarm_south_start_x = 6957;
  35. #autofarm_south_start_z = -2977;
  36. #autofarm_south_num_rows = 7;
  37. #autofarm_south_num_cols = 35;
  38. #autofarm_south_num_levels = 4;
  39. &autofarm_south_crop = potato;
  40.  
  41. // North-west Carrot farm
  42. #carrot_farm_start_x = 6915;
  43. #carrot_farm_start_z = -3019;
  44. #carrot_farm_num_rows = 7;
  45. #carrot_farm_num_cols = 26;
  46. #carrot_farm_num_levels = 4;
  47. &carrot_farm_crop = carrot;
  48.  
  49. // See if we are at the start of a field
  50. if( (%XPOS% = #autofarm_north_start_x) && (%ZPOS% = #autofarm_north_start_z));
  51. log("Starting work on the north-side of the autofarm.");
  52. #field_start_x = #autofarm_north_start_x;
  53. #field_start_z = #autofarm_north_start_z;
  54. #num_rows = #autofarm_north_num_rows;
  55. #num_cols = #autofarm_north_num_cols;
  56. #num_levels = #autofarm_north_num_levels;
  57. &crop = %&autofarm_north_crop%;
  58. elseif( (%XPOS% = #autofarm_south_start_x) && (%ZPOS% = #autofarm_south_start_z));
  59. log("Starting work on the south-side of the autofarm.");
  60. #field_start_x = #autofarm_south_start_x;
  61. #field_start_z = #autofarm_south_start_z;
  62. #num_rows = #autofarm_south_num_rows;
  63. #num_cols = #autofarm_south_num_cols;
  64. #num_levels = #autofarm_south_num_levels;
  65. &crop = %&autofarm_south_crop%;
  66. elseif( (%XPOS% = #carrot_farm_start_x) && (%ZPOS% = #carrot_farm_start_z));
  67. log("Starting work on the north-west carrot farm.");
  68. #field_start_x = #carrot_farm_start_x;
  69. #field_start_z = #carrot_farm_start_z;
  70. #num_rows = #carrot_farm_num_rows;
  71. #num_cols = #carrot_farm_num_cols;
  72. #num_levels = #carrot_farm_num_levels;
  73. &crop = %&carrot_farm_crop%;
  74. else;
  75. log("You are not at the start of a field. Move to North-east corner of a field.");
  76. stop();
  77. endif;
  78.  
  79.  
  80. log("Harvest tool = %&harvest_tool%");
  81. log("Repair tool = %&repair_tool%");
  82. log("Crop = %&crop%");
  83.  
  84.  
  85. // General Info:
  86. // look(0, 90), is facing north(0) and down(90)
  87. // look(180,90), is facing south(180) and down(90)
  88.  
  89.  
  90. // **********************************************************************
  91. // Algorithm
  92. // **********************************************************************
  93.  
  94. // Make sure the bot starts at the correct spot (North-East corner of the field)
  95. IF( (%XPOS% != %#field_start_x%) || (%ZPOS% != %#field_start_z%));
  96. LOG("Unable to start bot. Move to X=%#field_start_x%, Z=%#field_start_z%");
  97. stop();
  98. endif;
  99.  
  100.  
  101. // ==============================
  102. // Loop over each level (of autofarm)
  103. // ==============================
  104. sprint;
  105. #even = 0;
  106. for(#levelnum, 1, %#num_levels%);
  107.  
  108. &harvest_direction_z = "south";
  109.  
  110. if(%#even% = 0);
  111. log("even = 0");
  112. // Start at the north-east corner
  113. #start_x = #field_start_x;
  114. #end_x = (%#start_x% - %#num_cols%) + 1;
  115. &harvest_direction_x = "west";
  116. else;
  117. log("even = 1");
  118. // Start at the north-west corner
  119. #start_x = (#field_start_x - #num_cols) + 1;
  120. #end_x = (#start_x + #num_cols) - 1;
  121. &harvest_direction_x = "east";
  122. endif;
  123. #start_z = #field_start_z + ((#levelnum - 1) * #num_rows);
  124. #end_z = (#start_z + #num_rows) - 1;
  125.  
  126. log("field_start_x = %#field_start_x%");
  127. log("num_cols = %#num_cols%");
  128. log("start_x = %#start_x%");
  129.  
  130. log("Start = (%#start_x%, %#start_z%)");
  131. log("End = (%#end_x%, %#end_z%)");
  132.  
  133.  
  134. // ======================================================
  135. // Loop over each column
  136. // ======================================================
  137. do(%#num_cols%);
  138.  
  139.  
  140. // ======================================================
  141. // Eat food
  142. // ======================================================
  143. do;
  144. log("Eating food. Hunger = %HUNGER%");
  145. pick(%&food%);
  146. if(%ITEM% != %&food%);
  147. log("Bot does not have food.");
  148. stop();
  149. endif;
  150. key(use);
  151. while(%HUNGER% < 20);
  152.  
  153.  
  154. // ======================================================
  155. // Harvest the crop
  156. // ======================================================
  157. log("Harvesting");
  158.  
  159. // Select the harvesting tool
  160. pick(%&harvest_tool%);
  161. if(%ITEM% != %&harvest_tool%);
  162. log("Harvest tool unavailable. Stopping bot.");
  163. stop();
  164. endif;
  165.  
  166. // Set up the end points
  167. if(%&harvest_direction_z% = "south");
  168. #dest_z = #end_z;
  169. else;
  170. #dest_z = #start_z;
  171. endif;
  172.  
  173. // Walk and harvest a column
  174. do;
  175. if(%&harvest_direction_z% = "south");
  176. look(180,90);
  177. else;
  178. look(0,90);
  179. endif;
  180.  
  181. keydown(forward);
  182. key(attack);
  183. while(%ZPOS% != %#dest_z%);
  184. keyup(forward);
  185.  
  186.  
  187.  
  188. // ======================================================
  189. // Repair the soil
  190. // ======================================================
  191. log("Repairing");
  192.  
  193. // Select the repair tool (hoe)
  194. pick(%&repair_tool%);
  195. if(%ITEM% != %&repair_tool%);
  196. log("Repair tool unavailable. Stopping bot.");
  197. stop();
  198. endif;
  199.  
  200. // Setup the end points
  201. if(%&harvest_direction_z% = "south");
  202. #dest_z = #start_z;
  203. else;
  204. #dest_z = #end_z;
  205. endif;
  206.  
  207. // Walk and repair a column
  208. do;
  209. if(%&harvest_direction_z% = "south");
  210. look(0,90);
  211. else;
  212. look(180,90);
  213. endif;
  214.  
  215. keydown(forward);
  216. key(use);
  217. while(%ZPOS% != %#dest_z%);
  218. keyup(forward);
  219.  
  220.  
  221.  
  222. // ======================================================
  223. // Plant the crop
  224. // ======================================================
  225. log("Planting");
  226.  
  227. // Select the crop to plant
  228. pick(%&crop%);
  229. if(%ITEM% != %&crop%);
  230. log("Harvest tool unavailable. Stopping bot.");
  231. stop();
  232. endif;
  233.  
  234. // Set up the end points
  235. if(%&harvest_direction_z% = "south");
  236. #dest_z = #end_z;
  237. else;
  238. #dest_z = #start_z;
  239. endif;
  240.  
  241. // Walk and plant a column
  242. do;
  243. if(%&harvest_direction_z% = "south");
  244. look(180,90);
  245. else;
  246. look(0,90);
  247. endif;
  248.  
  249. keydown(forward);
  250. key(use);
  251. while(%ZPOS% != %#dest_z%);
  252. keyup(forward);
  253.  
  254.  
  255. // ======================================================
  256. // Move to next column
  257. // ======================================================
  258. if(%XPOS% != %#end_x%);
  259. log("Moving to next column");
  260. if(%&harvest_direction_x% = "west");
  261. #dest_x = %XPOS%-1;
  262. else;
  263. #dest_x = %XPOS%+1;
  264. endif;
  265.  
  266. do;
  267. if(%&harvest_direction_x% = "west");
  268. look(270,90);
  269. else;
  270. look(90,90);
  271. endif;
  272. keydown(forward);
  273. while(%XPOS% != %#dest_x%);
  274. keyup(forward);
  275. endif;
  276.  
  277. log("At next column to process");
  278.  
  279. // ======================================================
  280. // Change the harvesting direction
  281. // ======================================================
  282. if(%&harvest_direction_z% = "south");
  283. &harvest_direction_z = "north";
  284. else;
  285. &harvest_direction_z = "south";
  286. endif;
  287.  
  288.  
  289. // ======================================================
  290. // LOOP: END OF WORKING ON THE COLUMN. PROCEED TO NEXT.
  291. // ======================================================
  292. loop;
  293.  
  294.  
  295. // =============================
  296. // End of harvesting one level
  297. // =============================
  298.  
  299.  
  300. log("Bot is moving one column off field.");
  301.  
  302. // Move one column off the field
  303. if(%&harvest_direction_x% = "west");
  304. #dest_x = %XPOS% - 1;
  305. else;
  306. #dest_x = %XPOS% + 1;
  307. endif;
  308. do;
  309. if(%&harvest_direction_x% = "west");
  310. look(270,90);
  311. else;
  312. look(90,90);
  313. endif;
  314.  
  315. keydown(forward);
  316. while(%XPOS% != %#dest_x%);
  317. keyup(forward);
  318.  
  319. log("Bot should be off-field by 1 column");
  320.  
  321. // Now bot should be 1 column off the field, either on west or east side
  322. // Bot needs to move to the next starting location
  323.  
  324. if(%#even% = 1);
  325. // Start at the north-east corner
  326. #dest_x = #field_start_x;
  327. else;
  328. // Start at the north-west corner
  329. #dest_x = (#field_start_x - #num_cols) + 1;
  330. endif;
  331. #dest_z = #field_start_z + ((#levelnum) * #num_rows);
  332.  
  333. if(#levelnum != #num_levels);
  334. log("Moving to start of next level");
  335.  
  336. if(%ZPOS% != %#dest_z%);
  337. // Move north-south direction
  338. do;
  339. look(180,90);
  340. keydown(forward);
  341. while(%ZPOS% != %#dest_z%);
  342. keyup(forward);
  343. endif;
  344.  
  345. // Move east-west direction
  346. if(%XPOS% != %#dest_x%);
  347. do;
  348. if(#even = 0);
  349. look(90,90);
  350. else;
  351. look(270,90);
  352. endif;
  353.  
  354. keydown(forward);
  355. while(%XPOS% != %#dest_x%);
  356. keyup(forward);
  357. endif;
  358.  
  359. log("Bot should be at start of next level");
  360.  
  361. // Verify
  362. if( (%XPOS% != %#dest_x%) || (%ZPOS% != %#dest_z%));
  363. log("Error. Bot went off-course. Stopping.");
  364. stop();
  365. endif;
  366. endif;
  367.  
  368. toggle(#even);
  369.  
  370. // Loop at the next level
  371. next;
  372.  
  373.  
  374. // ======================================================
  375. // End of harvesting the entire field.
  376. // Walk back to the start of the field.
  377. // ======================================================
  378. // East-West direction
  379. if(%XPOS% != (%#field_start_x%+1));
  380. log("Moving back to the start (East-West).);
  381. do;
  382. look(90,90);
  383. keydown(forward);
  384. while(%XPOS% != (%#field_start_x%+1));
  385. keyup(forward);
  386. endif;
  387.  
  388. // North-South direction
  389. if(%ZPOS% != %#field_start_z%);
  390. log("Moving back to the start (North-South)");
  391. // move north
  392. do;
  393. look(0,90);
  394. keydown(forward);
  395. //log("z = %ZPOS%, and start_z = %#field_start_z%");
  396. while(%ZPOS% != %#field_start_z%);
  397. keyup(forward);
  398. endif;
  399.  
  400. // Move one unit over to finish moving to the start
  401. do;
  402. look(270,90);
  403. keydown(forward);
  404. while(%XPOS% != %#field_start_x%);
  405. keyup(forward);
  406.  
  407. unsprint;
  408.  
  409. // ======================================================
  410. // Bot Checkup.
  411. // Bot should be back at the start of the field, if not
  412. // something went wrong.
  413. // ======================================================
  414. if( (%XPOS% != %#field_start_x%) || (%ZPOS% != %#field_start_z%) );
  415. log("Bot went off-course. Something strange happened. Stopping bot.”);
  416. stop();
  417. endif;
  418.  
  419.  
  420. // ======================================================
  421. // Success!
  422. // ======================================================
  423. log("Bot stopped successfully. Harvest complete.")
  424.  
  425. stop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement