Advertisement
Guest User

sapi.lua

a guest
Mar 4th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local ser = require("serialization")
  4. local fs = require("filesystem")
  5.  
  6. local sapi = {}
  7. sapi.robot = {}
  8. sapi.modem = {}
  9. sapi.map = {}
  10. sapi.lift = {}
  11. sapi.component = {}
  12.  
  13. ----------Local-------------
  14. local function moved(func)
  15.   if not require("robot").detect() then
  16.     require("robot").forward()
  17.   else
  18.     func.wall()
  19.   end
  20. end
  21.  
  22. local function turned(side, mSide)    
  23.   if side == 1 then
  24.     if mSide == 4 then
  25.       require("robot").turnLeft()
  26.       side = 4
  27.     end
  28.   elseif side == 4 then
  29.     if mSide == 1 then
  30.       require("robot").turnRight()
  31.       side = 1
  32.     end
  33.   end
  34.   while side ~= mSide do
  35.     if side < mSide then
  36.       require("robot").turnRight()
  37.       side = side + 1
  38.       if side > 4 then
  39.         side = 1
  40.       end
  41.     elseif mSide < side then
  42.       require("robot").turnLeft()
  43.       side = side - 1
  44.       if side < 1 then
  45.         side = 4
  46.       end
  47.     end
  48.   end
  49. end
  50.  
  51. local function mColors(color)
  52.   if color > 0 and color < 7 then
  53.     return myColors[color]
  54.   elseif color < 1 then
  55.     return myColors[6+color]
  56.   elseif color > 6 then
  57.     return myColors[color - 6]
  58.   end
  59. end
  60.  
  61. local function redPulse(side,color)
  62.   red.setBundledOutput(side, color, 251)
  63.   os.sleep(0.02)
  64.   red.setBundledOutput(side, color, 0)
  65. end
  66. ----------Robot---------------
  67.  
  68. function sapi.robot.moveToCord(a, b, func)
  69.   while b.x > a.x do
  70.     turned(a.side, 1)
  71.     moved(func)
  72.     a.x = a.x + 1
  73.     a.side = 1
  74.   end
  75.   while b.x < a.x do
  76.     turned(a.side, 3)
  77.     moved(func)
  78.     a.x = a.x - 1
  79.     a.side = 3
  80.   end
  81.  
  82.   while b.z > a.z do
  83.     turned(a.side, 2)
  84.     moved(func)
  85.     a.z = a.z + 1
  86.     a.side = 2
  87.   end
  88.   while b.z < a.z do
  89.     turned(a.side, 4)
  90.     moved(func)
  91.     a.z = a.z - 1
  92.     a.side = 4
  93.   end
  94.  
  95.   while b.y > a.y do
  96.     if not require("robot").detectUp() then
  97.       require("robot").up()
  98.       a.y = a.y + 1
  99.     else
  100.       func.uWall()
  101.     end
  102.   end
  103.   while b.y < a.y do
  104.     if not require("robot").detectDown() then
  105.       require("robot").down()
  106.       a.y = a.y - 1
  107.     else
  108.       func.dWall()
  109.     end
  110.   end
  111.   if b.side ~= nil then
  112.     turned(a.side, b.side)
  113.   else
  114.     return a.side
  115.   end
  116. end
  117.  
  118. ------------Map-------------
  119.  
  120. function sapi.map.algLi2D(grid,ax, ay, bx, by)
  121.   local W      = #grid[1]
  122.   local H      = #grid
  123.   local WALL   = -1
  124.   local BLANK  = -2
  125.   local d, x, y, k
  126.   local stop
  127.   local px={}
  128.   local py={}
  129.   dx = {1, 0, -1, 0}
  130.   dy = {0, 1, 0, -1}
  131.   d = 0
  132.   grid[ay][ax] = 0
  133.   while not stop and grid[by][bx] == BLANK do
  134.     stop = true
  135.     for y=0,H do
  136.       for x=0,W do
  137.         if grid[y][x] == d then
  138.           for k=1,4 do
  139.             gx = x + dx[k]
  140.             gy = y + dy[k]
  141.             --if gx > 0 and gy > 0 then
  142.               if grid[gy][gx] == BLANK then
  143.                 stop=false
  144.                 grid[y + dy[k]][x + dx[k]] = d + 1
  145.               end
  146.             --end
  147.           end
  148.         end
  149.       end
  150.     end
  151.     d=d+1
  152.   end
  153.   if grid[by][bx] == BLANK then return false,nil,nil end
  154.   len = grid[by][bx]
  155.   x = bx
  156.   y = by
  157.   d = len
  158.   while d > 0 do
  159.     px[d] = x
  160.     py[d] = y
  161.     for k=1,4 do
  162.       if grid[y + dy[k]][x + dx[k]] == d then
  163.         x = x + dx[k]
  164.         y = y + dy[k]
  165.       end
  166.     end
  167.     d=d-1
  168.   end
  169.   px[0] = ax
  170.   py[0] = ay
  171.   return true,px,py
  172. end
  173.  
  174. function sapi.map.algLi3D(grid,a, b)
  175.   local W      = #grid[1]
  176.   local H      = #grid
  177.   local J      = #grid[1][1]
  178.   local WALL   = -1
  179.   local BLANK  = -2
  180.   local d, x, y, z, k
  181.   local stop
  182.   local px={}
  183.   local py={}
  184.   local pz={}
  185.   local dx = {1, 0, -1, 0, 0, 0}
  186.   local dy = {0, 1, 0, -1, 0, 0}
  187.   local dz = {0, 0, 0, 0, 1, -1}
  188.   local d = 0
  189.   grid[a[2]][a[1]][a[3]] = 0
  190.   while not stop and grid[b[2]][b[1]][b[3]] == BLANK do
  191.     stop = true
  192.     for y=0,H do
  193.       for x=0,W do
  194.         for z=0,J do
  195.           if grid[y][x][z] == d then
  196.             for k=1,6 do
  197.               if x + dx[k]>0 and y + dy[k]>0 and z + dz[k]>0 then
  198.                 if grid[y + dy[k]][x + dx[k]][z + dz[k]] == BLANK then
  199.                   stop=false
  200.                   grid[y + dy[k]][x + dx[k]][z + dz[k]] = d + 1
  201.                 end
  202.               end
  203.             end
  204.           end
  205.         end
  206.       end
  207.     end
  208.     d=d+1
  209.   end
  210.   if grid[b[2]][b[1]][b[3]] == BLANK then return false,nil,nil end
  211.   len = grid[b[2]][b[1]][b[3]]
  212.   x = b[1]
  213.   y = b[2]
  214.   z = b[3]
  215.   d = len
  216.   while d > 0 do
  217.     px[d] = x
  218.     py[d] = y
  219.     pz[d] = z
  220.     for k=1,6 do
  221.       if grid[y + dy[k]][x + dx[k]][z + dz[k]] == d then
  222.         x = x + dx[k]
  223.           y = y + dy[k]
  224.         z = z + dz[k]
  225.       end
  226.     end
  227.     d=d-1
  228.   end
  229.   px[0] = a[1]
  230.   py[0] = a[2]
  231.   pz[0] = a[3]
  232.   return true,px,py,pz
  233. end
  234.  
  235.  -------------Modem-------------
  236.  
  237. function sapi.modem.sendFile(fileName)
  238.   local modem = component.modem
  239.   local f = io.open(fileName)
  240.   modem.open(742)
  241.   modem.broadcast(742, ser.serialize({"tFile", fileName}))
  242.   _, _, from, _, _, message = event.pull("modem_message")
  243.   if message == "tFileDone" then
  244.     local tAdress = from
  245.     for line in f:lines() do
  246.       modem.send(tAdress, 742, line)
  247.       _, _, from, _, _, message = event.pull("modem_message")
  248.     end
  249.     modem.send(tAdress, 742, "finish")
  250.   end
  251.   f:close()
  252. end
  253.  
  254. function sapi.modem.receiveFile()
  255.   local modem = component.modem
  256.   modem.open(742)
  257.   while not stop do
  258.     evType, _, from, port, _, message = event.pull()
  259.     if evType == "key_down" then
  260.       if from == 115 then
  261.         stop = true
  262.       end
  263.     elseif evType == "modem_message" and ser.unserialize(message)[1] == "tFile" then
  264.       tAdress = from
  265.       local tFile = fs.open(ser.unserialize(message)[2], "wb")
  266.       modem.send(from, 742, "tFileDone")
  267.       while not stop do
  268.         _, _, from, _, _, message = event.pull("modem_message")
  269.         if from == tAdress and message ~= "finish" and message ~= nil then
  270.           tFile:write(message.."\n")
  271.           os.sleep(0.01)
  272.           modem.send(tAdress, 742, "next")
  273.         elseif from == tAdress and message == "finish" then
  274.           stop = true
  275.         end
  276.       end
  277.       tFile:close()
  278.       stop = true
  279.     end
  280.   end
  281. end
  282.  
  283. -------------Lift---------------
  284.  
  285. function sapi.lift.init(userCabHeight, usernZicle, userColors, userLayers)
  286.   local nZicle = usernZicle
  287.   local cabHeight = userCabHeight
  288.   local liftLayers = userLayers
  289.   local liftColors = userColors
  290. end
  291.  
  292. function sapi.lift.up()
  293.   redPulse(sides.top, mColors(cabHeight))
  294.   redPulse(sides.top, mColors(cabHeight+2))
  295.   cabHeight = cabHeight + 1
  296.   if cabHeight == 7 then
  297.     cabHeight = 1
  298.     nZicle = nZicle + 1
  299.   end
  300. end
  301.  
  302. function sapi.lift.down()
  303.   redPulse(sides.top, mColors(cabHeight+4))
  304.   redPulse(sides.top, mColors(cabHeight+2))
  305.   cabHeight = cabHeight - 1
  306.   if cabHeight == 0 then
  307.     cabHeight = 6
  308.     nZicle = nZicle - 1
  309.   end
  310. end
  311.  
  312. function sapi.lift.goTo(toLayer)
  313.   while liftLayers[toLayer] > nZicle * 6 + cabHeight do
  314.     sapi.lift.up()
  315.   end
  316.   while liftLayers[toLayer] < nZicle * 6 + cabHeight do
  317.     sapi.lift.down()
  318.   end
  319. end
  320.  
  321. function sapi.lift.getHeight()
  322.   return cabHeight, nZicle
  323. end
  324.  
  325. ----------Component---------
  326.  
  327. function sapi.component.init(fileName)
  328.   sapi.component.file = io.open(fileName, "a")
  329. end
  330.  
  331. function sapi.component.addComponent(cID, cName)
  332.   local addedComponent = {}
  333.   addedComponent.name = cName
  334.   addedComponent.ID = cID
  335.   local str = ser.serialize(addedComponent)
  336.   sapi.component.file:write(str.."\n")
  337. end
  338.  
  339. function sapi.component.returnID(nameFile,cName)
  340.   local componentInfo
  341.   for line in io.lines(nameFile) do
  342.     componentInfo = ser.unserialize(line)
  343.     if componentInfo.name == cName then
  344.       return componentInfo.ID
  345.     end
  346.   end
  347. end
  348.  
  349. function sapi.component.close()
  350.   sapi.component.file:close()
  351. end
  352.  
  353. -------------------
  354.  
  355. return sapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement