Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Interpreter = 18
- -- Controller = 12
- local CONTROLLER = 12
- local has_no_fuel = false
- local performing_task = false
- local ready = false
- local function handleControllerMessage(msg)
- -- Received as value - 5
- if msg == "bake_bread" then
- print("Setting bake_bread signal")
- redstone.setAnalogOutput("front", 10)
- elseif msg == "farm" then
- print("Setting farm signal")
- redstone.setAnalogOutput("front", 11)
- end
- sleep(5)
- redstone.setOutput("front", false)
- end
- local function handleController()
- local res = rednet.receive(1)
- if not res then
- return
- end
- local id, msg = res
- if id == CONTROLLER then
- handleControllerMessage(msg)
- end
- end
- local function send_has_fuel()
- print("Sending has_fuel")
- rednet.send(CONTROLLER, "has_fuel")
- end
- local function send_has_no_fuel()
- print("Sending has_no_fuel")
- rednet.send(CONTROLLER, "has_no_fuel")
- end
- local function send_performing_task()
- print("Sending performing_task")
- rednet.send(CONTROLLER, "performing_task")
- end
- local function send_done_task()
- print("Sending done_task")
- rednet.send(CONTROLLER, "done_task")
- end
- local function handleBot()
- local val = redstone.getAnalogInput("front")
- if val == 11 then
- -- No fuel, tell the controller
- if has_no_fuel then
- -- Already sent message
- return
- end
- has_no_fuel = true
- send_has_no_fuel()
- elseif val == 10 then
- -- Busy performing task
- if performing_task then
- -- Already sent message
- return
- end
- performing_task = true
- send_performing_task()
- elseif val == 0 and has_no_fuel then
- -- Changed to having fuel
- has_no_fuel = false
- send_has_fuel()
- elseif val == 9 and performing_task then
- -- Done with task
- performing_task = false
- send_done_task()
- elseif val == 8 then
- -- Ready
- if not ready then
- print("Sending ready")
- rednet.send(CONTROLLER, "ready")
- ready = true
- end
- end
- end
- local function interpret()
- while true do
- handleBot()
- handleController()
- end
- end
- local function init()
- rednet.open("right")
- interpret()
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement