Advertisement
Guest User

drone.fsh

a guest
Jan 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. drn = component.proxy(component.list("drone")())
  2. inv = component.proxy(component.list("inventory_controller")())
  3. mod = component.proxy(component.list("modem")())
  4.  
  5. address = nil
  6. port    = 3341
  7.  
  8. facing = {
  9.        [1] = { -- N
  10.               forward  = {x=0,y=0,z=-1},
  11.               backward = {x=0,y=0,z=1}
  12.              },
  13.        [2] = { -- E
  14.               forward  = {x=1,y=0,z=0},
  15.               backward = {x=-1,y=0,z=0}
  16.              },
  17.        [3] = { -- S
  18.               forward  = {x=0,y=0,z=1},
  19.               backward = {x=0,y=0,z=-1}
  20.              },
  21.        [4] = { -- W
  22.               forward  = {x=-1,y=0,z=0},
  23.               backward = {x=1,y=0,z=0}
  24.              }}
  25. direct = 1
  26. light =  {[1] = 0x0000FF,[2] = 0xFFFF00,[3] = 0xFF0000,[4] = 0x00FF00}
  27. sides =  {["Q"] = 0,["E"] = 1,["W"] = 2,["S"] = 3,["A"] = 4,["D"] = 5}
  28. pX = {}
  29. pY = {}
  30. pZ = {}
  31. pI = 0
  32. pC = 0
  33. pXk  = false
  34. pYk  = false
  35. pZk  = false
  36.  
  37. function sleep(tm)
  38.   local go = computer.uptime() + tm
  39.   while (go > computer.uptime()) do
  40.     computer.pullSignal(go - computer.uptime())
  41.   end
  42. end
  43.  
  44. function pZero(i)
  45.   pX[i] = 0
  46.   pY[i] = 0
  47.   pZ[i] = 0
  48. end
  49.  
  50. function addPath(px,py,pz)
  51.   if (px~=0) then
  52.     if (pXk==false) then
  53.       pI = pI + 1
  54.       pZero(pI)
  55.       pXk = true
  56.       pYk = false
  57.       pZk = false
  58.     end
  59.   elseif (py~=0) then
  60.     if (pYk==false) then
  61.       pI = pI + 1
  62.       pZero(pI)
  63.       pXk = false
  64.       pYk = true
  65.       pZk = false
  66.     end
  67.   elseif (pz~=0) then
  68.     if (pZk==false) then
  69.       pI = pI + 1
  70.       pZero(pI)
  71.       pXk = false
  72.       pYk = false
  73.       pZk = true
  74.     end
  75.   end
  76.   pX[pI] = pX[pI] + px
  77.   pY[pI] = pY[pI] + py
  78.   pZ[pI] = pZ[pI] + pz
  79. end
  80.  
  81. function goBack()
  82.   local i = pI
  83.   if (pC~=1) then
  84.     while (i~=0) do
  85.       sleep(2)
  86.       drn.move(-1 * pX[i], -1 * pY[i], -1 * pZ[i])
  87.       i = i - 1
  88.     end
  89.   end
  90.   pC = 1
  91. end
  92.  
  93. function goTo()
  94.   local i = 1
  95.   if (pC~=pI) then
  96.     while (i<=pI) do
  97.       sleep(2)
  98.       drn.move(pX[i],pY[i],pZ[i])
  99.       i = i + 1
  100.     end
  101.   end
  102.   pC = pI
  103. end
  104.  
  105. side = 0
  106. sideMod = false
  107. suckSlot = 1
  108.  
  109. function suck()
  110.   local i = 4
  111.   while (i>0) do
  112.     --inv.suckFromSlot(side,suckSlot)
  113.     drn.suck(0)
  114.     sleep(0.4)
  115.     suckSlot = suckSlot + 1
  116.     i = i - 1
  117.   end
  118. end
  119.  
  120. mod.open(port)
  121. drn.setLightColor(light[direct])
  122. drn.select(1)
  123.  
  124. while true do
  125.   local signal,_,remote,iport,_,cmd = computer.pullSignal()
  126.   if (signal=="modem_message") then
  127.     if (port==iport) then
  128.       if (address==nil) then address = remote end
  129.       if(address==remote) then
  130.        
  131.         if (sideMod==false) then
  132.           if     (cmd=="W") then
  133.             local mfx = facing[direct].forward.x
  134.             local mfy = facing[direct].forward.y
  135.             local mfz = facing[direct].forward.z
  136.             addPath(mfx,mfy,mfz)
  137.             drn.move(mfx,mfy,mfz)
  138.           elseif (cmd=="S") then
  139.             local mbx = facing[direct].backward.x
  140.             local mby = facing[direct].backward.y
  141.             local mbz = facing[direct].backward.z
  142.             addPath(mbx,mby,mbz)
  143.             drn.move(mbx,mby,mbz)
  144.           elseif (cmd=="A") then
  145.             direct = direct - 1
  146.             if (direct<=0) then direct = 4 end
  147.             drn.setLightColor(light[direct])
  148.           elseif (cmd=="D") then
  149.             direct = direct + 1
  150.             if (direct>=5) then direct = 1 end
  151.             drn.setLightColor(light[direct])
  152.           elseif (cmd=="Q") then
  153.             drn.move(0,-1,0)
  154.             addPath(0,-1,0)
  155.           elseif (cmd=="E") then
  156.             drn.move(0,1,0)
  157.             addPath(0,1,0)
  158.           end
  159.         else
  160.           side = sides[cmd]
  161.         end
  162.  
  163.         if (cmd=="1") then
  164.           if (sideMod==true) then sideMod = false
  165.           else sideMod = true end
  166.         elseif (cmd=="2") then
  167.           suck()
  168.         elseif (cmd=="3") then
  169.  
  170.         end
  171.  
  172.         if (cmd=="Z") then
  173.           goBack()
  174.         elseif (cmd=="X") then
  175.           goTo()
  176.         elseif (cmd=="C") then
  177.          
  178.         end
  179.       end
  180.     end
  181.   end
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement