Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local ser = require("serialization")
- local fs = require("filesystem")
- local sapi = {}
- sapi.robot = {}
- sapi.modem = {}
- sapi.map = {}
- sapi.lift = {}
- sapi.component = {}
- ----------Local-------------
- local function moved(func)
- if not require("robot").detect() then
- require("robot").forward()
- else
- func.wall()
- end
- end
- local function turned(side, mSide)
- if side == 1 then
- if mSide == 4 then
- require("robot").turnLeft()
- side = 4
- end
- elseif side == 4 then
- if mSide == 1 then
- require("robot").turnRight()
- side = 1
- end
- end
- while side ~= mSide do
- if side < mSide then
- require("robot").turnRight()
- side = side + 1
- if side > 4 then
- side = 1
- end
- elseif mSide < side then
- require("robot").turnLeft()
- side = side - 1
- if side < 1 then
- side = 4
- end
- end
- end
- end
- local function mColors(color)
- if color > 0 and color < 7 then
- return myColors[color]
- elseif color < 1 then
- return myColors[6+color]
- elseif color > 6 then
- return myColors[color - 6]
- end
- end
- local function redPulse(side,color)
- red.setBundledOutput(side, color, 251)
- os.sleep(0.02)
- red.setBundledOutput(side, color, 0)
- end
- ----------Robot---------------
- function sapi.robot.moveToCord(a, b, func)
- while b.x > a.x do
- turned(a.side, 1)
- moved(func)
- a.x = a.x + 1
- a.side = 1
- end
- while b.x < a.x do
- turned(a.side, 3)
- moved(func)
- a.x = a.x - 1
- a.side = 3
- end
- while b.z > a.z do
- turned(a.side, 2)
- moved(func)
- a.z = a.z + 1
- a.side = 2
- end
- while b.z < a.z do
- turned(a.side, 4)
- moved(func)
- a.z = a.z - 1
- a.side = 4
- end
- while b.y > a.y do
- if not require("robot").detectUp() then
- require("robot").up()
- a.y = a.y + 1
- else
- func.uWall()
- end
- end
- while b.y < a.y do
- if not require("robot").detectDown() then
- require("robot").down()
- a.y = a.y - 1
- else
- func.dWall()
- end
- end
- if b.side ~= nil then
- turned(a.side, b.side)
- else
- return a.side
- end
- end
- ------------Map-------------
- function sapi.map.algLi2D(grid,ax, ay, bx, by)
- local W = #grid[1]
- local H = #grid
- local WALL = -1
- local BLANK = -2
- local d, x, y, k
- local stop
- local px={}
- local py={}
- dx = {1, 0, -1, 0}
- dy = {0, 1, 0, -1}
- d = 0
- grid[ay][ax] = 0
- while not stop and grid[by][bx] == BLANK do
- stop = true
- for y=0,H do
- for x=0,W do
- if grid[y][x] == d then
- for k=1,4 do
- gx = x + dx[k]
- gy = y + dy[k]
- --if gx > 0 and gy > 0 then
- if grid[gy][gx] == BLANK then
- stop=false
- grid[y + dy[k]][x + dx[k]] = d + 1
- end
- --end
- end
- end
- end
- end
- d=d+1
- end
- if grid[by][bx] == BLANK then return false,nil,nil end
- len = grid[by][bx]
- x = bx
- y = by
- d = len
- while d > 0 do
- px[d] = x
- py[d] = y
- for k=1,4 do
- if grid[y + dy[k]][x + dx[k]] == d then
- x = x + dx[k]
- y = y + dy[k]
- end
- end
- d=d-1
- end
- px[0] = ax
- py[0] = ay
- return true,px,py
- end
- function sapi.map.algLi3D(grid,a, b)
- local W = #grid[1]
- local H = #grid
- local J = #grid[1][1]
- local WALL = -1
- local BLANK = -2
- local d, x, y, z, k
- local stop
- local px={}
- local py={}
- local pz={}
- local dx = {1, 0, -1, 0, 0, 0}
- local dy = {0, 1, 0, -1, 0, 0}
- local dz = {0, 0, 0, 0, 1, -1}
- local d = 0
- grid[a[2]][a[1]][a[3]] = 0
- while not stop and grid[b[2]][b[1]][b[3]] == BLANK do
- stop = true
- for y=0,H do
- for x=0,W do
- for z=0,J do
- if grid[y][x][z] == d then
- for k=1,6 do
- if x + dx[k]>0 and y + dy[k]>0 and z + dz[k]>0 then
- if grid[y + dy[k]][x + dx[k]][z + dz[k]] == BLANK then
- stop=false
- grid[y + dy[k]][x + dx[k]][z + dz[k]] = d + 1
- end
- end
- end
- end
- end
- end
- end
- d=d+1
- end
- if grid[b[2]][b[1]][b[3]] == BLANK then return false,nil,nil end
- len = grid[b[2]][b[1]][b[3]]
- x = b[1]
- y = b[2]
- z = b[3]
- d = len
- while d > 0 do
- px[d] = x
- py[d] = y
- pz[d] = z
- for k=1,6 do
- if grid[y + dy[k]][x + dx[k]][z + dz[k]] == d then
- x = x + dx[k]
- y = y + dy[k]
- z = z + dz[k]
- end
- end
- d=d-1
- end
- px[0] = a[1]
- py[0] = a[2]
- pz[0] = a[3]
- return true,px,py,pz
- end
- -------------Modem-------------
- function sapi.modem.sendFile(fileName)
- local modem = component.modem
- local f = io.open(fileName)
- modem.open(742)
- modem.broadcast(742, ser.serialize({"tFile", fileName}))
- _, _, from, _, _, message = event.pull("modem_message")
- if message == "tFileDone" then
- local tAdress = from
- for line in f:lines() do
- modem.send(tAdress, 742, line)
- _, _, from, _, _, message = event.pull("modem_message")
- end
- modem.send(tAdress, 742, "finish")
- end
- f:close()
- end
- function sapi.modem.receiveFile()
- local modem = component.modem
- modem.open(742)
- while not stop do
- evType, _, from, port, _, message = event.pull()
- if evType == "key_down" then
- if from == 115 then
- stop = true
- end
- elseif evType == "modem_message" and ser.unserialize(message)[1] == "tFile" then
- tAdress = from
- local tFile = fs.open(ser.unserialize(message)[2], "wb")
- modem.send(from, 742, "tFileDone")
- while not stop do
- _, _, from, _, _, message = event.pull("modem_message")
- if from == tAdress and message ~= "finish" and message ~= nil then
- tFile:write(message.."\n")
- os.sleep(0.01)
- modem.send(tAdress, 742, "next")
- elseif from == tAdress and message == "finish" then
- stop = true
- end
- end
- tFile:close()
- stop = true
- end
- end
- end
- -------------Lift---------------
- function sapi.lift.init(userCabHeight, usernZicle, userColors, userLayers)
- local nZicle = usernZicle
- local cabHeight = userCabHeight
- local liftLayers = userLayers
- local liftColors = userColors
- end
- function sapi.lift.up()
- redPulse(sides.top, mColors(cabHeight))
- redPulse(sides.top, mColors(cabHeight+2))
- cabHeight = cabHeight + 1
- if cabHeight == 7 then
- cabHeight = 1
- nZicle = nZicle + 1
- end
- end
- function sapi.lift.down()
- redPulse(sides.top, mColors(cabHeight+4))
- redPulse(sides.top, mColors(cabHeight+2))
- cabHeight = cabHeight - 1
- if cabHeight == 0 then
- cabHeight = 6
- nZicle = nZicle - 1
- end
- end
- function sapi.lift.goTo(toLayer)
- while liftLayers[toLayer] > nZicle * 6 + cabHeight do
- sapi.lift.up()
- end
- while liftLayers[toLayer] < nZicle * 6 + cabHeight do
- sapi.lift.down()
- end
- end
- function sapi.lift.getHeight()
- return cabHeight, nZicle
- end
- ----------Component---------
- function sapi.component.init(fileName)
- sapi.component.file = io.open(fileName, "a")
- end
- function sapi.component.addComponent(cID, cName)
- local addedComponent = {}
- addedComponent.name = cName
- addedComponent.ID = cID
- local str = ser.serialize(addedComponent)
- sapi.component.file:write(str.."\n")
- end
- function sapi.component.returnID(nameFile,cName)
- local componentInfo
- for line in io.lines(nameFile) do
- componentInfo = ser.unserialize(line)
- if componentInfo.name == cName then
- return componentInfo.ID
- end
- end
- end
- function sapi.component.close()
- sapi.component.file:close()
- end
- -------------------
- return sapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement