Skip to content

Instantly share code, notes, and snippets.

@mkarneim
Last active March 9, 2018 13:10
Show Gist options
  • Select an option

  • Save mkarneim/3398d37f4cf076a13773d3d496cad231 to your computer and use it in GitHub Desktop.

Select an option

Save mkarneim/3398d37f4cf076a13773d3d496cad231 to your computer and use it in GitHub Desktop.
Conway's Game of Life for Minecraft
--[[ mickkay/gol.lua
Conway's Game of Life for Minecraft
Created with Wizards of Lua Magic
by MickKay
## How To Run this Spell Inside Minecraft?
1) Install the Wizards of Lua modification into your Minecraft game.
Instructions available at http://www.wizards-of-lua.net/installation
2) Execute the following command from the Minecraft chat:
/wol shared-file gist get 3398d37f4cf076a13773d3d496cad231 mickkay
3) Find some nice place and put a new command block there with
the following contents:
/lua spell:move("forward"); wol=require("mickkay.gol"); wol.random(32,32); wol.start(32,32)
4) Execute this command block.
5) To stop the game any time later please execute the following command:
/wol spell break byName mickkay.gol
Have fun!
More infomation about Conway's Game of Life can be found at https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
--]]
local pkg = {}
local ALIVE = Blocks.get("obsidian")
local DEAD = Blocks.get("wool")
local readGen
local createVectors
local makeTorus
local evaluate
local log
function Vec3:floor()
return Vec3(math.floor(self.x),math.floor(self.y),math.floor(self.z))
end
function pkg.random(length, width)
for l=1,length do
for w=1,width do
if math.random() <0.5 then
spell.block=ALIVE
else
spell.block=DEAD
end
spell:move("right")
end
spell:move("left", width)
spell:move("forward")
end
spell:move("back", length)
end
function pkg.stripes(length, width, bw, ww, rot)
for l=1,length do
for w=1,width do
if (l*rot+w)%(bw+ww)<bw then
spell.block=ALIVE
else
spell.block=DEAD
end
spell:move("right")
end
spell:move("left", width)
spell:move("forward")
end
spell:move("back", length)
end
function pkg.clear(length, width)
for l=1,length do
for w=1,width do
spell.block=DEAD
spell:move("right")
end
spell:move("left", width)
spell:move("forward")
end
spell:move("back", length)
end
function pkg.start(length, width)
spell:execute("wol spell break byName mickkay.gol")
spell.name = "mickkay.gol"
vecs = createVectors(length, width)
nextGen = readGen(length, width)
while true do
local tStart = Time.realtime
gen = nextGen
nextGen = {}
nextGen[0]={}
nextGen[length+1]={}
for l=1,length do
nextGen[l]={}
for w=1,width do
local n = 0
for z=-1,1 do
for x=-1,1 do
if not (x==0 and z==0) then
if gen[l+x] and gen[l+x][w+z]==1 then
n = n + 1
end
end
end
end
local c = gen[l][w]
local e = evaluate(c,n)
nextGen[l][w] = e
end
end
makeTorus(nextGen,length,width)
for l=1,length do
for w=1,width do
spell.pos = vecs[l][w]
if nextGen[l][w]==1 then
spell.block=ALIVE
else
spell.block=DEAD
end
end
end
local tEnd = Time.realtime
--log(tEnd-tStart)
sleep(1)
end
end
function createVectors(length, width)
local res = {}
res[0]={}
res[length+1]={}
for l=1,length do
res[l]={}
for w=1,width do
res[l][w]=spell.pos
spell:move("right")
end
spell:move("left", width)
spell:move("forward")
end
spell:move("back", length)
makeTorus(res,length,width)
return res
end
function readGen(length, width)
local res = {}
for l=1,length do
res[l]={}
for w=1,width do
if spell.block.name==ALIVE.name then
res[l][w]=1
else
res[l][w]=0
end
spell:move("right")
end
spell:move("left", width)
spell:move("forward")
end
spell:move("back", length)
makeTorus(res,length,width)
return res
end
function makeTorus(tbl, length, width)
tbl[0]={}
tbl[length+1]={}
for l=1,length do
tbl[l][width+1]=tbl[l][1]
tbl[l][0]=tbl[l][width]
end
for w=1,width do
tbl[length+1][w]=tbl[1][w]
tbl[0][w]=tbl[length][w]
end
tbl[0][0]=tbl[length][width]
tbl[0][width+1]=tbl[length][1]
tbl[length+1][width+1]=tbl[1][1]
tbl[length+1][0]=tbl[1][width]
end
function evaluate(c,n)
if c == 1 then
if n < 2 then
return 0
elseif n == 2 or n == 3 then
return 1
else
return 0
end
else
if n == 3 then
return 1
else
return 0
end
end
end
-- Logs the given message into the chat
function log( message, ...)
local n = select('#', ...)
if n>0 then
message = string.format(message, ...)
end
spell:execute([[
/say %s
]], message)
end
return pkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment