Advertisement
xeritt

OpenComputers Robot farmer with remote control

Sep 3rd, 2018
520
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local robot = require("robot")
  2. local comp = require("computer")
  3. local mod = require("component").modem
  4. --local file = require("component").filesystem
  5. local event = require("event")
  6. local shell = require('shell')
  7. local fRun = true
  8. local size = 9
  9. local cardID = ""
  10. local status = "wait"
  11.  
  12. function round2(num, numDecimalPlaces)
  13.   return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
  14. end
  15.  
  16. function readcardID()
  17.   cardID = "97b51251-7036-4a13-a303-92f7dcf0a83a"
  18. end
  19.  
  20.  
  21. function harvest()
  22.   status="harvest"
  23.   -- fly to start
  24.   robot.forward()
  25.   for y=1, size do
  26.     -- harvest one line
  27.     robot.swingDown()
  28.     robot.useDown()
  29.     robot.placeDown()
  30.     for x=1, (size-1) do
  31.       robot.forward()
  32.       robot.swingDown()
  33.       robot.useDown()
  34.       robot.placeDown()
  35.     end
  36.         -- turn
  37.     if y%2 == 1 then
  38.           robot.turnLeft()
  39.           robot.forward()
  40.           robot.turnLeft()
  41.         else
  42.           robot.turnRight()
  43.           robot.forward()
  44.           robot.turnRight()
  45.         end
  46.   end
  47.   -- return
  48.   for y=1, (size-1) do
  49.     robot.forward()
  50.   end
  51.   robot.turnLeft()
  52.   for y=1, (size) do
  53.     robot.forward()
  54.   end
  55.   robot.turnRight()
  56.   robot.forward()
  57.   robot.turnAround()
  58. end
  59.  
  60. function unload()
  61.   robot.turnAround()
  62.   for c = 2, 16 do
  63.     robot.select(c)
  64.     if robot.count() > 0 then
  65.       robot.drop()
  66.     else
  67.       robot.select(1)
  68.       break
  69.     end
  70.   end
  71.   robot.turnAround()
  72. end
  73.  
  74. function onChatMessage(...)
  75.     local fOwner = false;
  76.     local com = ""
  77.     for n,arg in ipairs({...}) do
  78.         if n==3 then
  79.           print("src:"..arg.."!")
  80.           if arg==cardID then
  81.             print("Owner")
  82.             fOwner=true
  83.           end
  84.         end
  85.         if n==6 then
  86.           print("com:"..arg)
  87.           com = arg
  88.         end
  89.     end
  90.     if fOwner==true then
  91.       if com=="status" then
  92.         print("ok")
  93.         mod.broadcast(225, status)
  94.       end
  95.       if com=="exit" then
  96.         mod.broadcast(225, 'ok')
  97.         fRun=false
  98.         os.exit()
  99.       end
  100.     else
  101.       --fRun=false
  102.       print("no owner signal")      
  103.       --os.exit()
  104.     end
  105. end
  106.  
  107. readcardID()
  108. --os.exit()
  109. mod.open(225)
  110. event.listen("modem_message", onChatMessage)
  111.  
  112. while fRun==true do
  113.   harvest()
  114.   status="wait"
  115.   if robot.count(2) == 64 then
  116.     unload()
  117.   end
  118.  
  119.   -- one hour sleeping
  120.   for i=1, 3600 do
  121.     if fRun==false then
  122.       break
  123.     end
  124.     os.sleep(1)
  125.     print(round2(1*i/36,2)..'%')
  126.   end
  127.   --
  128. end
  129.  
  130. mod.close(225)
Advertisement
RAW Paste Data Copied
Advertisement