richie3366

SIO 2

Jul 8th, 2014
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. local io = peripheral.wrap("right")
  2.  
  3.  
  4. pos = {}
  5. if(not peripheral.isPresent("left")) then
  6.   turtle.select(1)
  7.   turtle.equipLeft()
  8.   ent = peripheral.wrap("left")
  9.   pos = ent.getPos()
  10.   turtle.equipLeft()
  11. else
  12.   ent = peripheral.wrap("left")
  13.   pos = ent.getPos()
  14. end
  15.  
  16. if io.isConnected() then
  17.   io.disconnect()
  18. end
  19.  
  20. local key=fs.open("key","r").readAll()
  21.  
  22. io.connect("turtle.sandhose.fr:4321/turtle","key="..key.."&name="..os.getComputerLabel())
  23.  
  24. --[[local timerPending = false--]]
  25.  
  26.  
  27. nextDirs = {left = {WEST="SOUTH", SOUTH="EAST", EAST="NORTH", NORTH="WEST"}, right = {SOUTH="WEST", EAST="SOUTH", NORTH="EAST", WEST="NORTH"}}
  28.  
  29. moveDirs = {
  30.   NORTH = {0, 0, -1}, SOUTH = {0, 0, 1}, EAST = {1, 0, 0}, WEST = {-1, 0, 0}, up = {0, 1, 0}, down = {0, -1, 0}
  31. }
  32.  
  33. oppositeDirs = {WEST = "EAST", EAST = "WEST", NORTH = "SOUTH", SOUTH = "NORTH"}
  34.  
  35. function addVectorToPos(v)
  36.   pos.X = pos.X + v[1]
  37.   pos.Y = pos.Y + v[2]
  38.   pos.Z = pos.Z + v[3]
  39. end
  40.  
  41. function updatePos(a)
  42.  
  43.   if(pos ~= {}) then
  44.     if(a == "left" or a == "right") then
  45.       pos.dir = nextDirs[a][pos.dir]
  46.     end
  47.     if(a == "forward") then
  48.       addVectorToPos(moveDirs[pos.dir])
  49.     end
  50.     if(a == "back") then
  51.       addVectorToPos(moveDirs[oppositeDirs[pos.dir]])
  52.     end
  53.     if(a == "up" or a == "down") then
  54.       addVectorToPos(moveDirs[a])
  55.     end
  56.   end
  57. end
  58.  
  59. while true do
  60.   if io.isConnected() then    
  61.    
  62.    
  63.      
  64.     if peripheral.isPresent("left") then
  65.       if(ent == nil) then ent = peripheral.wrap("left") end
  66.       pos = ent.getPos()
  67.       local entities = ent.getNearbyEnt()
  68.       io.emit("update entities", entities)
  69.     end
  70.      
  71.     if(pos ~= {}) then
  72.       io.emit("set location", pos)
  73.     end
  74.    
  75.     io.emit("update data", { fuel=turtle.getFuelLevel() })
  76.     --[[        
  77.     if timerPending == false then
  78.       os.startTimer(0.5)
  79.       timerPending = true
  80.     end
  81.     --]]
  82.     local event, p1, p2 = os.pullEvent("socketio_event")
  83.     if event == "socketio_event" then
  84.       if p1 == "command" then
  85.         if p2 == "turnLeft" then
  86.           turtle.turnLeft()
  87.           updatePos("left")
  88.         elseif p2 == "turnRight" then
  89.           turtle.turnRight()
  90.           updatePos("right")
  91.         elseif p2 == "forward" then
  92.           turtle.forward()
  93.           updatePos("forward")
  94.         elseif p2 == "back" then
  95.           turtle.back()
  96.           updatePos("back")
  97.         elseif p2 == "up" then
  98.           turtle.up()
  99.           updatePos("up")
  100.         elseif p2 == "down" then
  101.           turtle.down()
  102.           updatePos("down")
  103.         elseif p2 == "swap" then
  104.           turtle.equipLeft()
  105.         elseif p2 == "attack" then
  106.           turtle.attack()
  107.         else
  108.           print("Unknown Command")
  109.         end
  110.       end  
  111.     elseif event == "timer" then
  112.       print("tick")
  113.       timerPending = false
  114.     end
  115.     --[[
  116.     if timerPending == false then
  117.       os.startTimer(0.5)
  118.       timerPending = true
  119.     end
  120.     --]]
  121.   end
  122. end
Advertisement
Add Comment
Please, Sign In to add comment