Advertisement
Guest User

farmbot.lua

a guest
May 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local rp = require("robotplus")
  2. local r = require("robot")
  3. local modem = require("component").modem
  4. local event = require("event")
  5.  
  6. dofile("/var/props.lua")
  7.  
  8. modem.open(in_port)
  9.  
  10. local last_id
  11.  
  12. while true do
  13.   local _, _, host, _, _, id, name, x, z, farm_type = event.pull("modem_message")
  14.  
  15.   if last_id ~= id then
  16.     last_id = id
  17.     if name == "farmbot_check" then
  18.       print("responding to check")
  19.       modem.send(host, out_port, "ok", r.name())
  20.     else
  21.       local pos = rp.findWPByName(name, 40)
  22.       if pos then
  23.         print("moving to " .. name)
  24.         rp.jump(pos.position, jump_height)
  25.      
  26.         local action = (farm_type == "break") and r.swingDown or r.useDown
  27.      
  28.         current = { x = 0, z = 0 }
  29.      
  30.         y = pos.position[2]
  31.      
  32.         if x < 0 then
  33.           current.x = x + 1
  34.           rp.moveX(current.x)
  35.           x = math.abs(x)
  36.         end
  37.      
  38.         if z < 0 then
  39.           current.z = z + 1
  40.           rp.moveZ(current.z)
  41.           z = math.abs(z)
  42.         end
  43.      
  44.         rp.turnEast()
  45.      
  46.         for i = 1, z do
  47.           action()
  48.        
  49.           for j = 2, x do
  50.             r.forward()
  51.             if i % 2 == 0 then
  52.               current.x = current.x - 1
  53.             else
  54.               current.x = current.x + 1
  55.             end
  56.             action()
  57.           end
  58.          
  59.           if i == z then
  60.             break
  61.           end
  62.        
  63.           if i % 2 == 0 then
  64.             r.turnLeft()
  65.             r.forward()
  66.             r.turnLeft()
  67.           else
  68.             r.turnRight()
  69.             r.forward()
  70.             r.turnRight()
  71.           end
  72.        
  73.           current.z = current.z + 1
  74.           event.pull() --yield
  75.         end
  76.        
  77.         rp.moveX(-current.x)
  78.         rp.moveZ(-current.z)
  79.          
  80.         local drop = rp.findWPByName(dropoff, 40)
  81.         rp.jump(drop.position, jump_height - y)
  82.        
  83.         for i = 1, 16 do
  84.           r.select(i)
  85.           r.dropDown()
  86.         end
  87.       end
  88.     end
  89.   end
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement