Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- +--------------------------------------------------------+
- -- | |
- -- | partTerm API |
- -- | |
- -- +--------------------------------------------------------+
- local version = "Version 1.0.1"
- -- By Jeffrey Alexander, aka Bomb Bloke.
- -- Draws pictures in the sky using the /particle command.
- -- Requires a Command Computer, plus ComputerCraft 1.76+ / Minecraft 1.8+.
- -- http://www.computercraft.info/forums2/index.php?/topic/26705-mc18-partterm-api/
- -------------------------------------------------------------
- local curCommands, maxCommands, tasks = 0, 50, {}
- local colourChar, windows = {}, {}
- local CCPal = {[0] = {240,240,240},{242,178, 51},{229,127,216},{153,178,242},
- {222,222,108},{127,204, 25},{242,178,204},{ 76, 76, 76},
- {153,153,153},{ 76,153,178},{178,102,229},{ 51,102,204},
- {127,102, 76},{ 87,166, 78},{204, 76, 76},{ 25, 25, 25}}
- do
- local hex, temp1, temp2 = "0123456789abcdef", {}, {}
- for i = 1, 16 do
- colourChar[2 ^ (i - 1)] = hex:sub(i, i)
- local thisPal = CCPal[i - 1]
- temp1[hex:sub(i, i)] = " " .. tostring(thisPal[1] / 255 - 1) .. " " .. tostring(thisPal[2] / 255) .. " " .. tostring(thisPal[3] / 255) .. " 1"
- temp2[hex:sub(i, i)] = " " .. tostring(thisPal[1] / 255) .. " " .. tostring(thisPal[2] / 255) .. " " .. tostring(thisPal[3] / 255) .. " 1"
- end
- temp1["f"] = " -0.01 0 0 0.05"
- CCPal = {["reddust"] = temp1, ["mobSpell"] = temp2, ["mobSpellAmbient"] = temp2}
- end
- local function snooze()
- local myEvent = tostring({})
- os.queueEvent(myEvent)
- os.pullEvent(myEvent)
- end
- local function doCommand(doThis)
- while curCommands >= maxCommands do os.pullEvent("task_complete") end
- tasks[commands.execAsync(doThis)] = true
- curCommands = curCommands + 1
- end
- local function rotatePoint(x, y, angle)
- local sin, cos = math.sin(angle), math.cos(angle)
- return x * cos - y * sin, x * sin + y * cos
- end
- function getHorizontalAngle(x1, z1, x2, z2)
- return math.deg(math.atan2(z2 - z1, x2 - x1)) -- + 90
- end
- function getVerticalAngle(x1, y1, z1, x2, y2, z2)
- return math.deg(math.atan2(math.sqrt((x2 - x1) ^ 2 + (z2 - z1) ^ 2), y2 - y1)) -- - 90
- end
- function setDensity(commands)
- if type(commands) ~= "number" then
- error("partTerm.setDensity(): expected number")
- elseif commands < 1 then
- commands = 1
- elseif commands > 250 then
- commands = 250
- end
- curCommands, maxCommands, tasks = 0, math.floor(commands), {}
- end
- function remove(window)
- windows[window] = nil
- end
- function removeAll()
- windows = {}
- end
- function render()
- parallel.waitForAny(
- function()
- while true do
- local event, id = os.pullEvent("task_complete")
- if tasks[id] then curCommands, tasks[id] = curCommands - 1, nil end
- end
- end,
- function()
- while true do
- for window, values in pairs(windows) do
- local buffer, x, y, z, width, height, transCol, rotHori, rotVert, visible, spacing, bumps, partType = unpack(values)
- if visible then
- local rowXBump, rowYBump, rowZBump, colXBump, colYBump, colZBump, offXBump, offYBump, offZBump = unpack(bumps)
- local yXBump, yYBump, yZBump, yy, pal = 0, 0, 0, 1, CCPal[partType]
- x, y, z = x + offXBump, y + offYBump, z + offZBump
- while yy <= height do
- local thisRow = buffer[yy]
- local xXBump, xYBump, xZBump = yXBump, yYBump, yZBump
- for xx = 1, width do
- local thisChar = thisRow[xx]
- if thisChar ~= transCol then
- local curX, curY, curZ = x + xXBump, y + xYBump, z + xZBump
- if math.floor(curX) == curX then curX = curX + 0.0001 end
- if math.floor(curY) == curY then curY = curY + 0.0001 end
- if math.floor(curZ) == curZ then curZ = curZ + 0.0001 end
- doCommand("particle " .. partType .. " ".. curX .. " " .. curY .. " " .. curZ .. (pal and pal[thisChar] or " 0 0 0 1"))
- end
- xXBump, xYBump, xZBump = xXBump + rowXBump, xYBump + rowYBump, xZBump + rowZBump
- end
- yXBump, yYBump, yZBump, yy = yXBump + colXBump, yYBump + colYBump, yZBump + colZBump, yy + 1
- end
- end
- snooze()
- end
- end
- end
- )
- end
- function createWindow(x, y, z, width, height, transCol, rotHori, rotVert, visible, spacing, partType, center)
- local buffer, window, tCol, bCol, curX, curY = {}, {}, colours.white, colours.black, 1, 1
- local vals = {buffer, x, y, z, width, height, colourChar[transCol] or false, nil, nil, type(visible) ~= "boolean" and true or visible, spacing or 0.2, nil, partType or "reddust", type(center) ~= "boolean" and true or center}
- windows[window] = vals
- window.blit = function(_, _, bC)
- local width, height = vals[5], vals[6]
- local bClen = #bC
- if curX > width or curX + bClen < 2 or curY < 1 or curY > height then
- curX = curX + bClen
- return
- end
- if curX < 1 then
- bC = bC:sub(2 - curX)
- curX, bClen = 1, #bC
- end
- if curX + bClen - 1 > width then bC, bClen = bC:sub(1, width - curX + 1), width - curX + 1 end
- local row = buffer[curY]
- for x = 1, bClen do row[x + curX - 1] = bC:sub(x, x) end
- curX = curX + bClen
- end
- window.write = function(text)
- window.blit(nil, nil, string.rep(colourChar[bCol], #tostring(text)))
- end
- window.clearLine = function()
- local colChar, row = colourChar[bCol], buffer[curY]
- for x = 1, vals[5] do row[x] = colChar end
- end
- window.clear = function()
- local colChar, width = colourChar[bCol], vals[5]
- for y = 1, vals[6] do
- local row = {}
- for x = 1, width do row[x] = colChar end
- buffer[y] = row
- end
- end
- window.getCursorPos = function()
- return curX, curY
- end
- window.setCursorPos = function(newX, newY)
- curX, curY = math.floor(newX), math.floor(newY)
- end
- window.setCursorBlink = function() end
- window.isColour = function()
- return true
- end
- window.isColor = window.isColour
- window.getSize = function()
- return vals[5], vals[6]
- end
- window.scroll = function(lines)
- lines = math.floor(lines)
- if lines ~= 0 then
- local newBuff, colChar, width, height = {}, colourChar[bCol], vals[5], vals[6]
- for i = 1, height do
- if i + lines > 0 and i + lines <= height then
- newBuff[i] = buffer[i + lines]
- else
- local row = {}
- for x = 1, width do row[x] = colChar end
- newBuff[i] = row
- end
- end
- buffer, vals[1] = newBuff, newBuff
- end
- end
- window.setTextColour = function(newCol)
- tCol = newCol
- end
- window.setTextColor = window.setTextColour
- window.setBackgroundColour = function(newCol)
- bCol = newCol
- end
- window.setBackgroundColor = window.setBackgroundColour
- window.getTextColour = function()
- return tCol
- end
- window.getTextColor = window.getTextColour
- window.getBackgroundColour = function()
- return bCol
- end
- window.getBackgroundColor = window.getBackgroundColour
- window.setVisible = function(newVis)
- vals[10] = newVis and true or false
- end
- window.setSpacing = function(newSpacing)
- vals[11] = newSpacing
- window.setAngles(window.getAngles())
- end
- window.getPosition = function()
- return vals[2], vals[3], vals[4]
- end
- window.getAngles = function()
- return vals[8], vals[9]
- end
- window.setParticle = function(newPart)
- vals[13] = newPart
- end
- window.center = function(center)
- vals[14] = type(center) ~= "boolean" and true or center
- window.setAngles(window.getAngles())
- end
- window.reposition = function(newX, newY, newZ, newWidth, newHeight)
- if type(newX) == "number" then vals[2] = newX end
- if type(newY) == "number" then vals[3] = newY end
- if type(newZ) == "number" then vals[4] = newZ end
- if newWidth or newHeight then
- local oldW, oldH, colChar = vals[5], vals[6], colourChar[bCol]
- if not newWidth then newWidth = oldW end
- if not newHeight then newHeight = oldH end
- local newBuff = {}
- for y = 1, newHeight do
- local row = {}
- if y <= oldH then
- local oldRow = buffer[y]
- for x = 1, newWidth do row[x] = oldRow[x] or colChar end
- else
- for x = 1, newWidth do row[x] = colChar end
- end
- newBuff[y] = row
- end
- buffer, vals[1], vals[5], vals[6] = newBuff, newBuff, newWidth, newHeight
- window.setAngles(window.getAngles())
- end
- end
- window.setAngles = function(rotHori, rotVert)
- if type(rotHori) == "number" then
- rotHori = math.rad(rotHori + 180)
- vals[8] = rotHori
- else rotHori = vals[8] end
- if type(rotVert) == "number" then
- rotVert = math.rad(rotVert)
- vals[9] = rotVert
- else rotVert = vals[9] end
- local spacing = vals[11]
- local rowXBump, rowYBump, rowZBump
- rowZBump, rowYBump = rotatePoint(0, 0, rotVert)
- rowXBump, rowZBump = rotatePoint(1, rowZBump, rotHori)
- rowXBump, rowYBump, rowZBump = rowXBump * spacing, -(rowYBump * spacing), rowZBump * spacing
- local colXBump, colYBump, colZBump
- colZBump, colYBump = rotatePoint(0, 1, rotVert)
- colXBump, colZBump = rotatePoint(0, colZBump, rotHori)
- colXBump, colYBump, colZBump = colXBump * spacing, -(colYBump * spacing), colZBump * spacing
- local offXBump, offYBump, offZBump
- if vals[14] then
- local width, height = -(vals[5] / 2), -(vals[6] / 2)
- offXBump, offYBump, offZBump = rowXBump * width + colXBump * height, rowYBump * width + colYBump * height, rowZBump * width + colZBump * height
- else offXBump, offYBump, offZBump = 0, 0, 0 end
- vals[12] = {rowXBump, rowYBump, rowZBump, colXBump, colYBump, colZBump, offXBump, offYBump, offZBump}
- end
- window.setAngles(rotHori or 0, rotVert or 0)
- window.clear()
- return window
- end
Add Comment
Please, Sign In to add comment