Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- +--------------------------------------------------------+
- -- | |
- -- | skyTerm API |
- -- | |
- -- +--------------------------------------------------------+
- local version = "Version 1.1.0"
- -- By Jeffrey Alexander, aka Bomb Bloke.
- -- Draws pictures in the sky using the /setblock command.
- -- Requires a Command Computer, plus ComputerCraft 1.76+ / Minecraft 1.8+.
- -- http://www.computercraft.info/forums2/index.php?/topic/22967-skyterm/
- -------------------------------------------------------------
- -- 1.76+ required due to Command Computer bug in older ComputerCraft builds:
- -- https://github.com/dan200/ComputerCraft/issues/36
- -------------------------------------------------------------
- local palIn = fs.open("world.pal", "r")
- palette = textutils.unserialise(palIn.readAll())
- palIn.close()
- local curCommands, maxCommands, tasks, colourNum = 0, 50, {}, {}
- for i = 1, 16 do
- colourNum[math.pow(2, i - 1)] = ("0123456789abcdef"):sub(i, i)
- colourNum[("0123456789abcdef"):sub(i, i)] = i - 1
- end
- local function doCommand(doThis)
- while curCommands >= maxCommands do os.pullEvent("task_complete") end
- tasks[commands.execAsync(doThis)] = true
- curCommands = curCommands + 1
- end
- function watch()
- while true do
- local event, id = os.pullEvent("task_complete")
- if tasks[id] then curCommands, tasks[id] = curCommands - 1, nil end
- end
- end
- function create(x, y, z, facing, width, height, transCol)
- if not (type(x) == "number" and type(y) == "number" and type(z) == "number" and type(facing) == "string" and type(width) == "number" and type(height) == "number" and (type(transCol) == "number" or type(transCol) == "nil")) then
- error("skyTermSmall.create: Expected: number x, number y, number z, string facing, number width, number height.", 2)
- end
- transCol = colourNum[transCol]
- local termObject, textColour, backgroundColour, curX, curY = {}, colours.white, colours.black, 1, 1
- function termObject.isColour()
- return true
- end
- termObject.isColor = termObject.isColour
- function termObject.getSize()
- return width, height
- end
- function termObject.setCursorBlink() end
- function termObject.setCursorPos(newX, newY)
- if type(newX) ~= "number" or type(newY) ~= "number" then error("skyTermSmall.setCursorPos: Expected: number, number Got: " .. type(newX) .. ", " .. type(newY), 2) end
- curX, curY = math.floor(newX), math.floor(newY)
- end
- function termObject.getCursorPos()
- return curX, curY
- end
- function termObject.setTextColour(newCol)
- if type(newCol) ~= "number" or not colourNum[newCol] then error("skyTermSmall.setTextColour: Expected: colour", 2) end
- textColour = newCol
- end
- termObject.setTextColor = termObject.setTextColour
- function termObject.setBackgroundColour(newCol)
- if type(newCol) ~= "number" or not colourNum[newCol] then error("skyTermSmall.setBackgroundColour: Expected: colour", 2) end
- backgroundColour = newCol
- end
- termObject.setBackgroundColor = termObject.setBackgroundColour
- function termObject.getTextColour()
- return textColour
- end
- termObject.getTextColor = termObject.getTextColour
- function termObject.getBackgroundColour()
- return backgroundColour
- end
- termObject.getBackgroundColor = termObject.getBackgroundColour
- local facing, toCoordsString = facing:lower()
- if facing == "north" then toCoordsString = function() return tostring(x - curX + 1).." "..tostring(y - curY + 1).." "..tostring(z) end
- elseif facing == "east" then toCoordsString = function() return tostring(x).." "..tostring(y - curY + 1).." "..tostring(z - curX + 1) end
- elseif facing == "south" then toCoordsString = function() return tostring(x + curX - 1).." "..tostring(y - curY + 1).." "..tostring(z) end
- elseif facing == "west" then toCoordsString = function() return tostring(x).." "..tostring(y - curY + 1).." "..tostring(z + curX - 1) end
- else error("skyTermSmall.create: Invalid facing. Expected: north, south, east, or west.", 2) end
- function termObject.setPixel(x, y, c)
- if x < 0 or y < 0 or x > width or y > height then return end
- curX, curY = x, y
- local palette = palette[c]
- doCommand("setblock " .. toCoordsString() .. " " .. palette[5] .. " " .. palette[6])
- end
- function termObject.blit(_, _, bg)
- if #bg == 0 or curY < 1 or curY > height or curX + #bg < 2 or curX > width then return end
- if curX < 1 then
- bg = bg:sub(2 - curX)
- curX = 1
- end
- if curX + #bg - 1 > width then bg = bg:sub(1, curX + #bg - width) end
- for i = 1, #bg do
- local thisChar = bg:sub(i, i)
- doCommand("setblock " .. toCoordsString() .. (thisChar == transCol and " minecraft:air 0" or (" minecraft:wool " .. colourNum[thisChar])))
- curX = curX + 1
- end
- end
- function termObject.write(text)
- termObject.blit(text, text, string.rep(colourNum[backgroundColour], #text))
- end
- function termObject.clearLine()
- local oldX = curX
- curX = 1
- termObject.blit(nil, nil, string.rep(colourNum[backgroundColour], width))
- curX = oldX
- end
- function termObject.clear()
- local oldY = curY
- for i = 1, height do
- curY = i
- termObject.clearLine()
- end
- curY = oldY
- end
- function termObject.scroll(lines)
- if type(lines) ~= "number" or lines < 1 then error("skyTermSmall.scroll: Expected: number higher than 0", 2) end
- if lines < height then
- local oldX, oldY = curX, curY
- for j = lines + 1, height do for i = 1, width do
- local thisBlock = commands.getBlockInfo((facing == "north") and (x - i + 1) or (facing == "south") and (x + i - 1) or x, y - j + 1, (facing == "east") and (z - i + 1) or (facing == "west") and (z + i - 1) or z)
- curX, curY = i, j - lines
- doCommand("setblock " .. toCoordsString() .. " " .. thisBlock.name .. " " .. thisBlock.metadata)
- end end
- for i = height - lines + 1, height do
- curY = i
- termObject.clearLine()
- end
- curX, curY = oldX, oldY
- else termObject.clear() end
- end
- return termObject
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement