Skip to content

Instantly share code, notes, and snippets.

@josefnpat josefnpat/main.lua Secret
Last active Dec 30, 2015

Embed
What would you like to do?
GnuPlot Universe
love.window.setMode(1680,1050)
function gnuplot_reload()
os.execute('gnuplot uni.plot')
uniplot = love.graphics.newImage('uniplot.png')
end
function uni_reload()
local uni_stat_map = love.image.newImageData('uni_stat_map.gif')
package.loaded[ 'universegen' ] = nil
unilib = nil
unilib = require('universegen')
uni = nil
uni = unilib.new(uni_stat_map)
uni:render()
local f = io.open("data.csv",'w')
f:write(uni:toPlot())
f:close()
gnuplot_reload()
end
function love.load()
uni_reload()
end
function love.update(dt)
end
function love.draw()
love.graphics.draw(uniplot)
end
function love.keypressed(key)
if key == "r" then
uni_reload()
end
end
set datafile separator ","
set view 30,60
splot 'data.csv' with points palette pt 8
set term pngcairo mono enhanced
set xlabel "X"
set ylabel "Y"
set zlabel "Z"
set terminal png size 1680,1050
set mouse
set xrange [-512:512]
set yrange [-512:512]
set zrange [-512:512]
set out 'uniplot.png'
replot
universegen = {}
function universegen.new(mapImageData,steps,ztight,bchance)
local tmp = {}
tmp.size = mapImageData:getWidth()
tmp.report = 0.01
tmp.steps = steps or 1024*8
tmp.stepsTotal = tmp.steps
tmp.zMult = ztight or 0.125
tmp.bchance = bchance or 0.01
tmp.map = mapImageData
tmp.data = {}
tmp.render = universegen.render
tmp.attemptStep = universegen.attemptStep
tmp.toPlot = universegen.toPlot
tmp.addPoint = universegen.addPoint
return tmp
end
function universegen.render(self)
local curReport = self.report
print("Rendering ..")
while self.steps > 0 do
if self:attemptStep() then
self.steps = self.steps - 1
end
if self.steps/self.stepsTotal > curReport then
print((curReport*100).."%")
curReport = curReport + self.report
end
end
print("Done.")
end
function universegen.attemptStep(self)
local px,py = math.random(1,self.size-1),math.random(1,self.size-1)
local pz = math.floor(
math.sin(math.random()) * math.random(-self.size*self.zMult,self.size*self.zMult)
)
local r,g,b = self.map:getPixel(px,py)
local rx,ry,rz = px-self.size/2,py-self.size/2,pz
local mag = math.sqrt( rx^2 + ry^2 + rz^2) / self.size
local calc_bchance = self.bchance * mag
local map_chance = ((r+g+b)/3)/255
local chance = calc_bchance + map_chance*(1-calc_bchance)
if 1-math.cos(chance) > math.random() then
return self:addPoint(rx,ry,rz)
end
return false
end
function universegen.addPoint(self,x,y,z)
if not self.data[x] then
self.data[x] = {}
end
if not self.data[x][y] then
self.data[x][y] = {}
end
if not self.data[x][y][z] then
self.data[x][y][z] = 1
return true
else
self.data[x][y][z] = self.data[x][y][z] + 1
return false
end
end
function universegen.toPlot(self)
local output = ""
for x,vx in pairs(self.data) do
for y,vy in pairs(vx) do
for z,vz in pairs(vy) do
output = output .. x .. ","..y..","..z.."\n"
end
end
end
return output
end
return universegen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.