Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local term = require("term")
- local event = require("event")
- local component = require("component")
- local os = require("os")
- local robot = require("robot")
- local serialization = require("serialization")
- local modem = component.modem
- local geo = component.geolyzer
- local computerAddress = "07bdaacd-57ba-467b-8522-3f40cb93e491"
- inv = component.inventory_controller
- pos = {}
- pos.x = 0
- pos.y = 0
- pos.z = 0
- local vector = "north"
- local message = " "
- function setCursorPos (posX, posY)
- term.setCursor(posX, posY)
- end
- function renderText(text)
- term.write(text)
- end
- function getInventoryInfo()
- info = getInventory()
- sendMsgToComputer(info)
- executed = true
- end
- function executeTurnLeft()
- print("message from computer received=LFT")
- print("executing command robot.turnLeft()")
- if vector == "north" then
- vector = "west"
- elseif vector == "west" then
- vector = "south"
- elseif vector == "south" then
- vector = "east"
- elseif vector == "east" then
- vector = "north"
- end
- robot.turnLeft()
- print(vector)
- sendMsgToComputer("robot.turnLeft()")
- executed = true
- end
- function executeTurnRight()
- print("message from computer received=RHT")
- print("executing command robot.turnRight()")
- if vector == "north" then
- vector = "east"
- elseif vector == "east" then
- vector = "south"
- elseif vector == "south" then
- vector = "west"
- elseif vector == "west" then
- vector = "north"
- end
- robot.turnRight()
- print(vector)
- sendMsgToComputer("robot.turnRight()")
- executed = true
- end
- function executeForward()
- print("message from computer received=FWD")
- print("executing command robot.forward()")
- if vector == "north" then
- pos.y = pos.y + 1
- elseif vector == "east" then
- pos.x = pos.x + 1
- elseif vector == "south" then
- pos.y = pos.y - 1
- elseif vector == "west" then
- pos.x = pos.x - 1
- end
- robot.forward()
- print(vector)
- sendMsgToComputer("robot.forward()")
- executed = true
- end
- function executeBack()
- print("message from computer received=BWD")
- print("executing command robot.back()")
- if vector == "north" then
- pos.y = pos.y - 1
- elseif vector == "east" then
- pos.x = pos.x - 1
- elseif vector == "south" then
- pos.y = pos.y + 1
- elseif vector == "west" then
- pos.x = pos.x + 1
- end
- robot.back()
- print(vector)
- sendMsgToComputer("robot.back()")
- executed = true
- end
- function executeUp()
- print("message from computer received=UP")
- print("executing command robot.up()")
- pos.z = pos.z + 1
- robot.up()
- print(vector)
- sendMsgToComputer("robot.up()")
- executed = true
- end
- function executeDown()
- print("message from computer received=DWN")
- print("executing command robot.down()")
- pos.z = pos.z - 1
- robot.down()
- print(vector)
- sendMsgToComputer("robot.down()")
- executed = true
- end
- function executeDetctUpward()
- print("message from computer recieved="..message)
- print("executing command geo.analyze(1)")
- detctionResult = geo.analyze(1)
- sendMsgToComputer("geo.analyze(1)")
- os.sleep(0.2)
- sendMsgToComputer(detctionResult.name)
- executed = true
- end
- function executeDetctDownward()
- print("message from computer recieved="..message)
- print("executing command robot.detectDown()")
- detctionResult = geo.analyze(0)
- sendMsgToComputer("geo.analyze(0)")
- os.sleep(0.2)
- sendMsgToComputer(detctionResult.name)
- executed = true
- end
- function executeDetctForward()
- print("message from computer recieved="..message)
- print("executing command geo.analyze(3)")
- detctionResult = geo.analyze(3)
- print("detctionResult = "..detctionResult.name)
- sendMsgToComputer("geo.analyze(3)")
- os.sleep(0.2)
- sendMsgToComputer(detctionResult.name)
- executed = true
- end
- function executeUseForward()
- print("message from computer recieved="..message)
- print("executing command robot.use(3)")
- robot.use(3)
- sendMsgToComputer("robot.use(3)")
- executed = true
- end
- function getInventory()
- local inv = component.inventory_controller
- local inventory = {}
- for i = 1, 16 do
- a = inv.getStackInInternalSlot(i)
- if a ~= nil then
- table.insert(inventory, i, a)
- else
- table.insert(inventory, i, "empty")
- end
- end
- return inventory
- end
- local messageHandlers = {
- LFT = executeTurnLeft,
- RHT = executeTurnRight,
- FWD = executeForward,
- BWD = executeBack,
- UP = executeUp,
- DWN = executeDown,
- DCT_UPW = executeDetctUpward,
- DCT_DWD = executeDetctDownward,
- DCT_FWD = executeDetctForward,
- USE_FWD = executeUseForward,
- GET_INV = getInventoryInfo,
- }
- -- Open modem on port 5
- modem.open(5)
- -- Function to listen for incoming modem messages
- function modemListen()
- -- Wait for a modem message event
- local _, _, from, port, _, msg = event.pull("modem_message")
- -- If a message was received
- if msg then
- -- Call the function associated with the message
- local handler = messageHandlers[msg]
- if handler then
- handler()
- os.sleep(0.1)
- end
- end
- end
- function sendMsgToComputer (msg)
- msg2 = serialization.serialize(msg)
- modem.open(5)
- modem.setStrength(255)
- sendMessageSuccess = modem.send(computerAddress, 5, msg2)
- print("message "..tostring(msg2).." sent="..tostring(sendMessageSuccess))
- end
- while true do
- modemListen()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement