Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | None | 0 0
  1. local WorldGenerator = {}
  2. points={}
  3. points2={}
  4.  
  5. function WorldGenerator.prnt(text)
  6.     print("hello")
  7.  
  8.     load()
  9.     run()
  10.     points2 = copy(points)
  11.     load()
  12.     math.randomseed(os.time()+24)
  13.     run()
  14.  
  15.     dat = love.image.newImageData( 512, 512 )
  16.  
  17.     dat:mapPixel(pixelFunc)
  18.  
  19.     print(mx)
  20.  
  21.     return dat
  22.  
  23.  
  24. end
  25.  
  26. function pixelFunc(x,y,r,g,b,a)
  27.  
  28.  
  29.  
  30.     --print(points2[x])
  31.  
  32.     if points2[x] ~= nil then
  33.         --print("not",x)
  34.         mx=points2[x]..points[x]
  35.     end
  36.  
  37.     if x > 0 and y > 0 then
  38.  
  39.     g = math.ceil(math.abs((points2[x]+points2[y])))
  40.     --if g < 0 then g=1 end
  41.     --print(math.abs((points2[x]+points2[y])/5))
  42.  
  43.     end
  44.  
  45.  
  46.  
  47.     return 41,g,41,255
  48.  
  49. end
  50.  
  51.  
  52. function load()
  53.     smoothness = 10
  54.     iterations = 9
  55.     points = {0,0}
  56.     math.randomseed(os.time())
  57. end
  58.  
  59. function draw()
  60.     love.graphics.print("smoothness = "..smoothness ,10,10)
  61.     for i = 1, #points - 1 do
  62.         love.graphics.line(i, math.floor(points[i]+0.5)+250, i+1, math.floor(points[i+1]+0.5)+250)
  63.     end
  64. end
  65.  
  66. function keypressed(key)
  67.     if key == "down" and smoothness > 1 then
  68.         smoothness = smoothness - 1
  69.         run()
  70.     elseif key == "up" then
  71.         smoothness = smoothness + 1
  72.         run()
  73.     end
  74. end
  75.  
  76. function run()
  77.     beginratio = math.floor((9^2-1)/(smoothness / 10))
  78.     points = {0,0}
  79.     for i = 1, iterations do
  80.         routine()
  81.         beginratio = beginratio/2
  82.     end
  83. end
  84.  
  85. function routine()
  86.     add = 0
  87.     for point = 1, #points do
  88.         point = point+add
  89.         if point >= #points then return end
  90.         point1 = points[point]
  91.         point2 = points[point+1]
  92.         point3 = (point1+point2)/2+math.random(-beginratio,beginratio)
  93.         table.insert(points, point+1, point3)
  94.         add = add+1
  95.     end
  96. end
  97.  
  98.  
  99.  
  100. function copy(obj, seen)
  101.   if type(obj) ~= 'table' then return obj end
  102.   if seen and seen[obj] then return seen[obj] end
  103.   local s = seen or {}
  104.   local res = setmetatable({}, getmetatable(obj))
  105.   s[obj] = res
  106.   for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
  107.   return res
  108. end
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. return WorldGenerator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement