Skip to content

Instantly share code, notes, and snippets.

@inmatarian
inmatarian / altair.lua
Created February 7, 2018 23:50
some crap I did years ago for fun
__DEBUG_BASIC = true
local function NULLFUNC() end
local old_print, print = print, NULLFUNC
if __DEBUG_BASIC then
print = function(...) old_print(("%i"):format(debug.getinfo(2, 'l').currentline), ...) end
end
local unpack = unpack or table.unpack
local class
@inmatarian
inmatarian / example_output.txt
Last active May 12, 2017 19:26
random text replacement generator
lua gen.lua --times=25 sample.txt
Your pack contains another rucksack containing a shard of bone, some lint and an old bronze ring.
Your bag contains a tin of tobacco, an old bronze ring and a tin of tobacco.
Your pack contains a tin of tobacco, some lint and another bag containing another pack containing a handkerchief.
Your knapsack contains another bag containing an old bronze ring, a tin of tobacco and a tin of tobacco.
Your pack contains an old silver ring, a tin of tobacco and a shard of bone.
Your purse contains another backpack containing a tin of tobacco, some lint and some lint.
Your bag contains some coins, another rucksack containing some coins and some coins.
Your bag contains a tin of tobacco, an old silver ring and an old silver ring.
@inmatarian
inmatarian / tictactoe-challenge-minified.lua
Created January 15, 2017 19:44
Tic Tac Toe in Love2d, 1k Challenge, Minified from 1347 to 948 bytes
L=love;G=L.graphics;W=267;H=200;F=math.floor;L.load=function()K={0,0,0,0,0,0,0,0,0}end;B={1,3,7,9,5,2,4,6,8}L.mousepressed=function(x,y,a)if C(K,1)or C(K,2)then L.load()else i=F(y/H)*3+F(x/W)+1;if K[i]<1 then K[i]=1;if C(K,1)then return end;for i=1,9 do if K[i]<1 and C(K,2,i)then K[i]=2;return end end;for i=1,9 do if K[i]<1 and C(K,1,i)then K[i]=2;return end end;for i=1,9 do if K[B[i]]<1 then K[B[i]]=2;return end end else for i=1,9 do if K[i]<1 then return end end;L.load()end end end;R=function(b,c,x,y,d)return b[x]==c and b[y]==c and b[d]==c end;C=function(b,c,e)if e then b={unpack(b)}b[e]=c end;return R(b,c,1,2,3)or R(b,c,4,5,6)or R(b,c,7,8,9)or R(b,c,1,4,7)or R(b,c,2,5,8)or R(b,c,3,6,9)or R(b,c,1,5,9)or R(b,c,3,5,7)end;L.draw=function()for i=1,9 do x=(i-1)%3*W;y=F((i-1)/3)*H;G.line(x+W,0,x+W,y+H)G.line(0,y+H,x+W,y+H)if K[i]==1 then G.line(x,y,x+W,y+H)G.line(x,y+H,x+W,y)elseif K[i]==2 then G.circle('line',x+W/2,y+H/2,H/2)end end end
@inmatarian
inmatarian / lfsr.lua
Created January 11, 2017 17:27
Linear Feedback Shift Register pseudorandom number generator in lua
-- LFSRs are good for games where you want a random number generator with a short period,
-- so that speed runners can device tactics to control the RNG's state.
-- Otherwise they're a bit of an obsolete piece of tech.
-- See: https://en.wikipedia.org/wiki/Linear-feedback_shift_register
-- LFSRs work by shifting all of the bits to the right by one, and using the shifted
-- out bit to XOR in at specific taps. Another way to put it is that they first check
-- if the state is odd, then divides by two, and if it was odd, Exclusive-Ors in a value.
-- The bit shifted out is suitably random. Want more bits, cycle the generator more.
@inmatarian
inmatarian / tail_recursion.js
Created October 14, 2016 16:07
Proper tail recursion in javascript
// object containing the call and parameters, and optionally context
function tail_call() {
this.params = Array.prototype.slice.call(arguments);
this.fn = this.params.shift();
}
tail_call.prototype.self = function (context) {
this.that = context;
return this;
}
@inmatarian
inmatarian / bfc.lua
Created October 7, 2016 01:44
Brainfuck compiler to lua, also because why not.
preamble = [=[
local p, d = 1, {}
function g() return d[p] or 0 end
function s(v) d[p] = v end
function o() io.write(string.char(g())) end
function i() s(string.byte(io.read(1))) end
]=]
subst = {
['>'] = "p=p+1\n",
@inmatarian
inmatarian / bf-fn.lua
Created October 7, 2016 01:28
brainfuck, recursive interpreter implementation, cuz why not.
function abs(x)
if x < 0 then
return -1
else
return 1
end
end
oper = {}
@inmatarian
inmatarian / optparse.lua
Created April 12, 2016 04:20
Minimal command line options parser (with tests)
-- Fast options parser
-- License: Public Domain
-- Can parse --arg=value types
-- Anything not --arg or --arg=value are positional types
-- Everything is a string, doesn't parse anything for you
-- OS/Host usually does the string split for you, so I don't bother.
-- Isn't POSIX compliant, because -arg and --arg and ---arg are treated the same.
function optparse(...)
local cfg = {}
@inmatarian
inmatarian / PassCodec.lua
Last active July 16, 2017 01:08
80s Style Video Game Passcode Encoder/Decoder
--[=[
Silly little 80s nostalgia game passcode generator.
Of course, the JUSTIN BAILEY password was hardcoded in and not decoded,
but having a convincing password entry form that works is the piece that
makes hard coded passwords all the more magic.
The codes do embed an inverse checksum at the end, which means newbies
have slightly more math to do in order to hack ur gaems. When you Decode
local complex = {
__tostring = function(self) return ("(% .5f % .5fi)"):format(self[1], self[2]) end
}
local A = -2 * math.pi
local function C(t) return setmetatable(t, complex) end
local function cexp(x)
local er = math.exp(x[1])
return C{ er*math.cos(x[2]), er*math.sin(x[2]) }
end