Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor")
- local modem = peripheral.find("modem", rednet.open) -- doing it like this finds all modem peripherals and then opens rednet on them
- local tPos
- local pos
- local tolerance = 5
- --- MAIN
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setTextScale(0.5)
- monitor.write("Hello, ID: " .. os.computerID()) --the id of the computer
- function vec3ToString(vec3)
- local str = ("(x:" .. math.floor(vec3.x+0.5) .. ", y:" .. math.floor(vec3.y+0.5) .. ", z:" .. math.floor(vec3.z+0.5) .. ")")
- return str
- end
- function receivePosMessage()
- local id, message = rednet.receive(nil,0.1)
- if (message == nil) then return end
- tPos = message
- end
- function update()
- pos = ship.getWorldspacePosition()
- receivePosMessage()
- if(tPos == nil or pos == nil) then
- return
- end
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write("Message from control: " .. vec3ToString(tPos))
- monitor.setCursorPos(1,2)
- monitor.write("Our position: " .. vec3ToString(pos))
- local deltaPos = vector.new(tPos.x-pos.x,0.0,tPos.z-pos.z)
- local ang = (-(math.pi/2)+(math.atan2(deltaPos.z,deltaPos.x))) * 180/math.pi
- if(ang<0) then
- ang = ang + 360
- end
- local yaw = (ship.getEulerAnglesYXZ().y * -180/math.pi)
- yaw = (yaw + 360) % 360
- ang = (ang + 360) % 360
- monitor.setCursorPos(1,3)
- monitor.write("ANG: " .. ang .. ", MY ANG: " .. yaw)
- monitor.setCursorPos(1,4)
- local angDiff = yaw-ang
- angDiff = (angDiff + 180) % 360-180
- if(angDiff> 180) then
- angDiff = angDiff-360
- elseif (angDiff <-180) then
- angDiff = angDiff + 360
- end
- monitor.write("ANG DIFF: " .. angDiff)
- print("ANG: " .. ang .. ", MY ANG: " .. yaw)
- print("ANG DIFF: " .. (angDiff))
- if(angDiff < -tolerance) then
- redstone.setOutput("right", true)
- redstone.setOutput("left", false)
- elseif(angDiff > tolerance) then
- redstone.setOutput("right", false)
- redstone.setOutput("left", true)
- else
- redstone.setOutput("right", false)
- redstone.setOutput("left", false)
- end
- end
- while true do
- sleep(0.1)
- update()
- end
Advertisement
Add Comment
Please, Sign In to add comment