Advertisement
Guest User

Transformice Hills with Interpolated Noise

a guest
Sep 9th, 2014
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. -- Noise functions
  2. Noise = {
  3.     SEED = nil,
  4.     PERIOD = 40,
  5.     NUMBER_OF_VERTICES = 4,
  6.     MAX_HEIGHT = 400,
  7.     INTERPOLATION_TYPE = 1,
  8.     generatedNoise = {},
  9.     genNoise =  function(maxVertices)
  10.                     math.random()
  11.                     for i = 0, maxVertices do
  12.                         table.insert(Noise.generatedNoise, math.random())
  13.                     end
  14.                 end,
  15.     getInterpolatedNoiseAt =    function(x)
  16.                                     if (x < 1.0 or x >= Noise.PERIOD * Noise.NUMBER_OF_VERTICES) then return 0 end
  17.                                    
  18.                                     local x1 = math.floor(x / Noise.PERIOD) + 1
  19.                                     local x2 = math.ceil(x / Noise.PERIOD) + 1
  20.                                     local y1 = Noise.generatedNoise[x1] * Noise.MAX_HEIGHT
  21.                                     local y2 = Noise.generatedNoise[x2] * Noise.MAX_HEIGHT
  22.                                     x1 = x1 * Noise.PERIOD * 10
  23.                                     x2 = x2 * Noise.PERIOD * 10
  24.                                     x = x * 10
  25.                                     local value = nil
  26.                                    
  27.                                     if (Noise.INTERPOLATION_TYPE == 0) then
  28.                                         value = ((y2 - y1) / (x2 - x1)) * (x - x1) + y2 -- linear interpolation
  29.                                     elseif (Noise.INTERPOLATION_TYPE == 1) then
  30.                                         value = -(0.5 * (y2 - y1)) * math.sin(math.pi/(x2 - x1)*(x - x1) + 4.72)+(y1 + 0.5 * (y2 - y1)) -- sine interpolation
  31.                                        
  32.                                     -- add extra elseif's here for different types of interpolation values you may want
  33.                                     else
  34.                                         value = 0/0 -- no interpolation
  35.                                     end
  36.                                    
  37.                                     if (value ~= value) then return y1 else return value end
  38.                                 end,
  39.     init =  function()
  40.                 if (Noise.SEED == nil) then Noise.SEED = os.time() end
  41.                 math.randomseed(Noise.SEED)
  42.  
  43.                 Noise.genNoise(Noise.NUMBER_OF_VERTICES)
  44.             end
  45. }
  46. -- Map manipulation
  47. function newMap()
  48.     Noise.init()
  49.    
  50.     local mapXML = '<C><P L="1600" /><Z><S>'
  51.    
  52.     for i = 1, (Noise.NUMBER_OF_VERTICES * Noise.PERIOD) do
  53.         local height = Noise.getInterpolatedNoiseAt(i)
  54.         mapXML = mapXML .. '<S L="10" H="' .. height .. '" X="' .. i * 10 .. '" Y="' .. 400 - (height / 2) .. '" T="6" P="0,0,0.3,0.2,0,0,0,0" />'
  55.     end
  56.    
  57.     mapXML = mapXML .. '</S><D><DS Y="50" X="50" /></D><O /></Z></C>'
  58.    
  59.     tfm.exec.newGame(mapXML)
  60. end
  61.  
  62. -- chat and such
  63. system.disableChatCommandDisplay('seed')
  64.  
  65. function eventChatCommand(name, cmd)
  66.     if (cmd == 'seed') then
  67.         print('Seed for this terrain : ' .. Noise.SEED)
  68.     end
  69. end
  70.  
  71. newMap()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement