Advertisement
sanderronde

interpreter.lua

Jan 3rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. -- Interpreter = 18
  2. -- Controller = 12
  3. local CONTROLLER = 12
  4.  
  5. local has_no_fuel = false
  6. local performing_task = false
  7.  
  8. local function handleControllerMessage(msg)
  9.     -- Received as value - 5
  10.     if msg == "bake_bread" then
  11.         redstone.setAnalogOutput("front", 10)
  12.     elseif msg == "farm" then
  13.         redstone.setAnalogOutput("front", 11)
  14.     end
  15.  
  16.     sleep(5)
  17.     redstone.setOutput("front", false)
  18. end
  19.  
  20. local function handleController()
  21.     local res = rednet.receive(1)
  22.     if not res then
  23.         return
  24.     end
  25.     local id, msg = res
  26.  
  27.     if id == CONTROLLER then
  28.         handleControllerMessage(msg)
  29.     end
  30. end
  31.  
  32. local function send_has_fuel()
  33.     rednet.send(CONTROLLER, "has_fuel")
  34. end
  35.  
  36. local function send_has_no_fuel()
  37.     rednet.send(CONTROLLER, "has_no_fuel")
  38. end
  39.  
  40. local function send_performing_task()
  41.     rednet.send(CONTROLLER, "performing_task")
  42. end
  43.  
  44. local function send_done_task()
  45.     rednet.send(CONTROLLER, "done_task")
  46. end
  47.  
  48. local function handleBot()
  49.     local val = redstone.getAnalogInput("front")
  50.     if val == 11 then
  51.         -- No fuel, tell the controller
  52.         if has_no_fuel then
  53.             -- Already sent message
  54.             return
  55.         end
  56.         has_no_fuel = true
  57.         send_has_no_fuel()
  58.     elseif val == 10 then
  59.         -- Busy performing task
  60.         if performing_task then
  61.             -- Already sent message
  62.             return
  63.         end
  64.         performing_task = true
  65.         send_performing_task()
  66.     elseif val == 0 and has_no_fuel then
  67.         -- Changed to having fuel
  68.         has_no_fuel = false
  69.         send_has_fuel()
  70.     elseif val == 9 and performing_task then
  71.         -- Done with task
  72.         performing_task = false
  73.         send_done_task()
  74.     elseif val == 8 then
  75.         -- Ready
  76.         rednet.send(CONTROLLER, "ready")
  77.     end
  78. end
  79.  
  80. local function interpret()
  81.     while true do
  82.         handleBot()
  83.         handleController()
  84.     end
  85. end
  86.  
  87. local function init()
  88.     rednet.open("right")
  89.     interpret()
  90. end
  91.  
  92. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement