Last active
August 14, 2024 17:58
-
-
Save Kan18/7c3dbd03ad0196edb043347557b4ce33 to your computer and use it in GitHub Desktop.
Random cursed ComputerCraft lua snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/966486623921508392 ]=] | |
local function checkForTerm | |
(_,...)_=...==_..'' | |
and error("Terminated", 0) return ... | |
end | |
function os.pullEvent(...) | |
return checkForTerm("terminate", coroutine.yield(...)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/964510220812431450 ]=] | |
local function sleep(n) | |
return textutils.slowWrite((" "):rep(n/20)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1038804048096276573 ]=] | |
--[=[rgb: 0xFFFFFF or something]=] | |
function toLamp(rgb,r,g,b) | |
r,g,b=rgb/2^19, | |
rgb/2048%32,rgb/8%32 | |
return (r-r%1)*1024+(g-g%1)*32+b-b%1 | |
end | |
--[=[alternative implementation:]=] | |
function toLamp(rgb) | |
return (rgb/524288-rgb/524288%1)*1024+(rgb/2048%32-rgb/2048%1)*32+rgb/8%32-rgb/8%1; | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i = 2, 2000 do | |
print(i) | |
debug.setlocal(1, 3, ({debug.getlocal(1, 1)})[2] - ({debug.getlocal(1, 3)})[2]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x=0x31p1;m,b=("").char(x):rep(0x7dp10 | |
),0x19p4;_G.e,_G.S={m:unpack((m.char( | |
m:byte()+0x1e):rep(b)..m.char(m:byte( | |
)+0xfp1-1):rep(b)):rep(0x5p5))},'spe' | |
..'aker';_G.p,q,e[#e]=peripheral,load | |
[[__=(...)and(p.call(...,'playAudio', | |
e))]]p.find(S,q);repeat until q(({os. | |
pullEventRaw(S..'_audio_empty')})[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i = 1, 10 do print( | |
([[%s]]):format | |
{}:byte | |
[[-0x.2p3 ]]<0x.ep6 | |
)end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/977917832467718204 ]=] | |
local function getFileNameWhenRequired() | |
local t=1 repeat t=t+1 until debug.getinfo( | |
t,"S").short_src=='require.lua'local o,a,n= | |
loadfile,debug.getlocal(t, 1) _G.loadfile = | |
function(qfile)n= qfile;_G.loadfile = o end | |
package[ 'loaders' ][ 2 ] ( n ); return n | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/1205358173351968798 ]=] | |
--[=[note: only tested on recent CC:T/Cobalt version]=] | |
local function isluaclosure(func, ...) | |
local ok, msg = pcall(isluaclosure, func, ...) | |
if not ok then | |
local ok2, msg2 = pcall(function(...) func(...) end, ...) | |
return ok2 or msg2 ~= select(2, pcall(function(...) error'' end)):gsub(":(%d+):", function(x) return ":"..(x-1)..":" end) | |
else | |
return msg | |
end | |
end | |
print(isluaclosure(fs.list, '/')) | |
local old = fs.list | |
function fs.list(...) return old(...) end | |
print(isluaclosure(fs.list, '/')) | |
function fs.list() end | |
print(isluaclosure(fs.list, '/')) | |
print(isluaclosure(math.sin, 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1176547902349381692 ]=] | |
setfenv(1, _G) | |
setmetatable(_G, _G) | |
function __tostring() return "Lemmmy" end | |
__index, __call = assert(assert, assert) | |
print(Lemmmy:reproduce_lemmmy()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/1234700943757410355 ]=] | |
local function z(x) return function(...)return x(...) or z(x)end end local function l()return z | |
(coroutine.wrap(load))(coroutine.yield)[[return]]end | |
local tab = l'''{'(1)','(2)','(3)'}''''' | |
print(tab) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/859454303604047933 ]=] | |
getfenv(read).os=setmetatable({pullEvent=coroutine.yield},{__index=os}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/912158960990187570 ]=] | |
local function numberToBinary(num)local n=("0"):rep(53)local str=""while""do | |
n=n:sub(1,-2)..(n:sub(-1,-1)+1)for i=1,#str do n=n:gsub("02","10"):gsub("12" | |
,"20")end str=n:gsub("^0*","")local z=tonumber(str,2)if tonumber(str,2)==num | |
then return str end end end | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/949121837881761823 ]=] | |
--[=[bogosort style! it looks shorter so it is more optimized.. ]=] | |
local function numberToBinary(num,n,m,l)m=math;l=m.log;n=("0"):rep(m.ceil(l( | |
num)/l(2)))while""do n=n:gsub(".",function()return math.random(0,1)end)if | |
tonumber(n,2)==num then return(n)end;end;end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/973676435954864199 ]=] | |
print((("11111111"):gsub("1",function()a, z= | |
"abcdefghijklmnopqrstuvwxyz" , math.random ( | |
52 )return( a..a:upper() ):sub (z,z)end))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1006975030170746920 ]=] | |
--[=[inspired from https://discord.com/channels/477910221872824320/477911902152949771/970043466874773575 ]=] | |
t,y,m,z= term,0,math.random,{"f" | |
,"0"}w ,h=t.getSize() while( y<h | |
*w )do(t.setCursorPos) (y%w+1,y/ | |
w+1)y,f=y +1,m(1, 2)t.blit(z[1]. | |
char(m(2^7,159)),z[f],z[3-f])end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1044607819858587738 ]=] | |
do local | |
g,d,s=_G,debug,'metatable' f=g | |
['set'..s]f(g,{__newindex=f({} | |
,{__mode ='v'}),__index=load[[ | |
local t,b=...;for i,v in next, | |
t do b[i],t[i]=v,z end return( | |
b)]](g,{}),['__'..s..'']=g})d[ | |
'set'..s], d[''..'get'..s] =z; | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[ recently committed to the potatOS source code! ]=] | |
local k=load[=================[ | |
local _=({load[=======[local _; | |
return pcall(load[=[return load | |
]=][=[=]=],function()_=load[==[ | |
return debug.getinfo(#[=[===]=] | |
).func[=[return fs]=][=[]=]]==] | |
[=[]=]end),_]=======][=[==]=]}) | |
[#[=======[==]=======]]return _ | |
]=================][===[==]===] | |
-- under certain sandboxes, k will be equal to the non-sandboxed fs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/919924947567116340 ]=] | |
print( | |
("123456"):gsub("(%d)", "%"..math.random(0,1).." ") | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1121173213876076754 ]=] | |
--NOTE: no longer works on latest CC:T | |
local function unserialize(s) | |
local func = load("return " .. s, "unserialize", "t", {}) | |
if not func then return end | |
local d = s.dump(func) | |
for i = 45, 41 + ("i"):unpack(d:sub(41)) * 4, 4 do | |
local ins = d:byte(i) % 64 | |
if ins == 21 or ins == 28 or ins == 29 or ins == 36 then | |
return | |
end | |
end | |
local ok, result = pcall(func) | |
if ok then | |
return result | |
end | |
end | |
-- alternatively (this version is much more likely to be buggier imo) | |
local function unserialize2(s) | |
local func = load("return " .. s, "unserialize", "t", {}) | |
if not func then return nil end | |
local c = coroutine.create(func) | |
local count = 0 | |
debug.sethook(c, function() | |
count = count + 1 | |
if count == 2 then error("Cannot call functions") end | |
end, "c") | |
debug.getmetatable("").__index = {} | |
local ok, result = coroutine.resume(c) | |
debug.getmetatable("").__index = string | |
if ok then return result end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
z=131072;_G.e={("b"):rep(z):unpack(("\0"):rep(z))}_G.p | |
, q,e[#e]=peripheral,load[[p.call(...,'playAudio',e)]] | |
p.find("speaker", q);repeat until q(( {coroutine.yield | |
"speaker_audio_empty"} )[1+1-1+1+1+1-1+1-1+1-1+1-1-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/627203797021753394/949904272718430228 ]=] | |
local s=debug.getmetatable "__metatable" | |
s.__unm=string.reverse | |
s.__sub=function(a,b)return type(b)=="string"and (a:gsub(b,""))or | |
type(b)=="number"and a:sub(1,-b-1)or type(b)=="function"and a: | |
gsub(".",b)or string.char(math.random(1,255)) end | |
print(-"Hello, world!") | |
print("Hello, world!" - "world") | |
print("Hello, world!" - 3) | |
print("Hello, world!" - load'return("").char((...):byte()-1)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[NOTE: these are separate code snippets, but grouped in the same file for convenience ]=] | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1038804048096276573 ]=] | |
local x = {} | |
x.__index = x | |
setmetatable(x, x) | |
x = x.x | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1006959235080409098 ]=] | |
--NOTE: no longer works on latest CC:T | |
local y = fs.open("y", "wb").write | |
y(27)y(32)y(33) | |
shell.run("y") | |
--[=[see https://discord.com/channels/477910221872824320/627203797021753394/961414699654737964 ]=] | |
assert(load("x "..("a"):rep(5e7))) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1006661382156070982 ]=] | |
--NOTE: no longer works on latest CC:T | |
for a in b,c,d,e do end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[NOTE: these are separate code snippets, but grouped in the same file for convenience ]=] | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1006675279630061669 ]=] | |
print(#({1, 2, 3, nil, 5})) | |
print(#({1, 2, 3, nil, 5, 6})) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/930601147994099722 ]=] | |
for i, v in __inext, {"a","b","c"}, 0 do | |
print(i,v) | |
end | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/954554641872470016 ]=] | |
os.queueEvent("paste", "term.getCursorPos)(") | |
os.queueEvent("key", keys.enter) | |
os.queueEvent("paste", "exit)(") | |
os.queueEvent("key", keys.enter) | |
shell.run("lua") | |
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/952250352760139786 ]=] | |
local function fn(...) | |
print(select("#", ...)) | |
end | |
print(fn()) | |
print(fn(nil)) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/926194258082431096 ]=] | |
--NOTE: no longer works on latest CC:T | |
local free = fs.getFreeSpace('hdd') | |
local longstring = ("a"):rep(free * 0.6) | |
local fileHandle = fs.open("test", "w") | |
fileHandle.write(longstring) | |
fileHandle.close() | |
fs.move("test", "test2") | |
--[=[see https://discord.com/channels/477910221872824320/477912057560432680/878843511129116704 ]=] | |
print(2^3^4^5 == 6^7^8^9) | |
--[=[additionally, ]=] | |
x=1e16; print(x == x + 1) | |
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/872134055565619211 ]=] | |
--[=[disclaimer: this snippet and the one after it work in standard PUC Lua with floats, but not computercraft ]=] | |
x = loadstring("return 0")() | |
y = loadstring("return -0")() | |
print(x == y) | |
print(tostring(x) == tostring(y)) | |
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/901693092355248138 ]=] | |
print(loadstring("local m = 0; print((0 ..1)+1)")()) | |
print(loadstring("local m = -0; print((0 ..1)+1)")()) | |
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/1099366153681391677 ]=] | |
local m = {1, 2, 3, 4, 5} | |
m[1] = nil | |
m[2] = nil | |
print(textutils.serialize(m), #m) | |
setmetatable(m, {__mode="k"}) | |
setmetatable(m, nil) | |
print(textutils.serialize(m), #m) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1117118736973963264 ]=] | |
--NOTE: no longer works on latest CC:T | |
print("Newline: ", load("a\n()")) | |
print("Space:", load("a ()")) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1047632753983766558 ]=] | |
x = 1-1e-15 | |
print("a", x) | |
print("b", x == 1) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1237541825309315092 ]=] | |
x=1print(x) | |
y = (x ..'')+0 | |
print("c", y) | |
print("d", y == 1) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1137024677240381470 ]=] | |
print(not 4^5) | |
print(not 4*5) | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1121473665968648242 ]=] | |
local t = [[('%s'):format{}:sub(8)]] | |
for i = 1, 8 do print(textutils.unserialize(t)) end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/872578147344867349 ]=] | |
local function to9551(s) | |
if s == "true" then return true end | |
if s == "false" then return false end | |
return (debug.getinfo(2).currentline % 2 == 1) and true or false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1237531257286692956 ]=] | |
local var = "3" | |
if pcall(load"return - -(.....'')<=...", var) then | |
print "var is truly a number" | |
else | |
print "var is not truly a number" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1138135734637826108 ]=] | |
local f = debug.getinfo(1).func | |
debug.sethook(function() | |
if debug.getinfo(2).func == f then | |
local i = 1 | |
local l, b | |
repeat | |
l, b = debug.getlocal(2, i) | |
if b == 4 then debug.setlocal(2, i, 5) end | |
i = i + 1 | |
until not l | |
end | |
end, '', 1) | |
print(2 + 2) | |
print(2 ^ 2) | |
print(1 + 1 + 1 + 1) | |
print("loop") | |
for i = 1, 4 do print(i) end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=[This is derived from something in https://github.com/9551-Dev/epic ]=] | |
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/1024401870728482978 ]=] | |
f=_G;f.t,w,b,f.r,p,f.c,f.l,g=term,"write","blit" | |
,math.random,"setCursorPos",colors,load;f.h,f.i, | |
f.v=t[w],t[b],t[p]t[p]=l's,y= t.getSize() v(\ | |
r(s),r(y))'f.m = l 'return(...):gsub ( ".",l\ | |
"q=(...)return q.char((q:byte()-1)%32+129)" )'t[ | |
w]=l'h(m(...))'t[b]=l'i(m(...), ...)' f.colors = | |
setmetatable({ }, {__index=l '_=c[({...})[2 ]]\ | |
return pcall (r,_)and 2^ r(0,15) or _'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#'a'--[[ | |
print("2") | |
--]] | |
print("3") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment