Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Hideously Smashed Together by Compilr, a Hideous Smash-Stuff-Togetherer, (c) 2014 oeed --
- -- This file REALLLLLLLY isn't suitable to be used for anything other than being executed --
- -- To extract all the files, run: "<filename> --extract" in the Shell --
- --DONT WORRY ABOUT
- --ANYTHING BELOW HERE
- --JUST RUN THIS FILE AS
- --> build --extract
- --THEN RUN STARTUP
- --(or main.lua if
- --your a fancy)
- local files = {
- [ "rgb.lua" ] = "-- RGB API version 1.0 by CrazedProgrammer\
- -- You can find info and documentation on these pages:\
- -- http://cp.msdev.nl/computercraft/rgb-api/\
- -- You may use this in your ComputerCraft programs and modify it without asking.\
- -- However, you may not publish this API under your name without asking me.\
- -- If you have any suggestions, bug reports or questions then please send an email to:\
- -- crazedprogrammer@gmail.com\
- local hex = {\"F0F0F0\", \"F2B233\", \"E57FD8\", \"99B2F2\", \"DEDE6C\", \"7FCC19\", \"F2B2CC\", \"4C4C4C\", \"999999\", \"4C99B2\", \"B266E5\", \"3366CC\", \"7F664C\", \"57A64E\", \"CC4C4C\", \"191919\"}\
- local rgb = {}\
- \
- local frgb_memory = {}\
- \
- for i=1,16,1 do\
- rgb[i] = {tonumber(hex[i]:sub(1, 2), 16), tonumber(hex[i]:sub(3, 4), 16), tonumber(hex[i]:sub(5, 6), 16)}\
- end\
- local rgb2 = {}\
- for i=1,16,1 do\
- rgb2[i] = {}\
- for j=1,16,1 do\
- rgb2[i][j] = {(rgb[i][1] * 34 + rgb[j][1] * 20) / 54, (rgb[i][2] * 34 + rgb[j][2] * 20) / 54, (rgb[i][3] * 34 + rgb[j][3] * 20) / 54}\
- end\
- end\
- \
- colors.fromRGB = function (r, g, b)\
- local sav = bit.blshift( ( bit.blshift( r, 8) + g ), 8) + b\
- if frgb_memory[sav] then return frgb_memory[sav] end\
- local dist = 1e100\
- local d = 1e100\
- local color = -1\
- for i=1,16,1 do\
- d = math.sqrt((math.max(rgb[i][1], r) - math.min(rgb[i][1], r)) ^ 2 + (math.max(rgb[i][2], g) - math.min(rgb[i][2], g)) ^ 2 + (math.max(rgb[i][3], b) - math.min(rgb[i][3], b)) ^ 2)\
- if d < dist then\
- dist = d\
- color = i - 1\
- end\
- end\
- frgb_memory[sav] = 2 ^ color\
- return 2 ^ color\
- end\
- \
- colors.toRGB = function(color)\
- return unpack(rgb[math.floor(math.log(color) / math.log(2) + 1)])\
- end\
- \
- colors.fromRGB2 = function (r, g, b)\
- local dist = 1e100\
- local d = 1e100\
- local color1 = -1\
- local color2 = -1\
- for i=1,16,1 do\
- for j=1,16,1 do\
- d = math.sqrt((math.max(rgb2[i][j][1], r) - math.min(rgb2[i][j][1], r)) ^ 2 + (math.max(rgb2[i][j][2], g) - math.min(rgb2[i][j][2], g)) ^ 2 + (math.max(rgb2[i][j][3], b) - math.min(rgb2[i][j][3], b)) ^ 2)\
- if d < dist then\
- dist = d\
- color1 = i - 1\
- color2 = j - 1\
- end\
- end\
- end\
- return 2 ^ color1, 2 ^ color2\
- end\
- \
- colors.toRGB2 = function(color1, color2, str)\
- local c1 = math.floor(math.log(color1) / math.log(2) + 1)\
- local c2 = math.floor(math.log(color2) / math.log(2) + 1)\
- return math.floor(rgb2[c1][c2][1]), math.floor(rgb2[c1][c2][2]), math.floor(rgb2[c1][c2][3])\
- end\
- \
- colours.fromRGB = colors.fromRGB\
- colours.toRGB = colors.toRGB\
- colours.fromRGB2 = colors.fromRGB2\
- colours.toRGB2 = colors.toRGB2",
- [ "main.lua" ] = "dofile(\"display.lua\")\
- dofile(\"canvas.lua\")\
- dofile(\"rgb.lua\")\
- dofile(\"bitmap.lua\")\
- --^^ MAIN FILES ^^\
- \
- dofile(\"stars3D.lua\")\
- --^^ PROGRAMS ^^\
- \
- \
- local display = Display()\
- \
- --[[display:drawLine(5,4,11,11)\
- display:drawLine(5,11,11,4)\
- \
- display:setSize(102,57)\
- \
- local bitmapTest = Bitmap(102,57)\
- \
- bitmapTest:setBG_RGB(127,127,127)\
- \
- bitmapTest:clear(127)\
- bitmapTest:drawPixel(4,4,255,0,0)\
- bitmapTest:drawPixel(5,4,255,0,0)\
- bitmapTest:drawPixel(5,5,255,0,0)\
- bitmapTest:drawPixel(4,5,255,0,0)\
- bitmapTest:drawPixel(4,6,255,0,0)\
- bitmapTest:drawPixel(5,6,255,0,0)\
- bitmapTest:drawPixel(6,5,255,0,0)\
- \
- display:drawImage(bitmapTest,0,0)\
- \
- \
- local happy = Bitmap.createBitmapFromFile(\"happy.bmp\")\
- happy:setBG_C(colors.black)\
- display:drawImage(happy,6,12)\
- \
- local happy2 = Bitmap.createBitmapFromFile(\"happy2.bmp\")\
- happy2:setBG_C(colors.black)\
- display:drawImage(happy2,6,30)\
- \
- local testBmp = Bitmap.createBitmapFromFile(\"test.bmp\")\
- testBmp:setBG_C(colors.black)\
- display:drawImage(testBmp,20,6)\
- \
- os.pullEvent(\"char\") --255 242 82 <<DELETE THIS COMMENT>> 255 240 43 ]]\
- \
- \
- \
- \
- local stars = Stars3D(200, 1, 0.5)\
- local stars_target = Bitmap(102,57)\
- stars_target:setBG_C(colors.black)\
- \
- local previousTime = os.clock()\
- local yieldCatch = 0\
- while true do\
- local currentTime = os.clock()\
- local delta = currentTime - previousTime\
- yieldCatch = yieldCatch + 1\
- \
- stars:updateAndRender(stars_target, delta)\
- \
- display:drawImage(stars_target,0,0)\
- display:flush()\
- previousTime = currentTime\
- if yieldCatch > 6 then\
- sleep(0)\
- end\
- end\
- \
- os.pullEvent(\"char\")",
- startup = "shell.run(\"main.lua\")",
- [ "canvas.lua" ] = "Character = {}\
- Character.__index = Character\
- \
- function Character.create()\
- \009local self = {}\
- \009setmetatable(self, Character)\
- \009\
- \009self.textColor = colors.white\
- \009self.backgroundColor = colors.black\
- \009self.pixelMode = false\
- \009self.character = \" \"\
- \009self.pixel = {{false, false, false},{false, false, false}}\
- \009self.invert = false\
- \009return self\
- end\
- \
- function Character:update()\
- \009if self.pixelMode then\
- \009\009local char = 128\
- \009\009if not self.pixel[2][3] then\
- \009\009\009char = char + (self.pixel[1][1] and 1 or 0)\
- \009\009\009char = char + (self.pixel[2][1] and 2 or 0)\
- \009\009\009char = char + (self.pixel[1][2] and 4 or 0)\
- \009\009\009char = char + (self.pixel[2][2] and 8 or 0)\
- \009\009\009char = char + (self.pixel[1][3] and 16 or 0)\
- \009\009\009self.invert = false\
- \009\009else\
- \009\009\009char = char + (self.pixel[1][1] and 0 or 1)\
- \009\009\009char = char + (self.pixel[2][1] and 0 or 2)\
- \009\009\009char = char + (self.pixel[1][2] and 0 or 4)\
- \009\009\009char = char + (self.pixel[2][2] and 0 or 8)\
- \009\009\009char = char + (self.pixel[1][3] and 0 or 16)\
- \009\009\009self.invert = true\
- \009\009end\
- \009\009self.character = string.char(char)\
- \009end\
- end\
- \
- function Character:draw()\
- \009if not self.invert then\
- \009\009term.setBackgroundColor(self.backgroundColor)\
- \009\009term.setTextColor(self.textColor)\
- \009else\
- \009\009term.setBackgroundColor(self.textColor)\
- \009\009term.setTextColor(self.backgroundColor)\
- \009end\
- \009term.write(self.character)\
- end\
- \
- Canvas = {}\
- Canvas.__index = Canvas\
- \
- function Canvas.create()\
- \009local self = {}\
- \009setmetatable (self, {__index=Canvas})\
- \009\
- \009self.x = 1\
- \009self.y = 1\
- \009self.character = {}\
- \009local w, h = term.getSize()\
- \009self:setSize(w, h)\
- \009\
- \009return self\
- end\
- \
- function Canvas:setSize(width, height)\
- \009self.__width = width\
- \009self.__height = height\
- \009self.character = {}\
- \009for i=1, self.__width do\
- \009\009self.character[i] = {}\
- \009\009for j=1, self.__height do\
- \009\009\009self.character[i][j] = Character.create()\
- \009\009end\
- \009end\
- end\
- \
- function Canvas:draw()\
- \009for j=self.y,(self.__height + self.y - 1) do\
- \009\009term.setCursorPos(self.x, j)\
- \009\009for i=self.x,(self.__width + self.x - 1) do\
- \009\009\009self.character[i][j]:draw()\
- \009\009end\
- \009end\
- end\
- \
- function Canvas:setPixel(x, y, value, bg, fg)\
- \009x = x - 1\
- \009y = y - 1\
- \009local charX = math.floor(x / 2)\
- \009local charY = math.floor(y / 3)\
- \009pixelX = x - charX * 2\
- \009pixelY = y - charY * 3\
- \009charX = charX + 1\
- \009charY = charY + 1\
- if bg then self.character[charX][charY].backgroundColor = bg end\
- if fg then self.character[charX][charY].textColor = fg end\
- \009self.character[charX][charY].pixelMode = true\
- \009self.character[charX][charY].pixel[pixelX + 1][pixelY + 1] = value;\
- \009self.character[charX][charY]:update()\
- end\
- \
- function Canvas:setCharacter(x, y, char)\
- \009self.character[charX][charY].invert = false\
- \009self.character[charX][charY].pixelMode = false\
- \009self.character[charX][charY].character = char;\
- end",
- [ "display.lua" ] = "Display = {}\
- do\
- local Display = _G.Display\
- local math = _G.math\
- \
- function Display.__init__()\
- local self={canvas=0}\
- \
- self.canvas = Canvas.create()\
- \
- setmetatable(self, {__index=Display})\
- return self\
- end\
- \
- setmetatable(Display, {__call=Display.__init__})\
- \
- function Display:drawLine(x1,y1,x2,y2,bg,fg) --Basically copied from paintutils\
- bg = bg or colors.black\
- fg = fg or colors.white\
- \
- x1,y1 = math.floor(x1),math.floor(y1)\
- x2,y2 = math.floor(x2),math.floor(y2)\
- \
- if x1==x2 and y1==y2 then\
- self.canvas:setPixel(x1,y1,true,bg,fg)\
- end\
- \
- local minX = x1 > x2 and x2 or x1\
- local minY,maxX,maxY\
- if minX == x1 then\
- minY = y1\
- maxX = x2\
- maxY = y2\
- else\
- minY = y2\
- maxX = x1\
- maxY = y1\
- end\
- \
- local xDiff = x2 - x1\
- local yDiff = y2 - y1\
- \
- if xDiff > ( yDiff < 0 and -yDiff or yDiff ) then --optimization bruh\
- local y = minY\
- local dy = yDiff / xDiff\
- for x=minX,maxX do\
- self.canvas:setPixel(x, math.floor( y + 0.5), true,bg,fg )\
- y = y + dy\
- end\
- else\
- local x = minX\
- local dx = xDiff / yDiff\
- if maxY >= minY then\
- for y=minY,maxY do\
- self.canvas:setPixel( math.floor( x + 0.5 ), y, true,bg,fg )\
- x = x + dx\
- end\
- else\
- for y=minY,maxY,-1 do\
- self.canvas:setPixel( math.floor( x + 0.5 ), y, true,bg,fg )\
- x = x - dx\
- end\
- end\
- end\
- end\
- \
- function Display:setSize(w,h)\
- self.canvas:setSize(w,h)\
- end\
- \
- --TODO: Add scaling for drawImage?\
- function Display:drawImage(bitmap, x, y)\
- for i=1,bitmap.width do\
- local off = (i-1) * 3\
- for j=1,bitmap.height do\
- local offy = bitmap.width*(j-1)*3 + off\
- local r = bitmap.components[offy + 1]\
- local g = bitmap.components[offy + 2]\
- local b = bitmap.components[offy + 3]\
- --print(i..\";\"..j..\";\"..tostring(r)..g..b)\
- local color = colors.fromRGB(r,g,b)\
- if color == bitmap.bg then\
- self.canvas:setPixel( i+x, j+y, false, bitmap.bg)\
- else\
- self.canvas:setPixel( i+x, j+y, true, bitmap.bg, color)\
- end\
- end\
- end\
- end\
- \
- function Display:flush()\
- self.canvas:draw()\
- end\
- end\
- \
- --local canvas = Canvas.create()\
- --canvas:setPixel(1,1,true)\
- --canvas:setPixel(3,3,true)\
- --canvas:setPixel(3,2,true, colors.red, colors.green)\
- --canvas:draw()\
- --os.pullEvent(\"char\")",
- [ "stars3D.lua" ] = "Stars3D = {}\
- do\
- local Stars3D = _G.Stars3D\
- function Stars3D.__init__(_, numStars, spread, speed)\
- local self = {}\
- \
- self.m_spread = spread\
- self.m_speed = speed\
- \
- self.m_starX = {}\
- self.m_starY = {}\
- self.m_starZ = {}\
- \
- setmetatable(self, {__index=Stars3D})\
- \
- for i=1,numStars do\
- self:InitStar(i)\
- end\
- \
- return self\
- end\
- \
- setmetatable(Stars3D, {__call=Stars3D.__init__})\
- \
- --target is a bitmap object\
- function Stars3D:updateAndRender(target, delta)\
- target:clear(0)\
- \
- local halfWidth = target.width/2\
- local halfHeight = target.height/2\
- for i=1,#self.m_starX do\
- self.m_starZ[i] = self.m_starZ[i] - (delta * self.m_speed)\
- \
- if self.m_starZ[i] <= 0 then\
- self:InitStar(i)\
- end\
- \
- local x = (self.m_starX[i]/self.m_starZ[i]) * halfWidth + halfWidth\
- local y = (self.m_starY[i]/self.m_starZ[i]) * halfHeight + halfHeight\
- \
- if (x < 0 or x >= target.width) or (y < 0 or y >= target.height) then\
- self:InitStar(i)\
- else\
- if self.m_starZ[i] > 0.60*self.m_spread then\
- target:drawPixel(x, y, 76, 76, 76)\
- elseif self.m_starZ[i] > 0.33*self.m_spread then\
- target:drawPixel(x, y, 153, 153, 153)\
- else\
- target:drawPixel(x, y, 255, 255, 255)\
- end\
- --term.setCursorPos(1,19)\
- --term.write(i..\": (\"..self.m_starX[i]..\",\"..y..\")\")\
- --os.pullEvent(\"char\")\
- end\
- end\
- end\
- \
- function Stars3D:InitStar(i)\
- self.m_starX[i] = (math.random(-100,100)/100) * self.m_spread\
- self.m_starY[i] = (math.random(-100,100)/100) * self.m_spread\
- self.m_starZ[i] = (math.random(0.001,100)/100) * self.m_spread\
- end\
- end",
- [ "bitmap.lua" ] = "Bitmap = {}\
- do\
- local Bitmap = _G.Bitmap\
- \
- function Bitmap.createBitmapFromFile(filename)\
- local handle = fs.open(filename, \"rb\")\
- local header = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}\
- for i=1,54 do\
- local byteVal = handle.read()\
- header[i] = byteVal\
- write(\"[\"..i..\"]\"..byteVal)\
- end\
- print(\"\\nFile size = \"..header[5]*65536 + header[4]*256 + header[3]..\"\\n\")\
- local offset = header[12]*256+header[11]\
- print(\"Offset to image = \"..offset)\
- local width = header[19]\
- local height = header[23]\
- \
- local size = 3 * width * height\
- \
- local outdata = {}\
- local padding = (4-((width*3)%4))%4\
- for i=1,size do\
- outdata[i] = handle.read()\
- if i%(width*3)==0 then\
- for i=1,padding do\
- handle.read() --padding data we dont need or want\
- end\
- end\
- end\
- \
- handle.close()\
- \
- for i=1, size, 3 do\
- local tmp = outdata[i]\
- outdata[i] = outdata[i+2]\
- outdata[i+2] = tmp\
- end\
- \
- local outmap = Bitmap(width, height)\
- \
- for i=1,width do\
- for j=1,height do\
- --write(\"{\"..happy[i]..\",\"..happy[i+1]..\",\"..happy[i+2]..\"},\")\
- local poff = (j-1)*width*3 + (i-1)*3\
- outmap:drawPixel(i,height-j+1,outdata[poff + 1],outdata[poff + 2],outdata[poff + 3]) --for some reason the bmps were inverted vertically?\
- end\
- end\
- \
- return outmap\
- end\
- \
- function Bitmap.__init__(_, nWidth, nHeight, bg)\
- local self = {width = nWidth, height = nHeight,\
- components = {}, bg = bg or colors.black}\
- for i=1,(nWidth*nHeight*3) do\
- self.components[i]=1\
- end\
- \
- setmetatable(self, {__index=Bitmap})\
- return self\
- end\
- \
- setmetatable(Bitmap, {__call=Bitmap.__init__})\
- \
- --takes in a byte\
- function Bitmap:clear(shade)\
- for i=1,#self.components do\
- self.components[i] = shade\
- end\
- end\
- \
- function Bitmap:setBG_C(bg)\
- self.bg = bg\
- end\
- \
- function Bitmap:setBG_RGB(r,g,b)\
- self.bg = colors.fromRGB(r,g,b)\
- end\
- \
- function Bitmap:drawPixel(x,y,r,g,b)\
- x,y = math.floor(x),math.floor(y)\
- local index = ((x-1) + (y-1) * self.width) * 3 + 1\
- self.components[index ] = r\
- self.components[index + 1] = g\
- self.components[index + 2] = b\
- end\
- end",
- }
- local function run(tArgs)
- local fnFile, err = loadstring(files['startup'], 'startup')
- if err then
- error(err)
- end
- local function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function resolveTreeForPath(path, single)
- local _files = files
- local parts = split(path, '/')
- if parts then
- for i, v in ipairs(parts) do
- if #v > 0 then
- if _files[v] then
- _files = _files[v]
- else
- _files = nil
- break
- end
- end
- end
- elseif #path > 0 and path ~= '/' then
- _files = _files[path]
- end
- if not single or type(_files) == 'string' then
- return _files
- end
- end
- local oldFs = fs
- local env
- env = {
- fs = {
- list = function(path)
- local list = {}
- if fs.exists(path) then
- list = fs.list(path)
- end
- for k, v in pairs(resolveTreeForPath(path)) do
- if not fs.exists(path .. '/' ..k) then
- table.insert(list, k)
- end
- end
- return list
- end,
- exists = function(path)
- if fs.exists(path) then
- return true
- elseif resolveTreeForPath(path) then
- return true
- else
- return false
- end
- end,
- isDir = function(path)
- if fs.isDir(path) then
- return true
- else
- local tree = resolveTreeForPath(path)
- if tree and type(tree) == 'table' then
- return true
- else
- return false
- end
- end
- end,
- isReadOnly = function(path)
- if not fs.isReadOnly(path) then
- return false
- else
- return true
- end
- end,
- getName = fs.getName,
- getSize = fs.getSize,
- getFreespace = fs.getFreespace,
- makeDir = fs.makeDir,
- move = fs.move,
- copy = fs.copy,
- delete = fs.delete,
- combine = fs.combine,
- open = function(path, mode)
- if fs.exists(path) then
- return fs.open(path, mode)
- elseif type(resolveTreeForPath(path)) == 'string' then
- local handle = {close = function()end}
- if mode == 'r' then
- local content = resolveTreeForPath(path)
- handle.readAll = function()
- return content
- end
- local line = 1
- local lines = split(content, '\n')
- handle.readLine = function()
- if line > #lines then
- return nil
- else
- return lines[line]
- end
- line = line + 1
- end
- return handle
- else
- error('Cannot write to read-only file (compilr archived).')
- end
- else
- return fs.open(path, mode)
- end
- end
- },
- loadfile = function( _sFile )
- local file = env.fs.open( _sFile, "r" )
- if file then
- local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
- file.close()
- return func, err
- end
- return nil, "File not found: ".._sFile
- end,
- dofile = function( _sFile )
- local fnFile, e = env.loadfile( _sFile )
- if fnFile then
- setfenv( fnFile, getfenv(2) )
- return fnFile()
- else
- error( e, 2 )
- end
- end
- }
- setmetatable( env, { __index = _G } )
- local tAPIsLoading = {}
- env.os.loadAPI = function( _sPath )
- local sName = fs.getName( _sPath )
- if tAPIsLoading[sName] == true then
- printError( "API "..sName.." is already being loaded" )
- return false
- end
- tAPIsLoading[sName] = true
- local tEnv = {}
- setmetatable( tEnv, { __index = env } )
- local fnAPI, err = env.loadfile( _sPath )
- if fnAPI then
- setfenv( fnAPI, tEnv )
- fnAPI()
- else
- printError( err )
- tAPIsLoading[sName] = nil
- return false
- end
- local tAPI = {}
- for k,v in pairs( tEnv ) do
- tAPI[k] = v
- end
- env[sName] = tAPI
- tAPIsLoading[sName] = nil
- return true
- end
- env.shell = shell
- setfenv( fnFile, env )
- fnFile(unpack(tArgs))
- end
- local function extract()
- local function node(path, tree)
- if type(tree) == 'table' then
- fs.makeDir(path)
- for k, v in pairs(tree) do
- node(path .. '/' .. k, v)
- end
- else
- local f = fs.open(path, 'w')
- if f then
- f.write(tree)
- f.close()
- end
- end
- end
- node('', files)
- end
- local tArgs = {...}
- if #tArgs == 1 and tArgs[1] == '--extract' then
- extract()
- else
- run(tArgs)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement