MCBoat

Farm_proc

Jul 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. --[[main script for farming procedure]]
  2.  
  3. --load functions responsible for positioning/repositioning/farming
  4. os.loadAPI("APIS/positioners");
  5. os.loadAPI("APIS/farmers");
  6.  
  7. --action turtle start farming condition
  8. assert(positioners.check_start_pos(), "Could not initialise the turtle for the farming procedure\nCould not identify it's starting location\n(are there birch planks directly above and beneath it?");
  9.  
  10. --check Netherrack can be found from start block, declare 'dir' also as local
  11. local init_result, dir = positioners.find_next_neth()
  12. assert(init_result, "Could not find the appropriate direction to travel in from\n the turtle starting position, is the farm set up correctly?");
  13.  
  14.  
  15. local loop_vars = {["count"] = 0, ["blockId"] = nil, ["dir"] = nil, ["prevManvr"] = nil, ["action"] = ""};
  16.  
  17. --**input** assertion tests for required fuel level and seed requirements
  18. assert(turtle.getFuelLevel() > 50, "Fuel level is below required level to start farming, - terminating");
  19.  
  20. --given 'result' == true can start farming
  21. turtle.forward();
  22.  
  23. print("Gone forwards!");
  24.  
  25. --open log file (overwrite) and save to io class handle
  26. log = io.open("logs/proc_log", "w");
  27. print("Opened log!");
  28.  
  29. --start main farming process loop
  30. while turtle.getFuelLevel() > 0 do
  31.  
  32.   print("entered loop");
  33.   --reset action variable
  34.   loop_vars["action"] = "";
  35.  
  36.   --sort log variables
  37.   loop_vars["count"] = loop_vars["count"] + 1;
  38.  
  39.   --identify the block above (returns inv slot(1-4) it matches with or false if it doesn't)
  40.   loop_vars["blockId"] = positioners.id();
  41.  
  42.   if blockId == 1 then
  43.     loop_vars["action"] = "Netherrack above ";
  44.    
  45.     if loop_vars["dir"] == nil then
  46.       print("Netherrack found, planting");
  47.      
  48.       farmers.plant();
  49.      
  50.       --shift turtle forward such that it can evaluate a new block. Ensure that moves forward in case of obstruction
  51.       assert(turtle.forward(), "The turtle is impeded from moving forwards in farming route");
  52.      
  53.       --log statement
  54.       loop_vars["action"] = loop_vars["action"].."plant and move forward";
  55.      
  56.     else
  57.       --initiate change in direction procedure
  58.       assert(positioners.change_direction(loop_vars["dir"]), "Failed to complete turtle change direction procedure");
  59.      
  60.       --reset dir
  61.       loop_vars["dir"] = nil;
  62.      
  63.       loop_vars["action"] = loop_vars["action"].." actioned 'positioners.change_position(dir)'.";
  64.     end;
  65.    
  66.   elseif blockId == 3 then
  67.  
  68.     turtle.forward();    
  69.     --log statement
  70.     loop_vars["action"] = "Proceeding past marble block";
  71.    
  72.   elseif blockId == false then
  73.     print("Finding next Netherrack block.");
  74.    
  75.     --retrace back to the most recent 'legitimate' block");
  76.     turtle.back();
  77.    
  78.     --call function to reposition turtle for next farming line
  79.     result, loop_vars["dir"] = positioners.find_next_neth(loop_vars["prevMan"]);
  80.     assert(result, "A new netherrack block could not be found in any direction.");
  81.    
  82.     --assign dir result to prevMan (information for next positioners.change_position() call
  83.     loop_vars["prevMan"] = loop_vars["dir"];
  84.    
  85.     --log statement
  86.     loop_vars["action"] = "No 'legitimate' block above, 'positioners.find_next_neth() successful,\nassigning dir accordingly.";
  87.    
  88.   else
  89.     --log statement
  90.    log.write("Unhandled exception in farm_proc main loop");
  91.     log.close();
  92.    
  93.    error("positioners.find_next_neth() did not find an appropriate block above it.");
  94.   end;
  95.  
  96.   print("farm_proc loop");
  97.  
  98.   --write current turn to log file
  99.   --log.write("Turn no: "..loop_vars["count"].."\n    Id()== "..tostring(loop_vars["blockId"]).."\n    Action: "..loop_vars["action"].."\n    Previous maneuver: "..loop_vars["prevMan"].."\n\n");
  100. end;
  101.  
  102. --updating log
  103. log.write("Error - turtle ran out of fuel\n");
  104. log.close();
  105.  
  106. error("Turtle ran out of fuel whilst planting - terminated");
Add Comment
Please, Sign In to add comment