Advertisement
BombBloke

skyTerm (ComputerCraft)

Apr 18th, 2015
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.06 KB | None | 0 0
  1. -- +--------------------------------------------------------+
  2. -- |                                                        |
  3. -- |                      skyTerm API                       |
  4. -- |                                                        |
  5. -- +--------------------------------------------------------+
  6.  
  7. local version = "Version 1.1.0"
  8.  
  9. -- By Jeffrey Alexander, aka Bomb Bloke.
  10. -- Draws pictures in the sky using the /setblock command.
  11. -- Requires a Command Computer, plus ComputerCraft 1.76+ / Minecraft 1.8+.
  12. -- http://www.computercraft.info/forums2/index.php?/topic/22967-skyterm/
  13.  
  14. -------------------------------------------------------------
  15.  
  16. -- 1.76+ required due to Command Computer bug in older ComputerCraft builds:
  17. -- https://github.com/dan200/ComputerCraft/issues/36
  18.  
  19. -------------------------------------------------------------
  20.  
  21. local palIn = fs.open("world.pal", "r")
  22. palette = textutils.unserialise(palIn.readAll())
  23. palIn.close()
  24.  
  25. local curCommands, maxCommands, tasks, colourNum = 0, 50, {}, {}
  26.  
  27. for i = 1, 16 do
  28.     colourNum[math.pow(2, i - 1)] = ("0123456789abcdef"):sub(i, i)
  29.     colourNum[("0123456789abcdef"):sub(i, i)] = i - 1
  30. end
  31.  
  32. local function doCommand(doThis)
  33.     while curCommands >= maxCommands do os.pullEvent("task_complete") end
  34.     tasks[commands.execAsync(doThis)] = true
  35.     curCommands = curCommands + 1
  36. end
  37.  
  38. function watch()
  39.     while true do
  40.         local event, id = os.pullEvent("task_complete")
  41.         if tasks[id] then curCommands, tasks[id] = curCommands - 1, nil end
  42.     end
  43. end
  44.  
  45. function create(x, y, z, facing, width, height, transCol)
  46.     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
  47.         error("skyTermSmall.create: Expected: number x, number y, number z, string facing, number width, number height.", 2)
  48.     end
  49.    
  50.     transCol = colourNum[transCol]
  51.    
  52.     local termObject, textColour, backgroundColour, curX, curY = {}, colours.white, colours.black, 1, 1
  53.    
  54.     function termObject.isColour()
  55.         return true
  56.     end
  57.    
  58.     termObject.isColor = termObject.isColour
  59.    
  60.     function termObject.getSize()
  61.         return width, height
  62.     end
  63.    
  64.     function termObject.setCursorBlink() end
  65.    
  66.     function termObject.setCursorPos(newX, newY)
  67.         if type(newX) ~= "number" or type(newY) ~= "number" then error("skyTermSmall.setCursorPos: Expected: number, number Got: " .. type(newX) .. ", " .. type(newY), 2) end
  68.         curX, curY = math.floor(newX), math.floor(newY)
  69.     end
  70.    
  71.     function termObject.getCursorPos()
  72.         return curX, curY
  73.     end
  74.    
  75.     function termObject.setTextColour(newCol)
  76.         if type(newCol) ~= "number" or not colourNum[newCol] then error("skyTermSmall.setTextColour: Expected: colour", 2) end
  77.         textColour = newCol
  78.     end
  79.    
  80.     termObject.setTextColor = termObject.setTextColour
  81.    
  82.     function termObject.setBackgroundColour(newCol)
  83.         if type(newCol) ~= "number" or not colourNum[newCol] then error("skyTermSmall.setBackgroundColour: Expected: colour", 2) end
  84.         backgroundColour = newCol
  85.     end
  86.    
  87.     termObject.setBackgroundColor = termObject.setBackgroundColour
  88.    
  89.     function termObject.getTextColour()
  90.         return textColour
  91.     end
  92.    
  93.     termObject.getTextColor = termObject.getTextColour
  94.    
  95.     function termObject.getBackgroundColour()
  96.         return backgroundColour
  97.     end
  98.    
  99.     termObject.getBackgroundColor = termObject.getBackgroundColour
  100.    
  101.     local facing, toCoordsString = facing:lower()
  102.     if facing == "north" then toCoordsString = function() return tostring(x - curX + 1).." "..tostring(y - curY + 1).." "..tostring(z) end
  103.     elseif facing == "east" then toCoordsString = function() return tostring(x).." "..tostring(y - curY + 1).." "..tostring(z - curX + 1) end
  104.     elseif facing == "south" then toCoordsString = function() return tostring(x + curX - 1).." "..tostring(y - curY + 1).." "..tostring(z) end
  105.     elseif facing == "west" then toCoordsString = function() return tostring(x).." "..tostring(y - curY + 1).." "..tostring(z + curX - 1) end
  106.     else error("skyTermSmall.create: Invalid facing. Expected: north, south, east, or west.", 2) end
  107.    
  108.     function termObject.setPixel(x, y, c)
  109.         if x < 0 or y < 0 or x > width or y > height then return end
  110.         curX, curY = x, y
  111.         local palette = palette[c]
  112.         doCommand("setblock " .. toCoordsString() .. " " .. palette[5] .. " " .. palette[6])
  113.     end
  114.    
  115.     function termObject.blit(_, _, bg)
  116.         if #bg == 0 or curY < 1 or curY > height or curX + #bg < 2 or curX > width then return end
  117.        
  118.         if curX < 1 then
  119.             bg = bg:sub(2 - curX)
  120.             curX = 1
  121.         end
  122.        
  123.         if curX + #bg - 1 > width then bg = bg:sub(1, curX + #bg - width) end
  124.        
  125.         for i = 1, #bg do
  126.             local thisChar = bg:sub(i, i)
  127.             doCommand("setblock " .. toCoordsString() .. (thisChar == transCol and " minecraft:air 0" or (" minecraft:wool " .. colourNum[thisChar])))
  128.             curX = curX + 1
  129.         end
  130.     end
  131.    
  132.     function termObject.write(text)
  133.         termObject.blit(text, text, string.rep(colourNum[backgroundColour], #text))
  134.     end
  135.    
  136.     function termObject.clearLine()
  137.         local oldX = curX
  138.         curX = 1
  139.         termObject.blit(nil, nil, string.rep(colourNum[backgroundColour], width))
  140.         curX = oldX
  141.     end
  142.    
  143.     function termObject.clear()
  144.         local oldY = curY
  145.         for i = 1, height do
  146.             curY = i
  147.             termObject.clearLine()
  148.         end
  149.         curY = oldY
  150.     end
  151.    
  152.     function termObject.scroll(lines)
  153.         if type(lines) ~= "number" or lines < 1 then error("skyTermSmall.scroll: Expected: number higher than 0", 2) end
  154.        
  155.         if lines < height then
  156.             local oldX, oldY = curX, curY
  157.  
  158.             for j = lines + 1, height do for i = 1, width do
  159.                 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)
  160.                 curX, curY = i, j - lines
  161.                 doCommand("setblock " .. toCoordsString() .. " " .. thisBlock.name .. " " .. thisBlock.metadata)
  162.             end end
  163.  
  164.             for i = height - lines + 1, height do
  165.                 curY = i
  166.                 termObject.clearLine()
  167.             end
  168.  
  169.             curX, curY = oldX, oldY
  170.         else termObject.clear() end
  171.     end
  172.    
  173.     return termObject
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement