Skip to content

Instantly share code, notes, and snippets.

@Kan18
Last active July 30, 2023 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kan18/7c3dbd03ad0196edb043347557b4ce33 to your computer and use it in GitHub Desktop.
Save Kan18/7c3dbd03ad0196edb043347557b4ce33 to your computer and use it in GitHub Desktop.
Random cursed ComputerCraft lua snippets
--[=[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
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/964510220812431450 ]=]
local function sleep(n)
return textutils.slowWrite((" "):rep(n/20))
end
--[=[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
for i = 2, 2000 do
print(i)
debug.setlocal(1, 3, ({debug.getlocal(1, 1)})[2] - ({debug.getlocal(1, 3)})[2])
end
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])
--[=[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
--[=[see https://discord.com/channels/477910221872824320/477911902152949771/859454303604047933 ]=]
getfenv(read).os=setmetatable({pullEvent=coroutine.yield},{__index=os})
--[=[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
--[=[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)))
--[=[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
--[=[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
--[=[see https://discord.com/channels/477910221872824320/825968186406338580/919924947567116340 ]=]
print(
("123456"):gsub("(%d)", "%"..math.random(0,1).." ")
)
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])
--[=[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)')
--[=[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 ]=]
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 ]=]
for a in b,c,d,e do end
--[=[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 ]=]
--[=[this might be fixed in 1.20 ]=]
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 ]=]
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)
y = (x ..'')+0
print("c", y)
print("d", y == 1)
--[=[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 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 _'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment